Skip to content

Commit 28a2826

Browse files
authored
Start using optimization (-O0/-O2/-O3/-Os) and debug (-g) flags from CMAKE_CXX_FLAGS_${CFLAGS_BUILD_TYPE} (#33388)
1 parent ee26fea commit 28a2826

File tree

4 files changed

+33
-62
lines changed

4 files changed

+33
-62
lines changed

CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,21 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" STREQU
633633
set(SWIFT_COMPILER_IS_MSVC_LIKE TRUE)
634634
endif()
635635

636+
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
637+
# CMake's default for CMAKE_CXX_FLAGS_RELEASE is "-O3 -DNDEBUG". Let's avoid "-O3" for consistency
638+
# between Release and RelWithDebInfo. And let's not set -DNDEBUG because we're setting that manually
639+
# based on LLVM_ENABLE_ASSERTIONS.
640+
set(CMAKE_CXX_FLAGS_RELEASE "-O2")
641+
642+
_compute_lto_flag("${SWIFT_TOOLS_ENABLE_LTO}" _lto_flag_out)
643+
if(_lto_flag_out)
644+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_RELEASE} -gline-tables-only")
645+
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -gline-tables-only")
646+
endif()
647+
else()
648+
649+
endif()
650+
636651
#
637652
# Configure SDKs.
638653
#

cmake/modules/AddSwift.cmake

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,18 @@ 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-
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()
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}}")
139137

138+
if(optimized)
140139
# Omit leaf frame pointers on x86 production builds (optimized, no debug
141140
# info, and no asserts).
142-
is_build_type_with_debuginfo("${CMAKE_BUILD_TYPE}" debug)
143-
if(NOT debug AND NOT LLVM_ENABLE_ASSERTIONS)
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.
144145
if(SWIFT_HOST_VARIANT_ARCH MATCHES "i?86")
145146
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
146147
target_compile_options(${target} PRIVATE -momit-leaf-frame-pointer)
@@ -149,27 +150,6 @@ function(_add_host_variant_c_compile_flags target)
149150
endif()
150151
endif()
151152
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()
173153
endif()
174154

175155
if(SWIFT_HOST_VARIANT_SDK STREQUAL WINDOWS)

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,18 @@ 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-
if(optimized)
168-
if("${CFLAGS_BUILD_TYPE}" STREQUAL "MinSizeRel")
169-
list(APPEND result "-Os")
170-
else()
171-
list(APPEND result "-O2")
172-
endif()
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}}")
173171

172+
if(optimized)
174173
# Omit leaf frame pointers on x86 production builds (optimized, no debug
175174
# info, and no asserts).
176-
is_build_type_with_debuginfo("${CFLAGS_BUILD_TYPE}" debug)
177-
if(NOT debug AND NOT CFLAGS_ENABLE_ASSERTIONS)
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).
178179
if("${CFLAGS_ARCH}" STREQUAL "i386" OR "${CFLAGS_ARCH}" STREQUAL "i686")
179180
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
180181
list(APPEND result "-momit-leaf-frame-pointer")
@@ -183,27 +184,6 @@ function(_add_target_variant_c_compile_flags)
183184
endif()
184185
endif()
185186
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()
207187
endif()
208188

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

utils/build-script-impl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,8 +1576,6 @@ for host in "${ALL_HOSTS[@]}"; do
15761576
"${cmake_options[@]}"
15771577
-DCMAKE_C_FLAGS="$(llvm_c_flags ${host})"
15781578
-DCMAKE_CXX_FLAGS="$(llvm_c_flags ${host})"
1579-
-DCMAKE_C_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
1580-
-DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
15811579
-DCMAKE_BUILD_TYPE:STRING="${LLVM_BUILD_TYPE}"
15821580
-DLLVM_TOOL_SWIFT_BUILD:BOOL=NO
15831581
-DLLVM_TOOL_LLD_BUILD:BOOL=TRUE
@@ -1756,8 +1754,6 @@ for host in "${ALL_HOSTS[@]}"; do
17561754
"${cmake_options[@]}"
17571755
-DCMAKE_C_FLAGS="$(swift_c_flags ${host})"
17581756
-DCMAKE_CXX_FLAGS="$(swift_c_flags ${host})"
1759-
-DCMAKE_C_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
1760-
-DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
17611757
-DCMAKE_BUILD_TYPE:STRING="${SWIFT_BUILD_TYPE}"
17621758
-DLLVM_ENABLE_ASSERTIONS:BOOL=$(true_false "${SWIFT_ENABLE_ASSERTIONS}")
17631759
-DSWIFT_ANALYZE_CODE_COVERAGE:STRING=$(toupper "${SWIFT_ANALYZE_CODE_COVERAGE}")

0 commit comments

Comments
 (0)