13

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

Helmut Januschka
  • 1,578
  • 3
  • 16
  • 34
  • 1
    This is an excellent question--one that I have had for a couple of years but have not asked on StackOverflow. I do not think there is a way to get the set of -D flags used when invoking cmake. The answer sakra gave below shows how to get see the exact commands that are a result of the cmake invocation, but that does not give you the args used for the cmake configure invocation. A correct answer is more difficult than one might expect since a user may continually reconfigure a cmake build directory using `cmake -Danotherflag=value .`. – Phil Jun 16 '15 at 20:39
  • thats sad! in fact i really love cmake, it solves like 3.5mio issues (i learned to live with) from autotools - but the lack of getting user supplied commandline flags, is really bad. – Helmut Januschka Jun 17 '15 at 06:14
  • see my edited question - it alteast for me solves my issue (in a not perfect way anyway) – Helmut Januschka Jun 17 '15 at 06:50
  • What about the generated CMakeCache.txt it contain the used flags... – intika Feb 19 '19 at 07:11

3 Answers3

2

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)

Peter Petrik
  • 9,701
  • 5
  • 41
  • 65
  • thx for your answer i know that features, based on the cmakecache but for my question its more like - i have a build folder on server A - and want to compile the source on a different server from scratch (in autotools days - i just downloaded the source to the new server - copied the configure command from config status, and run this on the new server) see my edited question - this is actually a way i can live with – Helmut Januschka Jun 17 '15 at 07:08
  • 1
    Given your most recent edit where you say you can live with keeping track of ``MY_CMAKE_FLAGS``, you should consider the cmake module [FeatureSummary](http://www.cmake.org/cmake/help/v3.3/module/FeatureSummary.html). – Phil Jun 17 '15 at 13:56
  • may be CMakeCache.txt could be copied to the new project build dir and running cmake again will take the cache file in consideration... – intika Feb 19 '19 at 07:17
1

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.

sakra
  • 62,199
  • 16
  • 168
  • 151
1

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

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149