DLR-AMR/t8code

CMake: setting CMAKE_CXX_FLAGS from the cli breaks the build system

maelk3 opened this issue · 0 comments

Bug report

Describe the bug
When setting the CMake environment variable CMAKE_CXX_FLAGS from the cli (e.g. -DCMAKE_CXX_FLAGS="-Wall"), cmake produces ill-formed compile commands containing semicolons.

To Reproduce

When configuring like so:

$ cmake -S ../t8code/ -B build_RelWithDebInfo -GNinja \
                   -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
                   -DCMAKE_BUILD_TYPE=RelWithDebInfo  \
                   -DCMAKE_CXX_FLAGS="-fdiagnostics-color=always"

Building results in:

$ cmake --build build_RelWithDebInfo/ -j 8
[159/688] Building CXX object test/CMakeFiles/t8_gtest_cmesh_readmshfile.dir/t8_cmesh/t8_gtest_cmesh_readmshfile.cxx.o
FAILED: test/CMakeFiles/t8_gtest_cmesh_readmshfile.dir/t8_cmesh/t8_gtest_cmesh_readmshfile.cxx.o 
/tools/modulesystem/spack-22.2/opt/spack/linux-ubuntu20.04-x86_64_v3/gcc-9.4.0/gcc-11.3.0-avy3yt67apn6ntfapginc4al4misogxc/bin/g++ -D-Wall -O2 -g -DNDEBUG" -DT8_CC=\"/tools/modulesystem/spack-22.2/opt/spack/linux-ubuntu20.04-x86_64_v3/gcc-9.4.0/gcc-11.3.0-avy3yt67apn6ntfapginc4al4misogxc/bin/gcc\" -DT8_CFLAGS="\"-Wall -O2 -g -DNDEBUG\"" -DT8_CMAKE_BUILD -DT8_CXX=\"/tools/modulesystem/spack-22.2/opt/spack/linux-ubuntu20.04-x86_64_v3/gcc-9.4.0/gcc-11.3.0-avy3yt67apn6ntfapginc4al4misogxc/bin/g++\" -DT8_CXXFLAGS=\"-fdiagnostics-color=always -DT8_ENABLE_MPI -DT8_ENABLE_MPIIO -DT8_LDFLAGS=\"\" -DT8_LIBS="\"P4EST::P4EST SC::SC MPI::MPI_C\"" -DT8_PACKAGE_STRING="\"t8 2.0.0\"" -DT8_VERSION=\"2.0.0\" -DT8_VERSION_MAJOR=2 -DT8_VERSION_MINOR=0 -DT8_VERSION_PATCH=0 -DT8_VERSION_POINT=655-gb6902e3f0 -I/localdata1/kare_ma/t8code/src -I/localdata1/kare_ma/t8code/p4est/src -I/localdata1/kare_ma/builds/build_RelWithDebInfo/p4est/include -I/localdata1/kare_ma/builds/build_RelWithDebInfo/sc/include -I/localdata1/kare_ma/t8code/sc/src -I/localdata1/kare_ma/t8code/test/../thirdparty/googletest-mpi -I/localdata1/kare_ma/t8code/test/.. -isystem /tools/modulesystem/spack-22.2/opt/spack/linux-ubuntu20.04-x86_64_v3/gcc-11.3.0/openmpi-4.1.4-et3mvomberawonlbtmlhmgizdbt7kpyo/include -isystem /tools/modulesystem/spack-22.2/opt/spack/linux-ubuntu20.04-x86_64_v3/gcc-9.4.0/zlib-1.2.12-ilkcdjtcxbcazc2nrczn6j2fwduunxhy/include -fdiagnostics-color=always;-Wall -O2 -g -DNDEBUG -pthread -std=c++17 -MD -MT test/CMakeFiles/t8_gtest_cmesh_readmshfile.dir/t8_cmesh/t8_gtest_cmesh_readmshfile.cxx.o -MF test/CMakeFiles/t8_gtest_cmesh_readmshfile.dir/t8_cmesh/t8_gtest_cmesh_readmshfile.cxx.o.d -o test/CMakeFiles/t8_gtest_cmesh_readmshfile.dir/t8_cmesh/t8_gtest_cmesh_readmshfile.cxx.o -c /localdata1/kare_ma/t8code/test/t8_cmesh/t8_gtest_cmesh_readmshfile.cxx
/bin/sh: -c: line 1: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 2: syntax error: unexpected end of file
[165/688] Building C object p4est/test/CMakeFiles/test_iterate2.dir/test_iterate2.c.o
ninja: build stopped: subcommand failed.

This is due to the fact that in the CMake build system, we append to the CMAKE_CXX_FLAGS variable, which results in adding a ';' between user defined c++ flags and the additional flags that we set. Ideally, the t8code build system should not play around with the CMAKE_CXX_FLAGS and only set flags to targets through target_compile_options.

Expected behavior
The compile commands should be well-formed.

Estimated priority

"Priority: medium" Should be solved within half a year