Cmake - get used command line flags "-D"

I recently switched several projects from autotools to cmake.

One common thing that I loved about autotools is that I go into the src build directory. there is config.log / config.status - where the ./configure --params command is located at the top, so it's easy to restart the old command line flags used.

(for example, after compiling some things - I want to add another --enable-this - copy and paste from config.log / status - and restart ./configure --old-params --enable-this )

in cmake - I have a bunch of -D flags - how can I find the command line used, for example, in config.log / status - with the cmake project?

I know there is CMakeCache ... - but its hard to extract used flags

change

i came up with the following solution:

 #save commandline to rebuild this :) set(USED_CMD_LINE "cmake ") set(MY_CMAKE_FLAGS CMAKE_BUILD_TYPE CMAKE_INSTALL_PREFIX ENABLE_SSL ENABLE_LUA ENABLE_SSH ENABLE_SNMP MYSQL_USER MYSQL_PASS MYSQL_HOST MYSQL_DB FULL_FEATURES USE_COVERAGE) FOREACH(cmd_line_loop IN ITEMS ${MY_CMAKE_FLAGS}) if(${cmd_line_loop}) STRING(CONCAT USED_CMD_LINE ${USED_CMD_LINE} "-D" ${cmd_line_loop} "=" ${${cmd_line_loop}} " ") endif() ENDFOREACH(cmd_line_loop) STRING(CONCAT USED_CMD_LINE ${USED_CMD_LINE} " .. ") #store to a file aka "config.status" FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/config.status ${USED_CMD_LINE} ) 

creates a config.status file in the build folder - containing all the specified cmake parameters.

about:

  • seems to solve my problem.
  • seems to work with subsequent cmake calls

against

  • Failed to set chmod to FILE(write ? Variable
  • MY_CMAKE_FLAGS contains known flags - must be manually updated if a new flag is added

considers

+7
cmake
source share
3 answers

One feature that may be useful is the inclusion of the CMAKE_EXPORT_COMPILE_COMMANDS flag in the CMake project cache. During build, this will cause CMake to generate the JSON file compile_commands.json in the binary directory, which contains the exact compiler requests for all translation units.

+1
source share

Cmake does not give you an easy way to list all the -D flags used (defines). However, for correctly written CMakeLists, there is no need to know the full command line with all -D flags in order to change one specific define / parameter.

Consider this snipple:

 SET(my_var_1 TRUE CACHE BOOL "my var 1") SET(my_var_2 TRUE CACHE BOOL "my var 2") message(STATUS "my_var_1 ${my_var_1}") message(STATUS "my_var_2 ${my_var_2}") 

First cake call:

 >cmake .. -Dmy_var_1=FALSE -- my_var_1 FALSE -- my_var_2 TRUE -- Configuring done -- Generating done -- Build files have been written to: out 

Second cakeake call:

 >cmake .. -Dmy_var_2=FALSE -- my_var_1 FALSE -- my_var_2 FALSE -- Configuring done -- Generating done -- Build files have been written to: out 

Please note that my_var_1=FALSE is not even specified explicitly (taken from the cache)

0
source share

You can see what is done in the bootstrap script in the CMake source code:

 # Write our default settings to Bootstrap${_cmk}/InitialCacheFlags.cmake. echo ' # Generated by '"${cmake_source_dir}"'/bootstrap # Default cmake settings. These may be overridden any settings below. set (CMAKE_INSTALL_PREFIX "'"${cmake_prefix_dir}"'" CACHE PATH "Install path prefix, prepended onto install directories." FORCE) set (CMAKE_DOC_DIR "'"${cmake_doc_dir}"'" CACHE PATH "Install location for documentation (relative to prefix)." FORCE) set (CMAKE_MAN_DIR "'"${cmake_man_dir}"'" CACHE PATH "Install location for man pages (relative to prefix)." FORCE) set (CMAKE_DATA_DIR "'"${cmake_data_dir}"'" CACHE PATH "Install location for data (relative to prefix)." FORCE) ' > "${cmake_bootstrap_dir}/InitialCacheFlags.cmake" [...] "${cmake_bootstrap_dir}/cmake" "${cmake_source_dir}" "-C${cmake_bootstrap_dir}/InitialCacheFlags.cmake" "-G${cmake_bootstrap_generator}" ${cmake_options} ${cmake_bootstrap_system_libs} " $@ " 

boostrap script creates the InitialCacheFlags.cmake file and then InitialCacheFlags.cmake it using the cmake -C option.

And - if you also want to output the values ​​to stdout - this CMake script source cache also accepts message() commands in addition to set(... CACHE) commands.

See also How to save CMake build settings

0
source share

All Articles