Search code examples
cmakeiar

How can I escape <> characters in a CMake generator expression?


I'm writing a CMake file to support the IAR toolchain including ASM language but leave the option open for tools like GCC in the future.

I wrote up a generator expression like this:

target_compile_options(my_library PRIVATE
   
    # ASM language options
    $<$<COMPILE_LANG_AND_ID:ASM,IAR>:
        -s+
        -w+
        --cpu Cortex-M7
        --fpu VFPv5_sp
        -M<> # This is a problem 
    >
    
)

The last option for the assembler requires the use of <> but this understandably causes CMake a problem because > indicates the end of a generator expression. I tried escaping the characters with backslashes such as -M\<\> or with quotes like -M'<>' or '"-M'<>'"` but it didn't work. I even tried defining a separate variable but the output commands are still wrong:

set(MY_IAR_ASM_FLAG "-M<>")
target_compile_options(my_library PRIVATE
   
    # ASM language options
    $<$<COMPILE_LANG_AND_ID:ASM,IAR>:
        ${MY_IAR_ASM_FLAG}
    >

I end up with a stray > on the command line in all cases.

What's the right way to escape these characters so I get the right ASM switches? Or is there a good workaround?


Solution

  • You could use the CMake escaped characters (e.g., $<ANGLE-R>) like this

    target_compile_options(my_library PRIVATE
    
    # ASM language options
    $<$<COMPILE_LANG_AND_ID:ASM,IAR>:
        -s+
        -w+
        --cpu Cortex-M7
        --fpu VFPv5_sp
        -M<$<ANGLE-R> # This is a problem 
    >
    )