CMake How to check target for build

10,288

Solution 1

I have found a solution:

add_custom_command(
        OUTPUT fidl_files_generated
        DEPENDS "./interfaces/*.fidl"
        COMMAND touch fidl_files_generated
        COMMAND /home/redra/Projects/Automotive/cgen/commonapi-generator/commonapi-generator-linux-x86_64 -sk ../interfaces/ *.fidl
        COMMAND /home/redra/Projects/Automotive/cgen/commonapi_dbus_generator/commonapi-dbus-generator-linux-x86_64 ../interfaces/ *.fidl
)

add_custom_target(fidl_gen
                  DEPENDS fidl_files_generated)

set(GENERATED_FILES "../cmake-build-debug/src-gen/v1/commonapi/HelloWorldDBusProxy.cpp"
                    "../cmake-build-debug/src-gen/v1/commonapi/HelloWorldDBusDeployment.cpp"
                    "../cmake-build-debug/src-gen/v1/commonapi/HelloWorldDBusStubAdapter.cpp"
                    "../cmake-build-debug/src-gen/v1/commonapi/HelloWorldStubDefault.cpp"
                    "../cmake-build-debug/src-gen/v1/commonapi/HelloWorldDBusDeployment.cpp")
set_source_files_properties(${GENERATED_FILES} PROPERTIES GENERATED TRUE)

add_executable(
        CommonAPI_Server
        ${SOURCE_FILES}
        ${GENERATED_FILES})
add_dependencies(CommonAPI_Server fidl_gen)

Thanks all for support !!

Special thanks to Angew for support and help !!!!

Solution 2

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.

Solution 3

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)?

Share:
10,288
Denis Kotov
Author by

Denis Kotov

Some of my recent personal projects: ICC - Inter Component Communication (C++ Library/Framework) for Thread Safe Component oriented architecture. Very powerful library, take a look at examples folder ;) PUB - P.U.B. - Pack Unpack Buffers, C++ library that is used for packing and unpacking messages between components (serializer, deserializer) ferris-gc - Ferris Gc is Garbage Collector for Rust Programming Language cpp_gc - Cpp Gc is Garbage Collector for C++ Programming Language BlazorGooglePay - BlazorGooglePay is Blazor Wrapper for GooglePay Js library

Updated on July 21, 2022

Comments

  • Denis Kotov
    Denis Kotov almost 2 years

    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 ...