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_FLAGS
contains the known flags - needs to be manually updated if a new flag is added
regards
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 filecompile_commands.json
in the binary directory that contains the exact compiler calls for all translation units.You may want to take a look at what is done in the bootstrap script in CMake's source code:
The
boostrap
script is generating aInitialCacheFlags.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 acceptsmessage()
commands besides theset(... CACHE)
commands.See also How to store CMake build settings
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:
First cmake invocation:
Second cmake invocation:
Note that
my_var_1=FALSE
even it is not explicitely stated (taken from cache)