Skip to content

Commit f2b09d1

Browse files
committed
Allow opting-out of the 0.0.0 crate version fallback
1 parent 8da6166 commit f2b09d1

File tree

5 files changed

+47
-18
lines changed

5 files changed

+47
-18
lines changed

cargo/private/cargo_build_script.bzl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ load("//rust:rust_common.bzl", "BuildInfo", "CrateGroupInfo", "DepInfo")
1111
# buildifier: disable=bzl-visibility
1212
load(
1313
"//rust/private:rustc.bzl",
14+
"env_vars_from_version",
1415
"get_compilation_mode_opts",
1516
"get_linker_and_args",
1617
)
@@ -407,13 +408,7 @@ def _cargo_build_script_impl(ctx):
407408
})
408409

409410
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
411+
env.update(env_vars_from_version(ctx.attr.version))
417412

418413
# Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
419414
# We hope that the linker env is sufficient for the whole cc_toolchain.

rust/private/rustc.bzl

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,40 @@ is_proc_macro_dep_enabled = rule(
129129
build_setting = config.bool(flag = True),
130130
)
131131

132-
def _get_rustc_env(attr, toolchain, crate_name):
133-
"""Gathers rustc environment variables
132+
def env_vars_from_version(version):
133+
"""Gathers rustc environment variables corresponding to the version
134134
135135
Args:
136-
attr (struct): The current target's attributes
137-
toolchain (rust_toolchain): The current target's rust toolchain context
138-
crate_name (str): The name of the crate to be compiled
136+
version (str): The version of the crate to be compiled
139137
140138
Returns:
141139
dict: Rustc environment variables
142140
"""
143-
version = attr.version if hasattr(attr, "version") else "0.0.0"
144141
major, minor, patch = version.split(".", 2)
145142
if "-" in patch:
146143
patch, pre = patch.split("-", 1)
147144
else:
148145
pre = ""
149146

147+
return {
148+
"CARGO_PKG_VERSION": version,
149+
"CARGO_PKG_VERSION_MAJOR": major,
150+
"CARGO_PKG_VERSION_MINOR": minor,
151+
"CARGO_PKG_VERSION_PATCH": patch,
152+
"CARGO_PKG_VERSION_PRE": pre,
153+
}
154+
155+
def _get_rustc_env(attr, toolchain, crate_name):
156+
"""Gathers rustc environment variables
157+
158+
Args:
159+
attr (struct): The current target's attributes
160+
toolchain (rust_toolchain): The current target's rust toolchain context
161+
crate_name (str): The name of the crate to be compiled
162+
163+
Returns:
164+
dict: Rustc environment variables
165+
"""
150166
result = {
151167
"CARGO_CFG_TARGET_ARCH": "" if toolchain.target_arch == None else toolchain.target_arch,
152168
"CARGO_CFG_TARGET_OS": "" if toolchain.target_os == None else toolchain.target_os,
@@ -155,12 +171,13 @@ def _get_rustc_env(attr, toolchain, crate_name):
155171
"CARGO_PKG_DESCRIPTION": "",
156172
"CARGO_PKG_HOMEPAGE": "",
157173
"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,
163174
}
175+
176+
version_default = None if toolchain._incompatible_do_not_inject_degenerate_version_to_rustc_env else "0.0.0"
177+
version = getattr(attr, "version", version_default)
178+
if version:
179+
result.update(env_vars_from_version(version))
180+
164181
if hasattr(attr, "_is_proc_macro_dep_enabled") and attr._is_proc_macro_dep_enabled[IsProcMacroDepEnabledInfo].enabled:
165182
is_proc_macro_dep = "0"
166183
if hasattr(attr, "_is_proc_macro_dep") and attr._is_proc_macro_dep[IsProcMacroDepInfo].is_proc_macro_dep:

rust/settings/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ load(
2727
"incompatible_change_clippy_error_format",
2828
"incompatible_change_rust_test_compilation_output_directory",
2929
"incompatible_do_not_include_data_in_compile_data",
30+
"incompatible_do_not_inject_degenerate_version_to_rustc_env",
3031
"lto",
3132
"no_std",
3233
"pipelined_compilation",
@@ -111,6 +112,8 @@ incompatible_change_rust_test_compilation_output_directory()
111112

112113
incompatible_do_not_include_data_in_compile_data()
113114

115+
incompatible_do_not_inject_degenerate_version_to_rustc_env()
116+
114117
lto()
115118

116119
no_std()

rust/settings/settings.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,15 @@ def incompatible_do_not_include_data_in_compile_data():
478478
issue = "https://github.com/bazelbuild/rules_rust/issues/2977",
479479
)
480480

481+
def incompatible_do_not_inject_degenerate_version_to_rustc_env():
482+
"""A flag to control whether we inject env vars corresponding to version 0.0.0 if version is ommitted.
483+
"""
484+
incompatible_flag(
485+
name = "incompatible_do_not_inject_degenerate_version_to_rustc_env",
486+
build_setting_default = False,
487+
issue = "https://github.com/bazelbuild/rules_rust/issues/3674",
488+
)
489+
481490
def codegen_units():
482491
"""The default value for `--codegen-units` which also affects resource allocation for rustc actions.
483492

rust/toolchain.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ def _rust_toolchain_impl(ctx):
760760
_incompatible_change_rust_test_compilation_output_directory = ctx.attr._incompatible_change_rust_test_compilation_output_directory[IncompatibleFlagInfo].enabled,
761761
_toolchain_generated_sysroot = ctx.attr._toolchain_generated_sysroot[BuildSettingInfo].value,
762762
_incompatible_do_not_include_data_in_compile_data = ctx.attr._incompatible_do_not_include_data_in_compile_data[IncompatibleFlagInfo].enabled,
763+
_incompatible_do_not_inject_degenerate_version_to_rustc_env = ctx.attr._incompatible_do_not_inject_degenerate_version_to_rustc_env[IncompatibleFlagInfo].enabled,
763764
_no_std = no_std,
764765
_codegen_units = ctx.attr._codegen_units[BuildSettingInfo].value,
765766
_experimental_use_allocator_libraries_with_mangled_symbols = ctx.attr.experimental_use_allocator_libraries_with_mangled_symbols,
@@ -986,6 +987,10 @@ rust_toolchain = rule(
986987
default = Label("//rust/settings:incompatible_do_not_include_data_in_compile_data"),
987988
doc = "Label to a boolean build setting that controls whether to include data files in compile_data.",
988989
),
990+
"_incompatible_do_not_inject_degenerate_version_to_rustc_env": attr.label(
991+
default = Label("//rust/settings:incompatible_do_not_inject_degenerate_version_to_rustc_env"),
992+
doc = "Label to a boolean build setting that controls whether to set rustc env vars based on a crate version of '0.0.0' if one is not explicitly provided. ",
993+
),
989994
"_no_std": attr.label(
990995
default = Label("//rust/settings:no_std"),
991996
),

0 commit comments

Comments
 (0)