Skip to content

Commit e3eb655

Browse files
committed
Stop setting duplicative CARGO_PKG env vars
1 parent 8da6166 commit e3eb655

File tree

7 files changed

+9
-44
lines changed

7 files changed

+9
-44
lines changed

cargo/private/cargo_build_script.bzl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ def _cargo_build_script_impl(ctx):
391391
env.update({
392392
"CARGO_CRATE_NAME": name_to_crate_name(pkg_name),
393393
"CARGO_MANIFEST_DIR": manifest_dir,
394-
"CARGO_PKG_NAME": pkg_name,
395394
"HOST": toolchain.exec_triple.str,
396395
"NUM_JOBS": "1",
397396
"OPT_LEVEL": compilation_mode_opt_level,
@@ -406,15 +405,6 @@ def _cargo_build_script_impl(ctx):
406405
"PROFILE": {"dbg": "debug", "fastbuild": "debug", "opt": "release"}.get(ctx.var["COMPILATION_MODE"], "unknown"),
407406
})
408407

409-
if ctx.attr.version:
410-
version = ctx.attr.version.split("+")[0].split(".")
411-
patch = version[2].split("-") if len(version) > 2 else [""]
412-
env["CARGO_PKG_VERSION_MAJOR"] = version[0]
413-
env["CARGO_PKG_VERSION_MINOR"] = version[1] if len(version) > 1 else ""
414-
env["CARGO_PKG_VERSION_PATCH"] = patch[0]
415-
env["CARGO_PKG_VERSION_PRE"] = patch[1] if len(patch) > 1 else ""
416-
env["CARGO_PKG_VERSION"] = ctx.attr.version
417-
418408
# Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
419409
# We hope that the linker env is sufficient for the whole cc_toolchain.
420410
cc_toolchain, feature_configuration = find_cc_toolchain(ctx)

cargo/private/cargo_build_script_wrapper.bzl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ def cargo_build_script(
145145
pkg_name = name_to_pkg_name(name)
146146

147147
rustc_env = dict(rustc_env)
148-
if "CARGO_PKG_NAME" not in rustc_env:
149-
rustc_env["CARGO_PKG_NAME"] = pkg_name
150148
if "CARGO_CRATE_NAME" not in rustc_env:
151149
rustc_env["CARGO_CRATE_NAME"] = name_to_crate_name(name_to_pkg_name(name))
152150

crate_universe/BUILD.bazel

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
22
load("@cui//:defs.bzl", "aliases", "all_crate_deps")
33
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_doc", "rust_doc_test", "rust_library", "rust_test")
4-
load("//crate_universe:version.bzl", "VERSION")
4+
load("//cargo:defs.bzl", "cargo_toml_env_vars")
5+
6+
cargo_toml_env_vars(
7+
name = "cargo_toml_env_vars",
8+
src = "Cargo.toml",
9+
)
510

611
exports_files(
712
glob([
@@ -72,7 +77,7 @@ rust_library(
7277
# for more information). Set stamp = -1 to indicate that it should respect
7378
# the value of the --stamp commandline flag.
7479
stamp = -1,
75-
version = VERSION,
80+
rustc_env_files = [":cargo_toml_env_vars"],
7681
visibility = ["//visibility:public"],
7782
deps = all_crate_deps(normal = True),
7883
)
@@ -81,7 +86,6 @@ rust_binary(
8186
name = "cargo_bazel_bin",
8287
srcs = ["src/main.rs"],
8388
edition = "2021",
84-
version = VERSION,
8589
visibility = ["//visibility:public"],
8690
deps = [":cargo_bazel"],
8791
)
@@ -156,6 +160,8 @@ rust_test(
156160
),
157161
)
158162

163+
# TODO(zbarsky): At this point version.bzl only exists to trigger `cargo_bazel_release` GH workflow.
164+
# Consider swapping that to the main repo's `version.bzl` and remove this.
159165
rust_test(
160166
name = "versions_test",
161167
srcs = ["tests/version_test.rs"],

rust/private/rustc.bzl

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,26 +140,10 @@ def _get_rustc_env(attr, toolchain, crate_name):
140140
Returns:
141141
dict: Rustc environment variables
142142
"""
143-
version = attr.version if hasattr(attr, "version") else "0.0.0"
144-
major, minor, patch = version.split(".", 2)
145-
if "-" in patch:
146-
patch, pre = patch.split("-", 1)
147-
else:
148-
pre = ""
149-
150143
result = {
151144
"CARGO_CFG_TARGET_ARCH": "" if toolchain.target_arch == None else toolchain.target_arch,
152145
"CARGO_CFG_TARGET_OS": "" if toolchain.target_os == None else toolchain.target_os,
153146
"CARGO_CRATE_NAME": crate_name,
154-
"CARGO_PKG_AUTHORS": "",
155-
"CARGO_PKG_DESCRIPTION": "",
156-
"CARGO_PKG_HOMEPAGE": "",
157-
"CARGO_PKG_NAME": attr.name,
158-
"CARGO_PKG_VERSION": version,
159-
"CARGO_PKG_VERSION_MAJOR": major,
160-
"CARGO_PKG_VERSION_MINOR": minor,
161-
"CARGO_PKG_VERSION_PATCH": patch,
162-
"CARGO_PKG_VERSION_PRE": pre,
163147
}
164148
if hasattr(attr, "_is_proc_macro_dep_enabled") and attr._is_proc_macro_dep_enabled[IsProcMacroDepEnabledInfo].enabled:
165149
is_proc_macro_dep = "0"

test/build_env/src/build.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
fn main() {
2-
println!(
3-
"cargo:rustc-env=CARGO_PKG_NAME_FROM_BUILD_SCRIPT={}",
4-
env!("CARGO_PKG_NAME")
5-
);
62
println!(
73
"cargo:rustc-env=CARGO_CRATE_NAME_FROM_BUILD_SCRIPT={}",
84
env!("CARGO_CRATE_NAME")

test/build_env/tests/cargo.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
#[test]
22
fn cargo_env_vars() {
3-
assert_eq!(env!("CARGO_PKG_NAME"), "cargo_env-vars_test");
43
assert_eq!(env!("CARGO_CRATE_NAME"), "cargo_env_vars_test");
5-
assert_eq!(
6-
env!("CARGO_PKG_NAME_FROM_BUILD_SCRIPT"),
7-
"cargo_build_script_env-vars"
8-
);
94
assert_eq!(
105
env!("CARGO_CRATE_NAME_FROM_BUILD_SCRIPT"),
116
"cargo_build_script_env_vars"
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
#[test]
22
fn cargo_env_vars() {
3-
assert_eq!(
4-
env!("CARGO_PKG_NAME"),
5-
"cargo-env-vars-custom-crate-name-test"
6-
);
73
assert_eq!(env!("CARGO_CRATE_NAME"), "custom_crate_name");
84
}

0 commit comments

Comments
 (0)