What is the name of CMake's default build target? - c++

I have a custom target, and I want it to depend on the default target (the one that is built with make).
add_custom_target(foo ....)
add_dependency(foo default_target_name_goes_here)
What is the name of the default target?
I've tried ALL, ALL_BUILD, MyProjectsName, DEFAULT,...
Finding anything in the CMake documentation is always an unsuccessful adventure...
UPDATE: it seems CMake was designed in such a way that this is extremely hard to fix/implement: bugreport getting +1's since 2009. Who indeed would like to have a custom target that depends on, for example, the all target? Or in other words: who does ever write make && make test?...

The default build target does not exist as a CMake target at CMake configure time. It is only exists in the generated build system. Therefore it is not possible to have the default target depend on a custom target.

I think a possible solution depends strongly on the use case. E.g. if this is for executing a test after the system has been build you would use CTest instead of calling make directly.
To your CMakeLists.txt you would add:
add_test(NAME foo COMMAND ...)
and then use CTest for building and executing:
ctest --build-and-test ...
More generally speaking and not considering the question of why you would like to do it - I think the best thing would be to just name and rely on concrete target dependencies instead of just taking ALL targets - I just wanted to add two possibilities to do what you wanted to do.
One would be to determine/track the list of all targets used as discussed here. This would look e.g. for library targets like this (getting your own/private GlobalTargetList):
macro(add_library _target)
_add_library(${_target} ${ARGN})
set_property(GLOBAL APPEND PROPERTY GlobalTargetList ${_target})
endmacro()
and use it at the end of your main CMakeLists.txt with
get_property(_allTargets GLOBAL PROPERTY GlobalTargetList)
add_dependencies(foo ${_allTargets})
Edit: Global BUILDSYSTEM_TARGETS property was released with CMake 3.7
The second - less favorable - approach does require that the foo target is not part of the ALL build (otherwise you end-up in an endless loop):
add_custom_target(foo)
set_target_properties(foo PROPERTIES EXCLUDE_FROM_ALL 1)
add_custom_command(
TARGET foo
PRE_BUILD
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target ALL_BUILD --config $<CONFIGURATION>
)

Related

CMake Interface dependency with custom build type

So, I've found really strange behaviour in CMake creating dependency on target_link_library..
It's hard to explain in one sentence, so here is a list of requirements (I hope this all will make sence in the end)
your project must have custom build type defined through CMAKE_CONFIGURATION_TYPES ('Tools' in this example)
you must have at least 3 targets:
executable (or simply main target) (test-exe in this example)
interface library which link to main target (this could be something other than INTERFACE library, but the next target must be linked to it via interface property only) (test-env in this example)
static library which links to the interface library with specific generator expression, which is depends on custom build type (something like that 'target_link_libraries(test-env INTERFACE $<$CONFIG:Tools:test-lib>)') (test-lib in this example)
Here is the code of the CMakeLists.txt file to make it little bit clearer:
cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR)
project(multiconfiguration-test LANGUAGES CXX)
set(CMAKE_CONFIGURATION_TYPES Debug Release Tools)
set(CMAKE_CXX_FLAGS_TOOLS ${CMAKE_CXX_FLAGS_DEBUG})
set(CMAKE_EXE_LINKER_FLAGS_TOOLS ${CMAKE_EXE_LINKER_FLAGS_DEBUG})
set(CMAKE_STATIC_LINKER_FLAGS_TOOLS ${CMAKE_STATIC_LINKER_FLAGS_DEBUG})
add_library(test-env INTERFACE)
# EXCLUDE_FROM_ALL used to not build this target by default
add_library(test-lib STATIC EXCLUDE_FROM_ALL "test-lib.cpp")
target_link_libraries(test-env INTERFACE $<$<CONFIG:Tools>:test-lib>)
add_executable(test-exe "test-exe.cpp")
target_link_libraries(test-exe PRIVATE test-env)
(Files test-exe.cpp and test-lib.cpp are trivial, test-lib.cpp has a simple function, and test-exe.cpp declares this function and then calls it from main.)
When you would try to build this project with visual studio 2019/2017 generators (with "Tools" as configuration type of course: cmake --build . --config Tools), you will have next error:
LINK : fatal error LNK1104: cannot open file 'Tools\test-lib.lib' [<none_important_path_to_the_project>\test-exe.vcxproj]
And, what is important, you will not see in the terminal target test-lib being build.
So, what happened is target test-exe knows it must be linked against test-lib, but the build system doesn't know that target test-exe is dependent on target test-lib.
And now the most strange thing! If we will link this library like that: target_link_libraries(test-env INTERFACE $<$<CONFIG:Debug>:test-lib>) (so build type must be Debug), and still build project with Tools as a build type, you will see in the terminal that target test-lib is now building! (yes we have link error because test-exe can't find the function which is defined in test-lib, but this is at least expected)
So, what actually happens, the link flag of the target test-exe is correctly depends on the generator expression BUT, the actual build dependency, somehow, transforms any custom build type in this generator expression to the Debug.
Again this only happens with requirements I pointed up above, so it's not only the fault of generator expression, it's also connected to the interface dependency as well..
If we can't break any of the requirements, one possible solution will be to add direct dependency of test-lib target to test-env (like that: add_dependecies(test-env test-lib)), but this is not perfect, because even if we will use test-lib only then where is Tools as build type, we still will build this library each time, which can be undesired behavior.
I'm not really asking for solution here (but if you have one please share), I'm asking for the explanation why this even happening? Is this a bug or a really strange feature?
EDIT Small update I've found just now:
It must not even be the custom build type. The same bug can be encountered even with Release build type, so the final code can look as simple as this:
cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR)
project(multiconfiguration-test LANGUAGES CXX)
add_library(test-env INTERFACE)
add_library(test-lib STATIC EXCLUDE_FROM_ALL "test-lib.cpp")
target_link_libraries(test-env INTERFACE $<$<CONFIG:Release>:test-lib>)
add_executable(test-exe "test-exe.cpp")
target_link_libraries(test-exe PRIVATE test-env)
and be build with next command: cmake --build . --config Release
Looks like a bug with the Visual Studio generator to me. I've just tested the Ninja Multi-Config generator both on Linux and on Windows and there cmake --build <build-dir> --config Release works just fine.

