diff --git a/cmake/AwsPrebuildDependency.cmake b/cmake/AwsPrebuildDependency.cmake index 0a52f69a8..9898ba4f2 100644 --- a/cmake/AwsPrebuildDependency.cmake +++ b/cmake/AwsPrebuildDependency.cmake @@ -75,3 +75,39 @@ function(aws_prebuild_dependency) DESTINATION ${CMAKE_INSTALL_PREFIX} ) endfunction() + +# Get list of variables provided via command line arguments (i.e. passed as -DVARIABLE=VALUE). This function iterates +# over all variables and determines if they're provided via command line or not by checking their help strings. CMake +# sets help strings for variables provided via command line to the "No help, variable specified on the command line." +# phrase since at least v3.0. +# Via https://cmake.org/pipermail/cmake/2018-January/067002.html +# +# NOTE The project() call resets help strings for some variables (e.g. CMAKE_INSTALL_PREFIX). +# +# Populate AWS_CMAKE_CMD_ARGS with command line variables and their values. +function(aws_get_cmd_arguments_for_prebuild_dependency) + if (AWS_CMAKE_CMD_ARGS) + message(DEBUG "AWS_CMAKE_CMD_ARGS variable is already set, resetting it") + set(AWS_CMAKE_CMD_ARGS "") + endif() + + set(variables_to_ignore CMAKE_INSTALL_PREFIX) + + get_cmake_property(vars CACHE_VARIABLES) + foreach(var ${vars}) + get_property(currentHelpString CACHE "${var}" PROPERTY HELPSTRING) + if ("${currentHelpString}" MATCHES "No help, variable specified on the command line.") + if("${var}" IN_LIST variables_to_ignore) + # TODO Remove. + cmake(WARNING "Ignoring ${var}") + continue() + endif() + set(escaped_var ${${var}}) + # To store a list within another list, it needs to be escaped first. + string(REPLACE ";" "\\\\;" escaped_var "${${var}}") + list(APPEND AWS_CMAKE_CMD_ARGS "-D${var}=${escaped_var}") + endif() + endforeach() + # Store cmd variables in the cache. + set(AWS_CMAKE_CMD_ARGS ${AWS_CMAKE_CMD_ARGS} CACHE STRING "Command line variables" FORCE) +endfunction()