target_compile_options: CMake Removes Required Flags
I found this issue while targeting WASM with Emscripten.
It seems as though CMake combines some compiler flags in a key-pair fashion.
For example, -Dkey1 value1 -Dkey1 value2
becomes -Dkey1 value1 value2
.
This causes a difficult situation when using CMake with Emscripten, which requires multiple -s OPT
arguments.
The same behavior can be seen without Emscripten, if you simulate it with include flags.
# Becomes -I dir1 dir2
target_compile_options(example PRIVATE -I dir1 -I dir2)
Unlike include directories, em++
cannot interpret abbreviated forms like -skey1=value -skey2=value
, which would fix the issue. I will follow up with them on supporting this as well. As it stands though, there's already a lot of community documentation that describes flags as -s key=value
. It might be best to handle this behaviour on CMake as well.
A complete and minimal example:
// example.cpp
int main() { return 0; }
# CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
add_executable(example example.cpp)
if(EMSCRIPTEN)
target_compile_options(example PRIVATE -s USE_SDL2=1 -s FULL_ES2=1)
else(EMSCRIPTEN)
target_compile_options(example PRIVATE -I dir1 -I dir2)
endif(EMSCRIPTEN)
On the Emscripten side of things, with the SDK installed, you'd configure with:
emcmake cmake ..
# make, ninja, etc...
I tried to trace this in the source code, and I believe it happens cmGeneratorTarget.cpp::processOptions
.