Skip to content

Extend freestanding to support targeting Darwin platforms #40202

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 6 commits into from
Dec 8, 2021
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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,9 @@ set(SWIFT_DARWIN_PLATFORMS "IOS" "IOS_SIMULATOR" "TVOS" "TVOS_SIMULATOR" "WATCHO
set(SWIFT_APPLE_PLATFORMS ${SWIFT_DARWIN_PLATFORMS})
if(SWIFT_FREESTANDING_FLAVOR STREQUAL "apple")
list(APPEND SWIFT_APPLE_PLATFORMS "FREESTANDING")
if(SWIFT_FREESTANDING_IS_DARWIN)
list(APPEND SWIFT_DARWIN_PLATFORMS "FREESTANDING")
endif()
endif()

# Configuration flags passed to all of our invocations of gyb. Try to
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if(SWIFT_ENABLE_REFLECTION)
endif()

set(swiftDarwin_target_sdks ALL_APPLE_PLATFORMS)
if(SWIFT_FREESTANDING_FLAVOR STREQUAL "apple")
if(SWIFT_FREESTANDING_FLAVOR STREQUAL "apple" AND NOT SWIFT_FREESTANDING_IS_DARWIN)
set(swiftDarwin_target_sdks ALL_APPLE_PLATFORMS FREESTANDING)
endif()

Expand Down
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ normalize_boolean_spelling(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)
normalize_boolean_spelling(SWIFT_HAVE_LIBXML2)
normalize_boolean_spelling(SWIFT_INCLUDE_TOOLS)
normalize_boolean_spelling(SWIFT_STDLIB_STATIC_PRINT)
normalize_boolean_spelling(SWIFT_ENABLE_DISPATCH)
normalize_boolean_spelling(SWIFT_STDLIB_ENABLE_OBJC_INTEROP)
is_build_type_optimized("${SWIFT_STDLIB_BUILD_TYPE}" SWIFT_OPTIMIZED)

set(profdata_merge_worker
Expand Down
36 changes: 26 additions & 10 deletions test/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -938,18 +938,24 @@ if run_vendor == 'apple':
# The "freestanding" tests will link against the static libswiftCore.a and
# cannot use any of Obj-C / Dispatch / Foundation.
if "-freestanding" in config.variant_suffix:
config.target_runtime = "native"
config.available_features.remove('libdispatch')
config.available_features.remove('foundation')
config.available_features.remove('objc_interop')
if not config.swift_freestanding_is_darwin:
config.target_runtime = "native"
if not config.swift_enable_dispatch:
config.available_features.remove('libdispatch')
if not config.swift_freestanding_is_darwin:
config.available_features.remove('foundation')
if not config.swift_stdlib_enable_objc_interop:
config.available_features.remove('objc_interop')
config.available_features.add('freestanding')

# Build all "freestanding" tests with -disable-objc-interop
swift_execution_tests_extra_flags += ' -Xfrontend -disable-objc-interop'
if not config.swift_stdlib_enable_objc_interop:
swift_execution_tests_extra_flags += ' -Xfrontend -disable-objc-interop'

# The Concurrency module is not available in "freestanding" mode.
swift_execution_tests_extra_flags += \
' -Xfrontend -disable-implicit-concurrency-module-import'
if not 'concurrency' in config.available_features:
swift_execution_tests_extra_flags += \
' -Xfrontend -disable-implicit-concurrency-module-import'

# To have visible references from symbolic manglings
swift_execution_tests_extra_flags += \
Expand All @@ -958,9 +964,11 @@ if run_vendor == 'apple':
# Link all "freestanding" tests with -dead_strip, which can effectively
# even remove parts of the stdlib and runtime, if it's not needed. Since
# it's a very desired behavior, let's enable it for all executable tests.
swift_execution_tests_extra_flags += ' -Xlinker -dead_strip'
if not config.swift_freestanding_is_darwin:
swift_execution_tests_extra_flags += ' -Xlinker -dead_strip'

swift_execution_tests_extra_flags += ' -experimental-hermetic-seal-at-link -lto=llvm-full'
if not config.swift_freestanding_is_darwin:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine for now, but the hermetic seal at link thing feels like it should have its own feature option sort of thing.

But that being said, I think this is good incremental work. Maybe put in a TODO.

swift_execution_tests_extra_flags += ' -experimental-hermetic-seal-at-link -lto=llvm-full'

# Build a resource dir for freestanding tests.
new_resource_dir = os.path.join(config.test_exec_root, "resource_dir")
Expand All @@ -972,6 +980,8 @@ if run_vendor == 'apple':
symlink_if_not_exists("clang", "clang")
symlink_if_not_exists("shims", "shims")
symlink_if_not_exists("freestanding", "macosx")
if config.swift_freestanding_is_darwin:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment here explaining why we are doing this?

symlink_if_not_exists("apinotes", "apinotes")
config.resource_dir_opt = "-resource-dir %s" % new_resource_dir
lit_config.note('Using freestanding resource dir: ' + new_resource_dir)

Expand Down Expand Up @@ -1160,7 +1170,13 @@ if run_vendor == 'apple':
'Running tests on %s version %s (%s)' %
(sw_vers_name, sw_vers_vers, sw_vers_build))

config.target_sdk_name = xcrun_sdk_name
if "-freestanding" in config.variant_suffix and config.swift_freestanding_is_darwin:
# This will ensure we are able to find libraries
# and resource dir at the correct path in tests that require
# Foundation and ObjectiveC capabilities
config.target_sdk_name = config.freestanding_sdk_name
else:
config.target_sdk_name = xcrun_sdk_name
config.target_ld = "%s ld -L%r" % (xcrun_prefix, make_path(test_resource_dir, config.target_sdk_name))

maccatalyst_extra_frameworks = ""
Expand Down
6 changes: 6 additions & 0 deletions test/lit.site.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ if "@SWIFT_STDLIB_STATIC_PRINT@" == "TRUE":
if "@SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING@" == "TRUE":
config.available_features.add('string_processing')

config.swift_freestanding_is_darwin = "@SWIFT_FREESTANDING_IS_DARWIN@" == "TRUE"
config.swift_enable_dispatch = "@SWIFT_ENABLE_DISPATCH@" == "TRUE"
config.swift_stdlib_enable_objc_interop = "@SWIFT_STDLIB_ENABLE_OBJC_INTEROP@" == "TRUE"
# Configured in DarwinSDKs.cmake
config.freestanding_sdk_name = "@SWIFT_SDK_FREESTANDING_LIB_SUBDIR@"

# Let the main config do the real work.
if config.test_exec_root is None:
config.test_exec_root = os.path.dirname(os.path.realpath(__file__))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

// REQUIRES: freestanding
// REQUIRES: executable_test
// UNSUPPORTED: objc_interop

// RUN: %{python} %utils/check_freestanding_dependencies.py --vendor %target-vendor --library %test-resource-dir/freestanding/libswiftCore.a --nm-path %llvm-nm
24 changes: 24 additions & 0 deletions utils/build-presets.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2564,6 +2564,30 @@ mixin-preset=stdlib_S_standalone_minimal_macho_x86_64,build
test
validation-test

[preset: stdlib_S_standalone_darwin_x86_64,build]
mixin-preset=
stdlib_S_standalone,build

build-subdir=stdlib_S_standalone_darwin
enable-experimental-differentiable-programming=0
enable-experimental-concurrency=0
enable-experimental-distributed=0

stdlib-deployment-targets=freestanding-x86_64
swift-primary-variant-sdk=FREESTANDING
swift-primary-variant-arch=x86_64
swift-freestanding-flavor=apple
swift-freestanding-sdk=macosx
swift-freestanding-is-darwin=1
swift-freestanding-triple-name=macosx11.0
swift-freestanding-module-name=macos
swift-freestanding-archs=x86_64

[preset: stdlib_S_standalone_darwin_x86_64,build,test]
mixin-preset=stdlib_S_standalone_darwin_x86_64,build

test
validation-test

#===----------------------------------------------------------------------===#
# Preset for Source Compatibility Suite
Expand Down
3 changes: 3 additions & 0 deletions utils/build_swift/build_swift/driver_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,9 @@ def create_argument_parser():
'module-only targets on Darwin platforms. These targets are '
'in addition to the full library targets.')

