cmake - get the used commandline flags “-D”

2019-06-15 13:42发布

i recently switched a few projects from autotools to cmake.

one common thing i liked on autotools is that - if i go into the src build directory. there is config.log/config.status - where at the top the ./configure --params command is listed - so it is easy to rerun the former used commandline flags.

(like after compiling some stuff - i want to add a another --enable-this - so copy & paste from config.log/status - and rerun the ./configure --old-params --enable-this)

in cmake - i have a bunch of -D flags - how can i find the used commandline like in config.log/status - with a cmake project?

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

edit:

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 file config.status in the build folder - containing all set cmake params.

pro:

  • seems to solve my problem
  • seems to work on subsequent cmake calls

con:

  • unable to set chmod on FILE(write ? the variable
  • MY_CMAKE_FLAGScontains the known flags - needs to be manually updated if a new flag is added

regards

标签: cmake
3条回答
萌系小妹纸
2楼-- · 2019-06-15 13:49

One feature that may be helpful is turning on the flag CMAKE_EXPORT_COMPILE_COMMANDS in the project's CMake cache. During build, this will make CMake generate a JSON file compile_commands.json in the binary directory that contains the exact compiler calls for all translation units.

查看更多
时光不老,我们不散
3楼-- · 2019-06-15 13:59

You may want to take a look at what is done in the bootstrap script in CMake's 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} "$@"

The boostrap script is generating a InitialCacheFlags.cmake file and is then preloading it with the cmake -C option.

And - if you additionally want to output the values to stdout - this initial-cache CMake script also accepts message() commands besides the set(... CACHE) commands.

See also How to store CMake build settings

查看更多
狗以群分
4楼-- · 2019-06-15 14:06

Cmake does not give you easy way to list all used -D flags (defines). However, for correctly written CMakeLists, it is not needed to know the full command line with all -D flags to change one particular define/option.

Consider this snipplet:

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 cmake invocation:

>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 cmake invocation:

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

Note that my_var_1=FALSE even it is not explicitely stated (taken from cache)

查看更多
登录 后发表回答