[CMake] Relaying all command line arguments from SuperBuild to ExternalProject_Add

Don Hinton hintonda at gmail.com
Wed Jan 31 22:14:20 EST 2018


Hi Saad:

On Wed, Jan 31, 2018 at 6:47 PM, Saad Khattak <saadrustam at gmail.com> wrote:

> Thanks J. I could use that to add preprocessor definitions.
>
> However, my question was how I could forward 'all' the command line
> options that the user uses to generate the SuperBuild project. Suppose I
> somehow managed to capture all the command line arguments which can include
> variables specific to CMake and/or projects (i.e. nothing to do with
> preprocessor definitions) in a variable called CL_CMAKE_ARGS.
>

The example you are using is close, but not quite correct.

You need to harvest the comandline arguments before calling `project()`,
just note that it won't work if CMakeCache.txt already exists.

Some CMAKE variables will still have HELPSTRINGS, even if passed via the
command line, and users can also set a random or blank HELPSTRING if they
use a cache file.  To overcome these issues, just harvest all cache
variables and exclude the invariant ones, e.g., CMAKE_COMMAND, etc., e.g.:

$ cat ../CMakeLists.txt
cmake_minimum_required(VERSION 3.5)

message(STATUS "*** All CACHE_VARIABLES ***")
get_cmake_property(vars CACHE_VARIABLES)
foreach(var ${vars})
  get_property(currentHelpString CACHE "${var}" PROPERTY HELPSTRING)
        message("${var} = [${${var}}]  --  ${currentHelpString}")
endforeach()

project(xxx)
message(STATUS "project() called.")

message(STATUS "*** Only CACHE_VARIABLES with HELPSTRING empty or matching
\"No help,...\" ***")
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." OR "${currentHelpString}" STREQUAL "")
                message("${var} = [${${var}}]  --  ${currentHelpString}")
        endif()
endforeach()

$ rm -rf * && cmake -DFOO=1 -DCMAKE_BUILD_TYPE=Release -GNinja ..
-- *** All CACHE_VARIABLES ***
CMAKE_BUILD_TYPE = [Release]  --  No help, variable specified on the
command line.
CMAKE_COMMAND = [/opt/local/bin/cmake]  --  Path to CMake executable.
CMAKE_CPACK_COMMAND = [/opt/local/bin/cpack]  --  Path to cpack program
executable.
CMAKE_CTEST_COMMAND = [/opt/local/bin/ctest]  --  Path to ctest program
executable.
CMAKE_EXTRA_GENERATOR = []  --  Name of external makefile project generator.
CMAKE_GENERATOR = [Ninja]  --  Name of generator.
CMAKE_GENERATOR_PLATFORM = []  --  Name of generator platform.
CMAKE_GENERATOR_TOOLSET = []  --  Name of generator toolset.
CMAKE_HOME_DIRECTORY = [/Users/dhinton/cmake_test]  --  Source directory
with the top level CMakeLists.txt file for this project
CMAKE_ROOT = [/opt/local/share/cmake-3.10]  --  Path to CMake installation.
FOO = [1]  --  No help, variable specified on the command line.
-- The C compiler identification is AppleClang 9.0.0.9000039
-- The CXX compiler identification is AppleClang 9.0.0.9000039
-- Check for working C compiler:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- project() called.
-- *** Only CACHE_VARIABLES with HELPSTRING empty or matching "No help,..."
***
FOO = [1]  --  No help, variable specified on the command line.
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/dhinton/cmake_test/build

hth...
don


> If I do message(${CL_CMAKE_ARGS}) I get:
>
>  -DMY_OPTION_ONE=ON -DMY_OPTION_TWO=OFF -DSOME_VAR_1="Foo"
>
> Now, when I try something like this:
>
> ExternalProject_Add(target
> ...
> CMAKE_ARGS ${CL_CMAKE_ARGS}
> ...
> )
>
> One would expect CL_CMAKE_ARGS to be expanded and the arguments passed to
> the ExternalProject_Add function, but that is not the case. I also tried:
>
> ExternalProject_Add(target
> ...
> CMAKE_ARGS "${CL_CMAKE_ARGS}"
> ...
> )
>
> Neither works. It seems like a bug in CMake but I wanted to confirm that I
> am not missing something.
>
> On Wed, Jan 31, 2018 at 3:27 PM J Decker <d3ck0r at gmail.com> wrote:
>
>>
>> (platfrom defines is a variable suitable for like ADD_DEFINITIONS(
>> ${PLATFORM_DEFINES} )
>>
>>
>>   string( REPLACE ";" " " PLATFORM_DEFINES_ARG "${PLATFORM_DEFINES}" )
>>   ExternalProject_Add( target
>>     ....
>> CMAKE_ARGS -DPLATFORM_DEFINES=${PLATFORM_DEFINES_ARG}
>>     ......
>>  )
>>
>>
>> and in the target cmakelists to use the argument
>>
>>
>> STRING( REPLACE " " ";" PLATFORM_DEFINES ${PLATFORM_DEFINES} )
>>
>> add_definitions( ${PLATFORM_DEFINES} )
>>
>>
>>
>> On Wed, Jan 31, 2018 at 9:36 AM, Saad Khattak <saadrustam at gmail.com>
>> wrote:
>>
>>> I have the following setup:
>>> Superbuild
>>>    - ExternalProject_Add(a...)
>>>    - ExternalProject_Add(b...)
>>>    - ExternalProject_Add(c...)
>>>    - ExternalProject_Add(d...)
>>>
>>> The SuperBuild is built from command line with some options e.g.
>>> -DMY_OPTION=TRUE. I would like all these options to be passed to each of
>>> the ExternalProject_Add CMAKE_ARGS.
>>>
>>> I tried to capture the arguments using the solution posted here:
>>> https://stackoverflow.com/a/10218582/368599
>>>
>>> The arguments are captured properly (i.e. I printed them out to make
>>> sure they are correct) but relaying them to ExternalProject_Add appears to
>>> be problematic. I tried the following ways to forward the arguments (as
>>> outlined in the stackoverflow solution):
>>>
>>> ExternalProject_Add(...
>>>   CMAKE_ARGS ${CMAKE_ARGS}
>>>   )
>>>
>>> ExternalProject_Add(...
>>>   CMAKE_ARGS "${CMAKE_ARGS}" # quotes
>>>   )
>>>
>>> ExternalProject_Add(...
>>>   CMAKE_ARGS "${MY_CMAKE_ARGS}" # changed the variable name in case it
>>> conflicts
>>>   )
>>>
>>> None of that seems to work. CMake appears to ignore the commands
>>> forwarded.
>>>
>>> Is there something I am missing?
>>>
>>> Thanks,
>>> Saad
>>>
>>>
>>> --
>>>
>>> Powered by www.kitware.com
>>>
>>> Please keep messages on-topic and check the CMake FAQ at:
>>> http://www.cmake.org/Wiki/CMake_FAQ
>>>
>>> Kitware offers various services to support the CMake community. For more
>>> information on each offering, please visit:
>>>
>>> CMake Support: http://cmake.org/cmake/help/support.html
>>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>>
>>> Visit other Kitware open-source projects at http://www.kitware.com/
>>> opensource/opensource.html
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> https://cmake.org/mailman/listinfo/cmake
>>>
>>>
>>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/
> opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180131/94277acb/attachment-0001.html>


More information about the CMake mailing list