option('--swift-freestanding-is-darwin', toggle_true,
help='True if the freestanding platform is a Darwin one.')

# -------------------------------------------------------------------------
in_group('Options to select projects')

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 @@ -218,6 +218,7 @@
'swift_disable_dead_stripping': False,
'swift_darwin_module_archs': None,
'swift_darwin_supported_archs': None,
'swift_freestanding_is_darwin': False,
'swift_stdlib_assertions': True,
'swift_stdlib_build_variant': 'Debug',
'swift_tools_max_parallel_lto_link_jobs':
Expand Down Expand Up @@ -681,6 +682,7 @@ class BuildScriptImplOption(_BaseOption):
StrOption('--stdlib-deployment-targets'),
StrOption('--swift-darwin-module-archs'),
StrOption('--swift-darwin-supported-archs'),
SetTrueOption('--swift-freestanding-is-darwin'),

PathOption('--android-deploy-device-path'),
PathOption('--android-icu-i18n'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def __init__(self, args, toolchain, source_dir, build_dir):
# Add experimental string processing flag.
self.cmake_options.extend(self._enable_experimental_string_processing)

# Add freestanding related flags.
self.cmake_options.extend(self._freestanding_is_darwin)

@classmethod
def is_build_script_impl_product(cls):
"""is_build_script_impl_product -> bool
Expand Down Expand Up @@ -171,6 +174,11 @@ def _enable_experimental_string_processing(self):
return [('SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING:BOOL',
self.args.enable_experimental_string_processing)]

@property
def _freestanding_is_darwin(self):
return [('SWIFT_FREESTANDING_IS_DARWIN:BOOL',
self.args.swift_freestanding_is_darwin)]

@classmethod
def get_dependencies(cls):
return [cmark.CMark,
Expand Down
22 changes: 19 additions & 3 deletions utils/swift_build_support/tests/products/test_swift.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def setUp(self):
enable_experimental_concurrency=False,
enable_experimental_distributed=False,
build_swift_stdlib_static_print=False,
enable_experimental_string_processing=False)
enable_experimental_string_processing=False,
swift_freestanding_is_darwin=False)

# Setup shell
shell.dry_run = True
Expand Down Expand Up @@ -97,7 +98,8 @@ def test_by_default_no_cmake_options(self):
'-DSWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY:BOOL=FALSE',
'-DSWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED:BOOL=FALSE',
'-DSWIFT_STDLIB_STATIC_PRINT=FALSE',
'-DSWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING:BOOL=FALSE'
'-DSWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING:BOOL=FALSE',
'-DSWIFT_FREESTANDING_IS_DARWIN:BOOL=FALSE',
]
self.assertEqual(set(swift.cmake_options), set(expected))

Expand All @@ -117,7 +119,8 @@ def test_swift_runtime_tsan(self):
'-DSWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY:BOOL=FALSE',
'-DSWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED:BOOL=FALSE',
'-DSWIFT_STDLIB_STATIC_PRINT=FALSE',
'-DSWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING:BOOL=FALSE'
'-DSWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING:BOOL=FALSE',
'-DSWIFT_FREESTANDING_IS_DARWIN:BOOL=FALSE',
]
self.assertEqual(set(swift.cmake_options), set(flags_set))

Expand Down Expand Up @@ -369,3 +372,16 @@ def test_experimental_string_processing_flags(self):
'TRUE'],
[x for x in swift.cmake_options
if 'DSWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING' in x])

def test_freestanding_is_darwin_flags(self):
self.args.swift_freestanding_is_darwin = True
swift = Swift(
args=self.args,
toolchain=self.toolchain,
source_dir='/path/to/src',
build_dir='/path/to/build')
self.assertEqual(
['-DSWIFT_FREESTANDING_IS_DARWIN:BOOL='
'TRUE'],
[x for x in swift.cmake_options
if 'SWIFT_FREESTANDING_IS_DARWIN' in x])
6 changes: 6 additions & 0 deletions validation-test/lit.site.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ if "@SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED@" == "TRUE":
if "@SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING@" == "TRUE":
config.available_features.add('string_processing')

config.swift_freestanding_is_darwin = "@SWIFT_FREESTANDING_IS_DARWIN@" == "TRUE"
config.swift_enable_dispatch = "@SWIFT_ENABLE_DISPATCH@" == "TRUE"
config.swift_stdlib_enable_objc_interop = "@SWIFT_STDLIB_ENABLE_OBJC_INTEROP@" == "TRUE"
# Configured in DarwinSDKs.cmake
config.freestanding_sdk_name = "@SWIFT_SDK_FREESTANDING_LIB_SUBDIR@"

# Let the main config do the real work.
config.test_exec_root = os.path.dirname(os.path.realpath(__file__))
lit_config.load_config(config, "@SWIFT_SOURCE_DIR@/validation-test/lit.cfg")