Skip to content
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

Consolidate default features logic #1397

Merged
merged 1 commit into from
Oct 14, 2024
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
2 changes: 2 additions & 0 deletions swift/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ bzl_library(
deps = [
":feature_names",
":package_specs",
":target_triples",
"@bazel_skylib//lib:collections",
"@bazel_skylib//lib:new_sets",
"@bazel_skylib//rules:common_settings",
],
)

Expand Down
74 changes: 74 additions & 0 deletions swift/internal/features.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,31 @@
load("@bazel_skylib//lib:new_sets.bzl", "sets")
load(
":feature_names.bzl",
"SWIFT_FEATURE_CACHEABLE_SWIFTMODULES",
"SWIFT_FEATURE_CHECKED_EXCLUSIVITY",
"SWIFT_FEATURE_COVERAGE",
"SWIFT_FEATURE_COVERAGE_PREFIX_MAP",
"SWIFT_FEATURE_DEBUG_PREFIX_MAP",
"SWIFT_FEATURE_DISABLE_CLANG_SPI",
"SWIFT_FEATURE_DISABLE_SYSTEM_INDEX",
"SWIFT_FEATURE_EMIT_SWIFTDOC",
"SWIFT_FEATURE_EMIT_SWIFTSOURCEINFO",
"SWIFT_FEATURE_ENABLE_BARE_SLASH_REGEX",
"SWIFT_FEATURE_ENABLE_BATCH_MODE",
"SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES",
"SWIFT_FEATURE_ENABLE_TESTING",
"SWIFT_FEATURE_FILE_PREFIX_MAP",
"SWIFT_FEATURE_FULL_DEBUG_INFO",
"SWIFT_FEATURE_INTERNALIZE_AT_LINK",
"SWIFT_FEATURE_NO_GENERATED_MODULE_MAP",
"SWIFT_FEATURE_OBJC_LINK_FLAGS",
"SWIFT_FEATURE_OPT_USES_WMO",
"SWIFT_FEATURE_REMAP_XCODE_PATH",
"SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE",
"SWIFT_FEATURE__FORCE_ALWAYSLINK_TRUE",
)
load(":package_specs.bzl", "label_matches_package_specs")
load(":target_triples.bzl", "target_triples")

def are_all_features_enabled(feature_configuration, feature_names):
"""Returns `True` if all features are enabled in the feature configuration.
Expand Down Expand Up @@ -207,6 +227,60 @@ def is_feature_enabled(feature_configuration, feature_name):
feature_name = feature_name,
)

def default_features_for_toolchain(ctx, target_triple):
"""Enables a common set of swift features based on build configuration.

We have a common set of features we'd like to enable for both
swift_toolchain and xcode_swift_toolchain. This method configures that set
of features based on what exec platform we're using (linux or apple) and
what platform we're targetting (linux, macos, ios, etc.).

Args:
ctx: Context of the swift toolchain rule building this list of features.
target_triple: Target triple configured for our toolchain.

