6

Say I have code that looks like this:

target_compile_options(${PROJECT_NAME} 
    PRIVATE
        $<$<CXX_COMPILER_ID:MSVC>:...Options...>
        $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:...Options...>
)

As evident, the second line (which checks if the compiler is GCC, Clang, or AppleClang), is very long in length. In order to shorten this to multiple lines, I tried the following:

target_compile_options(${PROJECT_NAME} 
    PRIVATE
        $<$<CXX_COMPILER_ID:MSVC>:...Options...>
        $<$<OR:$<CXX_COMPILER_ID:GNU>,
            $<CXX_COMPILER_ID:Clang>,
            $<CXX_COMPILER_ID:AppleClang>>:...Options...>
)

However, upon running CMake, I find that this doesn't work. I get the following error:

CMake Error at CMakeLists.txt:23 (target_compile_options):
  Error evaluating generator expression:
    $<OR:$<CXX_COMPILER_ID:GNU>,;$<CXX_COMPILER_ID:Clang>,;$<CXX_COMPILER_ID:AppleClang>>
  Parameters to $<OR> must resolve to either '0' or '1'.

As evident, CMake doesn't recongize that the next portion of the generator expression is on a consecutive line, not on the same one. Since I want to preserve readability, I do not want to have to resort to the first option. I realize that I can mess around with string(CONCAT ...) and place the result in a variable, but I want to avoid that, as I only have one target in my whole project, which would make the variable kind of useless.

How do I split such a generator expression into multiple lines in order to preserve readability?

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
  • Oh, really? Thanks for the link, that clears things up. However, I'm still confused about what you mean with the comma being a delimiter. The only time I use a comma is with the OR generator syntax, which requires a comma. (Syntax: `$`) – Arnav Borborah Jul 17 '18 at 12:13
  • Oh, yes, comma delimits arguments in OR generator expression. I forgot about that, sorry for noise. – Tsyvarev Jul 17 '18 at 12:22
  • 5
    Too bad this was marked as duplicate. I found a fairly nice solution. Given how the result of the genex can be split arbitrarily and the closing `>` can go anywhere, it can be split fairly nice. Very long `OR` genexes cannot be split unfortunately. – Meteorhead Feb 28 '19 at 13:05
  • @Meteorhead You might consider answering the question this is marked a duplicate of. Maybe your solution might help someone else! – Arnav Borborah Feb 28 '19 at 14:29

0 Answers0