Skip to content

Fix build scripts targeting the wrong architecture #1564

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

Merged
merged 2 commits into from
Oct 7, 2022
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
8 changes: 8 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,14 @@ tasks:
working_directory: examples/ios
build_targets:
- "//..."
ios_build:
name: iOS build script cross compile test
platform: macos
working_directory: examples/ios_build
test_flags:
- "--platforms=//:ios_x86_64"
test_targets:
- "//..."

buildifier:
version: latest
Expand Down
33 changes: 29 additions & 4 deletions cargo/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@ load("//rust/private:rustc.bzl", "BuildInfo", "get_compilation_mode_opts", "get_
# buildifier: disable=bzl-visibility
load("//rust/private:utils.bzl", "dedent", "expand_dict_value_locations", "find_cc_toolchain", "find_toolchain", "name_to_crate_name")

def strip_target(elems):
"""Remove '-target xxx' from C(XX)FLAGS.

The cpp toolchain adds '-target xxx' and '-isysroot xxx' to CFLAGS. If it is not stripped out before
the CFLAGS are provided to build scripts, it can cause the build to take on the host architecture
instead of the target architecture.

Args:
elems (list): A list of args

Returns:
list: the modified args
"""
skip_next = False
out_elems = []
for elem in elems:
if skip_next:
skip_next = False
continue
if elem == "-target" or elem == "-isysroot":
skip_next = True
continue
out_elems.append(elem)
return out_elems

def get_cc_compile_args_and_env(cc_toolchain, feature_configuration):
"""Gather cc environment variables from the given `cc_toolchain`

Expand All @@ -30,16 +55,16 @@ def get_cc_compile_args_and_env(cc_toolchain, feature_configuration):
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
)
cc_c_args = cc_common.get_memory_inefficient_command_line(
cc_c_args = strip_target(cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = C_COMPILE_ACTION_NAME,
variables = compile_variables,
)
cc_cxx_args = cc_common.get_memory_inefficient_command_line(
))
cc_cxx_args = strip_target(cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = CPP_COMPILE_ACTION_NAME,
variables = compile_variables,
)
))
cc_env = cc_common.get_environment_variables(
feature_configuration = feature_configuration,
action_name = C_COMPILE_ACTION_NAME,
Expand Down
1 change: 1 addition & 0 deletions examples/.bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ android
cargo_manifest_dir/external_crate
crate_universe
ios
ios_build
1 change: 1 addition & 0 deletions examples/ios_build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel-*
65 changes: 65 additions & 0 deletions examples/ios_build/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
load("@rules_rust//rust:defs.bzl", "rust_static_library")

rust_static_library(
name = "ios_build_lib",
srcs = glob(["**/*.rs"]),
edition = "2018",
deps = [
"@ios_build//:zstd",
],
)

filegroup(
name = "lib",
srcs = ["ios_build_lib"],
)

sh_test(
name = "check_arch",
srcs = ["check_arch.sh"],
args = ["$(location :lib)"],
data = [":lib"],
)

platform(
name = "macos_x86_64",
constraint_values = [
"@platforms//cpu:x86_64",
"@platforms//os:macos",
],
)

platform(
name = "macos_arm64",
constraint_values = [
"@platforms//cpu:arm64",
"@platforms//os:macos",
],
)

platform(
name = "ios_x86_64",
constraint_values = [
"@platforms//cpu:x86_64",
"@platforms//os:ios",
"@build_bazel_apple_support//constraints:simulator",
],
)

platform(
name = "ios_sim_arm64",
constraint_values = [
"@platforms//cpu:arm64",
"@platforms//os:ios",
"@build_bazel_apple_support//constraints:simulator",
],
)

platform(
name = "ios_arm64",
constraint_values = [
"@platforms//cpu:arm64",
"@platforms//os:ios",
"@build_bazel_apple_support//constraints:device",
],
)
63 changes: 63 additions & 0 deletions examples/ios_build/Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "cc"
version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
dependencies = [
"jobserver",
]

[[package]]
name = "ios_build"
version = "0.1.0"
dependencies = [
"zstd",
]

[[package]]
name = "jobserver"
version = "0.1.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "068b1ee6743e4d11fb9c6a1e6064b3693a1b600e7f5f5988047d98b3dc9fb90b"
dependencies = [
"libc",
]

[[package]]
name = "libc"
version = "0.2.134"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "329c933548736bc49fd575ee68c89e8be4d260064184389a5b77517cddd99ffb"

[[package]]
name = "zstd"
version = "0.11.2+zstd.1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20cc960326ece64f010d2d2107537f26dc589a6573a316bd5b1dba685fa5fde4"
dependencies = [
"zstd-safe",
]

[[package]]
name = "zstd-safe"
version = "5.0.2+zstd.1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d2a5585e04f9eea4b2a3d1eca508c4dee9592a89ef6f450c11719da0726f4db"
dependencies = [
"libc",
"zstd-sys",
]

[[package]]
name = "zstd-sys"
version = "2.0.1+zstd.1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fd07cbbc53846d9145dbffdf6dd09a7a0aa52be46741825f5c97bdd4f73f12b"
dependencies = [
"cc",
"libc",
]
63 changes: 63 additions & 0 deletions examples/ios_build/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions examples/ios_build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "ios_build"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
zstd = "0.11.2"
64 changes: 64 additions & 0 deletions examples/ios_build/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
workspace(name = "examples")

local_repository(
name = "rules_rust",
path = "../../",
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains(
edition = "2018",
extra_target_triples = [
"aarch64-apple-ios-sim",
"x86_64-apple-ios",
],
)

load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")

crate_universe_dependencies(bootstrap = True)

load("@rules_rust//crate_universe:defs.bzl", "crates_repository", "splicing_config")

crates_repository(
name = "ios_build",
cargo_lockfile = "//:Cargo.Bazel.lock",
# `generator` is not necessary in official releases.
# See load satement for `cargo_bazel_bootstrap`.
generator = "@cargo_bazel_bootstrap//:cargo-bazel",
lockfile = "//:cargo-bazel-lock.json",
manifests = ["//:Cargo.toml"],
splicing_config = splicing_config(
resolver_version = "2",
),
)

load(
"@ios_build//:defs.bzl",
ios_build_crate_repositories = "crate_repositories",
)

ios_build_crate_repositories()

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Used for Bazel CI
http_archive(
name = "bazelci_rules",
sha256 = "eca21884e6f66a88c358e580fd67a6b148d30ab57b1680f62a96c00f9bc6a07e",
strip_prefix = "bazelci_rules-1.0.0",
url = "https://github.com/bazelbuild/continuous-integration/releases/download/rules-1.0.0/bazelci_rules-1.0.0.tar.gz",
)

load("@bazelci_rules//:rbe_repo.bzl", "rbe_preconfig")

# Creates a default toolchain config for RBE.
# Use this as is if you are using the rbe_ubuntu16_04 container,
# otherwise refer to RBE docs.
rbe_preconfig(
name = "buildkite_config",
toolchain = "ubuntu1804-bazel-java11",
)
Loading