Where is CMAKE_SOURCE_DIR? - cmake

Is this variable always set to the directory containing the CMakeLists.txt file that I run cmake on?
For example, if I wish to run cmake on a CMakeLists.txt file that exists in the directory above my current directory, I would go: cmake ...
In this case, what is my CMAKE_SOURCE_DIR set to?

Assuming that you have 2 folders src and build where src contains your projects and build is the empty folder that you just created so you can deploy your out-of-tree build in it: CMAKE_SOURCE_DIR is the path to src where CMAKE_BINARY_DIR points to build.
Note that if you are doing an in-tree build, the 2 cache entries get the same value.
link: CMake Useful Variables .
EDIT
for further clarifications
<some location>/src/CMakeLists.txt ( so *src* is the root of your project )
<some location>/build
if you do something like
cd <some location>/build
cmake <some location>/src
you are making an out-of-tree build where CMAKE_SOURCE_DIR and CMAKE_BINARY_DIR get different values

Related

Cmake - how include paths with only read access? [duplicate]

Is it possible to specify an include directory when running cmake. For example
cmake . -INCLUDE=/foo/bar
The header files are in a separate directory from the sources that I would like to compile, and I would like to remedy this without tinkering with the Makefile generated by cmake.
Update
The project does have a CMakeLists.txt. Excerpt:
INCLUDE_DIRECTORIES(${EO_SOURCE_DIR}/src)
INCLUDE_DIRECTORIES(${EO_SOURCE_DIR}/src/ga)
INCLUDE_DIRECTORIES(${EO_SOURCE_DIR}/src/utils)
Can ${EO_SOURCE_DIR} be set from the command line?
If the path to your headers is fixed in relation to your sources, then you should be able to avoid having to pass this info via the command line.
Say your project's directory structure is:
/CMakeLists.txt
/my_sources/main.cpp
/my_sources/foo.cpp
/my_includes/foo.hpp
and in your CMakeLists.txt, you currently have something like:
add_executable(MyExe my_sources/main.cpp my_sources/foo.cpp)
then to add the /my_includes folder to the the list of include search paths, you only need to add the following:
include_directories(my_includes)
For further info about include_directories, run
cmake --help-command include_directories
Answer to update in question:
Yes, using the -D command line option just do
cmake . -DEO_SOURCE_DIR:PATH=<Path to required dir>
The variable ${EO_SOURCE_DIR} gets cached as a result of this, so you only need this -D argument once (unless the required path changes or you delete your CMakeCache file, etc.)
Proper way to do this is to define a variable in CMakeLists.txt and ask user to set it:
set(YOURLIB_INCLUDE_DIR "" CACHE FILEPATH "Path to yourlib includes")
if(NOT EXISTS ${YOURLIB_INCLUDE_DIR}/header.h)
message(SEND_ERROR "Can't find header.h in ${YOURLIB_INCLUDE_DIR})
endif()
include_directories(${YOURLIB_INCLUDE_DIR})
Now you can set it from the command line: cmake -D YOURLIB_INCLUDE_DIR=/path/to/yourlib/include .

cmake bind custom command to a existed top level target fails

I want to have one top level custom target, and nested submodules which are able to extend this already existed target by adding/binding custom_command to it. For now I face a problem: if add_custom_command(TARGET target_name ...) is used not in a file where the target is defined (target is defined on top level CMakeLists.txt), then this custom_command is simply ignored.
Details:
1) custom_target created on the top-level CMakelists.txt
cmake_minimum_required(VERSION 2.8)
add_custom_target(custom_tg
COMMAND ls > custom_target.txt
)
add_custom_command(TARGET custom_tg
COMMAND ls > custom_command1.txt
)
add_subdirectory(sub)
2) In subdirectory "sub" there is other CMakeLists.txt (module) which I was hoping is able to extend the existed target with custom command.
add_custom_command(TARGET custom_tg
COMMAND ls > custom_command2.txt
)
3) When I create build directory and run
cmake .. && make custom_tg
the output files are custom_target.txt and custom_command1.txt, but there is no custom_command2.txt, and that's actually my problem is.
If I look into build/CMakeFiles/custom_tg.dir/build.make I see no mention about custom_command2.txt there. Here is content of build.make file: http://pastebin.com/zVVS4sYL.
Actually I can't find any mention about custom_command2.txt in tree of files generated by Cmake, and this looks weird to me.
Looking forward for you help.
This is usually solved by constructing dependencies between targets. For each command in one of your subdirs you would first add the 'local' command/target and then make it a dependency for the top-level one. Thereby forcing cmake to execute all 'local' targets before it executes the top-level one.

