Skip to content

Add a 'standalone_minimal' preset to build a minimal, static, OS independent, self-contained binaries of stdlib. #33286

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 17 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
6 changes: 5 additions & 1 deletion cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
18 changes: 18 additions & 0 deletions cmake/modules/DarwinSDKs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
6 changes: 5 additions & 1 deletion stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
23 changes: 17 additions & 6 deletions stdlib/cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions utils/build-presets.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
#===----------------------------------------------------------------------===#
Expand Down
26 changes: 26 additions & 0 deletions utils/build-script-impl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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=(
Expand Down
5 changes: 5 additions & 0 deletions utils/build_swift/build_swift/driver_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
2 changes: 2 additions & 0 deletions utils/build_swift/tests/expected_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
7 changes: 7 additions & 0 deletions utils/swift_build_support/swift_build_support/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -207,6 +213,7 @@ class StdlibDeploymentTarget(object):
iOS, iOSSimulator,
AppleTV, AppleTVSimulator,
AppleWatch, AppleWatchSimulator,
Freestanding,
Linux,
FreeBSD,
OpenBSD,
Expand Down