From 3de1f756a2c894c67a985978255080c81c17eac4 Mon Sep 17 00:00:00 2001 From: Kuba Mracek Date: Thu, 10 Sep 2020 14:31:08 -0700 Subject: [PATCH 1/2] Use uppercase CMAKE_BUILD_TYPE when asking for CMAKE_CXX_FLAGS, explicitly use -O0 for Debug builds --- CMakeLists.txt | 4 ++++ cmake/modules/AddSwift.cmake | 4 +++- stdlib/cmake/modules/AddSwiftStdlib.cmake | 4 +++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a84adb0ca4636..dba61ecfaef14 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -630,6 +630,10 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" STREQU endif() if(NOT SWIFT_COMPILER_IS_MSVC_LIKE) + # CMake's default for CMAKE_CXX_FLAGS_DBEUG is "-g". Let's add "-O0", because we want to be able + # to append CMAKE_CXX_FLAGS_DEBUG to a list of compile flags that already contains a -O flag. + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0") + # CMake's default for CMAKE_CXX_FLAGS_RELEASE is "-O3 -DNDEBUG". Let's avoid "-O3" for consistency # between Release and RelWithDebInfo. Dropping -DNDEBUG from this setting is blocked by triggering # a test failure of Swift-Unit :: Syntax/./SwiftSyntaxTests/TypeSyntaxTests.MetatypeTypeWithAPIs diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 863095fdfa205..4c5c43a38cd18 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -133,7 +133,9 @@ function(_add_host_variant_c_compile_flags target) is_build_type_with_debuginfo("${CMAKE_BUILD_TYPE}" debuginfo) # Add -O0/-O2/-O3/-Os/-g/-momit-leaf-frame-pointer/... based on CMAKE_BUILD_TYPE. - target_compile_options(${target} PRIVATE "${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}}") + string(TOUPPER ${CMAKE_BUILD_TYPE} build_type_upper) + string(REPLACE " " ";" cxx_flags_list "${CMAKE_CXX_FLAGS_${build_type_upper}}") + target_compile_options(${target} PRIVATE ${cxx_flags_list}) if(optimized) # Omit leaf frame pointers on x86 production builds (optimized, no debug diff --git a/stdlib/cmake/modules/AddSwiftStdlib.cmake b/stdlib/cmake/modules/AddSwiftStdlib.cmake index afaf60ac15ede..826af2f76d0c2 100644 --- a/stdlib/cmake/modules/AddSwiftStdlib.cmake +++ b/stdlib/cmake/modules/AddSwiftStdlib.cmake @@ -167,7 +167,9 @@ function(_add_target_variant_c_compile_flags) is_build_type_with_debuginfo("${CFLAGS_BUILD_TYPE}" debuginfo) # Add -O0/-O2/-O3/-Os/-g/... based on CFLAGS_BUILD_TYPE. - list(APPEND result "${CMAKE_CXX_FLAGS_${CFLAGS_BUILD_TYPE}}") + string(TOUPPER ${CFLAGS_BUILD_TYPE} build_type_upper) + string(REPLACE " " ";" cxx_flags_list "${CMAKE_CXX_FLAGS_${build_type_upper}}") + list(APPEND result ${cxx_flags_list}) if(optimized) # Omit leaf frame pointers on x86 production builds (optimized, no debug From e49bf381728282264502df141d967da377964a7d Mon Sep 17 00:00:00 2001 From: Kuba Mracek Date: Fri, 11 Sep 2020 14:06:10 -0700 Subject: [PATCH 2/2] Use target_compile_options when passing -DNDEBUG to have a guaranteed order of flags --- cmake/modules/AddSwift.cmake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 4c5c43a38cd18..046d28bc5fbf5 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -233,12 +233,12 @@ function(_add_host_variant_c_compile_flags target) if(LLVM_ENABLE_ASSERTIONS) target_compile_options(${target} PRIVATE -UNDEBUG) else() - target_compile_definitions(${target} PRIVATE -DNDEBUG) + target_compile_options(${target} PRIVATE -DNDEBUG) endif() if(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS) - target_compile_definitions(${target} PRIVATE - SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS) + target_compile_options(${target} PRIVATE + -DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS) endif() if(SWIFT_ANALYZE_CODE_COVERAGE)