Adding debug flags after configuration is done - cmake

We are currently porting a large project from GNU autotools to CMake. An open problem that is of great interest to our users (Scientific Computing: users are developpers) is to switch to debug compiler flags without reconfiguring the whole project.
There is of course a workaround to add some thing like
set_property(TARGET <target> PROPERTY COMPILE_FLAGS <debugflags>)
to the CMakeLists.txt and run
make target
and count on cmakes caching abilities to only configure that particular
But for our users that are used to automakes
make CXXFLAGS="<debugflags>" <target>
this is no convincing way to go.
The same goes for having 2 built directories, one with and one without debug flags.
I have looked for more possibilites to mimic such behaviour without success. Do you know any? Or do you know whether any such features are planned for future cmake releases?

The "problem" is that you have to modify your CmakeLists file and
afterwards undo that change
You don't need to change the CMakeLists file for this. CMake allows specifying a build type on the command line for make based generators:
cmake -DCMAKE_BUILD_TYPE=Debug [...] && make
This already adds the -g compile flag for you. If you need additional project specific flags, you can add them conditionally depending on the build type.
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
# do your stuff
endif()
Note that once you have specified a build type, CMake will keep using that same build type for all subsequent runs unless you explicitly set a different one through the command line or delete the cache.

Related

CMake: How to set dependency from executable to custom command [duplicate]

