Skip to content

Commit 3763d81

Browse files
committed
[CMake] Handle multiple flags in ADDITIONAL_COMPILE_FLAGS properly
When multiple space-separated compile flags are specified in an `ADDITIONAL_COMPILE_FLAGS` cache string, the resulting flags are enclosed by double quotes because `ADDITIONAL_COMPILE_FLAGS` is a string (i.e. not a list) and CMake `target_compile_options` treats the multiple space-separated arguments as single argument containing spaces. For example, when libcxx is configured with the following multiple space-separated additional compile flags: cmake ... "-DLIBCXX_ADDITIONAL_COMPILE_FLAGS=--flag1 --flag2" ... The resulting compiler command line is as follows: cc ... "--flag1 --flag2" ... The above can be problematic for some compilers -- for instance, GCC treats it as a file path and prints out an error. This patch, by calling `separate_arguments` on `ADDITIONAL_COMPILE_FLAGS` to convert it into the standard semicolon-separated list form, which is properly handled by `target_compile_options`, ensures that multiple compile flags are handled as such. With this change, the resulting compiler command line is as follows: cc ... --flag1 --flag2 ... Signed-off-by: Stephanos Ioannidis <[email protected]>
1 parent 0f7d148 commit 3763d81

File tree

5 files changed

+8
-5
lines changed

5 files changed

+8
-5
lines changed

libcxx/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ set(LIBCXX_LINK_FLAGS "")
460460
set(LIBCXX_LIBRARIES "")
461461
set(LIBCXX_ADDITIONAL_COMPILE_FLAGS "" CACHE STRING
462462
"Additional Compile only flags which can be provided in cache")
463+
separate_arguments(LIBCXX_ADDITIONAL_COMPILE_FLAGS)
463464
set(LIBCXX_ADDITIONAL_LIBRARIES "" CACHE STRING
464465
"Additional libraries libc++ is linked to which can be provided in cache")
465466

@@ -549,7 +550,7 @@ function(cxx_add_basic_build_flags target)
549550
target_compile_definitions(${target} PRIVATE -D_LIBCPP_LINK_RT_LIB)
550551
endif()
551552
endif()
552-
target_compile_options(${target} PUBLIC "${LIBCXX_ADDITIONAL_COMPILE_FLAGS}")
553+
target_compile_options(${target} PUBLIC ${LIBCXX_ADDITIONAL_COMPILE_FLAGS})
553554
endfunction()
554555

555556
# Exception flags =============================================================

libcxxabi/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ set(LIBCXXABI_LINK_FLAGS "")
224224
set(LIBCXXABI_LIBRARIES "")
225225
set(LIBCXXABI_ADDITIONAL_COMPILE_FLAGS "" CACHE STRING
226226
"Additional Compile only flags which can be provided in cache")
227+
separate_arguments(LIBCXXABI_ADDITIONAL_COMPILE_FLAGS)
227228
set(LIBCXXABI_ADDITIONAL_LIBRARIES "" CACHE STRING
228229
"Additional libraries libc++abi is linked to which can be provided in cache")
229230

libcxxabi/src/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ set_target_properties(cxxabi_shared_objects
182182
if (CMAKE_POSITION_INDEPENDENT_CODE OR NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
183183
set_target_properties(cxxabi_shared_objects PROPERTIES POSITION_INDEPENDENT_CODE ON) # must set manually because it's an object library
184184
endif()
185-
target_compile_options(cxxabi_shared_objects PRIVATE "${LIBCXXABI_ADDITIONAL_COMPILE_FLAGS}")
185+
target_compile_options(cxxabi_shared_objects PRIVATE ${LIBCXXABI_ADDITIONAL_COMPILE_FLAGS})
186186

187187
add_library(cxxabi_shared SHARED)
188188
set_target_properties(cxxabi_shared
@@ -273,7 +273,7 @@ set_target_properties(cxxabi_static_objects
273273
CXX_STANDARD_REQUIRED OFF # TODO: Make this REQUIRED once we don't need to accommodate the LLVM documentation builders using an ancient CMake
274274
COMPILE_FLAGS "${LIBCXXABI_COMPILE_FLAGS}"
275275
)
276-
target_compile_options(cxxabi_static_objects PRIVATE "${LIBCXXABI_ADDITIONAL_COMPILE_FLAGS}")
276+
target_compile_options(cxxabi_static_objects PRIVATE ${LIBCXXABI_ADDITIONAL_COMPILE_FLAGS})
277277

278278
if(LIBCXXABI_HERMETIC_STATIC_LIBRARY)
279279
target_add_compile_flags_if_supported(cxxabi_static_objects PRIVATE -fvisibility=hidden)

libunwind/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ set(LIBUNWIND_COMPILE_FLAGS "")
164164
set(LIBUNWIND_LINK_FLAGS "")
165165
set(LIBUNWIND_ADDITIONAL_COMPILE_FLAGS "" CACHE STRING
166166
"Additional Compile only flags which can be provided in cache")
167+
separate_arguments(LIBUNWIND_ADDITIONAL_COMPILE_FLAGS)
167168
set(LIBUNWIND_ADDITIONAL_LIBRARIES "" CACHE STRING
168169
"Additional libraries libunwind is linked to which can be provided in cache")
169170

libunwind/src/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ else()
140140
target_compile_options(unwind_shared_objects PRIVATE -fno-rtti)
141141
endif()
142142
target_link_libraries(unwind_shared_objects PRIVATE unwind-headers ${LIBUNWIND_LIBRARIES})
143-
target_compile_options(unwind_shared_objects PUBLIC "${LIBUNWIND_ADDITIONAL_COMPILE_FLAGS}")
143+
target_compile_options(unwind_shared_objects PUBLIC ${LIBUNWIND_ADDITIONAL_COMPILE_FLAGS})
144144
target_link_libraries(unwind_shared_objects PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}")
145145
set_target_properties(unwind_shared_objects
146146
PROPERTIES
@@ -181,7 +181,7 @@ else()
181181
target_compile_options(unwind_static_objects PRIVATE -fno-rtti)
182182
endif()
183183
target_link_libraries(unwind_static_objects PRIVATE unwind-headers ${LIBUNWIND_LIBRARIES})
184-
target_compile_options(unwind_static_objects PUBLIC "${LIBUNWIND_ADDITIONAL_COMPILE_FLAGS}")
184+
target_compile_options(unwind_static_objects PUBLIC ${LIBUNWIND_ADDITIONAL_COMPILE_FLAGS})
185185
target_link_libraries(unwind_static_objects PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}")
186186
set_target_properties(unwind_static_objects
187187
PROPERTIES

0 commit comments

Comments
 (0)