1

I'm using ExternalProject_Add to build an external make-based project. I'd like to propagate flags according to the current build-configuration.

I basically have to following command:

ExternalProject_Add(
        ...
        BUILD_COMMAND make CFLAGS=${CMAKE_C_FLAGS_???}
        ...
        )

and I want to use CMAKE_C_FLAGS_DEBUG when the project is built in Debug, or whatever the current configuration is. How can that be done? I've tried this, but it doesn't parse:

ExternalProject_Add(
        ...
        BUILD_COMMAND make CFLAGS=${CMAKE_C_FLAGS_$<CONFIG>}
        ...
        )
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • If you use single-configuration build tool, you may obtain current configuration name via `CMAKE_BUILD_TYPE` variable. In case of multi-configuration build tool you seems out of luck: generator expressions work only where it is explicitly documented. – Tsyvarev Dec 15 '17 at 22:48
  • That’s a good start, but the problem is that that gives me e.g. Debug, where I need DEBUG for the variable name. – Björn Pollex Dec 16 '17 at 09:46
  • 1
    A variable's suffix is just an upper-case version of build type. Use `string(TOUPPER)` for this transformation. – Tsyvarev Dec 16 '17 at 12:21

1 Answers1

0

I haven't found a proper solution, but a reasonable compromise. Since the most important thing for us was to use the correct flags in the release build, I'm now checking specifically for that, using this rather convoluted workaround for the lack of if-then-else in generator expressions prior to 3.8:

set(default_flags "${CMAKE_C_FLAGS} -g -O2")
set(release_flags "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_RELEASE}")
set(flags $<$<CONFIG:Release>:${release_flags}>$<$<NOT:$<CONFIG:Release>>:${default_flags}>)

ExternalProject_Add(
    ...
    BUILD_COMMAND make CFLAGS=${flags}
    ...
    )
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283