CMake: compilation speed when including external makefile

I have a c++ cmake project. In this project I build (among other) one example, where I need to use another project, call it Foo. This Foo project does not offer a cmake build system. Instead, it has a pre-made Makefile.custom.in. In order to build an executable that uses Foo's features, one needs to copy this makefile in his project, and modify it (typically setting the SOURCES variable and a few compiler flags). Basically, this Makefile ends up having the sources for your executable and also all the source files for the Foo project. You will not end up using Foo as a library.
Now, this is a design I don't like, but for the sake of the question, let's say we stick with it.
To create my example inside my cmake build I added a custom target:
CONFIGURE_FILE( ${CMAKE_CURRENT_SOURCE_DIR}/Makefile.custom.in Makefile.custom)
ADD_CUSTOM_TARGET(my_target COMMAND $(MAKE) -f Makefile.custom
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
This works. I can specify some variables to cmake, which get resolved in the call to CONFIGURE_FILE, and I end up with a working Makefile.custom. Then, invoking make my_target from the build directory, I can build the executable. I can even add it to the all target (to save me the effort of typing make my_target) with
SET_TARGET_PROPERTIES(my_target PROPERTIES EXCLUDE_FROM_ALL FALSE)
Sweet. However, cmake appears to assign a single job to the custom target, slowing down my compilation time (the Foo source folder contains a couple dozens cpp files). On top of that, the make clean target does not forward to the custom makefile. I end up having to add another target:
ADD_CUSTOM_TARGET(really-clean COMMAND "$(MAKE)" clean
COMMAND "$(MAKE)" -f Makefile.custom clean
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
which, unlike my_target with all, I can't include in the clean target (can I?).
Now, I know that a cleaner solution would be to have the Foo project be built as an external project, and then link to it. However, I've been 'recommended' to use their Makefile.custom.in makefile, modifying the few lines I need (adding my sources, specifying compiler flags, and few other minor modifications). So, regardless of how neat and clean this design pattern is, my questions are:
is there a way to tell cmake that make should use more than 1 job when making the target my_target?
is there a cleaner way to include a pre-existing makefile in a cmake project? Note that I don't want (can't?) use Foo as a library (and link against it). I want (need?) to compile it together with my executable using a makefile not generated by cmake (well, cmake can help a bit, through CONFIGURE_FILE, by resolving some variables, but that's it).
Note: I am aware of ExternalProject (as suggested also in this answer), but I think it's not exactly what I need here (since it would build Foo and then use it as a library). Also, both my project and Foo are written exclusively in C++ (not sure this matter at all).
I hope the question makes sense (regardless of how ugly/annoying/unsatisfactory the resulting design would be).
Edit: I am using cmake version 3.5.2
First, since you define your own target, you can assign more cores to the build process for the target my_target, directly inside your CMakeLists.txt.
You can include the Cmake module ProcessCount to determine the number of cores in your machine and then use this for a parallel build.
include(ProcessorCount)
ProcessorCount(N)
if(NOT N EQUAL 0)
# given that cores != 0 you could modify
# math(EXPR N "${N}+1") # modify (increment/decrement) N at your will, in this case, just incrementing N by one
set(JOBS_IN_PARALLEL -j${N})
endif(NOT N EQUAL 0)
and when you define your custom target have something like the following:
ADD_CUSTOM_TARGET(my_target
COMMAND ${CMAKE_MAKE_PROGRAM} ${JOBS_IN_PARALLEL} -f Makefile.custom
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
by the way, I don't think there's the need for you to include also CMAKE_BUILD_TOOL among the COMMANDs in your target.
I believe that instead of modifying the lines as above, you could call
make -j8 my_target
and it might start 8 jobs (just an example) without modifying the CMakeLists.txt, but I cannot guarantee this works having defined the COMMAND the way you have, just try if that's enough.
For the second point, I cannot think right now of a "cleaner" way.

Generator expressions cmake: copying works in debug but not release mode

I am trying to figure out how to copy some libs depending on the config in cmake.
I tried this:
add_custom_command(TARGET Myapp
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<$<CONFIG:Debug>:${_LIBS_DEBUG}>
$<$<CONFIG:Release>:${_LIBS_RELEASE}>
$<TARGET_FILE_DIR:MyApp>)
It copies libs in Debug but not in release:
Is this supposed to be legal and should work?
If it is not legal (I do not get error), how can I achieve the same effect?
Turning my comments into an answer
What I normally do to debug those case is to add another COMMAND before the actual line in question that just echos the command line. In your case:
COMMAND ${CMAKE_COMMAND} -E echo
$<$<CONFIG:Debug>:${_LIBS_DEBUG}>
$<$<CONFIG:Release>:${_LIBS_RELEASE}>
I've run this a few tests and you will see that the $<1:...> and $<0:...> expressions are not evaluated.
So seeing this I was searching CMake's bug tracker database and this is a known issue and yet (as for CMake 3.5.2) unresolved: 0009974: CMake should support custom commands that can vary by configuration.
There are several ways proposed in this ticket that do work with existing versions of CMake.
In your case - until this issue is resolved and if you want to have it shell independent - I would do it the "old way" and call a CMake script:
CopyLibsByConfig.cmake.in
if (_CONFIG STREQUAL "Debug")
file(COPY #_LIBS_DEBUG# DESTINATION "${_DEST_PATH}")
else()
file(COPY #_LIBS_RELEASE# DESTINATION "${_DEST_PATH}")
endif()
CMakeLists.txt
...
configure_file(CopyLibsByConfig.cmake.in CopyLibsByConfig.cmake #ONLY)
add_custom_command(TARGET MyApp
POST_BUILD
COMMAND ${CMAKE_COMMAND}
-D _CONFIG=$<CONFIG>
-D _DEST_PATH="$<TARGET_FILE_DIR:MyApp>"
-P "${CMAKE_CURRENT_BINARY_DIR}/CopyLibsByConfig.cmake"
)
But the solution can very much depend on the files you want to copy to your binary output folder. And there are a lot of way doing it, like using install():
install(FILES ${_LIBS_DEBUG} CONFIGURATIONS Debug DESTINATION $<TARGET_FILE_DIR:MyApp>)
install(FILES ${_LIBS_RELEASE} CONFIGURATIONS Release DESTINATION $<TARGET_FILE_DIR:MyApp>)
set(CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD 1)
Obviously that's not the way install() is meant to be used, so consider using the INSTALL or PACKAGE targets properly to distribute your application and all its dependencies.
And if we are talking about Visual Studio runtime DLLs you most likely want to take a look at the InstallRequiredSystemLibraries CMake module.
Other solution is to use generator expression.
For example I have cppzmq (shared library) and cppzmq-static (static library with static dependencies). I would like to have faster debug builds so I use cppzmq in Debug build and in (other) e.g. release I want one big fat exec.
target_link_libraries(CommunicationCommonLib PUBLIC
$<IF:$<CONFIG:Debug>,cppzmq,cppzmq-static>
Dexode::EventBus
gcpp
protobuf::libprotobuf
)

Expected build-failure tests in CMake

Sometimes it's good to check that certain things fail to build, e.g.:
// Next line should fail to compile: can't convert const iterator to iterator.
my_new_container_type::iterator it = my_new_container_type::const_iterator();
Is it possible to incorporate these types of things into CMake/CTest? I'm looking for something like this in CMakeLists.txt:
add_build_failure_executable(
test_iterator_conversion_build_failure
iterator_conversion_build_failure.cpp)
add_build_failure_test(
test_iterator_conversion_build_failure
test_iterator_conversion_build_failure)
(Of course, these specific CMake directives don't exist, to the best of my knowledge.)
You can do this more or less as you described. You can add a target which will fail to compile, then add a test which invokes cmake --build to try to build the target. All that remains is to set the test property WILL_FAIL to true.
So, say you have your tests in a file named "will_fail.cpp" which contains:
#if defined TEST1
non-compiling code for test 1
#elif defined TEST2
non-compiling code for test 2
#endif
Then you can have something like the following in your CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(Example)
include(CTest)
# Add a couple of failing-to-compile targets
add_executable(will_fail will_fail.cpp)
add_executable(will_fail_again will_fail.cpp)
# Avoid building these targets normally
set_target_properties(will_fail will_fail_again PROPERTIES
EXCLUDE_FROM_ALL TRUE
EXCLUDE_FROM_DEFAULT_BUILD TRUE)
# Provide a PP definition to target the appropriate part of
# "will_fail.cpp", or provide separate files per test.
target_compile_definitions(will_fail PRIVATE TEST1)
target_compile_definitions(will_fail_again PRIVATE TEST2)
# Add the tests. These invoke "cmake --build ..." which is a
# cross-platform way of building the given target.
add_test(NAME Test1
COMMAND ${CMAKE_COMMAND} --build . --target will_fail --config $<CONFIGURATION>
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME Test2
COMMAND ${CMAKE_COMMAND} --build . --target will_fail_again --config $<CONFIGURATION>
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
# Expect these tests to fail (i.e. cmake --build should return
# a non-zero value)
set_tests_properties(Test1 Test2 PROPERTIES WILL_FAIL TRUE)
You can obviously wrap all of this into a function or macro if you have a lot of these to write.
#Fraser's answer is a good approach, in particular the WILL_FAIL property is good advice. There is an alternative to making the failing target part of the main project though. The use case in the question is pretty much what the ctest --build-and-test mode is meant for. Rather than making the expected-to-fail target part of the main build, you can put it in its own separate mini project which is then built as part of a test. An example of how this might look in the main project goes something like this:
add_test(NAME iter_conversion
COMMAND ${CMAKE_CTEST_COMMAND}
--build-and-test
${CMAKE_CURRENT_LIST_DIR}/test_iter
${CMAKE_CURRENT_BINARY_DIR}/test_iter
--build-generator ${CMAKE_GENERATOR}
--test-command ${CMAKE_CTEST_COMMAND}
)
set_tests_properties(iter_conversion PROPERTIES WILL_FAIL TRUE)
This has the advantage that it will be part of the project's test results and will therefore be more likely to get executed regularly as part of normal testing processes. In the above example, the test_iter directory is essentially it's own separate project. If you need to pass information to it from the main build, you can do that by adding --build-options to define cache variables to pass to it's CMake run. Check the latest docs for recently corrected/clarified help on this area.
For the particular example in the question I agree with the comment. This is should be tested with a static_assert rather than a build failure test.
For a way of adding build failure test with CMake (in the cases where this is a good idea) I recently created a library which more or less allows adding such tests with a single CMake function call. It expands on the accepted answer and allows build-failure test and checks that the failure happened because of a specific error (the expected error can be provided in CMake or in a source file): https://github.com/iboB/icm/blob/master/icm_build_failure_testing.cmake

CMake: relink when linked package is rebuilt

I apologize if this has been asked already but I can't find online the right way to get this thing to work.
I have a cmake project Foo which depends on another cmake project Bar. Goal: whenever Bar is reinstalled (changing only the libraries, not the headers), then Foo should re-link (without rebuilding, of course).
So, in the CMakeLists.txt in the top folder of project Foo (which has only one target, an executable) I have the cmake command
FIND_PACKAGE(Bar REQUIRED)
Which, correctly, finds project Bar at configure time. In the part where I create the target, I have
LINK_DIRECTORIES (${BAR_LIBRARY_DIRS} )
ADD_EXECUTABLE(foo.exe main.cpp)
TARGET_LINK_LIBRARIES(foo.exe ${BAR_LIBRARIES})
The two variables in there are defined in BarConfig.cmake, which is the one that FIND_PACKAGE(Bar) looks for, and contains only the following instructions
SET(BAR_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/../../../include")
SET(BAR_LIBRARY_DIRS "${CMAKE_CURRENT_LIST_DIR}/../../../lib")
SET(BAR_LIBRARIES bar)
I printed on screen those variables and they contain what you expect them to contain (/path/to/folder/include, /path/to/folder/lib and bar).
So, I read somewhere that cmake cannot add a dependency to a library which is not specified with its complete path. One should write
TARGET_LINK_LIBRARIES (foo.exe full-path-to-bar-libraries)
In that case, it works. But it's unsatisfactory. First, cause the path can change. But you can read it from a variable, you might say. True. But, second, even in that case, if the project Bar contains a number of libraries that is not known, one would have to sweat to create the correct string to add there...
However, I also read that if that library is also built and installed with cmake, it should work automatically. As a matter of fact, I have another project pair, A depends on B, both built with cmake. In that case the dependence works. Unfortunately, the project B is HUGE, and defines TONS of cmake macros, and I can't identify the part where it sets up the right variables.
Do you have any idea of how to get Foo to re-link (without rebuilding) every time that the library Bar is reinstalled? I would like to avoid to use full path.
Thanks
Edit: to be more clear: if library Bar set up a variable BAR_LIBRARIES containing all its libraries with the full path, then TARGET_LINK_LIBRARIES would work. However, most likely BAR_LIBRARIES would contain 'bar', rather than '/some/path/libbar.a'. I'd like cmake to be able, given the directories provided with LINK_DIRECTORIES and the library names provided with TARGET_LINK_LIBRARIES to put the two pieces together. E.g. if BAR_LIBRARY_DIRS contains '/folder1/;/folder2/' and BAR_LIBRARIES contains 'bar1;bar2', I'd like cmake to establish a dependency on libbar1.a and libbar2.a, found in any of the following:
/folder1/libbar1.a
/folder2/libbar1.a
/folder1/libbar2.a
/folder2/libbar2.a
And relink if any of the ones upon which a dependency has been created is changed since the last linking.
and contains only the following instructions
You don't need to set all this variables manually, just use CMakePackageConfigHelpers module:
configure_package_config_file(
"./FooConfig.cmake.in"
"${foo_config}"
INSTALL_DESTINATION ${CONFIG_INSTALL_DESTINATION}
PATH_VARS CONFIG_INSTALL_DESTINATION
)
install(
FILES "${foo_config}"
DESTINATION ${CONFIG_INSTALL_DESTINATION}
)
and install targets:
install(
TARGETS foo DESTINATION ${LIB_INSTALL_DESTINATION} EXPORT FooTargets
)
install(
EXPORT FooTargets NAMESPACE Foo:: DESTINATION ${CONFIG_INSTALL_DESTINATION}
)
Usage in other project:
find_package(Foo CONFIG REQUIRED)
target_link_libraries(boo Foo::foo)
That's all. Work like a charm (Makefile generator example):
> cmake -HFoo -B_builds/Foo -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="`pwd`/_install"
> cmake --build _builds/Foo/ --target install
> cmake -HBoo -B_builds/Boo -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="`pwd`/_install" -DCMAKE_VERBOSE_MAKEFILE=ON
> cmake --build _builds/Boo
One more time (check no relink):
> cmake --build _builds/Boo
Modify target Foo and reinstall:
> echo "void some(){}" >> Foo/foo.cpp
> cmake --build _builds/Foo/ --target install
Now build Boo:
> cmake --build _builds/Boo
...
Linking CXX executable boo.exe
...
Relink!