Patrick B.
Patrick B.

Reputation: 12353

How to pass combined compiler options with target_compile_options in CMake?

I'm using a customized clang/llvm to build my project. The customization is basically the addition of optimization passes. To pass options to my passes when compiling with clang I'm using:

clang [..] -mllvm -MyOption [..]

Now it happens that I need to pass multiple options this way:

clang [..] -mllvm -MyOption -mllvm -MyOption2=value [..]

This in combination with CMake's target_compile_options() stops working, CMake removes the second -mllvm because it seems to think it is duplicated.

target_compile_options(vslib INTERFACE -mllvm -MyOption)
target_compile_options(vslib INTERFACE -mllvm -MyOption2=val)

I tried putting " around both options, doesn't work.

Is there a way to achieve this with CMake?

Upvotes: 3

Views: 1805

Answers (2)

Emil Laine
Emil Laine

Reputation: 42828

https://cmake.org/cmake/help/v3.12/command/target_compile_options.html:

The set of options is de-duplicated to avoid repetition. While beneficial for individual options, the de-duplication step can break up option groups. For example, -D A -D B becomes -D A B. One may specify a group of options using shell-like quoting along with a SHELL: prefix. The SHELL: prefix is dropped and the rest of the option string is parsed using the separate_arguments() UNIX_COMMAND mode. For example, "SHELL:-D A" "SHELL:-D B" becomes -D A -D B.

So in your case that would be:

target_compile_options(vslib INTERFACE "SHELL:-mllvm -MyOption" "SHELL:-mllvm -MyOption2=val")

Upvotes: 7

KamilCuk
KamilCuk

Reputation: 140990

Try:

get_property(tmp TARGET vslib PROPERTY INTERFACE_COMPILE_OPTIONS)
list(APPEND tmp -mllvm)
list(APPEND tmp -MyOption)
list(APPEND tmp -mllvm)
list(APPEND tmp -MyOption2=value)
set_property(TARGET vslib PROPERTY INTERFACE_COMPILE_OPTIONS "${tmp}")

or maybe just:

set_property(TARGET vslib APPEND PROPERTY INTERFACE_COMPILE_OPTIONS -mllvm -MyOption)
set_property(TARGET vslib APPEND PROPERTY INTERFACE_COMPILE_OPTIONS -mllvm -MyOption2=value)

Upvotes: 0

Related Questions