Skip to content

Commit

Permalink
Merge branch 'bazelbuild:main' into stable_tars_for_release
Browse files Browse the repository at this point in the history
  • Loading branch information
jsharpe authored Oct 14, 2023
2 parents b32b235 + 01cf789 commit d5229bb
Show file tree
Hide file tree
Showing 22 changed files with 448 additions and 43 deletions.
10 changes: 2 additions & 8 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,11 @@ use_repo(
"ninja_1.11.1_toolchains",
"ninja_build_src",
"pkgconfig_src",
"rules_foreign_cc_framework_toolchain_freebsd",
"rules_foreign_cc_framework_toolchain_linux",
"rules_foreign_cc_framework_toolchain_macos",
"rules_foreign_cc_framework_toolchain_windows",
"rules_foreign_cc_framework_toolchains",
)

register_toolchains(
"@rules_foreign_cc_framework_toolchain_freebsd//:toolchain",
"@rules_foreign_cc_framework_toolchain_linux//:toolchain",
"@rules_foreign_cc_framework_toolchain_macos//:toolchain",
"@rules_foreign_cc_framework_toolchain_windows//:toolchain",
"@rules_foreign_cc_framework_toolchains//:all",
"@rules_foreign_cc//toolchains:built_make_toolchain",
"@rules_foreign_cc//toolchains:built_meson_toolchain",
"@rules_foreign_cc//toolchains:built_pkgconfig_toolchain",
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ The following flags are required in Bazel 4.x but not Bazel 5.x or newer:
Note that the rules may be compatible with older versions of Bazel but support may break
in future changes as these older versions are not tested.

Note also that the `runnable_binary` macro requires bazel 5.4.0, for the `rlocationpath` [path
variable](https://bazel.build/reference/be/make-variables)

## News

For more generalized updates, please see [NEWS.md](./NEWS.md) or checkout the
Expand Down
2 changes: 1 addition & 1 deletion examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use_repo(

bazel_dep(name = "platforms", version = "0.0.6")
bazel_dep(name = "rules_swift", version = "1.6.0", repo_name = "build_bazel_rules_swift")
bazel_dep(name = "rules_apple", version = "2.1.0", repo_name = "build_bazel_rules_apple")
bazel_dep(name = "rules_apple", version = "3.0.0-rc1", repo_name = "build_bazel_rules_apple")
bazel_dep(name = "apple_support", version = "1.4.1", repo_name = "build_bazel_apple_support")
bazel_dep(name = "rules_python", version = "0.22.0")

Expand Down
12 changes: 10 additions & 2 deletions examples/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ rbe_preconfig(
toolchain = "ubuntu1804-bazel-java11",
)

# TODO: Remove once bazel 4.x is no longer supported
http_archive(
name = "rules_proto",
sha256 = "20b240eba17a36be4b0b22635aca63053913d5c1ee36e16be36499d167a2f533",
strip_prefix = "rules_proto-11bf7c25e666dd7ddacbcd4d4c4a9de7a25175f8",
urls = ["https://github.com/bazelbuild/rules_proto/archive/11bf7c25e666dd7ddacbcd4d4c4a9de7a25175f8.tar.gz"],
)

http_archive(
name = "build_bazel_apple_support",
patches = ["//deps:apple_support.patch"],
Expand All @@ -94,8 +102,8 @@ http_archive(

http_archive(
name = "build_bazel_rules_apple",
sha256 = "12865e5944f09d16364aa78050366aca9dc35a32a018fa35f5950238b08bf744",
url = "https://github.com/bazelbuild/rules_apple/releases/download/0.34.2/rules_apple.0.34.2.tar.gz",
sha256 = "62b24b9c3c1eb5bdb6fe1a3f4bf541d6e61eac8997e87c25a50c821f85bf8ff2",
url = "https://github.com/bazelbuild/rules_apple/releases/download/3.0.0-rc1/rules_apple.3.0.0-rc1.tar.gz",
)

load(
Expand Down
120 changes: 120 additions & 0 deletions examples/platform_mappings
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
platforms:
@build_bazel_apple_support//platforms:macos_x86_64
--cpu=darwin_x86_64

@build_bazel_apple_support//platforms:macos_arm64
--cpu=darwin_arm64

@build_bazel_apple_support//platforms:darwin_arm64e
--cpu=darwin_arm64e

@build_bazel_apple_support//platforms:ios_i386
--cpu=ios_i386

@build_bazel_apple_support//platforms:ios_x86_64
--cpu=ios_x86_64

@build_bazel_apple_support//platforms:ios_sim_arm64
--cpu=ios_sim_arm64

@build_bazel_apple_support//platforms:ios_armv7
--cpu=ios_armv7

@build_bazel_apple_support//platforms:ios_arm64
--cpu=ios_arm64

@build_bazel_apple_support//platforms:ios_arm64e
--cpu=ios_arm64e

@build_bazel_apple_support//platforms:tvos_x86_64
--cpu=tvos_x86_64

@build_bazel_apple_support//platforms:tvos_sim_arm64
--cpu=tvos_sim_arm64

@build_bazel_apple_support//platforms:tvos_arm64
--cpu=tvos_arm64

@build_bazel_apple_support//platforms:watchos_i386
--cpu=watchos_i386

@build_bazel_apple_support//platforms:watchos_x86_64
--cpu=watchos_x86_64

@build_bazel_apple_support//platforms:watchos_arm64
--cpu=watchos_arm64

@build_bazel_apple_support//platforms:watchos_armv7k
--cpu=watchos_armv7k

@build_bazel_apple_support//platforms:watchos_arm64_32
--cpu=watchos_arm64_32

flags:
--cpu=darwin_x86_64
--apple_platform_type=macos
@build_bazel_apple_support//platforms:macos_x86_64

--cpu=darwin_arm64
--apple_platform_type=macos
@build_bazel_apple_support//platforms:macos_arm64

--cpu=darwin_arm64e
--apple_platform_type=macos
@build_bazel_apple_support//platforms:darwin_arm64e

--cpu=ios_i386
--apple_platform_type=ios
@build_bazel_apple_support//platforms:ios_i386

--cpu=ios_x86_64
--apple_platform_type=ios
@build_bazel_apple_support//platforms:ios_x86_64

--cpu=ios_sim_arm64
--apple_platform_type=ios
@build_bazel_apple_support//platforms:ios_sim_arm64

--cpu=ios_armv7
--apple_platform_type=ios
@build_bazel_apple_support//platforms:ios_armv7

--cpu=ios_arm64
--apple_platform_type=ios
@build_bazel_apple_support//platforms:ios_arm64

--cpu=ios_arm64e
--apple_platform_type=ios
@build_bazel_apple_support//platforms:ios_arm64e

--cpu=tvos_x86_64
--apple_platform_type=tvos
@build_bazel_apple_support//platforms:tvos_x86_64

--cpu=tvos_sim_arm64
--apple_platform_type=tvos
@build_bazel_apple_support//platforms:tvos_sim_arm64

--cpu=tvos_arm64
--apple_platform_type=tvos
@build_bazel_apple_support//platforms:tvos_arm64

--cpu=watchos_i386
--apple_platform_type=watchos
@build_bazel_apple_support//platforms:watchos_i386

--cpu=watchos_x86_64
--apple_platform_type=watchos
@build_bazel_apple_support//platforms:watchos_x86_64

--cpu=watchos_arm64
--apple_platform_type=watchos
@build_bazel_apple_support//platforms:watchos_arm64

--cpu=watchos_armv7k
--apple_platform_type=watchos
@build_bazel_apple_support//platforms:watchos_armv7k

--cpu=watchos_arm64_32
--apple_platform_type=watchos
@build_bazel_apple_support//platforms:watchos_arm64_32
1 change: 1 addition & 0 deletions foreign_cc/boost_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ boost_build = rule(
attrs = _attrs(),
fragments = CC_EXTERNAL_RULE_FRAGMENTS,
output_to_genfiles = True,
provides = [CcInfo],
implementation = _boost_build_impl,
toolchains = [
"@rules_foreign_cc//foreign_cc/private/framework:shell_toolchain",
Expand Down
4 changes: 4 additions & 0 deletions foreign_cc/cmake.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ load("//foreign_cc/private:transitions.bzl", "foreign_cc_rule_variant")
load(
"//foreign_cc/private/framework:platform.bzl",
"os_name",
"target_arch_name",
"target_os_name",
)
load(
Expand Down Expand Up @@ -253,6 +254,8 @@ def _create_configure_script(configureParameters):
configure_script = create_cmake_script(
workspace_name = ctx.workspace_name,
target_os = target_os_name(ctx),
target_arch = target_arch_name(ctx),
host_os = os_name(ctx),
generator = attrs.generator,
cmake_path = attrs.cmake_path,
tools = tools,
Expand Down Expand Up @@ -415,6 +418,7 @@ cmake = rule(
"@rules_foreign_cc//foreign_cc/private/framework:shell_toolchain",
"@bazel_tools//tools/cpp:toolchain_type",
],
provides = [CcInfo],
# TODO: Remove once https://github.com/bazelbuild/bazel/issues/11584 is closed and the min supported
# version is updated to a release of Bazel containing the new default for this setting.
incompatible_use_toolchain_transition = True,
Expand Down
1 change: 1 addition & 0 deletions foreign_cc/make.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ make = rule(
attrs = _attrs(),
fragments = CC_EXTERNAL_RULE_FRAGMENTS,
output_to_genfiles = True,
provides = [CcInfo],
implementation = _make,
toolchains = [
"@rules_foreign_cc//toolchains:make_toolchain",
Expand Down
1 change: 1 addition & 0 deletions foreign_cc/meson.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ meson = rule(
attrs = _attrs(),
fragments = CC_EXTERNAL_RULE_FRAGMENTS,
output_to_genfiles = True,
provides = [CcInfo],
implementation = _meson_impl,
toolchains = [
"@rules_foreign_cc//toolchains:meson_toolchain",
Expand Down
1 change: 1 addition & 0 deletions foreign_cc/ninja.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ ninja = rule(
attrs = _attrs(),
fragments = CC_EXTERNAL_RULE_FRAGMENTS,
output_to_genfiles = True,
provides = [CcInfo],
implementation = _ninja_impl,
toolchains = [
"@rules_foreign_cc//toolchains:ninja_toolchain",
Expand Down
37 changes: 30 additions & 7 deletions foreign_cc/private/cmake_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,30 @@ def _escape_dquote_bash_crosstool(text):
# We use a starlark raw string to prevent the need to escape backslashes for starlark as well.
return text.replace('"', r'\\\\\\\"')

_TARGET_OS_PARAMS = {
"android": {
"ANDROID": "YES",
"CMAKE_SYSTEM_NAME": "Linux",
},
"linux": {
"CMAKE_SYSTEM_NAME": "Linux",
},
}

_TARGET_ARCH_PARAMS = {
"aarch64": {
"CMAKE_SYSTEM_PROCESSOR": "aarch64",
},
"x86_64": {
"CMAKE_SYSTEM_PROCESSOR": "x86_64",
},
}

def create_cmake_script(
workspace_name,
target_os,
target_arch,
host_os,
generator,
cmake_path,
tools,
Expand All @@ -39,6 +60,8 @@ def create_cmake_script(
Args:
workspace_name: current workspace name
target_os: The target OS for the build
target_arch: The target arch for the build
host_os: The execution OS for the build
generator: The generator target for cmake to use
cmake_path: The path to the cmake executable
tools: cc_toolchain tools (CxxToolsInfo)
Expand Down Expand Up @@ -93,13 +116,13 @@ def create_cmake_script(
if not params.cache.get("CMAKE_RANLIB"):
params.cache.update({"CMAKE_RANLIB": ""})

# Avoid cmake passing wrong linker flags when targeting android on macOS
# https://github.com/bazelbuild/rules_foreign_cc/issues/289
if target_os == "android":
params.cache.update({
"ANDROID": "YES",
"CMAKE_SYSTEM_NAME": "Linux",
})
# Avoid CMake passing the wrong linker flags when cross compiling
# by setting CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR,
# see https://github.com/bazelbuild/rules_foreign_cc/issues/289,
# and https://github.com/bazelbuild/rules_foreign_cc/pull/1062
if target_os != host_os and target_os != "unknown":
params.cache.update(_TARGET_OS_PARAMS.get(target_os, {}))
params.cache.update(_TARGET_ARCH_PARAMS.get(target_arch, {}))

set_env_vars = [
"export {}=\"{}\"".format(key, _escape_dquote_bash(params.env[key]))
Expand Down
3 changes: 3 additions & 0 deletions foreign_cc/private/framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
cfg = "exec",
default = [],
),
"_aarch64_constraint": attr.label(default = Label("@platforms//cpu:aarch64")),
"_android_constraint": attr.label(default = Label("@platforms//os:android")),
# we need to declare this attribute to access cc_toolchain
"_cc_toolchain": attr.label(
Expand All @@ -219,6 +220,8 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
cfg = "exec",
default = Label("@rules_foreign_cc//foreign_cc/private/framework:platform_info"),
),
"_linux_constraint": attr.label(default = Label("@platforms//os:linux")),
"_x86_64_constraint": attr.label(default = Label("@platforms//cpu:x86_64")),
}

# A list of common fragments required by rules using this framework
Expand Down
19 changes: 18 additions & 1 deletion foreign_cc/private/framework/platform.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ def os_name(ctx):

return platform_info[ForeignCcPlatformInfo].os

def target_arch_name(ctx):
"""A helper function for getting the target architecture name based on the constraints
Args:
ctx (ctx): The current rule's context object
Returns:
str: The string of the current platform
"""
archs = ["x86_64", "aarch64"]
for arch in archs:
constraint = getattr(ctx.attr, "_{}_constraint".format(arch))
if constraint and ctx.target_platform_has_constraint(constraint[platform_common.ConstraintValueInfo]):
return arch

return "unknown"

def target_os_name(ctx):
"""A helper function for getting the target operating system name based on the constraints
Expand All @@ -75,7 +92,7 @@ def target_os_name(ctx):
Returns:
str: The string of the current platform
"""
operating_systems = ["android"]
operating_systems = ["android", "linux"]
for os in operating_systems:
constraint = getattr(ctx.attr, "_{}_constraint".format(os))
if constraint and ctx.target_platform_has_constraint(constraint[platform_common.ConstraintValueInfo]):
Expand Down
Loading

0 comments on commit d5229bb

Please sign in to comment.