Returns:
List of default features for our toolchain's build config.
"""

# Common features we turn on regardless of target.
features = [
SWIFT_FEATURE_CACHEABLE_SWIFTMODULES,
SWIFT_FEATURE_CHECKED_EXCLUSIVITY,
SWIFT_FEATURE_COVERAGE_PREFIX_MAP,
SWIFT_FEATURE_DEBUG_PREFIX_MAP,
SWIFT_FEATURE_DISABLE_CLANG_SPI,
SWIFT_FEATURE_DISABLE_SYSTEM_INDEX,
SWIFT_FEATURE_EMIT_SWIFTDOC,
SWIFT_FEATURE_EMIT_SWIFTSOURCEINFO,
SWIFT_FEATURE_ENABLE_BARE_SLASH_REGEX,
SWIFT_FEATURE_ENABLE_BATCH_MODE,
SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES,
SWIFT_FEATURE_FILE_PREFIX_MAP,
SWIFT_FEATURE_INTERNALIZE_AT_LINK,
SWIFT_FEATURE_OPT_USES_WMO,
SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE,
]

# Apple specific features
if target_triple.vendor == "apple":
features.extend([
SWIFT_FEATURE_OBJC_LINK_FLAGS,
SWIFT_FEATURE_REMAP_XCODE_PATH,
])

if getattr(ctx.fragments.objc, "alwayslink_by_default", False):
features.append(SWIFT_FEATURE__FORCE_ALWAYSLINK_TRUE)

# Linux specific features
if target_triples.unversioned_os(target_triple) == "linux":
features.extend([
SWIFT_FEATURE__FORCE_ALWAYSLINK_TRUE,
SWIFT_FEATURE_NO_GENERATED_MODULE_MAP,
])

return features

def _check_allowlists(
*,
allowlists,
Expand Down
47 changes: 0 additions & 47 deletions swift/internal/swift_autoconfiguration.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ load(
"SWIFT_FEATURE_DEBUG_PREFIX_MAP",
"SWIFT_FEATURE_EMIT_SWIFTDOC",
"SWIFT_FEATURE_EMIT_SWIFTSOURCEINFO",
"SWIFT_FEATURE_ENABLE_BARE_SLASH_REGEX",
"SWIFT_FEATURE_ENABLE_BATCH_MODE",
"SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES",
"SWIFT_FEATURE_FILE_PREFIX_MAP",
"SWIFT_FEATURE_LLD_GC_WORKAROUND",
"SWIFT_FEATURE_MODULE_MAP_NO_PRIVATE_HEADERS",
"SWIFT_FEATURE_NO_EMBED_DEBUG_MODULE",
Expand Down Expand Up @@ -74,43 +70,6 @@ def _swift_succeeds(repository_ctx, swiftc_path, *args):
swift_result = repository_ctx.execute([swiftc_path] + list(args))
return swift_result.return_code == 0

def _check_enable_batch_mode(repository_ctx, swiftc_path, _temp_dir):
"""Returns True if `swiftc` supports batch mode."""
return _swift_succeeds(
repository_ctx,
swiftc_path,
"-version",
"-enable-batch-mode",
)

def _check_skip_function_bodies(repository_ctx, swiftc_path, _temp_dir):
"""Returns True if `swiftc` supports skip function bodies."""
return _swift_succeeds(
repository_ctx,
swiftc_path,
"-version",
"-experimental-skip-non-inlinable-function-bodies",
)

def _check_file_prefix_map(repository_ctx, swiftc_path, _temp_dir):
"""Returns True if `swiftc` supports -file-prefix-map."""
return _swift_succeeds(
repository_ctx,
swiftc_path,
"-version",
"-file-prefix-map",
"foo=bar",
)

def _check_enable_bare_slash_regex(repository_ctx, swiftc_path, _temp_dir):
"""Returns True if `swiftc` supports debug prefix mapping."""
return _swift_succeeds(
repository_ctx,
swiftc_path,
"-version",
"-enable-bare-slash-regex",
)

def _check_supports_lld_gc_workaround(repository_ctx, swiftc_path, temp_dir):
"""Returns True if lld is being used and it supports nostart-stop-gc"""
source_file = _scratch_file(
Expand Down Expand Up @@ -201,10 +160,6 @@ def _compute_feature_values(repository_ctx, swiftc_path):
# the `swiftc` executable and a scratch directory, respectively. The function
# should return True if the feature is supported.
_FEATURE_CHECKS = {
SWIFT_FEATURE_ENABLE_BARE_SLASH_REGEX: _check_enable_bare_slash_regex,
SWIFT_FEATURE_ENABLE_BATCH_MODE: _check_enable_batch_mode,
SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES: _check_skip_function_bodies,
SWIFT_FEATURE_FILE_PREFIX_MAP: _check_file_prefix_map,
SWIFT_FEATURE_LLD_GC_WORKAROUND: _check_supports_lld_gc_workaround,
}

Expand Down Expand Up @@ -362,8 +317,6 @@ def _create_windows_toolchain(repository_ctx):
SWIFT_FEATURE_DEBUG_PREFIX_MAP,
SWIFT_FEATURE_EMIT_SWIFTDOC,
SWIFT_FEATURE_EMIT_SWIFTSOURCEINFO,
SWIFT_FEATURE_ENABLE_BATCH_MODE,
SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES,
SWIFT_FEATURE_NO_EMBED_DEBUG_MODULE,
SWIFT_FEATURE_MODULE_MAP_NO_PRIVATE_HEADERS,
]
Expand Down
34 changes: 9 additions & 25 deletions swift/toolchains/swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,16 @@ load("//swift/internal:attrs.bzl", "swift_toolchain_driver_attrs")
load("//swift/internal:autolinking.bzl", "autolink_extract_action_configs")
load(
"//swift/internal:feature_names.bzl",
"SWIFT_FEATURE_CACHEABLE_SWIFTMODULES",
"SWIFT_FEATURE_COVERAGE_PREFIX_MAP",
"SWIFT_FEATURE_DEBUG_PREFIX_MAP",
"SWIFT_FEATURE_DISABLE_CLANG_SPI",
"SWIFT_FEATURE_DISABLE_SYSTEM_INDEX",
"SWIFT_FEATURE_EMIT_SWIFTDOC",
"SWIFT_FEATURE_EMIT_SWIFTSOURCEINFO",
"SWIFT_FEATURE_INTERNALIZE_AT_LINK",
"SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD",
"SWIFT_FEATURE_NO_GENERATED_MODULE_MAP",
"SWIFT_FEATURE_OPT_USES_WMO",
"SWIFT_FEATURE_USE_AUTOLINK_EXTRACT",
"SWIFT_FEATURE_USE_GLOBAL_INDEX_STORE",
"SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE",
"SWIFT_FEATURE_USE_MODULE_WRAP",
)
load("//swift/internal:features.bzl", "features_for_build_modes")
load(
"//swift/internal:features.bzl",
"default_features_for_toolchain",
"features_for_build_modes",
)
load(
"//swift/internal:providers.bzl",
"SwiftCrossImportOverlayInfo",
Expand Down Expand Up @@ -462,19 +455,10 @@ def _swift_toolchain_impl(ctx):
features_for_build_modes(ctx) +
features_from_swiftcopts(swiftcopts = swiftcopts)
)
requested_features.extend([
SWIFT_FEATURE_CACHEABLE_SWIFTMODULES,
SWIFT_FEATURE_COVERAGE_PREFIX_MAP,
SWIFT_FEATURE_DEBUG_PREFIX_MAP,
SWIFT_FEATURE_DISABLE_CLANG_SPI,
SWIFT_FEATURE_DISABLE_SYSTEM_INDEX,
SWIFT_FEATURE_EMIT_SWIFTDOC,
SWIFT_FEATURE_EMIT_SWIFTSOURCEINFO,
SWIFT_FEATURE_INTERNALIZE_AT_LINK,
SWIFT_FEATURE_NO_GENERATED_MODULE_MAP,
SWIFT_FEATURE_OPT_USES_WMO,
SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE,
])
requested_features.extend(default_features_for_toolchain(
ctx = ctx,
target_triple = target_triple,
))

requested_features.extend(ctx.features)

Expand Down
47 changes: 9 additions & 38 deletions swift/toolchains/xcode_swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,19 @@ load(
load("//swift/internal:attrs.bzl", "swift_toolchain_driver_attrs")
load(
"//swift/internal:feature_names.bzl",
"SWIFT_FEATURE_CACHEABLE_SWIFTMODULES",
"SWIFT_FEATURE_CHECKED_EXCLUSIVITY",
"SWIFT_FEATURE_COVERAGE",
"SWIFT_FEATURE_COVERAGE_PREFIX_MAP",
"SWIFT_FEATURE_DEBUG_PREFIX_MAP",
"SWIFT_FEATURE_DISABLE_CLANG_SPI",
"SWIFT_FEATURE_DISABLE_SWIFT_SANDBOX",
"SWIFT_FEATURE_DISABLE_SYSTEM_INDEX",
"SWIFT_FEATURE_EMIT_SWIFTDOC",
"SWIFT_FEATURE_EMIT_SWIFTSOURCEINFO",
"SWIFT_FEATURE_ENABLE_BARE_SLASH_REGEX",
"SWIFT_FEATURE_ENABLE_BATCH_MODE",
"SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES",
"SWIFT_FEATURE_FILE_PREFIX_MAP",
"SWIFT_FEATURE_INTERNALIZE_AT_LINK",
"SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD",
"SWIFT_FEATURE_OBJC_LINK_FLAGS",
"SWIFT_FEATURE_OPT_USES_WMO",
"SWIFT_FEATURE_REMAP_XCODE_PATH",
"SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE",
"SWIFT_FEATURE__FORCE_ALWAYSLINK_TRUE",
)
load("//swift/internal:features.bzl", "features_for_build_modes")
load(
"//swift/internal:features.bzl",
"default_features_for_toolchain",
"features_for_build_modes",
)
load(
"//swift/internal:providers.bzl",
"SwiftCrossImportOverlayInfo",
Expand Down Expand Up @@ -626,29 +616,10 @@ def _xcode_swift_toolchain_impl(ctx):
cpp_fragment = cpp_fragment,
) + wmo_features_from_swiftcopts(swiftcopts = swiftcopts)
requested_features.extend(ctx.features)
requested_features.extend([
SWIFT_FEATURE_CACHEABLE_SWIFTMODULES,
SWIFT_FEATURE_CHECKED_EXCLUSIVITY,
SWIFT_FEATURE_COVERAGE_PREFIX_MAP,
SWIFT_FEATURE_DEBUG_PREFIX_MAP,
SWIFT_FEATURE_DISABLE_CLANG_SPI,
SWIFT_FEATURE_DISABLE_SYSTEM_INDEX,
SWIFT_FEATURE_EMIT_SWIFTDOC,
SWIFT_FEATURE_EMIT_SWIFTSOURCEINFO,
SWIFT_FEATURE_ENABLE_BARE_SLASH_REGEX,
SWIFT_FEATURE_ENABLE_BATCH_MODE,
SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES,
SWIFT_FEATURE_FILE_PREFIX_MAP,
SWIFT_FEATURE_INTERNALIZE_AT_LINK,
SWIFT_FEATURE_OBJC_LINK_FLAGS,
SWIFT_FEATURE_OPT_USES_WMO,
SWIFT_FEATURE_REMAP_XCODE_PATH,
SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE,
])

# The new driver had response file bugs in Xcode 13.x that are fixed in
if getattr(ctx.fragments.objc, "alwayslink_by_default", False):
requested_features.append(SWIFT_FEATURE__FORCE_ALWAYSLINK_TRUE)
requested_features.extend(default_features_for_toolchain(
ctx = ctx,
target_triple = target_triple,
))

if _is_xcode_at_least_version(xcode_config, "15.3"):
requested_features.append(SWIFT_FEATURE_DISABLE_SWIFT_SANDBOX)
Expand Down