Skip to content

Commit ef89b0d

Browse files
authored
Add a 'standalone_minimal' preset to build a minimal, static, OS independent, self-contained binaries of stdlib. (swiftlang#33286)
1 parent 671be54 commit ef89b0d

File tree

10 files changed

+124
-8
lines changed

10 files changed

+124
-8
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ function(_add_host_variant_c_compile_flags target)
131131

132132
is_build_type_optimized("${CMAKE_BUILD_TYPE}" optimized)
133133
if(optimized)
134-
target_compile_options(${target} PRIVATE -O2)
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()
135139

136140
# Omit leaf frame pointers on x86 production builds (optimized, no debug
137141
# info, and no asserts).

cmake/modules/DarwinSDKs.cmake

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ if(swift_build_osx)
2626
configure_target_variant(OSX-R "OS X Release" OSX R "Release")
2727
endif()
2828

29+
is_sdk_requested(FREESTANDING swift_build_freestanding)
30+
if(swift_build_freestanding)
31+
set(SWIFT_FREESTANDING_SDK "" CACHE STRING
32+
"Which SDK to use when building the FREESTANDING stdlib")
33+
set(SWIFT_FREESTANDING_TRIPLE_NAME "" CACHE STRING
34+
"Which triple name (e.g. 'none-macho') to use when building the FREESTANDING stdlib")
35+
set(SWIFT_FREESTANDING_ARCHS "" CACHE STRING
36+
"Which architectures to build when building the FREESTANDING stdlib")
37+
configure_sdk_darwin(
38+
FREESTANDING "FREESTANDING" ""
39+
"${SWIFT_FREESTANDING_SDK}" freestanding "${SWIFT_FREESTANDING_TRIPLE_NAME}" freestanding "${SWIFT_FREESTANDING_ARCHS}")
40+
set(SWIFT_SDK_FREESTANDING_LIB_SUBDIR "freestanding")
41+
configure_target_variant(FREESTANDING-DA "FREESTANDING Debug+Asserts" FREESTANDING DA "Debug+Asserts")
42+
configure_target_variant(FREESTANDING-RA "FREESTANDING Release+Asserts" FREESTANDING RA "Release+Asserts")
43+
configure_target_variant(FREESTANDING-R "FREESTANDING Release" FREESTANDING R "Release")
44+
configure_target_variant(FREESTANDING-S "FREESTANDING MinSizeRelease" FREESTANDING S "MinSizeRelease")
45+
endif()
46+
2947
# Compatible cross-compile SDKS for Darwin OSes: IOS, IOS_SIMULATOR, TVOS,
3048
# TVOS_SIMULATOR, WATCHOS, WATCHOS_SIMULATOR (archs hardcoded below).
3149

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ function(_add_target_variant_c_compile_flags)
180180

181181
is_build_type_optimized("${CFLAGS_BUILD_TYPE}" optimized)
182182
if(optimized)
183-
list(APPEND result "-O2")
183+
if("${CFLAGS_BUILD_TYPE}" STREQUAL "MinSizeRel")
184+
list(APPEND result "-Os")
185+
else()
186+
list(APPEND result "-O2")
187+
endif()
184188

185189
# Omit leaf frame pointers on x86 production builds (optimized, no debug
186190
# info, and no asserts).

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ function(compute_library_subdir result_var_name sdk arch)
1111
endif()
1212
endfunction()
1313

14+
# Return a swiftc flag (e.g. -O or -Onone) to control optimization level based
15+
# on the build type.
16+
function(swift_optimize_flag_for_build_type build_type result_var_name)
17+
if("${build_type}" STREQUAL "Debug")
18+
set("${result_var_name}" "-Onone" PARENT_SCOPE)
19+
elseif("${build_type}" STREQUAL "RelWithDebInfo" OR
20+
"${build_type}" STREQUAL "Release")
21+
set("${result_var_name}" "-O" PARENT_SCOPE)
22+
elseif("${build_type}" STREQUAL "MinSizeRel")
23+
set("${result_var_name}" "-Osize" PARENT_SCOPE)
24+
else()
25+
message(FATAL_ERROR "Unknown build type: ${build_type}")
26+
endif()
27+
endfunction()
28+
1429
# Process the sources within the given variable, pulling out any Swift
1530
# sources to be compiled with 'swift' directly. This updates
1631
# ${sourcesvar} in place with the resulting list and ${externalvar} with the
@@ -230,12 +245,8 @@ function(_add_target_variant_swift_compile_flags
230245
"-F${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}/../../../Developer/Library/Frameworks")
231246
endif()
232247

233-
is_build_type_optimized("${build_type}" optimized)
234-
if(optimized)
235-
list(APPEND result "-O")
236-
else()
237-
list(APPEND result "-Onone")
238-
endif()
248+
swift_optimize_flag_for_build_type("${CMAKE_BUILD_TYPE}" optimize_flag)
249+
list(APPEND result "${optimize_flag}")
239250

240251
is_build_type_with_debuginfo("${build_type}" debuginfo)
241252
if(debuginfo)

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ function(get_test_dependencies SDK result_var_name)
102102
("${SDK}" STREQUAL "IOS_SIMULATOR") OR
103103
("${SDK}" STREQUAL "TVOS_SIMULATOR") OR
104104
("${SDK}" STREQUAL "WATCHOS_SIMULATOR") OR
105+
("${SDK}" STREQUAL "FREESTANDING") OR
105106
("${SDK}" STREQUAL "LINUX") OR
106107
("${SDK}" STREQUAL "CYGWIN") OR
107108
("${SDK}" STREQUAL "FREEBSD") OR

utils/build-presets.ini

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,6 +2365,44 @@ mixin-preset=stdlib_DA_standalone,build
23652365
test
23662366
validation-test
23672367

2368+
[preset: stdlib_S_standalone,build]
2369+
mixin-preset=stdlib_base_standalone
2370+
2371+
build-subdir=stdlib_S_standalone
2372+
min-size-release
2373+
no-assertions
2374+
2375+
verbose-build
2376+
2377+
[preset: stdlib_SA_standalone,build]
2378+
mixin-preset=stdlib_base_standalone
2379+
2380+
build-subdir=stdlib_SA_standalone
2381+
min-size-release
2382+
assertions
2383+
2384+
verbose-build
2385+
2386+
[preset: mixin_stdlib_minimal]
2387+
enable-experimental-differentiable-programming=0
2388+
enable-experimental-concurrency=0
2389+
build-swift-dynamic-sdk-overlay=0
2390+
build-swift-dynamic-stdlib=0
2391+
build-swift-static-stdlib=1
2392+
2393+
[preset: stdlib_S_standalone_minimal_macho_x86_64,build]
2394+
mixin-preset=
2395+
stdlib_S_standalone,build
2396+
mixin_stdlib_minimal
2397+
2398+
stdlib-deployment-targets=freestanding-x86_64
2399+
swift-primary-variant-sdk=FREESTANDING
2400+
swift-primary-variant-arch=x86_64
2401+
swift-freestanding-sdk=macosx
2402+
# For now, until clang/swiftc works correctly with "none-macho" as the OS part of target triple.
2403+
swift-freestanding-triple-name=macosx11.0
2404+
swift-freestanding-archs=x86_64
2405+
23682406
#===----------------------------------------------------------------------===#
23692407
# Preset for Source Compatibility Suite
23702408
#===----------------------------------------------------------------------===#

utils/build-script-impl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ KNOWN_SETTINGS=(
193193
sil-verify-all "0" "If enabled, run the SIL verifier after each transform when building Swift files during this build process"
194194
stdlib-deployment-targets "" "space-separated list of targets to configure the Swift standard library to be compiled or cross-compiled for"
195195

196+
## FREESTANDING Stdlib Options
197+
swift-freestanding-sdk "" "which SDK to use when building the FREESTANDING stdlib"
198+
swift-freestanding-triple-name "" "which triple name (e.g. 'none-macho') to use when building the FREESTANDING stdlib"
199+
swift-freestanding-archs "" "space-separated list of which architectures to build when building the FREESTANDING stdlib"
200+
196201
## Uncategorised
197202
install-prefix "" "installation prefix"
198203
toolchain-prefix "" "the path to the .xctoolchain directory that houses the install prefix path"
@@ -1832,6 +1837,27 @@ for host in "${ALL_HOSTS[@]}"; do
18321837
)
18331838
fi
18341839

1840+
if [ "${SWIFT_FREESTANDING_SDK}" ] ; then
1841+
cmake_options=(
1842+
"${cmake_options[@]}"
1843+
-DSWIFT_FREESTANDING_SDK:STRING="${SWIFT_FREESTANDING_SDK}"
1844+
)
1845+
fi
1846+
1847+
if [ "${SWIFT_FREESTANDING_TRIPLE_NAME}" ] ; then
1848+
cmake_options=(
1849+
"${cmake_options[@]}"
1850+
-DSWIFT_FREESTANDING_TRIPLE_NAME:STRING="${SWIFT_FREESTANDING_TRIPLE_NAME}"
1851+
)
1852+
fi
1853+
1854+
if [[ "${SWIFT_FREESTANDING_ARCHS}" ]] ; then
1855+
cmake_options=(
1856+
"${cmake_options[@]}"
1857+
-DSWIFT_FREESTANDING_ARCHS:STRING="$(join ";" ${SWIFT_FREESTANDING_ARCHS[@]})"
1858+
)
1859+
fi
1860+
18351861
if [[ ! "${SKIP_BUILD_LLDB}" ]] ; then
18361862
lldb_build_dir=$(build_directory ${host} lldb)
18371863
cmake_options=(

utils/build_swift/build_swift/driver_arguments.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,11 @@ def create_argument_parser():
680680
help='build the Release variant of everything (default is '
681681
'%(default)s)')
682682

683+
option(['--min-size-release'], store('build_variant'),
684+
const='MinSizeRel',
685+
help='build the MinSizeRel variant of everything (default is '
686+
'%(default)s)')
687+
683688
# -------------------------------------------------------------------------
684689
in_group('Override build variant for a specific project')
685690

utils/build_swift/tests/expected_options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ class BuildScriptImplOption(_BaseOption):
412412
SetOption('--release', dest='build_variant', value='Release'),
413413
SetOption('--release-debuginfo',
414414
dest='build_variant', value='RelWithDebInfo'),
415+
SetOption('--min-size-release',
416+
dest='build_variant', value='MinSizeRel'),
415417
SetOption('--xcode', dest='cmake_generator', value='Xcode'),
416418
SetOption('-R', dest='build_variant', value='Release'),
417419
SetOption('-d', dest='build_variant', value='Debug'),

utils/swift_build_support/swift_build_support/targets.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ class StdlibDeploymentTarget(object):
179179
sdk_name="WATCHOS_SIMULATOR",
180180
is_simulator=True)
181181

182+
# A platform that's not tied to any particular OS, and it meant to be used
183+
# to build the stdlib as standalone and/or statically linked.
184+
Freestanding = Platform("freestanding",
185+
archs=["i386", "x86_64", "armv7", "armv7s", "armv7k",
186+
"arm64", "arm64e"])
187+
182188
Linux = Platform("linux", archs=[
183189
"x86_64",
184190
"i686",
@@ -207,6 +213,7 @@ class StdlibDeploymentTarget(object):
207213
iOS, iOSSimulator,
208214
AppleTV, AppleTVSimulator,
209215
AppleWatch, AppleWatchSimulator,
216+
Freestanding,
210217
Linux,
211218
FreeBSD,
212219
OpenBSD,

0 commit comments

Comments
 (0)