Reputation: 365
I have the following code in my CMakeLists.txt to set some compiler and linker flags for my emscripten project:
set_target_properties(prolog_bfs PROPERTIES COMPILE_FLAGS "-s USE_BOOST_HEADERS=1 -s DISABLE_EXCEPTION_CATCHING=0")
set_target_properties(prolog_bfs PROPERTIES LINK_FLAGS "--bind --emrun -s USE_BOOST_HEADERS=1 -s DISABLE_EXCEPTION_CATCHING=0")
This works perfectly fine and my compiler gets called with the options as it should (I still wonder where the spaces after em++ are coming from though, but this is not an issue):
em++ -s USE_BOOST_HEADERS=1 -s DISABLE_EXCEPTION_CATCHING=0 -std=gnu++17 -o xy.o -c xy.cpp
However, COMPILE_FLAGS
and LINK_FLAGS
are deprecated, so I want to migrate to the new/recommended approach of using target_compile_options()
and target_link_options()
instead.
Thus, I have changed my CMakeLists.txt like so:
target_compile_options(prolog_bfs PUBLIC -s USE_BOOST_HEADERS=1;-s DISABLE_EXCEPTION_CATCHING=0)
target_link_options(prolog_bfs PUBLIC --bind;--emrun;-s USE_BOOST_HEADERS=1;-s DISABLE_EXCEPTION_CATCHING=0)
I understand that the target_*_options function requires to separate flags with a semicolon, which I did. Apart from that, I don't see any other major differences.
Building my project with these changes will get the compiler called like this:
em++ -s USE_BOOST_HEADERS=1 DISABLE_EXCEPTION_CATCHING=0 -std=gnu++17 -o xy.o -c xy.cpp
Note that that the -s
before the second flag is missing. I don't understand why it disappears. Interestingly, the first one stays there.
How do I transform my initial CMakeLists.txt code into the modern approach without losing the -s
?
Upvotes: 2
Views: 1152
Reputation: 66089
By default, CMake de-duplicates compile and link options. This is explicitly stated in the documentation for target_compile_options
command. Also, the documentation suggests to use SHELL:
prefix for avoid breaking groups because of that:
The final set of compile or link options used for a target is constructed by accumulating options from the current target and the usage requirements of its dependencies. 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 aSHELL:
prefix. TheSHELL:
prefix is dropped, and the rest of the option string is parsed using theseparate_arguments()
UNIX_COMMAND
mode. For example,"SHELL:-D A" "SHELL:-D B"
becomes-D A -D B
.
That is, in your case you may specify
target_compile_options(prolog_bfs PUBLIC "SHELL:-s USE_BOOST_HEADERS=1" "SHELL:-s DISABLE_EXCEPTION_CATCHING=0")
Upvotes: 3