1

Is there any way to add IF ELSE ENDIF between ExternalProject_Add lines? For example

ExternalProject_Add(my_lib
    URL "https://github.com/nlohmann/json/archive/refs/tags/v3.11.2.zip"
    CMAKE_ARGS
        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX}
IF(WIN32)
        # Some Commands Here #
ELSE()
        # Another Some Commands Here #
ENDIF(WIN32)
)

You see some errors because of above lines.

Or I must create different commands for each of my conditions?

MH Alikhani
  • 110
  • 7

2 Answers2

2

Use a temporary variable.....

if(WIN32)
   set(ARGS some arguments)
else()
   set(ARGS other arguments)
endif()
ExternalProject_Add(my_lib
    URL "https://github.com/nlohmann/json/archive/refs/tags/v3.11.2.zip"
    CMAKE_ARGS
        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX}
    ${ARGS}
)
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Thanks for your answer. I Know we can use this way and pass some args to our externalproject_add. I just was looking for a way not to do like this way. – MH Alikhani Aug 29 '22 at 08:13
  • 1
    @MHAlikhani: CMake syntax doesn't allow nested function calls. Not sure what other alternatives you are looking for. There are [generator expressions](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html), which are intended for express a project specific when it is build using particular build type (CMAKE_BUILD_TYPE) in multiconfiguration generators. Parameter `CMAKE_ARGS` for `ExternalProject_Add` supports generator expressions (this is explicitly written in the documentation), so you may try to use them: https://stackoverflow.com/questions/53425282/ – Tsyvarev Aug 29 '22 at 11:23
  • That was hard topic for learn :D. I need more time on this approach you mentioned. I Found this topic good. not good for my question but about platform specific Instructions. [stackoverflow](https://stackoverflow.com/questions/9160335/os-specific-instructions-in-cmake-how-to). you can see Modern CMake Answer. @Tsyvarev – MH Alikhani Aug 29 '22 at 12:12
0

In cases where you need to deal with multiple target systems you could introduce variables with names containing CMAKE_SYSTEM_NAME and simply refer to the suitable variable in the command:

set(ARGS_Windows some arguments)
set(ARGS_Linux   other arguments)
set(ARGS_Darwin  other other arguments)

ExternalProject_Add(my_lib
    URL "https://github.com/nlohmann/json/archive/refs/tags/v3.11.2.zip"
    CMAKE_ARGS
        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX}
    ${ARGS_${CMAKE_SYSTEM_NAME}} # evaluates to the var value corresponding to the target system
)
fabian
  • 80,457
  • 12
  • 86
  • 114