Skip to content

Commit f75619f

Browse files
committed
build: do not touch the user specified flags
`CMAKE_CXX_FLAGS` are user controlled and should not be touched by the build system [1]. Use global `add_compile_options` and `add_compile_definitions` to control the flags for the targets. [1] https://cmake.org/cmake/help/book/mastering-cmake/
1 parent d21c339 commit f75619f

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

cmake/modules/SwiftSharedCMakeConfig.cmake

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -312,29 +312,41 @@ macro(swift_common_cxx_warnings)
312312
# Make unhandled switch cases be an error in assert builds
313313
if(DEFINED LLVM_ENABLE_ASSERTIONS)
314314
check_cxx_compiler_flag("-Werror=switch" CXX_SUPPORTS_WERROR_SWITCH_FLAG)
315-
append_if(CXX_SUPPORTS_WERROR_SWITCH_FLAG "-Werror=switch" CMAKE_CXX_FLAGS)
315+
if(CXX_SUPPORTS_WERROR_SWITCH_FLAG)
316+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Werror=switch>)
317+
endif()
316318

317-
check_cxx_compiler_flag("/we4062" CXX_SUPPORTS_WE4062)
318-
append_if(CXX_SUPPORTS_WE4062 "/we4062" CMAKE_CXX_FLAGS)
319+
if(MSVC)
320+
check_cxx_compiler_flag("/we4062" CXX_SUPPORTS_WE4062)
321+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/we4062>)
322+
endif()
319323
endif()
320324

321325
check_cxx_compiler_flag("-Werror -Wdocumentation" CXX_SUPPORTS_DOCUMENTATION_FLAG)
322-
append_if(CXX_SUPPORTS_DOCUMENTATION_FLAG "-Wdocumentation" CMAKE_CXX_FLAGS)
326+
if(CXX_SUPPORTS_DOCUMENTATION_FLAG)
327+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Wdocumentation>)
328+
endif()
323329

324330
check_cxx_compiler_flag("-Werror -Wimplicit-fallthrough" CXX_SUPPORTS_IMPLICIT_FALLTHROUGH_FLAG)
325-
append_if(CXX_SUPPORTS_IMPLICIT_FALLTHROUGH_FLAG "-Wimplicit-fallthrough" CMAKE_CXX_FLAGS)
331+
if(CXX_SUPPORTS_IMPLICIT_FALLTHROUGH_FLAG)
332+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Wimplicit-fallthrough>)
333+
endif()
326334

327335
# Check for -Wunreachable-code-aggressive instead of -Wunreachable-code, as that indicates
328336
# that we have the newer -Wunreachable-code implementation.
329337
check_cxx_compiler_flag("-Werror -Wunreachable-code-aggressive" CXX_SUPPORTS_UNREACHABLE_CODE_FLAG)
330-
append_if(CXX_SUPPORTS_UNREACHABLE_CODE_FLAG "-Wunreachable-code" CMAKE_CXX_FLAGS)
338+
if(CXX_SUPPORTS_UNREACHABLE_CODE_FLAG)
339+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Wunreachable-code>)
340+
endif()
331341

332342
check_cxx_compiler_flag("-Werror -Woverloaded-virtual" CXX_SUPPORTS_OVERLOADED_VIRTUAL)
333-
append_if(CXX_SUPPORTS_OVERLOADED_VIRTUAL "-Woverloaded-virtual" CMAKE_CXX_FLAGS)
343+
if(CXX_SUPPORTS_OVERLOADED_VIRTUAL)
344+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Woverloaded-virtual>)
345+
endif()
334346

335347
check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
336348
if(CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
337-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types")
349+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Wnested-anon-types>)
338350
endif()
339351

340352
# Check for '-fapplication-extension'. On OS X/iOS we wish to link all
@@ -344,16 +356,19 @@ macro(swift_common_cxx_warnings)
344356
# Disable C4068: unknown pragma. This means that MSVC doesn't report hundreds of warnings across
345357
# the repository for IDE features such as #pragma mark "Title".
346358
if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
347-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4068")
359+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/wd4068>)
360+
348361
check_cxx_compiler_flag("/permissive-" CXX_SUPPORTS_PERMISSIVE_FLAG)
349-
append_if(CXX_SUPPORTS_PERMISSIVE_FLAG "/permissive-" CMAKE_CXX_FLAGS)
362+
if(CXX_SUPPORTS_PERMISSIVE_FLAG)
363+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/permissive->)
364+
endif()
350365
endif()
351366

352367
# Disallow calls to objc_msgSend() with no function pointer cast.
353-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DOBJC_OLD_DISPATCH_PROTOTYPES=0")
368+
add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:OBJC_OLD_DISPATCH_PROTOTYPES=0>)
354369

355370
if(BRIDGING_MODE STREQUAL "PURE")
356-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPURE_BRIDGING_MODE")
371+
add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:PURE_BRIDGING_MODE>)
357372
endif()
358373
endmacro()
359374

0 commit comments

Comments
 (0)