Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake - get the used commandline flags "-D"

Tags:

cmake

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

like image 614
Helmut Januschka Avatar asked Jun 16 '15 14:06

Helmut Januschka


1 Answers

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)

like image 144
Peter Avatar answered Oct 21 '22 01:10

Peter