Add sources from other paths don't have CMakeLists.txt, and move output files to other path in CMake

My source tree:
/project
/project/src/<my sources>
/project/build/vs2008
/project/build/ubuntu
I want to put my CMakeLists.txt in vs2008 and ubuntu. I can accept put one CMakeLists.txt to each folder and put another global CMakeLists.txt on /project/build, but I just don't want any CMakeLists.txt in /project/src(So I can't use add_subdirectory command). I need my solution files of visual studio in /project/build/vs2008 and Makefile in /project/build/ubuntu. What commands I should know about?
I think there's nothing special you would need to know -- you just need to specify paths to your source files relative to where your CMakeLists.txt is located, and your targets should build the same as if you had placed CMakeLists.txt in your src folder. For example, project/build/ubuntu/CMakeLists.txt could look like this:
set( SrcDir ../../src )
add_executable( MyApp
${SrcDir}/file1.cpp
${SrcDir}/file2.cpp
# and so on
)

what does "<cmake_binary_dir>/bin" mean?

I want to populate with some script files in the /bin subdirectory of CMake directory, but can not find the /bin file.
What can I give guys some advice?
Thanks
I have to disagree with gvd's answer, since CMAKE_BINARY_DIR should always point to the top level directory of the build tree. If you need the location of the build tree where the current CMakelists.txt goes to, use CMAKE_CURRENT_BINARY_DIR
CMAKE_BINARY_DIR
if you are building in-source, this is the same as CMAKE_SOURCE_DIR,
otherwise this is the top level directory of your build tree
CMAKE_CURRENT_BINARY_DIR
if you are building in-source, this is the same as CMAKE_CURRENT_SOURCE_DIR,
otherwise this is the directory where the compiled or generated files from the
current CMakeLists.txt will go to
So the answer is: cmake_binary_dir/bin points to toplevelBuilddir/bin
CMAKE_BINARY_DIR is the build folder location of your CMakeLists.txt file.
Say you have:
MyProject/application/CMakeLists.txt
and your build output is in MyProject/build
when using CMAKE_BINARY_DIR in the above CMakeLists.txt, it points to MyProject/build/application/
Have a look at this list of CMake variables for more variables.

Generating a 'clean' target for a subdirectory in CMake

I want to generate a clean target for a subdirectory.
My project structure is like this:
app/
A
B
lib/
A
B
C
Sometimes I want to run clean on just app/A and don't want to clean the library.
Is it possible to tell CMake to generate a clean target for each directory?
Or a custom target like app-clean which will call clean on each application sub-directory?
You can just cd to ${CMAKE_BINARY_DIR}/app/A and run make clean there.
Of course, you can add_custom_target(app-A-clean ...) which would do this for you.
macro(add_clean_target dir)
add_custom_target(clean-${dir} COMMAND ${CMAKE_MAKE_PROGRAM} clean WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${dir})
endmacro(add_clean_target)
Now you can use it in such way:
add_clean_target(app-A)
Perhaps try to set up your CMakeLists.txt to have a project in each directory, and a "root" CMakeLists.txt which simply does add_subdirectory for all the sub-projects... then CMake will generate a solution-file or makefile in each project-directory. You can then "step into" such a directory and build and and clean one project.
For example, in one of my projects I have the following structure:
projectroot/
applications/
appA
libraries/
libA
libB
In projectroot I set up a CMakeLists.txt like this:
project(MyProject)
add_subdirectory(libraries)
add_subdirectory(applications)
In libraries/, I do the following:
project(Libraries)
add_subdirectory(libA)
add_subdirectory(libB)
add_subdirectory(libC)
And then in applications/:
project(Applications)
add_subdirectory(appA)
In this case, I can step into projectroot/applications/appA and call make or msbuild appA.sln and it will start building libA, B, C and then appA. Similarly you can call make clean or msbuild /t:Clean appA.sln.
The added benefit of an extra directory for all libraries is that I can build them at once by running make or msbuild libraries.sln in the libraries/ dir.