Skip to content

Commit 32d1606

Browse files
fmeumcopybara-github
authored andcommitted
Add "arch" struct field to repository_os
This new field provides access to the Java "os.arch" property to repository rules, which previously had to rely on uname (Unix) or additional env variables (Windows) to detect the architecture. This also fixes a small issue in the existing implementation of repository_ctx.os.name, which should use the root locale when converting the value of the "os.name" property to lowercase. Existing sites of manual architecture detection in shipped repository rules as well as redundant calls to lower() on the value of repository_ctx.os.name are cleaned up. Fixes bazelbuild#14685 Closes bazelbuild#14738. PiperOrigin-RevId: 427147225
1 parent 8734ccf commit 32d1606

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkOS.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.google.common.collect.ImmutableMap;
1818
import com.google.devtools.build.docgen.annot.DocCategory;
1919
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
20+
import java.util.Locale;
2021
import java.util.Map;
2122
import net.starlark.java.annot.StarlarkBuiltin;
2223
import net.starlark.java.annot.StarlarkMethod;
@@ -49,8 +50,20 @@ public ImmutableMap<String, String> getEnvironmentVariables() {
4950
@StarlarkMethod(
5051
name = "name",
5152
structField = true,
52-
doc = "A string identifying the current system Bazel is running on.")
53+
doc =
54+
"A string identifying the operating system Bazel is running on (the value of the"
55+
+ " \"os.name\" Java property).")
5356
public String getName() {
54-
return System.getProperty("os.name").toLowerCase();
57+
return System.getProperty("os.name").toLowerCase(Locale.ROOT);
58+
}
59+
60+
@StarlarkMethod(
61+
name = "arch",
62+
structField = true,
63+
doc =
64+
"A string identifying the architecture Bazel is running on (the value of the \"os.arch\""
65+
+ " Java property).")
66+
public String getArch() {
67+
return System.getProperty("os.arch").toLowerCase(Locale.ROOT);
5568
}
5669
}

tools/cpp/lib_cc_configure.bzl

+11-15
Original file line numberDiff line numberDiff line change
@@ -179,38 +179,34 @@ def execute(
179179

180180
def get_cpu_value(repository_ctx):
181181
"""Compute the cpu_value based on the OS name. Doesn't %-escape the result!"""
182-
os_name = repository_ctx.os.name.lower()
182+
os_name = repository_ctx.os.name
183+
arch = repository_ctx.os.arch
183184
if os_name.startswith("mac os"):
184185
# Check if we are on x86_64 or arm64 and return the corresponding cpu value.
185-
result = repository_ctx.execute(["uname", "-m"])
186-
return "darwin" + ("_arm64" if result.stdout.strip() == "arm64" else "")
186+
return "darwin" + ("_arm64" if arch == "aarch64" else "")
187187
if os_name.find("freebsd") != -1:
188188
return "freebsd"
189189
if os_name.find("openbsd") != -1:
190190
return "openbsd"
191191
if os_name.find("windows") != -1:
192-
arch = (get_env_var(repository_ctx, "PROCESSOR_ARCHITECTURE", "", False) or
193-
get_env_var(repository_ctx, "PROCESSOR_ARCHITEW6432", "", False))
194-
if arch == "ARM64":
192+
if arch == "aarch64":
195193
return "arm64_windows"
196194
else:
197195
return "x64_windows"
198196

199-
# Use uname to figure out whether we are on x86_32 or x86_64
200-
result = repository_ctx.execute(["uname", "-m"])
201-
if result.stdout.strip() in ["power", "ppc64le", "ppc", "ppc64"]:
197+
if arch in ["power", "ppc64le", "ppc", "ppc64"]:
202198
return "ppc"
203-
if result.stdout.strip() in ["s390x"]:
199+
if arch in ["s390x"]:
204200
return "s390x"
205-
if result.stdout.strip() in ["mips64"]:
201+
if arch in ["mips64"]:
206202
return "mips64"
207-
if result.stdout.strip() in ["riscv64"]:
203+
if arch in ["riscv64"]:
208204
return "riscv64"
209-
if result.stdout.strip() in ["arm", "armv7l"]:
205+
if arch in ["arm", "armv7l"]:
210206
return "arm"
211-
if result.stdout.strip() in ["aarch64"]:
207+
if arch in ["aarch64"]:
212208
return "aarch64"
213-
return "k8" if result.stdout.strip() in ["amd64", "x86_64", "x64"] else "piii"
209+
return "k8" if arch in ["amd64", "x86_64", "x64"] else "piii"
214210

215211
def is_cc_configure_debug(repository_ctx):
216212
"""Returns True if CC_CONFIGURE_DEBUG is set to 1."""

tools/jdk/local_java_repository.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def _local_java_repository_impl(repository_ctx):
132132
"workspace(name = \"{name}\")\n".format(name = repository_ctx.name),
133133
)
134134

135-
extension = ".exe" if repository_ctx.os.name.lower().find("windows") != -1 else ""
135+
extension = ".exe" if repository_ctx.os.name.find("windows") != -1 else ""
136136
java_bin = java_home_path.get_child("bin").get_child("java" + extension)
137137

138138
if not java_bin.exists:

tools/osx/xcode_configure.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def _impl(repository_ctx):
271271
repository_ctx: The repository context.
272272
"""
273273

274-
os_name = repository_ctx.os.name.lower()
274+
os_name = repository_ctx.os.name
275275
build_contents = "package(default_visibility = ['//visibility:public'])\n\n"
276276
if (os_name.startswith("mac os")):
277277
build_contents += _darwin_build_file(repository_ctx)

0 commit comments

Comments
 (0)