diff --git a/Modules/ExternalProject.cmake b/Modules/ExternalProject.cmake index 1be6cfdb8..c575a7315 100644 --- a/Modules/ExternalProject.cmake +++ b/Modules/ExternalProject.cmake @@ -32,6 +32,7 @@ # [CMAKE_COMMAND /.../cmake] # Specify alternative cmake executable # [CMAKE_GENERATOR gen] # Specify generator for native build # [CMAKE_ARGS args...] # Arguments to CMake command line +# [CMAKE_CACHE_ARGS args...] # Initial cache arguments, of the form -Dvar:string=on # #--Build step----------------- # [BINARY_DIR dir] # Specify build dir location # [BUILD_COMMAND cmd...] # Command to drive the native build @@ -549,6 +550,29 @@ function(_ep_set_directories name) endforeach() endfunction(_ep_set_directories) +function(_ep_write_initial_cache script_filename args) + # Write out values into an initial cache, that will be passed to CMake with -C + set(script_initial_cache "") + set(regex "^([^:]+):([^=]+)=(.*)$") + foreach(line ${args}) + string(REGEX REPLACE "^-D" "" line ${line}) + if("${line}" MATCHES "${regex}") + string(REGEX MATCH "${regex}" match "${line}") + set(name "${CMAKE_MATCH_1}") + set(type "${CMAKE_MATCH_2}") + set(value "${CMAKE_MATCH_3}") + set(setArg "set(${name} \"${value}\" CACHE ${type} \"Initial cache\" FORCE)") + set(script_initial_cache "${script_initial_cache}\n${setArg}") + endif() + endforeach() + # Write out the initial cache file to the location specified. + if(NOT EXISTS "${script_filename}.in") + file(WRITE "${script_filename}.in" "@script_initial_cache@\n") + endif() + configure_file("${script_filename}.in" "${script_filename}") + +endfunction(_ep_write_initial_cache) + function(ExternalProject_Get_Property name) foreach(var ${ARGN}) @@ -1224,6 +1248,14 @@ function(_ep_add_configure_command name) get_property(cmake_args TARGET ${name} PROPERTY _EP_CMAKE_ARGS) list(APPEND cmd ${cmake_args}) + # If there are any CMAKE_CACHE_ARGS, write an initial cache and use it + get_property(cmake_cache_args TARGET ${name} PROPERTY _EP_CMAKE_CACHE_ARGS) + if(cmake_cache_args) + set(_ep_cache_args_script "${CMAKE_CURRENT_BINARY_DIR}/${name}-cache.cmake") + _ep_write_initial_cache("${_ep_cache_args_script}" "${cmake_cache_args}") + list(APPEND cmd "-C${_ep_cache_args_script}") + endif() + get_target_property(cmake_generator ${name} _EP_CMAKE_GENERATOR) if(cmake_generator) list(APPEND cmd "-G${cmake_generator}" "${source_dir}") @@ -1246,6 +1278,7 @@ function(_ep_add_configure_command name) endif() configure_file(${tmp_dir}/${name}-cfgcmd.txt.in ${tmp_dir}/${name}-cfgcmd.txt) list(APPEND file_deps ${tmp_dir}/${name}-cfgcmd.txt) + list(APPEND file_deps ${_ep_cache_args_script}) get_property(log TARGET ${name} PROPERTY _EP_LOG_CONFIGURE) if(log)