Skip to content

Commit d9d929a

Browse files
authored
Revert "Start using optimization (-O0/-O2/-O3/-Os) and debug (-g) flags from CMAKE_CXX_FLAGS_${CFLAGS_BUILD_TYPE} (#33388)" (#33924)
1 parent 6f68c4c commit d9d929a

File tree

4 files changed

+60
-34
lines changed

4 files changed

+60
-34
lines changed

CMakeLists.txt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -629,22 +629,6 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" STREQU
629629
set(SWIFT_COMPILER_IS_MSVC_LIKE TRUE)
630630
endif()
631631

632-
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
633-
# CMake's default for CMAKE_CXX_FLAGS_RELEASE is "-O3 -DNDEBUG". Let's avoid "-O3" for consistency
634-
# between Release and RelWithDebInfo. Dropping -DNDEBUG from this setting is blocked by triggering
635-
# a test failure of Swift-Unit :: Syntax/./SwiftSyntaxTests/TypeSyntaxTests.MetatypeTypeWithAPIs
636-
# because unit tests don't currently explicitly set -DNDEBUG/-UNDEBUG.
637-
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
638-
639-
_compute_lto_flag("${SWIFT_TOOLS_ENABLE_LTO}" _lto_flag_out)
640-
if(_lto_flag_out)
641-
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_RELEASE} -gline-tables-only")
642-
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -gline-tables-only")
643-
endif()
644-
else()
645-
646-
endif()
647-
648632
#
649633
# Configure SDKs.
650634
#

cmake/modules/AddSwift.cmake

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,17 @@ function(_add_host_variant_c_compile_flags target)
130130
_add_host_variant_c_compile_link_flags(${target})
131131

132132
is_build_type_optimized("${CMAKE_BUILD_TYPE}" optimized)
133-
is_build_type_with_debuginfo("${CMAKE_BUILD_TYPE}" debuginfo)
134-
135-
# Add -O0/-O2/-O3/-Os/-g/-momit-leaf-frame-pointer/... based on CMAKE_BUILD_TYPE.
136-
target_compile_options(${target} PRIVATE "${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}}")
137-
138133
if(optimized)
134+
if("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
135+
target_compile_options(${target} PRIVATE -Os)
136+
else()
137+
target_compile_options(${target} PRIVATE -O2)
138+
endif()
139+
139140
# Omit leaf frame pointers on x86 production builds (optimized, no debug
140141
# info, and no asserts).
141-
if(NOT debuginfo AND NOT LLVM_ENABLE_ASSERTIONS)
142-
# Unfortunately, this cannot be folded into the standard
143-
# CMAKE_CXX_FLAGS_... because Apple multi-SDK builds use different
144-
# architectures for different SDKs.
142+
is_build_type_with_debuginfo("${CMAKE_BUILD_TYPE}" debug)
143+
if(NOT debug AND NOT LLVM_ENABLE_ASSERTIONS)
145144
if(SWIFT_HOST_VARIANT_ARCH MATCHES "i?86")
146145
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
147146
target_compile_options(${target} PRIVATE -momit-leaf-frame-pointer)
@@ -150,6 +149,27 @@ function(_add_host_variant_c_compile_flags target)
150149
endif()
151150
endif()
152151
endif()
152+
else()
153+
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
154+
target_compile_options(${target} PRIVATE -O0)
155+
else()
156+
target_compile_options(${target} PRIVATE /Od)
157+
endif()
158+
endif()
159+
160+
# CMake automatically adds the flags for debug info if we use MSVC/clang-cl.
161+
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
162+
is_build_type_with_debuginfo("${CMAKE_BUILD_TYPE}" debuginfo)
163+
if(debuginfo)
164+
_compute_lto_flag("${SWIFT_TOOLS_ENABLE_LTO}" _lto_flag_out)
165+
if(_lto_flag_out)
166+
target_compile_options(${target} PRIVATE -gline-tables-only)
167+
else()
168+
target_compile_options(${target} PRIVATE -g)
169+
endif()
170+
else()
171+
target_compile_options(${target} PRIVATE -g0)
172+
endif()
153173
endif()
154174

155175
if(SWIFT_HOST_VARIANT_SDK STREQUAL WINDOWS)

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,17 @@ function(_add_target_variant_c_compile_flags)
164164
MACCATALYST_BUILD_FLAVOR "${CFLAGS_MACCATALYST_BUILD_FLAVOR}")
165165

