2

I want to pass some settings to an External Project via a ProjectSettings.cmake file. I know I can use the CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE for this purpose (passed via CMAKE_ARGS to the ExternalProject_add()).

But I have to do this for many external projects that my main project builds and do not want to keep repeating it - I have a generic variable defined for CMAKE_ARGS that is presently passed to every call of ExternalProject_Add() and I want to just append to this.

Is there a wildcard mechanism for the PROJECT-NAME field of this CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE?

Or is there a way by which I can make the CMakeLists.txt of all the external projects I want to build include my ProjectSettings.cmake? Note that I do not have access to change all of them - hence the need to manipulate them from the parent project.

1 Answers1

0

Wrapping reoccurring code centrally is one of the major purposes of CMake macros and functions.

So in your case this would look something like this:

macro(My_ExternalProject_Add _name)
    ExternalProject_Add(
        ${_name}
        CMAKE_ARGS -DCMAKE_PROJECT_${_name}_INCLUDE:FILEPATH=ProjectSettings.cmake
        ${ARGN}
    )
endmacro()

The ARGN will add all parameters given after _name. So just place the macro() in your root CMakeLists.txt file and replace all ExternalProject_Add() calls with My_ExternalProject_Add().

Alternatively you could use CMAKE_ARGS -C ProjectSettings.cmake initial cache command line option to pre-load settings for an external project (then you won't need the project's name).

References

Florian
  • 39,996
  • 9
  • 133
  • 149
  • @GaneshSathyanarayanan Happy to help, and welcome to Stack Overflow. If this answer - or any other one - solved your issue, please mark it as accepted. – Florian Apr 11 '18 at 07:54
  • If wrapping would be a central purpose of cmake, then cmake would support better argument quoting. Everyone develops their own tiny little puny language and then cannot even forward argument lists to sub-calls where arguments may be empty... – user1050755 Apr 14 '21 at 14:18