From 24b64d323206bdd2f18d6b7665d21dbbfeb57a9d Mon Sep 17 00:00:00 2001 From: Jeroen Ketema <93738568+jketema@users.noreply.github.com> Date: Tue, 11 Aug 2026 15:40:13 +0000 Subject: [PATCH] Unified: Make Linux build hermetic --- BUILD.bazel | 8 + MODULE.bazel | 11 +- .../4.0.0-rc5-codeql.1/MODULE.bazel | 281 ++++++++++++++++++ .../patches/external_static_runtime.patch | 241 +++++++++++++++ .../4.0.0-rc5-codeql.1/source.json | 13 + .../modules/rules_swift/metadata.json | 42 +++ unified/BUILD.bazel | 8 +- unified/extractor/BUILD.bazel | 10 +- unified/swift-syntax-rs/.swift-version | 2 +- unified/swift-syntax-rs/BUILD.bazel | 76 +++-- unified/swift-syntax-rs/README.md | 2 +- unified/swift-syntax-rs/swift_runtime.bzl | 19 -- 12 files changed, 649 insertions(+), 64 deletions(-) create mode 100644 misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.1/MODULE.bazel create mode 100644 misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.1/patches/external_static_runtime.patch create mode 100644 misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.1/source.json create mode 100644 misc/bazel/registry/modules/rules_swift/metadata.json delete mode 100644 unified/swift-syntax-rs/swift_runtime.bzl diff --git a/BUILD.bazel b/BUILD.bazel index b2e4ea806785..07027f4ad9a7 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -3,3 +3,11 @@ exports_files([ "Cargo.lock", "Cargo.toml", ]) + +constraint_setting(name = "swift_runtime_linkage") + +constraint_value( + name = "static_swift_runtime", + constraint_setting = ":swift_runtime_linkage", + visibility = ["//visibility:public"], +) diff --git a/MODULE.bazel b/MODULE.bazel index e8d49c11bcb4..291cc7e8d94d 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -15,7 +15,7 @@ local_path_override( # see https://registry.bazel.build/ for a list of available packages bazel_dep(name = "platforms", version = "1.0.0") -bazel_dep(name = "rules_cc", version = "0.2.17") +bazel_dep(name = "rules_cc", version = "0.2.20") bazel_dep(name = "rules_go", version = "0.60.0") bazel_dep(name = "rules_java", version = "9.6.1") bazel_dep(name = "rules_pkg", version = "1.2.0") @@ -31,13 +31,13 @@ bazel_dep(name = "gazelle", version = "0.50.0") bazel_dep(name = "rules_dotnet", version = "0.21.5-codeql.1") bazel_dep(name = "googletest", version = "1.17.0.bcr.2") bazel_dep(name = "rules_rust", version = "0.69.0") -bazel_dep(name = "rules_swift", version = "4.0.0-rc4") +bazel_dep(name = "rules_swift", version = "4.0.0-rc5-codeql.1") bazel_dep(name = "swift-syntax", version = "603.0.2") # Needed so we can `use_repo` `local_config_xcode` and # `local_config_apple_cc_toolchains` below (referenced by the per-target # Xcode-config transition in `unified/swift-syntax-rs/xcode_transition.bzl`). -bazel_dep(name = "apple_support", version = "2.6.1") +bazel_dep(name = "apple_support", version = "2.8.0") bazel_dep(name = "zstd", version = "1.5.7.bcr.1") bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True) @@ -235,10 +235,10 @@ use_repo( # `unified/swift-syntax-rs` package is not loadable in that context. Keep this # in sync with `unified/swift-syntax-rs/.swift-version` (used by the `cargo` # build) and the `swift-syntax` release in `swift/Package.swift`. -swift = use_extension("@rules_swift//swift:extensions.bzl", "swift") +swift = use_extension("@rules_swift//swift:extensions.bzl", "swift", dev_dependency = True) swift.toolchain( name = "swift_toolchain", - swift_version = "6.3.2", + swift_version = "6.3.3", ) use_repo( swift, @@ -248,6 +248,7 @@ use_repo( register_toolchains( "@swift_toolchain//:swift_toolchain_exec_ubuntu22.04", + dev_dependency = True, ) # `apple_support`'s xcode_config and CC toolchains, needed by the Xcode diff --git a/misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.1/MODULE.bazel b/misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.1/MODULE.bazel new file mode 100644 index 000000000000..eda852ba393e --- /dev/null +++ b/misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.1/MODULE.bazel @@ -0,0 +1,281 @@ +"""rules_swift MODULE.bazel file""" + +module( + name = "rules_swift", + version = "4.0.0-rc5-codeql.1", + bazel_compatibility = [">=8.0.0"], + compatibility_level = 3, +) + +bazel_dep(name = "abseil-cpp", version = "20250814.1") +bazel_dep(name = "apple_support", version = "2.8.0") +bazel_dep(name = "bazel_linux_packages", version = "0.4.1") +bazel_dep(name = "bazel_features", version = "1.51.0") +bazel_dep(name = "bazel_skylib", version = "1.7.1") +bazel_dep(name = "nlohmann_json", version = "3.12.0.bcr.1") +bazel_dep(name = "platforms", version = "0.0.11") +bazel_dep(name = "protobuf", version = "34.0.bcr.1") +bazel_dep(name = "rules_cc", version = "0.2.14") +bazel_dep(name = "rules_shell", version = "0.3.0") +bazel_dep(name = "swift_argument_parser", version = "1.7.0") + +# Regenerate lockfiles with: +# bazel run @swift_ubuntu22.04_sysroot//:lock +# bazel run @swift_ubuntu22.04_aarch64_sysroot//:lock +apt = use_extension("@bazel_linux_packages//apt:extensions.bzl", "apt") +apt.ubuntu( + name = "swift_ubuntu22.04_sysroot", + architectures = ["amd64"], + lockfile = "//swift/internal/extensions:ubuntu22.04_sysroot.lock.json", + packages = [ + "libc6-dev", + "libcurl4-openssl-dev", + "libstdc++-11-dev", + "libxml2-dev", + "linux-libc-dev", + "zlib1g-dev", + ], + suites = ["jammy"], +) +apt.ubuntu( + name = "swift_ubuntu22.04_aarch64_sysroot", + architectures = ["arm64"], + lockfile = "//swift/internal/extensions:ubuntu22.04_aarch64_sysroot.lock.json", + packages = [ + "libc6-dev", + "libcurl4-openssl-dev", + "libstdc++-11-dev", + "libxml2-dev", + "linux-libc-dev", + "zlib1g-dev", + ], + suites = ["jammy"], +) +use_repo( + apt, + "swift_ubuntu22.04_aarch64_sysroot", + "swift_ubuntu22.04_sysroot", +) + +swift_autoconfiguration = use_repo_rule("//swift/internal:swift_autoconfiguration.bzl", "swift_autoconfiguration") + +swift_autoconfiguration(name = "rules_swift_local_config") + +http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +http_archive( + name = "com_github_apple_swift_protobuf", + build_file = "//third_party:com_github_apple_swift_protobuf/BUILD.overlay", + sha256 = "3fb50bd4d293337f202d917b6ada22f9548a0a0aed9d9a4d791e6fbd8a246ebb", + strip_prefix = "swift-protobuf-1.20.2/", + urls = ["https://github.com/apple/swift-protobuf/archive/1.20.2.tar.gz"], # pinned to grpc-swift version +) + +http_archive( + name = "com_github_grpc_grpc_swift", + build_file = "//third_party:com_github_grpc_grpc_swift/BUILD.overlay", + sha256 = "58b60431d0064969f9679411264b82e40a217ae6bd34e17096d92cc4e47556a5", + strip_prefix = "grpc-swift-1.16.0/", + urls = ["https://github.com/grpc/grpc-swift/archive/1.16.0.tar.gz"], # latest at time of writing +) + +http_archive( + name = "com_github_apple_swift_docc_symbolkit", + build_file = "//third_party:com_github_apple_swift_docc_symbolkit/BUILD.overlay", + sha256 = "de1d4b6940468ddb53b89df7aa1a81323b9712775b0e33e8254fa0f6f7469a97", + strip_prefix = "swift-docc-symbolkit-swift-5.10-RELEASE", + urls = ["https://github.com/apple/swift-docc-symbolkit/archive/refs/tags/swift-5.10-RELEASE.tar.gz"], +) + +http_archive( + name = "com_github_apple_swift_nio", + build_file = "//third_party:com_github_apple_swift_nio/BUILD.overlay", + sha256 = "9ec79852fd03d2e933ece3299ea6c8b8de6960625f7246fd65958409d1420215", + strip_prefix = "swift-nio-2.51.0/", + urls = ["https://github.com/apple/swift-nio/archive/2.51.0.tar.gz"], # pinned to grpc swift version + version needed to fix linux build +) + +http_archive( + name = "com_github_apple_swift_nio_http2", + build_file = "//third_party:com_github_apple_swift_nio_http2/BUILD.overlay", + sha256 = "f0edfc9d6a7be1d587e5b403f2d04264bdfae59aac1d74f7d974a9022c6d2b25", + strip_prefix = "swift-nio-http2-1.26.0/", + urls = ["https://github.com/apple/swift-nio-http2/archive/1.26.0.tar.gz"], # pinned to grpc-swift version +) + +http_archive( + name = "com_github_apple_swift_nio_transport_services", + build_file = "//third_party:com_github_apple_swift_nio_transport_services/BUILD.overlay", + sha256 = "f3498dafa633751a52b9b7f741f7ac30c42bcbeb3b9edca6d447e0da8e693262", + strip_prefix = "swift-nio-transport-services-1.15.0/", + urls = ["https://github.com/apple/swift-nio-transport-services/archive/1.15.0.tar.gz"], # pinned to grpc-swift version +) + +http_archive( + name = "com_github_apple_swift_nio_extras", + build_file = "//third_party:com_github_apple_swift_nio_extras/BUILD.overlay", + sha256 = "4684b52951d9d9937bb3e8ccd6b5daedd777021ef2519ea2f18c4c922843b52b", + strip_prefix = "swift-nio-extras-1.4.0/", + urls = ["https://github.com/apple/swift-nio-extras/archive/1.4.0.tar.gz"], # pinned to grpc-swift version +) + +http_archive( + name = "com_github_apple_swift_log", + build_file = "//third_party:com_github_apple_swift_log/BUILD.overlay", + sha256 = "5eaed6614cfaad882b8a0b5cb5d2177b533056b469ba431ad3f375193d370b70", + strip_prefix = "swift-log-1.6.3/", + urls = ["https://github.com/apple/swift-log/archive/1.6.3.tar.gz"], # pinned to version with linux build fix: https://github.com/apple/swift-log/pull/354 +) + +http_archive( + name = "com_github_apple_swift_nio_ssl", + build_file = "//third_party:com_github_apple_swift_nio_ssl/BUILD.overlay", + sha256 = "792882c884b2b89de0e9189557ea928bc019be2d9a89d63831876a746cbe9ce3", + strip_prefix = "swift-nio-ssl-2.26.0/", + urls = ["https://github.com/apple/swift-nio-ssl/archive/2.26.0.tar.gz"], # pinned to version with linux build fix: https://github.com/apple/swift-nio-ssl/pull/448 +) + +http_archive( + name = "com_github_apple_swift_collections", + build_file = "//third_party:com_github_apple_swift_collections/BUILD.overlay", + sha256 = "d9e4c8a91c60fb9c92a04caccbb10ded42f4cb47b26a212bc6b39cc390a4b096", + strip_prefix = "swift-collections-1.0.4/", + urls = ["https://github.com/apple/swift-collections/archive/1.0.4.tar.gz"], # pinned to swift-nio @ grpc-swift version +) + +http_archive( + name = "com_github_apple_swift_atomics", + build_file = "//third_party:com_github_apple_swift_atomics/BUILD.overlay", + sha256 = "1bee7f469f7e8dc49f11cfa4da07182fbc79eab000ec2c17bfdce468c5d276fb", + strip_prefix = "swift-atomics-1.1.0/", + urls = ["https://github.com/apple/swift-atomics/archive/1.1.0.tar.gz"], # pinned to swift-nio @ grpc-swift version +) + +http_archive( + name = "com_github_apple_swift_mmio", + build_file = "//third_party:com_github_apple_swift_mmio/BUILD.overlay", + patch_args = ["-p1"], + patches = [ + "//third_party:com_github_apple_swift_mmio/0001-style-parser-remove-trailing-comma-in-OneOfParser-in.patch", + ], + sha256 = "51334b771beadc30767c90cd24e2acab9604d62ad0bc64a3c0665e3d6f19d82c", + strip_prefix = "swift-mmio-0.1.1", + urls = ["https://github.com/apple/swift-mmio/archive/refs/tags/0.1.1.tar.gz"], # requires for the embedded toolchain example +) + +# When using the "global index store" feature we rely on `index-import` to allow +# using a global index. +# TODO: we must depend on two versions of index-import to support backwards +# compatibility between Xcode 16.3+ and older versions, we can remove the older +# version once we drop support for Xcode 16.x. +http_archive( + name = "rules_swift_index_import_5_8", + build_file = "//third_party:rules_swift_index_import/BUILD.overlay", + canonical_id = "index-import-5.8", + sha256 = "28c1ffa39d99e74ed70623899b207b41f79214c498c603915aef55972a851a15", + urls = ["https://github.com/MobileNativeFoundation/index-import/releases/download/5.8.0.1/index-import.tar.gz"], +) + +http_archive( + name = "rules_swift_index_import_6_1", + build_file = "//third_party:rules_swift_index_import/BUILD.overlay", + canonical_id = "index-import-6.1", + sha256 = "9a54fc1674af6031125a9884480a1e31e1bcf48b8f558b3e8bcc6b6fcd6e8b61", + urls = ["https://github.com/MobileNativeFoundation/index-import/releases/download/6.1.0.1/index-import.tar.gz"], +) + +system_sdk = use_extension("//swift:extensions.bzl", "system_sdk") +system_sdk.configure_sdks( + # NOTE: This doesn't apply to downstream repos + exclude_modules = { + "AppleTVOS": [ + "CoreAudio_Private", # TODO: Remove once CI >= Xcode 26.4 + ], + "AppleTVSimulator": [ + "CoreAudio_Private", # TODO: Remove once CI >= Xcode 26.4 + ], + "iPhoneSimulator": [ + "AssetsLibrary", # TODO: Remove once CI >= Xcode 26.4 + "CoreAudio_Private", + ], + "iPhoneOS": [ + "AssetsLibrary", # TODO: Remove once CI >= Xcode 26.4 + "CoreAudio_Private", # TODO: Remove once CI >= Xcode 26.4 + ], + "XROS": [ + "AccessoryTransportExtension", + "CoreAudio_Private", # TODO: Remove once CI >= Xcode 26.4 + ], + "XRSimulator": [ + "AccessoryTransportExtension", + "CoreAudio_Private", # TODO: Remove once CI >= Xcode 26.4 + ], + "WatchOS": [ + "BrowserEngineKit", + "CoreAudio_Private", # TODO: Remove once CI >= Xcode 26.4 + ], + "WatchSimulator": [ + "BrowserEngineKit", + "CoreAudio_Private", # TODO: Remove once CI >= Xcode 26.4 + ], + }, + include_all = True, +) +use_repo(system_sdk, "system_sdk") + +swift = use_extension("//swift:extensions.bzl", "swift", dev_dependency = True) +swift.toolchain( + name = "swift_toolchain", + swift_version = "6.3.2", +) +use_repo( + swift, + "swift_toolchain", + "swift_toolchain_ubuntu22.04", + "swift_toolchain_ubuntu22.04-aarch64", + "swift_toolchain_xcode", +) + +register_toolchains( + # We're using ubuntu22.04 here because that's what's used in the current rules_swift buildkite CI. + # In a perfect world, we would be able to have bazel automatically use the toolchain that just works. + # Unfortunately, we are not in this world yet. This would require either one of these two things to happen: + # * either have a constraint to select on the current distribution, which would allow us to register all + # the toolchains and have bazel automatically select the right one. But there is no such constraint today. + # * or have a swift universal Linux toolchain. Which is not the case today. + # Anyway, for now we will need to update this every time we update the CI environment. Contributors might also + # have to update this list if they're testing on a different Linux distribution. + "@swift_toolchain//:cc_toolchain_embedded_ubuntu22.04", + "@swift_toolchain//:cc_toolchain_exec_ubuntu22.04", + "@swift_toolchain//:cc_toolchain_embedded_ubuntu22.04-aarch64", + "@swift_toolchain//:cc_toolchain_embedded_xcode", + "@swift_toolchain//:swift_toolchain_embedded_ubuntu22.04", + "@swift_toolchain//:swift_toolchain_exec_ubuntu22.04", + "@swift_toolchain//:swift_toolchain_embedded_ubuntu22.04-aarch64", + "@swift_toolchain//:swift_toolchain_embedded_xcode", + dev_dependency = True, +) + +register_toolchains("//swift/toolchains:all") + +# Dev dependencies +bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.5.0", dev_dependency = True) +bazel_dep(name = "gazelle", version = "0.46.0", dev_dependency = True) +bazel_dep(name = "rules_go", version = "0.59.0", dev_dependency = True) # TODO: Remove when transitives update past this version +bazel_dep(name = "rules_python", version = "2.2.0", dev_dependency = True) +bazel_dep(name = "stardoc", version = "0.8.0", dev_dependency = True) +bazel_dep(name = "swift-syntax", version = "602.0.0.bcr.2", dev_dependency = True) +bazel_dep(name = "rules_swift_layering_check_external_test", version = "0.0.0", dev_dependency = True) +local_path_override( + module_name = "rules_swift_layering_check_external_test", + path = "third_party/rules_swift_layering_check_external_test", +) + +# TODO: In stardoc 0.7.1+, the `load` statements added to the docs are relative to the `alias` targets which is incorrect. +# To keep the docs without confusing load statements we patch a partial revert of: https://github.com/bazelbuild/stardoc/pull/216 +# https://github.com/bazelbuild/stardoc/issues/297 +single_version_override( + module_name = "stardoc", + patch_strip = 1, + patches = ["//third_party/patches:stardoc-revert-load-statements-in-docs.patch"], +) diff --git a/misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.1/patches/external_static_runtime.patch b/misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.1/patches/external_static_runtime.patch new file mode 100644 index 000000000000..a0d2cf5c6b06 --- /dev/null +++ b/misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.1/patches/external_static_runtime.patch @@ -0,0 +1,241 @@ +diff -ruN a/swift/extensions.bzl b/swift/extensions.bzl +--- a/swift/extensions.bzl 2026-08-05 15:42:57.000000000 +0000 ++++ b/swift/extensions.bzl 2026-08-11 12:40:19.656925161 +0000 +@@ -186,6 +186,7 @@ + name = repository_name, + sha256 = sha256, + platform = platform, ++ static_runtime = toolchain.static_runtime, + swift_version = swift_version, + ) + toolchains_build_file_content += toolchains_for_platform( +@@ -261,6 +262,9 @@ + `bazel run @rules_swift//tools/swift-releases -- list 6.2.4` + """, + ), ++ "static_runtime": attr.label( ++ doc = "External static runtime package to attach to the generated execution toolchains.", ++ ), + "swift_version": attr.string(doc = "Version of the swift toolchain to be installed. Cannot be used concurrently with `swift_version_file`"), + "swift_version_file": attr.label(doc = "A label to the .swift_version file to use. Cannot be used concurrently with `swift_version`"), + }) +diff -ruN a/swift/internal/extensions/standalone_toolchain.bzl b/swift/internal/extensions/standalone_toolchain.bzl +--- a/swift/internal/extensions/standalone_toolchain.bzl 2026-08-05 15:42:57.000000000 +0000 ++++ b/swift/internal/extensions/standalone_toolchain.bzl 2026-08-11 12:50:07.623900053 +0000 +@@ -100,6 +100,12 @@ + "BUILD.bazel", + repository_ctx.attr._build_template, + substitutions = { ++ "{external_static_runtime}": ( ++ ' external_static_runtime = "{}",'.format(repository_ctx.attr.static_runtime) if repository_ctx.attr.static_runtime else "" ++ ), ++ "{external_static_runtime_features}": ( ++ ' "swift.lld_gc_workaround",' if repository_ctx.attr.static_runtime else "" ++ ), + "{swift_version}": repository_ctx.attr.swift_version, + }, + ) +@@ -117,6 +123,9 @@ + "sha256": attr.string( + doc = "The expected SHA-256 of the file downloaded. This must match the SHA-256 of the file downloaded.", + ), ++ "static_runtime": attr.label( ++ doc = "External static runtime package used by this execution toolchain.", ++ ), + "swift_version": attr.string( + doc = "Version of the swift toolchain to be installed.", + mandatory = True, +diff -ruN a/swift/internal/extensions/toolchain.BUILD b/swift/internal/extensions/toolchain.BUILD +--- a/swift/internal/extensions/toolchain.BUILD 2026-08-05 15:42:57.000000000 +0000 ++++ b/swift/internal/extensions/toolchain.BUILD 2026-08-11 12:50:07.626900053 +0000 +@@ -334,7 +334,7 @@ + "swift._supports_upcoming_features", + "swift.no_embed_debug_module", + "swift.use_autolink_extract", +- "swift.lld_gc_workaround", ++{external_static_runtime_features} + "swift.use_module_wrap", + # TODO: This should be removed so that private headers can be used with + # explicit modules, but the build targets for CgRPC need to be cleaned up +@@ -359,6 +359,7 @@ + ), + "//conditions:default": [], + }), ++{external_static_runtime} + swift_tools = "tools", + version_file = ".swift-version", + ) +diff -ruN a/swift/toolchains/BUILD b/swift/toolchains/BUILD +--- a/swift/toolchains/BUILD 2026-08-05 15:42:57.000000000 +0000 ++++ b/swift/toolchains/BUILD 2026-08-11 12:40:19.664925161 +0000 +@@ -155,6 +155,7 @@ + "//swift/internal:features", + "//swift/internal:providers", + "//swift/internal:target_triples", ++ "//swift/internal:toolchain_utils", + "//swift/internal:utils", + "//swift/internal:wmo", + "//swift/toolchains/config:action_config", +diff -ruN a/swift/toolchains/swift_toolchain.bzl b/swift/toolchains/swift_toolchain.bzl +--- a/swift/toolchains/swift_toolchain.bzl 2026-08-05 15:42:57.000000000 +0000 ++++ b/swift/toolchains/swift_toolchain.bzl 2026-08-11 12:52:53.554892968 +0000 +@@ -70,6 +70,11 @@ + ) + load("//swift/internal:target_triples.bzl", "target_triples") + load( ++ "//swift/internal:toolchain_utils.bzl", ++ "get_swift_toolchain", ++ "use_swift_toolchain", ++) ++load( + "//swift/internal:utils.bzl", + "collect_cross_import_overlays", + "collect_implicit_deps_providers", +@@ -101,6 +106,64 @@ + ) + load("//swift/toolchains/config:tool_config.bzl", "ToolConfigInfo") + ++SwiftStaticRuntimeInfo = provider( ++ fields = { ++ "copts": "Compiler options required to use the runtime.", ++ "files": "Runtime and SDK files required by compile and link actions.", ++ "linkopts": "Linker options required to link the runtime.", ++ "root": "Execution-root-relative path to the runtime SDK.", ++ }, ++) ++ ++def _swift_static_runtime_impl(ctx): ++ root = ctx.label.workspace_root ++ files = depset(ctx.files.files) ++ ++ def expand_root(options): ++ return [option.replace("{root}", root) for option in options] ++ ++ return [ ++ DefaultInfo(files = files), ++ SwiftStaticRuntimeInfo( ++ copts = expand_root(ctx.attr.copts), ++ files = files, ++ linkopts = expand_root(ctx.attr.linkopts), ++ root = root, ++ ), ++ ] ++ ++swift_static_runtime = rule( ++ implementation = _swift_static_runtime_impl, ++ attrs = { ++ "copts": attr.string_list(), ++ "files": attr.label_list(allow_files = True, mandatory = True), ++ "linkopts": attr.string_list(), ++ }, ++) ++ ++def _swift_runtime_impl(ctx): ++ runtime_cc_info = get_swift_toolchain(ctx).dynamic_runtime_cc_info ++ files = [] ++ if runtime_cc_info: ++ for linker_input in runtime_cc_info.linking_context.linker_inputs.to_list(): ++ for library in linker_input.libraries: ++ dynamic_library = getattr(library, "dynamic_library", None) ++ if dynamic_library: ++ files.append(dynamic_library) ++ runtime = depset(files) ++ providers = [DefaultInfo( ++ files = runtime, ++ runfiles = ctx.runfiles(transitive_files = runtime), ++ )] ++ if runtime_cc_info: ++ providers.append(runtime_cc_info) ++ return providers ++ ++swift_runtime = rule( ++ implementation = _swift_runtime_impl, ++ toolchains = use_swift_toolchain(), ++) ++ + def _swift_compile_resource_set(_os, inputs_size): + # The `os` argument is unused, but the Starlark API requires both + # positional arguments. +@@ -658,6 +721,7 @@ + + def _swift_toolchain_impl(ctx): + toolchain_root = ctx.attr.root ++ external_static_runtime = ctx.attr.external_static_runtime[SwiftStaticRuntimeInfo] if ctx.attr.external_static_runtime else None + cc_toolchain = find_cc_toolchain(ctx) + target_system_name = _parse_target_system_name( + arch = ctx.attr.arch, +@@ -680,7 +744,7 @@ + "before invoking Bazel, or configure a Bazel LLVM CC toolchain. " + + "The current CC toolchain is configured to use '{}'.".format(cc_toolchain.compiler)) + +- sdkroot = _resolve_sdkroot(ctx, cc_toolchain) ++ sdkroot = external_static_runtime.root if external_static_runtime else _resolve_sdkroot(ctx, cc_toolchain) + + if ctx.attr.swift_tools: + if ctx.attr.swift_executable: +@@ -725,6 +789,18 @@ + ctx.attr.linkopts, + ctx.files.linker_inputs, + ) ++ elif external_static_runtime: ++ swift_linkopts_cc_info = CcInfo( ++ linking_context = cc_common.create_linking_context( ++ linker_inputs = depset([ ++ cc_common.create_linker_input( ++ owner = ctx.label, ++ user_link_flags = depset(external_static_runtime.linkopts), ++ additional_inputs = external_static_runtime.files, ++ ), ++ ]), ++ ), ++ ) + else: + ( + swift_linkopts_cc_info, +@@ -748,6 +824,8 @@ + swiftcopts.extend(ctx.attr._exec_copts[BuildSettingInfo].value) + else: + swiftcopts.extend(ctx.attr._copts[BuildSettingInfo].value) ++ if external_static_runtime: ++ swiftcopts.extend(external_static_runtime.copts) + + # Combine build mode features, autoconfigured features, and required + # features. +@@ -759,7 +837,7 @@ + target_triple = target_triple, + )) + +- if apple_common.dotted_version(ctx.attr.parsed_version) >= apple_common.dotted_version("6.3"): ++ if not external_static_runtime and apple_common.dotted_version(ctx.attr.parsed_version) >= apple_common.dotted_version("6.3"): + requested_features.append(SWIFT_FEATURE__SUPPORTS_HERMETIC_SWIFTMODULE) + + requested_features.extend(ctx.features) +@@ -772,6 +850,8 @@ + additional_tools = [ctx.file.version_file] + if ctx.attr.swift_tools: + additional_tools += ctx.attr.swift_tools[SwiftToolsInfo].additional_inputs ++ if external_static_runtime: ++ additional_tools += external_static_runtime.files.to_list() + + all_tool_configs = _all_tool_configs( + env = ctx.attr.env, +@@ -858,7 +938,7 @@ + system_modules = collect_implicit_deps_providers([]), + implicit_system_modules = collect_implicit_deps_providers([]), + swift_worker = ctx.attr._worker[DefaultInfo].files_to_run, +- const_protocols_to_gather = ctx.file.const_protocols_to_gather, ++ const_protocols_to_gather = None if external_static_runtime else ctx.file.const_protocols_to_gather, + test_configuration = struct( + binary_name = "{name}", + env = env, +@@ -937,6 +1017,10 @@ + """, + allow_files = True, + ), ++ "external_static_runtime": attr.label( ++ doc = "External static Swift runtime and SDK to use instead of the compiler's runtimes.", ++ providers = [[SwiftStaticRuntimeInfo]], ++ ), + "static_runtime": attr.label_list( + doc = """\ + Static Swift runtime archives and supporting linker files that are passed to diff --git a/misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.1/source.json b/misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.1/source.json new file mode 100644 index 000000000000..2b9e8a1fb6cf --- /dev/null +++ b/misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.1/source.json @@ -0,0 +1,13 @@ +{ + "integrity": "sha256-Ly2lS4AlZJMiyqAxKHyCB+2pGeP5aYkQ6zv8QheeSTI=", + "strip_prefix": "", + "docs_url": "https://github.com/bazelbuild/rules_swift/releases/download/4.0.0-rc5/rules_swift.4.0.0-rc5.docs.tar.gz", + "url": "https://github.com/bazelbuild/rules_swift/releases/download/4.0.0-rc5/rules_swift.4.0.0-rc5.tar.gz", + "patches": { + "external_static_runtime.patch": "sha256-bSt5nkHxkYNy4veoF33Oug14lLINNop0dt+xqap/dL8=" + }, + "patch_strip": 1, + "overlay": { + "MODULE.bazel": "sha256-6MmGjlqWNw3xg+jI6frTBBFCEpIVj6flnATZ+bNF3J0=" + } +} diff --git a/misc/bazel/registry/modules/rules_swift/metadata.json b/misc/bazel/registry/modules/rules_swift/metadata.json new file mode 100644 index 000000000000..b1bb127bf8f5 --- /dev/null +++ b/misc/bazel/registry/modules/rules_swift/metadata.json @@ -0,0 +1,42 @@ +{ + "homepage": "https://github.com/bazelbuild/rules_swift", + "maintainers": [ + { + "email": "keithbsmiley@gmail.com", + "github": "keith", + "name": "Keith Smiley", + "github_user_id": 283886 + }, + { + "email": "github@brentleyjones.com", + "github": "brentleyjones", + "name": "Brentley Jones", + "github_user_id": 158658 + }, + { + "email": "heyluispadron@gmail.com", + "github": "luispadron", + "name": "Luis Padron", + "github_user_id": 13840545 + }, + { + "email": "aaronsky@skyaaron.com", + "github": "skyaaron", + "name": "Aaron Sky", + "github_user_id": 10502938 + }, + { + "email": "cebic.ad@gmail.com", + "github": "adincebic", + "name": "Adin Cebic", + "github_user_id": 19636856 + } + ], + "repository": [ + "github:bazelbuild/rules_swift" + ], + "versions": [ + "4.0.0-rc5-codeql.1" + ], + "yanked_versions": {} +} diff --git a/unified/BUILD.bazel b/unified/BUILD.bazel index 1539c7fd2f9d..c8d8ab59e345 100644 --- a/unified/BUILD.bazel +++ b/unified/BUILD.bazel @@ -1,6 +1,5 @@ load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup") load("//misc/bazel:pkg.bzl", "codeql_pack", "codeql_pkg_files") -load("//misc/bazel:utils.bzl", "select_os") package(default_visibility = ["//visibility:public"]) @@ -41,12 +40,7 @@ codeql_pkg_files( codeql_pkg_files( name = "extractor-arch", - exes = [ - "//unified/extractor", - ] + select_os( - linux = ["//unified/swift-syntax-rs:swift_runtime_libs"], - otherwise = [], - ), + exes = ["//unified/extractor"], prefix = "tools/{CODEQL_PLATFORM}", ) diff --git a/unified/extractor/BUILD.bazel b/unified/extractor/BUILD.bazel index 7959ffe384ad..f8814c4572a1 100644 --- a/unified/extractor/BUILD.bazel +++ b/unified/extractor/BUILD.bazel @@ -13,10 +13,7 @@ codeql_rust_binary( "ast_types.yml", "swift_node_types.yml", ], - data = select({ - "@platforms//os:linux": ["//unified/swift-syntax-rs:swift_runtime_libs"], - "//conditions:default": [], - }), + data = ["//unified/swift-syntax-rs:swift_runtime"], proc_macro_deps = all_crate_deps( proc_macro = True, ), @@ -56,10 +53,7 @@ _TESTS = { "swift_node_types.yml", ] + spec["compile_data"], crate_root = "tests/%s.rs" % test_name, - data = spec["data"] + select({ - "@platforms//os:linux": ["//unified/swift-syntax-rs:swift_runtime_libs"], - "//conditions:default": [], - }), + data = spec["data"] + ["//unified/swift-syntax-rs:swift_runtime"], edition = "2024", proc_macro_deps = all_crate_deps( proc_macro = True, diff --git a/unified/swift-syntax-rs/.swift-version b/unified/swift-syntax-rs/.swift-version index 91e4a9f26224..7849b73dc743 100644 --- a/unified/swift-syntax-rs/.swift-version +++ b/unified/swift-syntax-rs/.swift-version @@ -1 +1 @@ -6.3.2 +6.3.3 diff --git a/unified/swift-syntax-rs/BUILD.bazel b/unified/swift-syntax-rs/BUILD.bazel index 0f04f3e9182c..a358c0d4b878 100644 --- a/unified/swift-syntax-rs/BUILD.bazel +++ b/unified/swift-syntax-rs/BUILD.bazel @@ -1,25 +1,50 @@ load("@rules_cc//cc:defs.bzl", "cc_library") load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test") +load("@rules_swift//swift/toolchains:swift_toolchain.bzl", "swift_runtime") load("//unified:platforms.bzl", "UNIFIED_SUPPORTED_PLATFORMS") -load(":swift_runtime.bzl", "swift_runtime_libs") load(":xcode_transition.bzl", "xcode_transition_swift_library") package(default_visibility = ["//visibility:public"]) -swift_runtime_libs( - name = "swift_runtime_libs", - toolchain = "@swift_toolchain_ubuntu22.04//:files", +config_setting( + name = "static_linux_runtime", + constraint_values = [ + "@platforms//os:linux", + "//:static_swift_runtime", + ], ) -# The `$ORIGIN` runpath makes an executable look beside itself instead, so -# we find the Swift runtime libraries we bundle with the extractor. -cc_library( - name = "swift_runtime_rpath", - linkopts = select({ - "@platforms//os:linux": ["-Wl,-rpath,$$ORIGIN"], +swift_runtime( + name = "_swift_runtime", +) + +filegroup( + name = "swift_runtime", + srcs = select({ + ":static_linux_runtime": [], + "@platforms//os:linux": [":_swift_runtime"], "//conditions:default": [], }), - target_compatible_with = UNIFIED_SUPPORTED_PLATFORMS, +) + +cc_library( + name = "static_runtime_group_start", + linkopts = ["-Wl,--start-group"], + target_compatible_with = ["@platforms//os:linux"], +) + +cc_library( + name = "static_runtime_group_end", + linkopts = [ + "-lc", + "-ldl", + "-lm", + "-lpthread", + "-lrt", + "-lutil", + "-Wl,--end-group", + ], + target_compatible_with = ["@platforms//os:linux"], ) # Swift FFI shim: wraps swift-syntax and exposes a small C ABI. @@ -45,10 +70,21 @@ rust_library( ), edition = "2024", target_compatible_with = UNIFIED_SUPPORTED_PLATFORMS, - deps = [ - ":swift_runtime_rpath", - ":swift_syntax_ffi", - ], + deps = select({ + ":static_linux_runtime": [ + # Keep these in linker order; sorting them produces an invalid group. + ":static_runtime_group_start", + ":swift_syntax_ffi", + ":static_runtime_group_end", + ], + "@platforms//os:linux": [ + ":_swift_runtime", + ":swift_syntax_ffi", + ], + "//conditions:default": [ + ":swift_syntax_ffi", + ], + }), ) # A debugging aid, for looking at the raw swift-syntax JSON for some input: @@ -57,10 +93,7 @@ rust_library( rust_binary( name = "swift-syntax-parse", srcs = ["src/main.rs"], - data = select({ - "@platforms//os:linux": [":swift_runtime_libs"], - "//conditions:default": [], - }), + data = [":swift_runtime"], edition = "2024", target_compatible_with = UNIFIED_SUPPORTED_PLATFORMS, deps = [":swift_syntax_rs"], @@ -70,10 +103,7 @@ rust_test( name = "swift_syntax_rs_test", size = "small", crate = ":swift_syntax_rs", - data = select({ - "@platforms//os:linux": [":swift_runtime_libs"], - "//conditions:default": [], - }), + data = [":swift_runtime"], edition = "2024", target_compatible_with = UNIFIED_SUPPORTED_PLATFORMS, ) diff --git a/unified/swift-syntax-rs/README.md b/unified/swift-syntax-rs/README.md index 8a93e4739d9d..f4e84e18f361 100644 --- a/unified/swift-syntax-rs/README.md +++ b/unified/swift-syntax-rs/README.md @@ -129,7 +129,7 @@ The build does not depend on any particular version manager. You need: - **Rust** — pinned to `1.88` by the repo-root [`rust-toolchain.toml`](../../rust-toolchain.toml), which `rustup` picks up automatically. - **Swift** — pinned to the version in [`.swift-version`](.swift-version) - (currently `6.3.2`), used to build `swift-syntax` `603.0.2`. Install it any way + (currently `6.3.3`), used to build `swift-syntax` `603.0.2`. Install it any way you like — [swift.org](https://www.swift.org/install/) or [swiftly](https://www.swift.org/swiftly/) (which reads `.swift-version`), or a system package. Just make sure `swift` (and `swiftc`) are on your `PATH` — diff --git a/unified/swift-syntax-rs/swift_runtime.bzl b/unified/swift-syntax-rs/swift_runtime.bzl deleted file mode 100644 index 899d97d71162..000000000000 --- a/unified/swift-syntax-rs/swift_runtime.bzl +++ /dev/null @@ -1,19 +0,0 @@ -"""Filter the Swift toolchain down to just its Linux runtime shared objects. - -The standalone toolchain's `:files` bundles the compiler, host libraries, clang -runtime, plugins, etc. For running a Swift-linked binary we only need the -runtime shared objects in `usr/lib/swift/linux/`. -""" - -def _swift_runtime_libs_impl(ctx): - libs = [ - f - for f in ctx.files.toolchain - if "/usr/lib/swift/linux/" in f.path and f.path.endswith(".so") - ] - return [DefaultInfo(files = depset(libs))] - -swift_runtime_libs = rule( - implementation = _swift_runtime_libs_impl, - attrs = {"toolchain": attr.label(allow_files = True)}, -)