Search code examples
cmakecmake-cache

CMake: print root project name using cmake commandline


Is it possible to print project name value of a CMake project using cmake.exe?

for example:

cmake -LA CMakeLists.txt | grep CMAKE_INSTALL_PREFIX

to get the install prefix configured for the project.

Is there any way similar to get CMAKE_PROJECT_NAME which is defined in project('xyz')?


Solution

  • The variable CMAKE_PROJECT_NAME is not a cached one, so it cannot be printed by running cmake -LA.

    Using variable CMAKE_PROJECT_INCLUDE it is possible to define a hook for project() calls. That hook could just print a project name.

    For example, consider such script project_hook.cmake:

    message(STATUS "Project name is ${PROJECT_NAME}")
    

    You could configure a CMake project with

    cmake -DCMAKE_PROJECT_INCLUDE=</path/to/project_hook.cmake> </path/to/source/dir> | grep -m1 "Project name is"
    

    so you will get the line

    Project name is <...>
    

    for the first project() call encountered in the CMake project.

    This approach works for CMake 3.15 or newer.