diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index c9f4f8208ec7a..604870d28b6e3 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -131,7 +131,11 @@ function(_add_host_variant_c_compile_flags target) is_build_type_optimized("${CMAKE_BUILD_TYPE}" optimized) if(optimized) - target_compile_options(${target} PRIVATE -O2) + if("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel") + target_compile_options(${target} PRIVATE -Os) + else() + target_compile_options(${target} PRIVATE -O2) + endif() # Omit leaf frame pointers on x86 production builds (optimized, no debug # info, and no asserts). diff --git a/cmake/modules/DarwinSDKs.cmake b/cmake/modules/DarwinSDKs.cmake index 20a5620fba6a7..dc570b7005834 100644 --- a/cmake/modules/DarwinSDKs.cmake +++ b/cmake/modules/DarwinSDKs.cmake @@ -26,6 +26,24 @@ if(swift_build_osx) configure_target_variant(OSX-R "OS X Release" OSX R "Release") endif() +is_sdk_requested(FREESTANDING swift_build_freestanding) +if(swift_build_freestanding) + set(SWIFT_FREESTANDING_SDK "" CACHE STRING + "Which SDK to use when building the FREESTANDING stdlib") + set(SWIFT_FREESTANDING_TRIPLE_NAME "" CACHE STRING + "Which triple name (e.g. 'none-macho') to use when building the FREESTANDING stdlib") + set(SWIFT_FREESTANDING_ARCHS "" CACHE STRING + "Which architectures to build when building the FREESTANDING stdlib") + configure_sdk_darwin( + FREESTANDING "FREESTANDING" "" + "${SWIFT_FREESTANDING_SDK}" freestanding "${SWIFT_FREESTANDING_TRIPLE_NAME}" freestanding "${SWIFT_FREESTANDING_ARCHS}") + set(SWIFT_SDK_FREESTANDING_LIB_SUBDIR "freestanding") + configure_target_variant(FREESTANDING-DA "FREESTANDING Debug+Asserts" FREESTANDING DA "Debug+Asserts") + configure_target_variant(FREESTANDING-RA "FREESTANDING Release+Asserts" FREESTANDING RA "Release+Asserts") + configure_target_variant(FREESTANDING-R "FREESTANDING Release" FREESTANDING R "Release") + configure_target_variant(FREESTANDING-S "FREESTANDING MinSizeRelease" FREESTANDING S "MinSizeRelease") +endif() + # Compatible cross-compile SDKS for Darwin OSes: IOS, IOS_SIMULATOR, TVOS, # TVOS_SIMULATOR, WATCHOS, WATCHOS_SIMULATOR (archs hardcoded below). diff --git a/stdlib/cmake/modules/AddSwiftStdlib.cmake b/stdlib/cmake/modules/AddSwiftStdlib.cmake index 2d9c5130dff04..1536bd0422411 100644 --- a/stdlib/cmake/modules/AddSwiftStdlib.cmake +++ b/stdlib/cmake/modules/AddSwiftStdlib.cmake @@ -180,7 +180,11 @@ function(_add_target_variant_c_compile_flags) is_build_type_optimized("${CFLAGS_BUILD_TYPE}" optimized) if(optimized) - list(APPEND result "-O2") + if("${CFLAGS_BUILD_TYPE}" STREQUAL "MinSizeRel") + list(APPEND result "-Os") + else() + list(APPEND result "-O2") + endif() # Omit leaf frame pointers on x86 production builds (optimized, no debug # info, and no asserts). diff --git a/stdlib/cmake/modules/SwiftSource.cmake b/stdlib/cmake/modules/SwiftSource.cmake index 5cc168883f3fd..59ec8d1d2bf46 100644 --- a/stdlib/cmake/modules/SwiftSource.cmake +++ b/stdlib/cmake/modules/SwiftSource.cmake @@ -11,6 +11,21 @@ function(compute_library_subdir result_var_name sdk arch) endif() endfunction() +# Return a swiftc flag (e.g. -O or -Onone) to control optimization level based +# on the build type. +function(swift_optimize_flag_for_build_type build_type result_var_name) + if("${build_type}" STREQUAL "Debug") + set("${result_var_name}" "-Onone" PARENT_SCOPE) + elseif("${build_type}" STREQUAL "RelWithDebInfo" OR + "${build_type}" STREQUAL "Release") + set("${result_var_name}" "-O" PARENT_SCOPE) + elseif("${build_type}" STREQUAL "MinSizeRel") + set("${result_var_name}" "-Osize" PARENT_SCOPE) + else() + message(FATAL_ERROR "Unknown build type: ${build_type}") + endif() +endfunction() + # Process the sources within the given variable, pulling out any Swift # sources to be compiled with 'swift' directly. This updates # ${sourcesvar} in place with the resulting list and ${externalvar} with the @@ -230,12 +245,8 @@ function(_add_target_variant_swift_compile_flags "-F${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}/../../../Developer/Library/Frameworks") endif() - is_build_type_optimized("${build_type}" optimized) - if(optimized) - list(APPEND result "-O") - else() - list(APPEND result "-Onone") - endif() + swift_optimize_flag_for_build_type("${CMAKE_BUILD_TYPE}" optimize_flag) + list(APPEND result "${optimize_flag}") is_build_type_with_debuginfo("${build_type}" debuginfo) if(debuginfo) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4513359e3e5d7..ec8710ec76948 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -102,6 +102,7 @@ function(get_test_dependencies SDK result_var_name) ("${SDK}" STREQUAL "IOS_SIMULATOR") OR ("${SDK}" STREQUAL "TVOS_SIMULATOR") OR ("${SDK}" STREQUAL "WATCHOS_SIMULATOR") OR + ("${SDK}" STREQUAL "FREESTANDING") OR ("${SDK}" STREQUAL "LINUX") OR ("${SDK}" STREQUAL "CYGWIN") OR ("${SDK}" STREQUAL "FREEBSD") OR diff --git a/utils/build-presets.ini b/utils/build-presets.ini index 9eaa76a841a99..bf3868dfc97c4 100644 --- a/utils/build-presets.ini +++ b/utils/build-presets.ini @@ -2365,6 +2365,44 @@ mixin-preset=stdlib_DA_standalone,build test validation-test +[preset: stdlib_S_standalone,build] +mixin-preset=stdlib_base_standalone + +build-subdir=stdlib_S_standalone +min-size-release +no-assertions + +verbose-build + +[preset: stdlib_SA_standalone,build] +mixin-preset=stdlib_base_standalone + +build-subdir=stdlib_SA_standalone +min-size-release +assertions + +verbose-build + +[preset: mixin_stdlib_minimal] +enable-experimental-differentiable-programming=0 +enable-experimental-concurrency=0 +build-swift-dynamic-sdk-overlay=0 +build-swift-dynamic-stdlib=0 +build-swift-static-stdlib=1 + +[preset: stdlib_S_standalone_minimal_macho_x86_64,build] +mixin-preset= + stdlib_S_standalone,build + mixin_stdlib_minimal + +stdlib-deployment-targets=freestanding-x86_64 +swift-primary-variant-sdk=FREESTANDING +swift-primary-variant-arch=x86_64 +swift-freestanding-sdk=macosx +# For now, until clang/swiftc works correctly with "none-macho" as the OS part of target triple. +swift-freestanding-triple-name=macosx11.0 +swift-freestanding-archs=x86_64 + #===----------------------------------------------------------------------===# # Preset for Source Compatibility Suite #===----------------------------------------------------------------------===# diff --git a/utils/build-script-impl b/utils/build-script-impl index 08e4778ac21ce..10fbaa1e854ea 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -224,6 +224,11 @@ KNOWN_SETTINGS=( sil-verify-all "0" "If enabled, run the SIL verifier after each transform when building Swift files during this build process" stdlib-deployment-targets "" "space-separated list of targets to configure the Swift standard library to be compiled or cross-compiled for" + ## FREESTANDING Stdlib Options + swift-freestanding-sdk "" "which SDK to use when building the FREESTANDING stdlib" + swift-freestanding-triple-name "" "which triple name (e.g. 'none-macho') to use when building the FREESTANDING stdlib" + swift-freestanding-archs "" "space-separated list of which architectures to build when building the FREESTANDING stdlib" + ## Uncategorised install-prefix "" "installation prefix" toolchain-prefix "" "the path to the .xctoolchain directory that houses the install prefix path" @@ -1863,6 +1868,27 @@ for host in "${ALL_HOSTS[@]}"; do ) fi + if [ "${SWIFT_FREESTANDING_SDK}" ] ; then + cmake_options=( + "${cmake_options[@]}" + -DSWIFT_FREESTANDING_SDK:STRING="${SWIFT_FREESTANDING_SDK}" + ) + fi + + if [ "${SWIFT_FREESTANDING_TRIPLE_NAME}" ] ; then + cmake_options=( + "${cmake_options[@]}" + -DSWIFT_FREESTANDING_TRIPLE_NAME:STRING="${SWIFT_FREESTANDING_TRIPLE_NAME}" + ) + fi + + if [[ "${SWIFT_FREESTANDING_ARCHS}" ]] ; then + cmake_options=( + "${cmake_options[@]}" + -DSWIFT_FREESTANDING_ARCHS:STRING="$(join ";" ${SWIFT_FREESTANDING_ARCHS[@]})" + ) + fi + if [[ ! "${SKIP_BUILD_LLDB}" ]] ; then lldb_build_dir=$(build_directory ${host} lldb) cmake_options=( diff --git a/utils/build_swift/build_swift/driver_arguments.py b/utils/build_swift/build_swift/driver_arguments.py index 40959dd529d1e..62acc4557d027 100644 --- a/utils/build_swift/build_swift/driver_arguments.py +++ b/utils/build_swift/build_swift/driver_arguments.py @@ -680,6 +680,11 @@ def create_argument_parser(): help='build the Release variant of everything (default is ' '%(default)s)') + option(['--min-size-release'], store('build_variant'), + const='MinSizeRel', + help='build the MinSizeRel variant of everything (default is ' + '%(default)s)') + # ------------------------------------------------------------------------- in_group('Override build variant for a specific project') diff --git a/utils/build_swift/tests/expected_options.py b/utils/build_swift/tests/expected_options.py index bbcd8577d94b3..9864063b6e7bb 100644 --- a/utils/build_swift/tests/expected_options.py +++ b/utils/build_swift/tests/expected_options.py @@ -412,6 +412,8 @@ class BuildScriptImplOption(_BaseOption): SetOption('--release', dest='build_variant', value='Release'), SetOption('--release-debuginfo', dest='build_variant', value='RelWithDebInfo'), + SetOption('--min-size-release', + dest='build_variant', value='MinSizeRel'), SetOption('--xcode', dest='cmake_generator', value='Xcode'), SetOption('-R', dest='build_variant', value='Release'), SetOption('-d', dest='build_variant', value='Debug'), diff --git a/utils/swift_build_support/swift_build_support/targets.py b/utils/swift_build_support/swift_build_support/targets.py index 6bc7abe319fb0..9370a1a82e0ee 100644 --- a/utils/swift_build_support/swift_build_support/targets.py +++ b/utils/swift_build_support/swift_build_support/targets.py @@ -179,6 +179,12 @@ class StdlibDeploymentTarget(object): sdk_name="WATCHOS_SIMULATOR", is_simulator=True) + # A platform that's not tied to any particular OS, and it meant to be used + # to build the stdlib as standalone and/or statically linked. + Freestanding = Platform("freestanding", + archs=["i386", "x86_64", "armv7", "armv7s", "armv7k", + "arm64", "arm64e"]) + Linux = Platform("linux", archs=[ "x86_64", "i686", @@ -207,6 +213,7 @@ class StdlibDeploymentTarget(object): iOS, iOSSimulator, AppleTV, AppleTVSimulator, AppleWatch, AppleWatchSimulator, + Freestanding, Linux, FreeBSD, OpenBSD,