Skip to content

Commit

Permalink
Add a flag to allow tools to be compiled as universal binaries (#712)
Browse files Browse the repository at this point in the history
This adds a new boolean flag
`--@build_bazel_rules_swift//swift:universal_tools` that when used will
force all the tools used in this repository to be compiled into
universal binaries. This allows execution running on different macOS
platforms (`arm64` and `x86_64`) to be compatible with each other and
can run natively on both.
  • Loading branch information
thii authored Jan 22, 2022
1 parent 218ae09 commit 2f9d3ac
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 5 deletions.
14 changes: 13 additions & 1 deletion swift/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:common_settings.bzl", "bool_setting")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "bool_setting")
load(
"//swift/internal:build_settings.bzl",
"per_module_swiftcopt_flag",
Expand Down Expand Up @@ -66,6 +66,18 @@ bool_setting(
build_setting_default = False,
)

bool_flag(
name = "universal_tools",
build_setting_default = False,
)

config_setting(
name = "universal_tools_config",
flag_values = {
"@build_bazel_rules_swift//swift:universal_tools": "true",
},
)

# Configuration setting for forcing generation of Apple targets.
# NOTE: this is only intended for use with transitions that want to force
# building of an Apple target when building for Linux.
Expand Down
4 changes: 2 additions & 2 deletions swift/internal/swift_grpc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,14 @@ The kind of definitions that should be generated:
"_protoc": attr.label(
cfg = "exec",
default = Label(
"@com_google_protobuf//:protoc",
"//tools/protoc_wrapper",
),
executable = True,
),
"_protoc_gen_swiftgrpc": attr.label(
cfg = "exec",
default = Label(
"@com_github_grpc_grpc_swift//:protoc-gen-swiftgrpc",
"@com_github_grpc_grpc_swift//:protoc-gen-swiftgrpc_wrapper",
),
executable = True,
),
Expand Down
4 changes: 2 additions & 2 deletions swift/internal/swift_protoc_gen_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -566,12 +566,12 @@ swift_protoc_gen_aspect = aspect(
),
"_protoc": attr.label(
cfg = "exec",
default = Label("@com_google_protobuf//:protoc"),
default = Label("//tools/protoc_wrapper"),
executable = True,
),
"_protoc_gen_swift": attr.label(
cfg = "exec",
default = Label("@com_github_apple_swift_protobuf//:ProtoCompilerPlugin"),
default = Label("@com_github_apple_swift_protobuf//:ProtoCompilerPlugin_wrapper"),
executable = True,
),
},
Expand Down
19 changes: 19 additions & 0 deletions third_party/com_github_apple_swift_protobuf/BUILD.overlay
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
load(
"@build_bazel_apple_support//rules:universal_binary.bzl",
"universal_binary",
)
load(
"@build_bazel_rules_swift//swift:swift.bzl",
"swift_binary",
Expand Down Expand Up @@ -31,3 +35,18 @@ swift_binary(
visibility = ["//visibility:public"],
deps = [":SwiftProtobufPluginLibrary"],
)

universal_binary(
name = "universal_ProtoCompilerPlugin",
binary = ":ProtoCompilerPlugin",
visibility = ["//visibility:public"],
)

alias(
name = "ProtoCompilerPlugin_wrapper",
actual = select({
"@build_bazel_rules_swift//swift:universal_tools_config": ":universal_ProtoCompilerPlugin",
"//conditions:default": ":ProtoCompilerPlugin",
}),
visibility = ["//visibility:public"],
)
19 changes: 19 additions & 0 deletions third_party/com_github_grpc_grpc_swift/BUILD.overlay
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load(
"@build_bazel_apple_support//rules:universal_binary.bzl",
"universal_binary",
)
load(
"@build_bazel_rules_swift//swift:swift.bzl",
"swift_binary",
Expand Down Expand Up @@ -65,3 +69,18 @@ swift_binary(
"@com_github_apple_swift_protobuf//:SwiftProtobufPluginLibrary",
],
)

universal_binary(
name = "universal_protoc-gen-swiftgrpc",
binary = ":protoc-gen-swiftgrpc",
visibility = ["//visibility:public"],
)

alias(
name = "protoc-gen-swiftgrpc_wrapper",
actual = select({
"@build_bazel_rules_swift//swift:universal_tools_config": ":universal_protoc-gen-swiftgrpc",
"//conditions:default": ":protoc-gen-swiftgrpc",
}),
visibility = ["//visibility:public"],
)
16 changes: 16 additions & 0 deletions tools/protoc_wrapper/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@build_bazel_apple_support//rules:universal_binary.bzl", "universal_binary")

package(default_visibility = ["//visibility:public"])

universal_binary(
name = "universal_protoc",
binary = "@com_google_protobuf//:protoc",
)

alias(
name = "protoc_wrapper",
actual = select({
"//swift:universal_tools_config": ":universal_protoc",
"//conditions:default": "@com_google_protobuf//:protoc",
}),
)
16 changes: 16 additions & 0 deletions tools/worker/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
load("@build_bazel_apple_support//rules:universal_binary.bzl", "universal_binary")

licenses(["notice"])

Expand Down Expand Up @@ -98,6 +99,21 @@ cc_binary(
}),
)

universal_binary(
name = "universal_worker",
binary = ":worker",
visibility = ["//visibility:public"],
)

alias(
name = "worker_wrapper",
actual = select({
"//swift:universal_tools_config": ":universal_worker",
"//conditions:default": ":worker",
}),
visibility = ["//visibility:public"],
)

# Consumed by Bazel integration tests.
filegroup(
name = "for_bazel_tests",
Expand Down

0 comments on commit 2f9d3ac

Please sign in to comment.