166166
is_build_type_optimized("${CFLAGS_BUILD_TYPE}" optimized)
167-
is_build_type_with_debuginfo("${CFLAGS_BUILD_TYPE}" debuginfo)
168-
169-
# Add -O0/-O2/-O3/-Os/-g/... based on CFLAGS_BUILD_TYPE.
170-
list(APPEND result "${CMAKE_CXX_FLAGS_${CFLAGS_BUILD_TYPE}}")
171-
172167
if(optimized)
168+
if("${CFLAGS_BUILD_TYPE}" STREQUAL "MinSizeRel")
169+
list(APPEND result "-Os")
170+
else()
171+
list(APPEND result "-O2")
172+
endif()
173+
173174
# Omit leaf frame pointers on x86 production builds (optimized, no debug
174175
# info, and no asserts).
175-
if(NOT debuginfo AND NOT CFLAGS_ENABLE_ASSERTIONS)
176-
# Unfortunately, this cannot be folded into the standard
177-
# CMAKE_CXX_FLAGS_... because Apple multi-SDK builds use different
178-
# architectures for different SDKs (CFLAGS_ARCH isn't constant here).
176+
is_build_type_with_debuginfo("${CFLAGS_BUILD_TYPE}" debug)
177+
if(NOT debug AND NOT CFLAGS_ENABLE_ASSERTIONS)
179178
if("${CFLAGS_ARCH}" STREQUAL "i386" OR "${CFLAGS_ARCH}" STREQUAL "i686")
180179
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
181180
list(APPEND result "-momit-leaf-frame-pointer")
@@ -184,6 +183,27 @@ function(_add_target_variant_c_compile_flags)
184183
endif()
185184
endif()
186185
endif()
186+
else()
187+
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
188+
list(APPEND result "-O0")
189+
else()
190+
list(APPEND result "/Od")
191+
endif()
192+
endif()
193+
194+
# CMake automatically adds the flags for debug info if we use MSVC/clang-cl.
195+
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
196+
is_build_type_with_debuginfo("${CFLAGS_BUILD_TYPE}" debuginfo)
197+
if(debuginfo)
198+
_compute_lto_flag("${CFLAGS_ENABLE_LTO}" _lto_flag_out)
199+
if(_lto_flag_out)
200+
list(APPEND result "-gline-tables-only")
201+
else()
202+
list(APPEND result "-g")
203+
endif()
204+
else()
205+
list(APPEND result "-g0")
206+
endif()
187207
endif()
188208

189209
if("${CFLAGS_SDK}" STREQUAL "WINDOWS")

utils/build-script-impl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,8 @@ for host in "${ALL_HOSTS[@]}"; do
17681768
"${cmake_options[@]}"
17691769
-DCMAKE_C_FLAGS="$(swift_c_flags ${host})"
17701770
-DCMAKE_CXX_FLAGS="$(swift_c_flags ${host})"
1771+
-DCMAKE_C_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
1772+
-DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
17711773
-DCMAKE_BUILD_TYPE:STRING="${SWIFT_BUILD_TYPE}"
17721774
-DLLVM_ENABLE_ASSERTIONS:BOOL=$(true_false "${SWIFT_ENABLE_ASSERTIONS}")
17731775
-DSWIFT_ANALYZE_CODE_COVERAGE:STRING=$(toupper "${SWIFT_ANALYZE_CODE_COVERAGE}")

0 commit comments

Comments
 (0)