From a219d24c1ca306130f90a95d8e112ea8ab33483f Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 21 Apr 2023 10:34:23 -0700 Subject: [PATCH] Consolidate default features logic PiperOrigin-RevId: 526077161 (cherry picked from commit b7abb388db2de17a709fcf32ae791b9fca5ee276) Cherry-pick notes: I took the liberty of unconditionally enabling some linux features that have been available in the Swift toolchain for some time now. Signed-off-by: Brentley Jones --- swift/internal/BUILD | 2 + swift/internal/features.bzl | 74 ++++++++++++++++++++++ swift/internal/swift_autoconfiguration.bzl | 47 -------------- swift/toolchains/swift_toolchain.bzl | 34 +++------- swift/toolchains/xcode_swift_toolchain.bzl | 47 +++----------- 5 files changed, 94 insertions(+), 110 deletions(-) diff --git a/swift/internal/BUILD b/swift/internal/BUILD index 354355332..61ca86b9a 100644 --- a/swift/internal/BUILD +++ b/swift/internal/BUILD @@ -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", ], ) diff --git a/swift/internal/features.bzl b/swift/internal/features.bzl index 758dc4f4d..3ab4145fc 100644 --- a/swift/internal/features.bzl +++ b/swift/internal/features.bzl @@ -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. @@ -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, diff --git a/swift/internal/swift_autoconfiguration.bzl b/swift/internal/swift_autoconfiguration.bzl index b4670e343..4a884311a 100644 --- a/swift/internal/swift_autoconfiguration.bzl +++ b/swift/internal/swift_autoconfiguration.bzl @@ -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", @@ -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( @@ -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, } @@ -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, ] diff --git a/swift/toolchains/swift_toolchain.bzl b/swift/toolchains/swift_toolchain.bzl index f43cbd306..152083123 100644 --- a/swift/toolchains/swift_toolchain.bzl +++ b/swift/toolchains/swift_toolchain.bzl @@ -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", @@ -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) diff --git a/swift/toolchains/xcode_swift_toolchain.bzl b/swift/toolchains/xcode_swift_toolchain.bzl index 491d79a0b..78e2dc4d6 100644 --- a/swift/toolchains/xcode_swift_toolchain.bzl +++ b/swift/toolchains/xcode_swift_toolchain.bzl @@ -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", @@ -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)