Source tree

A source tree means a directory tree of source code files. The following is a simple example:

MySourceTree
├── mylib
│   ├── CMakeLists.txt
│   ├── hello.cpp
│   └── hello.h
├── CMakeLists.txt
├── configure-build-and-run.bat
└── main.cpp

The root folder of the source tree contains the top level CMakeLists.txt file. Often the source tree will contain subfolders which in turn have CMakeLists.txt files. A parent CMakeLists.txt uses the command add_subdirectory to add each of its subdirectories to the build.

The cmake variable CMAKE_SOURCE_DIR represents the full path to the root folder of the source tree. Every CMakeLists.txt file under the source tree sees the same value of CMAKE_SOURCE_DIR.

Current source directory

For each CMakeLists.txt file, its current source directory means its containing folder. While executing the commands in a CMakeLists.txt file, the cmake variable CMAKE_CURRENT_SOURCE_DIR represents the full path to the CMakeLists.txt file's current source directory. It follows that each CMakeLists.txt file under the source tree sees a different value of CMAKE_CURRENT_SOURCE_DIR.

Many CMake commands, such as add_library, add_executable, add_jar and add_subdirectory are given paths to files or directories under the source tree. CMake allows these paths to be relative to CMAKE_CURRENT_SOURCE_DIR.

This tends to mean source files don't need to be qualified with a path - for example there is a call to add_library in the above example which references hello.cpp relative to CMAKE_CURRENT_SOURCE_DIR. This is in the same directory as the CMakeLists.txt file which references it, so doesn't require qualification:


add_library(mylib hello.cpp)