Skip to content

Start using optimization (-O0/-O2/-O3/-Os) and debug (-g) flags from CMAKE_CXX_FLAGS_${CFLAGS_BUILD_TYPE} #33388

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

Merged
merged 7 commits into from
Aug 18, 2020
Merged
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
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,21 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" STREQU
set(SWIFT_COMPILER_IS_MSVC_LIKE TRUE)
endif()

if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
# CMake's default for CMAKE_CXX_FLAGS_RELEASE is "-O3 -DNDEBUG". Let's avoid "-O3" for consistency
# between Release and RelWithDebInfo. And let's not set -DNDEBUG because we're setting that manually
# based on LLVM_ENABLE_ASSERTIONS.
set(CMAKE_CXX_FLAGS_RELEASE "-O2")

_compute_lto_flag("${SWIFT_TOOLS_ENABLE_LTO}" _lto_flag_out)
if(_lto_flag_out)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_RELEASE} -gline-tables-only")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -gline-tables-only")
endif()
else()

endif()

#
# Configure SDKs.
#
Expand Down
38 changes: 9 additions & 29 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,18 @@ function(_add_host_variant_c_compile_flags target)
_add_host_variant_c_compile_link_flags(${target})

is_build_type_optimized("${CMAKE_BUILD_TYPE}" optimized)
if(optimized)
if("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
target_compile_options(${target} PRIVATE -Os)
else()
target_compile_options(${target} PRIVATE -O2)
endif()
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}}")

if(optimized)
# Omit leaf frame pointers on x86 production builds (optimized, no debug
# info, and no asserts).
is_build_type_with_debuginfo("${CMAKE_BUILD_TYPE}" debug)
if(NOT debug AND NOT LLVM_ENABLE_ASSERTIONS)
if(NOT debuginfo AND NOT LLVM_ENABLE_ASSERTIONS)
# Unfortunately, this cannot be folded into the standard
# CMAKE_CXX_FLAGS_... because Apple multi-SDK builds use different
# architectures for different SDKs.
if(SWIFT_HOST_VARIANT_ARCH MATCHES "i?86")
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
target_compile_options(${target} PRIVATE -momit-leaf-frame-pointer)
Expand All @@ -149,27 +150,6 @@ function(_add_host_variant_c_compile_flags target)
endif()
endif()
endif()
else()
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
target_compile_options(${target} PRIVATE -O0)
else()
target_compile_options(${target} PRIVATE /Od)
endif()
endif()

# CMake automatically adds the flags for debug info if we use MSVC/clang-cl.
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
is_build_type_with_debuginfo("${CMAKE_BUILD_TYPE}" debuginfo)
if(debuginfo)
_compute_lto_flag("${SWIFT_TOOLS_ENABLE_LTO}" _lto_flag_out)
if(_lto_flag_out)
target_compile_options(${target} PRIVATE -gline-tables-only)
else()
target_compile_options(${target} PRIVATE -g)
endif()
else()
target_compile_options(${target} PRIVATE -g0)
endif()
endif()

if(SWIFT_HOST_VARIANT_SDK STREQUAL WINDOWS)
Expand Down
38 changes: 9 additions & 29 deletions stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,18 @@ function(_add_target_variant_c_compile_flags)
MACCATALYST_BUILD_FLAVOR "${CFLAGS_MACCATALYST_BUILD_FLAVOR}")

is_build_type_optimized("${CFLAGS_BUILD_TYPE}" optimized)
if(optimized)
if("${CFLAGS_BUILD_TYPE}" STREQUAL "MinSizeRel")
list(APPEND result "-Os")
else()
list(APPEND result "-O2")
endif()
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}}")

if(optimized)
# Omit leaf frame pointers on x86 production builds (optimized, no debug
# info, and no asserts).
is_build_type_with_debuginfo("${CFLAGS_BUILD_TYPE}" debug)
if(NOT debug AND NOT CFLAGS_ENABLE_ASSERTIONS)
if(NOT debuginfo AND NOT CFLAGS_ENABLE_ASSERTIONS)
# Unfortunately, this cannot be folded into the standard
# CMAKE_CXX_FLAGS_... because Apple multi-SDK builds use different
# architectures for different SDKs (CFLAGS_ARCH isn't constant here).
if("${CFLAGS_ARCH}" STREQUAL "i386" OR "${CFLAGS_ARCH}" STREQUAL "i686")
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
list(APPEND result "-momit-leaf-frame-pointer")
Expand All @@ -198,27 +199,6 @@ function(_add_target_variant_c_compile_flags)
endif()
endif()
endif()
else()
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
list(APPEND result "-O0")
else()
list(APPEND result "/Od")
endif()
endif()

# CMake automatically adds the flags for debug info if we use MSVC/clang-cl.
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
is_build_type_with_debuginfo("${CFLAGS_BUILD_TYPE}" debuginfo)
if(debuginfo)
_compute_lto_flag("${CFLAGS_ENABLE_LTO}" _lto_flag_out)
if(_lto_flag_out)
list(APPEND result "-gline-tables-only")
else()
list(APPEND result "-g")
endif()
else()
list(APPEND result "-g0")
endif()
endif()

if("${CFLAGS_SDK}" STREQUAL "WINDOWS")
Expand Down
4 changes: 0 additions & 4 deletions utils/build-script-impl
Original file line number Diff line number Diff line change
Expand Up @@ -1574,8 +1574,6 @@ for host in "${ALL_HOSTS[@]}"; do
"${cmake_options[@]}"
-DCMAKE_C_FLAGS="$(llvm_c_flags ${host})"
-DCMAKE_CXX_FLAGS="$(llvm_c_flags ${host})"
-DCMAKE_C_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
-DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
-DCMAKE_BUILD_TYPE:STRING="${LLVM_BUILD_TYPE}"
-DLLVM_TOOL_SWIFT_BUILD:BOOL=NO
-DLLVM_TOOL_LLD_BUILD:BOOL=TRUE
Expand Down Expand Up @@ -1754,8 +1752,6 @@ for host in "${ALL_HOSTS[@]}"; do
"${cmake_options[@]}"
-DCMAKE_C_FLAGS="$(swift_c_flags ${host})"
-DCMAKE_CXX_FLAGS="$(swift_c_flags ${host})"
-DCMAKE_C_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
-DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O2 -DNDEBUG"
-DCMAKE_BUILD_TYPE:STRING="${SWIFT_BUILD_TYPE}"
-DLLVM_ENABLE_ASSERTIONS:BOOL=$(true_false "${SWIFT_ENABLE_ASSERTIONS}")
-DSWIFT_ANALYZE_CODE_COVERAGE:STRING=$(toupper "${SWIFT_ANALYZE_CODE_COVERAGE}")
Expand Down