Skip to content
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

Add experimental import proc macro #1127

Closed
wants to merge 56 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
0479a28
Add new `import` macro prototype.
cfredric Nov 10, 2021
7e9aa4c
Be consistent about 3p/1p vs first-/third-party.
cfredric Dec 21, 2021
5116fa4
Swap loops to avoid useless iterations.
cfredric Dec 21, 2021
a2ea5fb
Avoid some extra loops when doing replacements.
cfredric Dec 21, 2021
136faba
Implement transition for disabling renaming of first-party crates.
cfredric Jan 28, 2022
1d47d78
Fix polarity of `should_encode_label_in_crate_name` function.
cfredric Jan 28, 2022
ceee3a6
Fix example; declare rustfmt dependencies.
cfredric Jan 28, 2022
501ad53
Fix a few more targets when flipping the rename_first_party_crates se…
cfredric Jan 28, 2022
0b069ed
Remove unnecessary transition.
cfredric Feb 3, 2022
f3bcbef
Fix unnecessary BUILD file changes.
cfredric Feb 3, 2022
f7bb612
Load `import`'s deps to fix examples/ workspace.
cfredric Feb 7, 2022
32043c2
Fix `examples` repo to pass tests with and without renaming.
cfredric Feb 7, 2022
af0786d
Run buildifier.
cfredric Feb 7, 2022
50396b4
Fix clippy problems.
cfredric Feb 7, 2022
5cdb3fb
Fix rustfmt issues.
cfredric Feb 7, 2022
e4676e7
Fix manual-stripping lint.
cfredric Feb 7, 2022
0c04b99
Use pattern-matching more effectively when parsing env vars.
cfredric Feb 7, 2022
e4a5b8c
mix in label in crate hash computation (#1083)
krasimirgg Jan 3, 2022
f5ab10b
Added unit tests for rustdoc rules (#1078)
UebelAndre Jan 4, 2022
9bae0a8
Updated tests (#1085)
UebelAndre Jan 6, 2022
45aba44
Flip `incompatible_disable_custom_test_launcher` (#1079)
UebelAndre Jan 6, 2022
5178cf2
Removed deprecated `dep` attribute from `rustdoc` rules (#1086)
UebelAndre Jan 6, 2022
0b8ad50
add support for targeting iOS simulator on Apple Silicon (#1073)
dae Jan 7, 2022
3d8f88e
Updated `bzl_library` targets to have a more discoverable name (#1088)
UebelAndre Jan 10, 2022
55649e7
Updated `build_bazel_rules_nodejs` to 4.6.0 (#1087)
UebelAndre Jan 10, 2022
8e77bdb
Fix proc_macro_dylib_path when they are built in both opt and debug (…
djmarcin Jan 11, 2022
9a39709
optimization: switch fastbuild opt default to 1 from 0 (#1091)
durin42 Jan 12, 2022
f94cc3e
Added Rust 1.58.0 (#1092)
UebelAndre Jan 13, 2022
ceaf856
Revert "optimization: switch fastbuild opt default to 1 from 0 (#1091…
UebelAndre Jan 14, 2022
8fb0b78
Deleted `incompatible_disable_custom_test_launcher` and related code …
UebelAndre Jan 17, 2022
126d18b
Add DEBUG env var for running build scripts (#1096)
djmarcin Jan 19, 2022
db2bcc9
Added Rust 1.58.1 (#1097)
UebelAndre Jan 21, 2022
66b4d7b
Updated rust_test docs (#1098)
UebelAndre Jan 21, 2022
c16516b
Replace host with exec configurations for grep_includes (#1099)
hlopko Jan 21, 2022
6d789f9
add ARM Cortex M3 triple mappings (#1103)
driftregion Jan 23, 2022
074bc60
Renamed `rust_toolchain.rust_lib` to `rust_toolchain.rust_std` (#1102)
UebelAndre Jan 24, 2022
e7b00b7
Allow users to configure the timeout of `cargo_bootstrap_repository`.…
UebelAndre Jan 24, 2022
e4fa16b
Made components of `rustc` bundles mandatory for `rust_toolchain`. (#…
UebelAndre Jan 24, 2022
0e77c2e
Allow cargo_bootstrap_repository to specify rustc and cargo separatel…
UebelAndre Jan 24, 2022
e2ebcb6
Fixed tests which relied on the git checkout's newline endings (#1106)
UebelAndre Jan 26, 2022
0bf2c2d
Replaced CI flags with compatibility flags for incompatible targets (…
UebelAndre Jan 27, 2022
444f2b3
Minor docs cleanup (#1108)
UebelAndre Jan 27, 2022
739c98d
Added `rules_rust_dependencies` and `rust_register_toolchains` macros…
UebelAndre Jan 27, 2022
b90d504
Fixed typo in docs (#1110)
UebelAndre Jan 27, 2022
1d2971c
fix is_exec_configuration() check (#1112)
krasimirgg Jan 28, 2022
444c696
add extra_exec_rustc_flags build config (#1113)
krasimirgg Jan 31, 2022
f9ef71c
Replace Targets and lists of files in `rust_toolchain` with depsets (…
UebelAndre Jan 31, 2022
a0f678e
Deduplicate input to ctx.expand_location (#1118)
illicitonion Feb 1, 2022
a4a3fa9
Calculate path values in the rust_toolchain construction (#1115)
UebelAndre Feb 3, 2022
4fcdeba
Fix Windows long path issue in examples (#1122)
UebelAndre Feb 4, 2022
f462643
support armv7 in tripple mappings (#1116)
harrysarson Feb 4, 2022
3f6f0d9
Remove --incompatible_remove_transitive_libs_from_dep_info (#1124)
scentini Feb 7, 2022
f1f3416
Remove `toolchain._crosstool` field (#1125)
scentini Feb 7, 2022
83803fd
Fix `rust_doc_test` using execpath paths instead of rootpath (#1129)
UebelAndre Feb 8, 2022
14ee068
third_party_dir env var should be case-sensitive
cfredric Feb 8, 2022
ef189fb
Add new `import` macro prototype.
cfredric Nov 10, 2021
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
4 changes: 0 additions & 4 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ tasks:
test_targets:
- "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245
- "//..."
# This test requires --incompatible_macos_set_install_name and Bazel 4.2.0+
- "-//ffi/rust_calling_c:matrix_dylib_test"
build_flags: *aspects_flags
windows_examples:
name: Examples
Expand All @@ -239,8 +237,6 @@ tasks:
windows_targets: &windows_targets
- "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245
- "//..."
- "-//ffi/rust_calling_c:matrix_dylib_test"
- "-//ffi/rust_calling_c:matrix_dynamically_linked"
# The bindgen rules currently do not work on windows
# see: https://github.com/bazelbuild/rules_rust/issues/919
- "-//bindgen/..."
Expand Down
24 changes: 21 additions & 3 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//rust:defs.bzl", "capture_clippy_output", "error_format", "extra_rustc_flags")
load("//rust:defs.bzl", "capture_clippy_output", "error_format", "extra_exec_rustc_flags", "extra_rustc_flags")

exports_files(["LICENSE"])

bzl_library(
name = "rules",
name = "bzl_lib",
srcs = [
":workspace.bzl",
],
visibility = ["//visibility:public"],
)

alias(
name = "rules",
actual = ":bzl_lib",
deprecation = "Please use the `@rules_rust//:bzl_lib` target instead",
visibility = ["//visibility:public"],
)

# This setting may be changed from the command line to generate machine readable errors.
error_format(
name = "error_format",
build_setting_default = "human",
visibility = ["//visibility:public"],
)

# This setting may be used to pass extra options to rustc from the command line.
# This setting may be used to pass extra options to rustc from the command line
# in non-exec configuration.
# It applies across all targets whereas the rustc_flags option on targets applies only
# to that target. This can be useful for passing build-wide options such as LTO.
extra_rustc_flags(
Expand All @@ -27,6 +35,16 @@ extra_rustc_flags(
visibility = ["//visibility:public"],
)

# This setting may be used to pass extra options to rustc from the command line
# in exec configuration.
# It applies across all targets whereas the rustc_flags option on targets applies only
# to that target. This can be useful for passing build-wide options such as LTO.
extra_exec_rustc_flags(
name = "extra_exec_rustc_flags",
build_setting_default = [],
visibility = ["//visibility:public"],
)

# This setting is used by the clippy rules. See https://bazelbuild.github.io/rules_rust/rust_clippy.html
label_flag(
name = "clippy.toml",
Expand Down
10 changes: 8 additions & 2 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
workspace(name = "rules_rust")

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

rust_repositories(include_rustc_srcs = True)
rules_rust_dependencies()

rust_register_toolchains(include_rustc_srcs = True)

load("@rules_rust//proto:repositories.bzl", "rust_proto_repositories")

Expand All @@ -20,6 +22,10 @@ load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_deps")

rust_analyzer_deps()

load("@rules_rust//util/import:deps.bzl", "import_deps")

import_deps()

load("@rules_rust//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_repositories")

rust_wasm_bindgen_repositories()
Expand Down
10 changes: 8 additions & 2 deletions bindgen/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ package(default_visibility = ["//visibility:public"])
toolchain_type(name = "bindgen_toolchain")

bzl_library(
name = "rules",
name = "bzl_lib",
srcs = glob(["**/*.bzl"]) + ["//bindgen/raze:crates.bzl"],
deps = ["//rust:rules"],
deps = ["//rust:bzl_lib"],
)

alias(
name = "rules",
actual = ":bzl_lib",
deprecation = "Please use the `@rules_rust//bindgen:bzl_lib` target instead",
)

rust_bindgen_toolchain(
Expand Down
8 changes: 7 additions & 1 deletion cargo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
package(default_visibility = ["//visibility:public"])

bzl_library(
name = "rules",
name = "bzl_lib",
srcs = glob(["**/*.bzl"]),
deps = ["//cargo/private:bzl_lib"],
)

alias(
name = "rules",
actual = ":bzl_lib",
deprecation = "Please use the `@rules_rust//cargo:bzl_lib` target instead",
)
52 changes: 41 additions & 11 deletions cargo/cargo_bootstrap.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""The `cargo_bootstrap` rule is used for bootstrapping cargo binaries in a repository rule."""

load("//cargo/private:cargo_utils.bzl", "get_cargo_and_rustc", "get_host_triple")
load("//cargo/private:cargo_utils.bzl", "get_host_triple", "get_rust_tools")
load("//rust:defs.bzl", "rust_common")

_CARGO_BUILD_MODES = [
Expand Down Expand Up @@ -29,7 +29,8 @@ def cargo_bootstrap(
environment = {},
quiet = False,
build_mode = "release",
target_dir = None):
target_dir = None,
timeout = 600):
"""A function for bootstrapping a cargo binary within a repository rule

Args:
Expand All @@ -43,6 +44,7 @@ def cargo_bootstrap(
build_mode (str, optional): The build mode to use
target_dir (path, optional): The directory in which to produce build outputs
(Cargo's --target-dir argument).
timeout (int, optional): Maximum duration of the Cargo build command in seconds,

Returns:
path: The path of the built binary within the target directory
Expand Down Expand Up @@ -78,6 +80,7 @@ def cargo_bootstrap(
args,
environment = env,
quiet = quiet,
timeout = timeout,
)

if result.return_code != 0:
Expand Down Expand Up @@ -174,9 +177,20 @@ def _cargo_bootstrap_repository_impl(repository_ctx):
version_str = repository_ctx.attr.version

host_triple = get_host_triple(repository_ctx)
tools = get_cargo_and_rustc(

if repository_ctx.attr.rust_toolchain_repository_template:
# buildifier: disable=print
print("Warning: `rust_toolchain_repository_template` is deprecated. Please use `rust_toolchain_cargo_template` and `rust_toolchain_rustc_template`")
cargo_template = "@{}{}".format(repository_ctx.attr.rust_toolchain_repository_template, "//:bin/{tool}")
rustc_template = "@{}{}".format(repository_ctx.attr.rust_toolchain_repository_template, "//:bin/{tool}")
else:
cargo_template = repository_ctx.attr.rust_toolchain_cargo_template
rustc_template = repository_ctx.attr.rust_toolchain_rustc_template

tools = get_rust_tools(
repository_ctx = repository_ctx,
toolchain_repository_template = repository_ctx.attr.rust_toolchain_repository_template,
cargo_template = cargo_template,
rustc_template = rustc_template,
host_triple = host_triple,
version = version_str,
)
Expand All @@ -188,13 +202,14 @@ def _cargo_bootstrap_repository_impl(repository_ctx):
environment = dict(_collect_environ(repository_ctx, "*").items() + _collect_environ(repository_ctx, host_triple.triple).items())

built_binary = cargo_bootstrap(
repository_ctx,
cargo_bin = tools.cargo,
rustc_bin = tools.rustc,
repository_ctx = repository_ctx,
cargo_bin = repository_ctx.path(tools.cargo),
rustc_bin = repository_ctx.path(tools.rustc),
binary = binary_name,
cargo_manifest = repository_ctx.path(repository_ctx.attr.cargo_toml),
build_mode = repository_ctx.attr.build_mode,
environment = environment,
timeout = repository_ctx.attr.timeout,
)

# Create a symlink so that the binary can be accesed via it's target name
Expand Down Expand Up @@ -247,18 +262,33 @@ cargo_bootstrap_repository = repository_rule(
"iso_date": attr.string(
doc = "The iso_date of cargo binary the resolver should use. Note: This can only be set if `version` is `beta` or `nightly`",
),
"rust_toolchain_cargo_template": attr.string(
doc = (
"The template to use for finding the host `cargo` binary. `{version}` (eg. '1.53.0'), " +
"`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), " +
"`{system}` (eg. 'darwin'), and `{tool}` (eg. 'rustc.exe') will be replaced in the string if present."
),
default = "@rust_{system}_{arch}//:bin/{tool}",
),
"rust_toolchain_repository_template": attr.string(
doc = "**Deprecated**: Please use `rust_toolchain_cargo_template` and `rust_toolchain_rustc_template`",
),
"rust_toolchain_rustc_template": attr.string(
doc = (
"The template to use for finding the host `rust_toolchain` repository. `{version}` (eg. '1.53.0'), " +
"`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{system}` (eg. 'darwin'), and `{arch}` (eg. 'aarch64') " +
"will be replaced in the string if present."
"The template to use for finding the host `rustc` binary. `{version}` (eg. '1.53.0'), " +
"`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), " +
"`{system}` (eg. 'darwin'), and `{tool}` (eg. 'rustc.exe') will be replaced in the string if present."
),
default = "rust_{system}_{arch}",
default = "@rust_{system}_{arch}//:bin/{tool}",
),
"srcs": attr.label_list(
doc = "Souce files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made",
allow_files = True,
),
"timeout": attr.int(
doc = "Maximum duration of the Cargo build command in seconds",
default = 600,
),
"version": attr.string(
doc = "The version of cargo the resolver should use",
default = rust_common.default_version,
Expand Down
12 changes: 8 additions & 4 deletions cargo/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def _build_script_impl(ctx):

toolchain_tools = [
# Needed for rustc to function.
toolchain.rustc_lib.files,
toolchain.rust_lib.files,
toolchain.rustc_lib,
toolchain.rust_std,
]

cc_toolchain = find_cpp_toolchain(ctx)
Expand All @@ -73,13 +73,17 @@ def _build_script_impl(ctx):
"CARGO_PKG_NAME": pkg_name,
"HOST": toolchain.exec_triple,
"OPT_LEVEL": compilation_mode_opt_level,
# This isn't exactly right, but Bazel doesn't have exact views of "debug" and "release", so...
"PROFILE": {"dbg": "debug", "fastbuild": "debug", "opt": "release"}.get(ctx.var["COMPILATION_MODE"], "unknown"),
"RUSTC": toolchain.rustc.path,
"TARGET": toolchain.target_flag_value,
# OUT_DIR is set by the runner itself, rather than on the action.
})

# This isn't exactly right, but Bazel doesn't have exact views of "debug" and "release", so...
env.update({
"DEBUG": {"dbg": "true", "fastbuild": "true", "opt": "false"}.get(ctx.var["COMPILATION_MODE"], "true"),
"PROFILE": {"dbg": "debug", "fastbuild": "debug", "opt": "release"}.get(ctx.var["COMPILATION_MODE"], "unknown"),
})

if ctx.attr.version:
version = ctx.attr.version.split("+")[0].split(".")
patch = version[2].split("-") if len(version) > 2 else [""]
Expand Down
101 changes: 74 additions & 27 deletions cargo/private/cargo_utils.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
"""Utility functions for the cargo rules"""

load("//rust/platform:triple.bzl", "triple")
load(
"//rust/platform:triple_mappings.bzl",
"system_to_binary_ext",
)
load("//rust/platform:triple_mappings.bzl", "system_to_binary_ext")

_CPU_ARCH_ERROR_MSG = """\
Command failed with exit code '{code}': {args}
Expand Down Expand Up @@ -107,41 +104,91 @@ def get_host_triple(repository_ctx, abi = None):

fail("Unhandled host os: {}", repository_ctx.os.name)

def get_cargo_and_rustc(repository_ctx, toolchain_repository_template, host_triple, version):
"""Retrieve a cargo and rustc binary based on the host triple.
def _resolve_repository_template(
template,
version = None,
triple = None,
arch = None,
vendor = None,
system = None,
abi = None,
tool = None):
"""Render values into a repository template string

Args:
repository_ctx (repository_ctx): The rule's context object
toolchain_repository_template (str): A template used to identify the host `rust_toolchain_repository`.
host_triple (struct): The host's triple. See `@rules_rust//rust/platform:triple.bzl`.
version (str): The version of Cargo+Rustc to use.

template (str): The template to use for rendering
version (str, optional): The Rust version used in the toolchain.
triple (str, optional): The host triple
arch (str, optional): The host CPU architecture
vendor (str, optional): The host vendor name
system (str, optional): The host system name
abi (str, optional): The host ABI
tool (str, optional): The tool to expect in the particular repository.
Eg. `cargo`, `rustc`, `stdlib`.
Returns:
struct: A struct containing the expected tools
string: The resolved template string based on the given parameters
"""
if version:
template = template.replace("{version}", version)

if triple:
template = template.replace("{triple}", triple)

if arch:
template = template.replace("{arch}", arch)

if vendor:
template = template.replace("{vendor}", vendor)

rust_toolchain_repository = toolchain_repository_template
rust_toolchain_repository = rust_toolchain_repository.replace("{version}", version)
rust_toolchain_repository = rust_toolchain_repository.replace("{triple}", host_triple.triple)
if system:
template = template.replace("{system}", system)

if host_triple.arch:
rust_toolchain_repository = rust_toolchain_repository.replace("{arch}", host_triple.arch)
if abi:
template = template.replace("{abi}", abi)

if host_triple.vendor:
rust_toolchain_repository = rust_toolchain_repository.replace("{vendor}", host_triple.vendor)
if tool:
template = template.replace("{tool}", tool)

if host_triple.system:
rust_toolchain_repository = rust_toolchain_repository.replace("{system}", host_triple.system)
return template

if host_triple.abi:
rust_toolchain_repository = rust_toolchain_repository.replace("{abi}", host_triple.abi)
def get_rust_tools(repository_ctx, cargo_template, rustc_template, host_triple, version):
"""Retrieve `cargo` and `rustc` labels based on the host triple.

Args:
repository_ctx (repository_ctx): The rule's context object
cargo_template (str): A template used to identify the label of the host `cargo` binary.
rustc_template (str): A template used to identify the label of the host `rustc` binary.
host_triple (struct): The host's triple. See `@rules_rust//rust/platform:triple.bzl`.
version (str): The version of Cargo+Rustc to use.

Returns:
struct: A struct containing the labels of expected tools
"""
extension = system_to_binary_ext(host_triple.system)

cargo_path = repository_ctx.path(Label("@{}{}".format(rust_toolchain_repository, "//:bin/cargo" + extension)))
rustc_path = repository_ctx.path(Label("@{}{}".format(rust_toolchain_repository, "//:bin/rustc" + extension)))
cargo_label = Label(_resolve_repository_template(
template = cargo_template,
version = version,
triple = host_triple.triple,
arch = host_triple.arch,
vendor = host_triple.vendor,
system = host_triple.system,
abi = host_triple.abi,
tool = "cargo" + extension,
))

rustc_label = Label(_resolve_repository_template(
template = rustc_template,
version = version,
triple = host_triple.triple,
arch = host_triple.arch,
vendor = host_triple.vendor,
system = host_triple.system,
abi = host_triple.abi,
tool = "rustc" + extension,
))

return struct(
cargo = cargo_path,
rustc = rustc_path,
cargo = cargo_label,
rustc = rustc_label,
)
Loading