Skip to content

Use uppercase CMAKE_BUILD_TYPE when asking for CMAKE_CXX_FLAGS, explicitly use -O0 for Debug builds #33896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which list already has -O?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikeash reported this when building a "release swift + debug stdlib" configuration. The stdlib in this case gets the "default compilation flags" and then we append the "debug stdlib" flags.

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
Expand Down
10 changes: 6 additions & 4 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -231,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)
Expand Down
4 changes: 3 additions & 1 deletion stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down