I have tried to find solution: How to check target for build ?
Consider the following CMake script:
cmake_minimum_required(VERSION 3.5.1)
project(cppTests)
# How to check at this point the target of build
if(TARGET "cppTests")
message(STATUS "Target is cppTests")
else()
message(STATUS "Target is not cppTests")
endif()
message(STATUS "Target is ${TARGET}")
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(cppTests ${SOURCE_FILES})
Then I call the following:
/home/username/Software/clion-2017.1.1/bin/cmake/bin/cmake --build /home/username/Projects/cppTests/cmake-build-debug --target cppTests -- -j 8
How can I check target cppTests in CMake script after --target options ? I am looking for something like MAKECMDGOALS in Makefiles. I have found any useful solution ...
The developer chooses which target(s) to build after CMake has generated the project files. When CMake is being run, CMake cannot know what target the developer will build. Therefore, what you are asking for doesn't really make sense for a CMake project. In a Makefile, there is no separate configure step, it is part of the build, which is how it can provide a feature like MAKECMDGOALS. It might also be worth reconsidering if you really want this functionality of Makefiles anyway, as it isn't meant for typical use (emphasis mine):
Make will set the special variable MAKECMDGOALS to the list of goals you specified on the command line. If no goals were given on the command line, this variable is empty. Note that this variable should be used only in special circumstances.
In your example, you are also misusing if(TARGET...)
. That construct is used to test whether a particular CMake target has been defined by the project (which in your example it doesn't until after the if()
command, so it would always evaluate to false). See the docs here for details.
What you're asking makes no sense in the CMake world. The timeline of a CMake project consists of 3 distinct steps:
configure time > generate time > build time
\---------------v--------------/ \---v----/
CMake is running make is running
In the configure step, CMake reads and parses the CMakeLists.txt
files and stores the processed data in memory. This happens when you press the Configure
button in CMake GUI.
In the generate step, CMake acts on the data in memory to generate buildsystem files (Makefiles, Visual Studio solutions & projects, etc.). This happens when you press the Generate
button in CMake GUI.
Running cmake
from the command line does a configure immediately followed by a generate.
Then CMake is done with the buildsystem and terminates its run. You now have a working buildsystem which can be processed by the build tool (such as make
or msbuild
). Even if you run this build tool using cmake --build
, this new invocation of cmake
does not read your CMakeLists.txt
files at all. It just accesses the bare minimum from the generated CMakeCache.txt
to figure out which build tool to run, and runs that.
You didn't specify what you're trying to achieve with the information, but perhaps you could work with things like add_custom_command(TARGET cppTests PRE_LINK)
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With