I'm trying to use add_custom_command to generate a file during the build. The command never seemed to be run, so I made this test file.
cmake_minimum_required( VERSION 2.6 )
add_custom_command(
OUTPUT hello.txt
COMMAND touch hello.txt
DEPENDS hello.txt
)
I tried running:
cmake .
make
And hello.txt was not generated. What have I done wrong?
The add_custom_target(run ALL ... solution will work for simple cases when you only have one target you're building, but breaks down when you have multiple top level targets, e.g. app and tests.
I ran into this same problem when I was trying to package up some test data files into an object file so my unit tests wouldn't depend on anything external. I solved it using add_custom_command and some additional dependency magic with set_property.
add_custom_command(
OUTPUT testData.cpp
COMMAND reswrap
ARGS testData.src > testData.cpp
DEPENDS testData.src
)
set_property(SOURCE unit-tests.cpp APPEND PROPERTY OBJECT_DEPENDS testData.cpp)
add_executable(app main.cpp)
add_executable(tests unit-tests.cpp)
So now testData.cpp will generated before unit-tests.cpp is compiled, and any time testData.src changes. If the command you're calling is really slow you get the added bonus that when you build just the app target you won't have to wait around for that command (which only the tests executable needs) to finish.
It's not shown above, but careful application of ${PROJECT_BINARY_DIR}, ${PROJECT_SOURCE_DIR} and include_directories() will keep your source tree clean of generated files.
Add the following:
add_custom_target(run ALL
DEPENDS hello.txt)
If you're familiar with makefiles, this means:
all: run
run: hello.txt
The problem with two existing answers is that they either make the dependency global (add_custom_target(name ALL ...)), or they assign it to a specific, single file (set_property(...)) which gets obnoxious if you have many files that need it as a dependency. Instead what we want is a target that we can make a dependency of another target.
The way to do this is to use add_custom_command to define the rule, and then add_custom_target to define a new target based on that rule. Then you can add that target as a dependency of another target via add_dependencies.
# this defines the build rule for some_file
add_custom_command(
OUTPUT some_file
COMMAND ...
)
# create a target that includes some_file, this gives us a name that we can use later
add_custom_target(
some_target
DEPENDS some_file
)
# then let's suppose we're creating a library
add_library(some_library some_other_file.c)
# we can add the target as a dependency, and it will affect only this library
add_dependencies(some_library some_target)
The advantages of this approach:
some_target is not a dependency for ALL, which means you only build it when it's required by a specific target. (Whereas add_custom_target(name ALL ...) would build it unconditionally for all targets.)
Because some_target is a dependency for the library as a whole, it will get built before all of the files in that library. That means that if there are many files in the library, we don't have to do set_property on every single one of them.
If we add DEPENDS to add_custom_command then it will only get rebuilt when its inputs change. (Compare this to the approach that uses add_custom_target(name ALL ...) where the command gets run on every build regardless of whether it needs to or not.)
For more information on why things work this way, see this blog post: https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/
This question is pretty old, but even if I follow the suggested recommendations, it does not work for me (at least not every time).
I am using Android Studio and I need to call cMake to build C++ library. It works fine until I add the code to run my custom script (in fact, at the moment I try to run 'touch', as in the example above).
First of,
add_custom_command
does not work at all.
I tried
execute_process (
COMMAND touch hello.txt
)
it works, but not every time!
I tried to clean the project, remove the created file(s) manually, same thing.
Tried cMake versions:
3.10.2
3.18.1
3.22.1
when they work, they produce different results, depending on cMake version, one file or several. This is not that important as long as they work, but that's the issue.
Can somebody shed light on this mystery?

Is it possible to force CMake to run add_compile_definitions() each time?

I have an embedded project (using ESP-IDF which builds projects with CMake), where I have a props.json file that contains several settings (e.g. "device type"). For example based on the actual value of "deviceType" the CMake open and read props.json by calling execute_process() and jq, then defines C preprocessor macros, such as: DEVICE_TYPE_A by using add_compile_definitions().
The problem is that, this will run only when I modify the CMakeLists.txt or clean the whole project, but I don't want to recompile each components when I change the props.json only the files that I wrote (so, depend on the settings). I'd like to make CMake read the file each time I build the project without cleaning it.
I did my research, so I know there are add_custom_target() and add_custom_command() that behave that way, however add_compile_definitions() cannot be called in a script. Is there a solution to achieve this or should I just use a header file configured by configure_file() and leave add_compile_definitions() alone?
This is actually pretty easy and you don't need to manually reconfigure CMake. Just add the following to the CMakeLists.txt in the directory containing your props.json file:
set_property(DIRECTORY . APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS props.json)
This will add props.json to the list of files that the CMake-generated build scans when determining whether to re-run the CMake configure step. See the docs on CMAKE_CONFIGURE_DEPENDS for more detail.
In general, you should never need to manually re-run CMake1 after the first configure. If you do, it is an indication that you have not communicated all of the necessary information for CMake to generate a correct build system.
1 There is one notable exception: Xcode is known to be buggy when re-running the CMake configure step automatically.

CMake compile options for compile test only

I am using CMake to cross compile a C project for an embedded (heterogeneous) multi-core system. The compiler takes an mandatory argument (-t<type>, the target type). This flag has to be set to pass CMake's compiler test. I am adding this flag in a toolchain file as follows:
add_compile_options(-tMYPLATFORMTYPE)
The problem with this approach is, all project files will be compiled with this flag. Is there a way to configure compile flags for the test compilation only, without affecting the main project configuration? (Note: Within the project different files shall have different values for this flag.)
What I am looking for is something like:
set(CMAKE_TRY_COMPILE_COMPILE_OPTIONS "-tMYPLATFORMTYPE")
I could disabled the compile test, but I would prefer to keep it.
You can check the IN_TRY_COMPILE property and set the flag for try-compile configurations only:
get_property(IS_IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE)
if(IS_IN_TRY_COMPILE)
add_compile_options(-tMYPLATFORMTYPE)
endif()

Creating a library in CMake depending on source files not available when generating build files

I have a CMake configuration file building two libraries:
a third-party library (here called ThirdPartyLib) containing a real-time OS / board support package from a supplier. It is built outside CMake using the autotools toolchain.
an extended version of the former library (here called ExtendedThirdPartyLib)
Unfortunately, some source code that I need (various tools) are not built in the ordinary build script for (1). Since I don't want to mess with the suppliers build script I want to add another library (2), building the missing files and thus extending the library from the supplier.
I want to able to do something like this in CMakeFiles.txt:
cmake_minimum_required(VERSION 3.2)
project(bsp)
include(ExternalProject)
ExternalProject_Add(
ThirdPartyLib
URL <http://some.url/bsp.tar.bz2
BUILD_COMMAND make -C ../external/ThirdPartyLib/src
)
set_target_properties(ThirdPartyLib PROPERTIES EXCLUDE_FROM_ALL TRUE)
add_library(ExtendedThirdPartyLib
${CMAKE_CURRENT_BINARY_DIR}/some/path/missing_file1.c
${CMAKE_CURRENT_BINARY_DIR}/some/path/missing_file2.c
)
add_dependencies(ExtendedThirdPartyLib ThirdPartyLib)
target_include_directories(ExtendedThirdPartyLib PUBLIC
${CMAKE_CURRENT_BINARY_DIR}/some/path/include
)
target_link_libraries(ExtendedThirdPartyLib ThirdPartyLib)
The problem here is that the path to missing_file1.c and missing_file2.c are not valid when CMake is generating the build files (they are extracted from the tarball from the supplier). CMake exits with an error output saying: "Cannot find source file".
Is there a neat way to make this work? I.e. is it possible to convince CMake that certain non-existant input files will exist when building of the library begins? Or is there any other recommended way to solve this issue?
(I have temporary made local copies of the files I need to build from the suppliers tarball, but that is of course not a good solution. If those files are changed in future versions of the suppliers package and I forget to overwrite my local copies it could be a horrible mess...
Another "solution" would be to create a small makefile outside CMake and use another ExternalProject_Add in the CMakeFiles.txt somehow. But that's not a good solution either, e.g. if compile and linker flags are modified I need to remember to change the makefile too.)
Personally, I dislike the ExternalProject_Add command, because it does way too many things for my taste, but I've digressed.
What if you do something like this, where bar is simulating your ExtendedThirdPartyLib target, since it depends on generated files
cmake_minimum_required(VERSION 3.11)
project(lol C)
set(SOURCES lol.c) # only this file exists
add_library(lol ${SOURCES})
set(FOO_FILES "foo1.c" "foo2.c")
add_custom_command(OUTPUT ${FOO_FILES}
COMMAND ${CMAKE_COMMAND} -E touch ${FOO_FILES}
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
COMMENT "Creating ${FOO_FILES}"
VERBATIM)
add_custom_target(foo DEPENDS ${FOO_FILES})
add_library(bar ${FOO_FILES})
add_dependencies(bar foo)
target_link_libraries(lol bar)
The whole approach hinges on the fact that the method, where produced/generated files are procured, is explicitly defined via the custom command and associated custom target.
You should modify the custom command to extract the required files (e.g. could even call some external script) from the tarball (which might require downloading with curl or something similar).

CMake compiler settings specific to the local working copy

Currently to change a compiler flag when using gcc, I edit the CMakeLists.txt for the build target:
if (UNIX)
add_definitions(-Wall)
add_definitions(-g)
#add_definitions(-O2)
endif (UNIX)
The problem with this is that git picks up the change. If I go ahead and commit this change, I will annoy the other developers who expect to use -O2 instead of -g but they get my version when they pull unrelated changes. Normally, I could just exclude this change from my commits, but when I make an actual change to the CMakeLists.txt file, there is no way to avoid pushing up my personal selection of compile flags.
Is there a way to tell CMake to create a file in the build/ directory (specific to each working copy, and therefore to each developer) that an individual person can modify to their heart's desire without touching project files (everything but build/). Naturally, our build/ is not committed to the git repository.
It might be helpful to note that when using Visual Studio instead of gcc, the IDE handles this for us through its UI which modifies the VS solution file in build/. The problem is that we have no such mechanism when using GNU Makefiles.
Our project is organized like this:
ourproject/
bin/
build/ <-- CMake-generated stuff goes here
lib/
src/
abuildtarget/
anotherbuildtarget/
source.cpp
source.h
CMakeLists.txt
You are using CMake incorrectly here. The add_definitions function is not for adding compiler options like you are doing; rather, it is to add pre-processor definitions such as add_definitions(-DDEBUG).
What you want to do is set the CMAKE_<language>_FLAGS when you configure to the options you want. If there is a standard set you need, then put that in the CMakeLists.txt file such as:
if(${CMAKE_Fortran_COMPILER_ID} STREQUAL "Intel")
set(CMAKE_Fortran_FLAGS_RELEASE "-O2 -xhost" CACHE STRING "" FORCE)
set(CMAKE_Fortran_FLAGS_NODEBUG "-O0" CACHE STRING "" FORCE)
mark_as_advanced(CMAKE_Fortran_FLAGS_NODEBUG)
set(CMAKE_Fortran_FLAGS_PROFILING "-O2 -xhost -p" CACHE STRING "" FORCE)
mark_as_advanced(CMAKE_Fortran_FLAGS_PROFILING)
set(CMAKE_Fortran_FLAGS_DEBUG
"-DDEBUG -g -check noarg_temp_created -C -traceback" CACHE STRING "" FORCE)
endif()
Where Fortran can be replaced by CXX or C.
In this case, the CMAKE_<language>_FLAGS_<build type> sets the flags based on the CMAKE_BUILD_TYPE variable. If it is set to Release, then it uses CMAKE_Fortran_FLAGS_RELEASE. We added several other possible build types. If the user wants something that isn't one of the standard build types, then they set CMAKE_<language>_FLAGS to whatever they want when configuring and it overrides the build type setting and uses the user-defined flags instead.
While there's almost certainly a better method of utilising cmake to solve your problem, this part of your question is easily addressed:
but when I make an actual change to the CMakeLists.txt file, there is no way to avoid pushing up my personal selection of compile flags.
This is only true if you commit your specific, personal file changes to your local repository, and even then you can either git-revert when you make a patch to push, or use a filter between your development and ready-to-push-to-other-developers branches - the rough idea can be found here, although you'll have to edit it pretty heavily to remove the offending personal lines from the questions example.
A better option, then, is to not commit the personal changes. From the answer here, you can easily commit specific changes to a file.
A thing to note, however, is that you'll end up with a permanently dirty work-tree if you've got uncommitted changes to a non-ignored file; this may or may not be an issue, depending on your workflow for merges etc.