Skip to content

Commit 6612d8c

Browse files
committed
Start using optimization (-O0/-O2/-O3/-Os) and debug (-g) flags from CMAKE_CXX_FLAGS_${CFLAGS_BUILD_TYPE}
1 parent 643aa2d commit 6612d8c

File tree

3 files changed

+14
-34
lines changed

3 files changed

+14
-34
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,15 @@ 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)
139134

135+
# Add -O0/-O2/-O3/-Os based on CMAKE_BUILD_TYPE.
136+
target_compile_options(${target} PRIVATE "${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}}")
137+
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)
144142
if(SWIFT_HOST_VARIANT_ARCH MATCHES "i?86")
145143
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
146144
target_compile_options(${target} PRIVATE -momit-leaf-frame-pointer)
@@ -149,26 +147,15 @@ function(_add_host_variant_c_compile_flags target)
149147
endif()
150148
endif()
151149
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()
158150
endif()
159151

160152
# CMake automatically adds the flags for debug info if we use MSVC/clang-cl.
161153
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
162-
is_build_type_with_debuginfo("${CMAKE_BUILD_TYPE}" debuginfo)
163154
if(debuginfo)
164155
_compute_lto_flag("${SWIFT_TOOLS_ENABLE_LTO}" _lto_flag_out)
165156
if(_lto_flag_out)
166157
target_compile_options(${target} PRIVATE -gline-tables-only)
167-
else()
168-
target_compile_options(${target} PRIVATE -g)
169158
endif()
170-
else()
171-
target_compile_options(${target} PRIVATE -g0)
172159
endif()
173160
endif()
174161

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ function(_add_target_variant_c_compile_flags)
179179
MACCATALYST_BUILD_FLAVOR "${CFLAGS_MACCATALYST_BUILD_FLAVOR}")
180180

181181
is_build_type_optimized("${CFLAGS_BUILD_TYPE}" optimized)
182+
is_build_type_with_debuginfo("${CFLAGS_BUILD_TYPE}" debuginfo)
183+
184+
# Add -O0/-O2/-O3/-Os based on CFLAGS_BUILD_TYPE.
185+
list(APPEND result "${CMAKE_CXX_FLAGS_${CFLAGS_BUILD_TYPE}}")
186+
182187
if(optimized)
183188
if("${CFLAGS_BUILD_TYPE}" STREQUAL "MinSizeRel")
184189
list(APPEND result "-Os")
@@ -188,8 +193,7 @@ function(_add_target_variant_c_compile_flags)
188193

189194
# Omit leaf frame pointers on x86 production builds (optimized, no debug
190195
# info, and no asserts).
191-
is_build_type_with_debuginfo("${CFLAGS_BUILD_TYPE}" debug)
192-
if(NOT debug AND NOT CFLAGS_ENABLE_ASSERTIONS)
196+
if(NOT debuginfo AND NOT CFLAGS_ENABLE_ASSERTIONS)
193197
if("${CFLAGS_ARCH}" STREQUAL "i386" OR "${CFLAGS_ARCH}" STREQUAL "i686")
194198
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
195199
list(APPEND result "-momit-leaf-frame-pointer")
@@ -198,26 +202,15 @@ function(_add_target_variant_c_compile_flags)
198202
endif()
199203
endif()
200204
endif()
201-
else()
202-
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
203-
list(APPEND result "-O0")
204-
else()
205-
list(APPEND result "/Od")
206-
endif()
207205
endif()
208206

209207
# CMake automatically adds the flags for debug info if we use MSVC/clang-cl.
210208
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
211-
is_build_type_with_debuginfo("${CFLAGS_BUILD_TYPE}" debuginfo)
212209
if(debuginfo)
213210
_compute_lto_flag("${CFLAGS_ENABLE_LTO}" _lto_flag_out)
214211
if(_lto_flag_out)
215212
list(APPEND result "-gline-tables-only")
216-
else()
217-
list(APPEND result "-g")
218213
endif()
219-
else()
220-
list(APPEND result "-g0")
221214
endif()
222215
endif()
223216

utils/build-script-impl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,7 @@ for host in "${ALL_HOSTS[@]}"; do
15751575
-DCMAKE_C_FLAGS="$(llvm_c_flags ${host})"
15761576
-DCMAKE_CXX_FLAGS="$(llvm_c_flags ${host})"
15771577
-DCMAKE_C_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
1578-
-DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
1578+
-DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O2 -g -DNDEBUG"
15791579
-DCMAKE_BUILD_TYPE:STRING="${LLVM_BUILD_TYPE}"
15801580
-DLLVM_TOOL_SWIFT_BUILD:BOOL=NO
15811581
-DLLVM_TOOL_LLD_BUILD:BOOL=TRUE
@@ -1755,7 +1755,7 @@ for host in "${ALL_HOSTS[@]}"; do
17551755
-DCMAKE_C_FLAGS="$(swift_c_flags ${host})"
17561756
-DCMAKE_CXX_FLAGS="$(swift_c_flags ${host})"
17571757
-DCMAKE_C_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
1758-
-DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
1758+
-DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O2 -g -DNDEBUG"
17591759
-DCMAKE_BUILD_TYPE:STRING="${SWIFT_BUILD_TYPE}"
17601760
-DLLVM_ENABLE_ASSERTIONS:BOOL=$(true_false "${SWIFT_ENABLE_ASSERTIONS}")
17611761
-DSWIFT_ANALYZE_CODE_COVERAGE:STRING=$(toupper "${SWIFT_ANALYZE_CODE_COVERAGE}")

0 commit comments

Comments
 (0)