From d6145a0836e5856e5b6183bd484c4a6a3f6927a0 Mon Sep 17 00:00:00 2001 From: aldersondrive Date: Tue, 21 Jan 2020 08:11:48 -0800 Subject: [PATCH] Add remaining changes for bootstrap build on OpenBSD. I have tested these changes via a bootstrap build on OpenBSD 6.6-current (amd64 architecture). The resulting `bazel` binary appeared to work in some simple testing involving `cc_library`, `cc_binary`, `java_library`, and `java_binary`. Known issues/limitations: - Building a `java_binary`'s deploy JAR fails because the `singlejar` tool fails to build. I intend to investigate soon. - Running Bazel requires a `--host_javabase=@local_jdk//:jdk` flag. - Sandboxing is unsupported. This change, split out of the larger PR https://github.com/bazelbuild/bazel/pull/10274, is part of the OpenBSD port in https://github.com/bazelbuild/bazel/issues/10250. Most of the changes in that larger PR have already been committed via several smaller PRs (see the PRs linked from https://github.com/bazelbuild/bazel/issues/10250). This PR collects the remaining changes. Closes #10567. PiperOrigin-RevId: 290745757 --- scripts/bootstrap/buildenv.sh | 5 +++++ scripts/packages/BUILD | 5 +++-- scripts/packages/template_bin.sh | 4 ++++ src/BUILD | 20 +++++++++++++++---- src/main/cpp/blaze_util_bsd.cc | 2 +- .../build/lib/bazel/rules/java/jdk.WORKSPACE | 5 +++++ src/main/tools/BUILD | 6 ++++-- src/main/tools/jdk.BUILD | 6 ++++++ src/main/tools/process-wrapper-legacy.h | 2 +- .../bazel/remote/remote_execution_test.sh | 2 +- src/test/shell/shell_utils.sh | 2 +- src/test/shell/unittest.bash | 7 ++++++- src/tools/singlejar/diag.h | 3 ++- src/tools/singlejar/mapped_file_posix.inc | 5 +++-- src/tools/singlejar/zip_headers.h | 2 +- tools/jdk/default_java_toolchain.bzl | 5 ++++- tools/platforms/BUILD | 7 +++++++ tools/platforms/BUILD.tools | 7 +++++++ 18 files changed, 77 insertions(+), 18 deletions(-) diff --git a/scripts/bootstrap/buildenv.sh b/scripts/bootstrap/buildenv.sh index a807af4f29a6fc..5f0c0032411d9c 100755 --- a/scripts/bootstrap/buildenv.sh +++ b/scripts/bootstrap/buildenv.sh @@ -93,6 +93,11 @@ freebsd) JAVA_HOME="${JAVA_HOME:-/usr/local/openjdk8}" ;; +openbsd) + # JAVA_HOME must point to a Java installation. + JAVA_HOME="${JAVA_HOME:-/usr/local/jdk-1.8.0}" + ;; + darwin) if [[ -z "$JAVA_HOME" ]]; then JAVA_HOME="$(/usr/libexec/java_home -v ${JAVA_VERSION}+ 2> /dev/null)" \ diff --git a/scripts/packages/BUILD b/scripts/packages/BUILD index 8a8a4c74f46f8c..71d17057525774 100644 --- a/scripts/packages/BUILD +++ b/scripts/packages/BUILD @@ -1,3 +1,5 @@ +load(":self_extract_binary.bzl", "self_extract_binary") + package(default_visibility = ["//scripts/packages:__subpackages__"]) filegroup( @@ -20,6 +22,7 @@ filegroup( ":zip-bazel-exe_nojdk", ], "//src/conditions:freebsd": [], + "//src/conditions:openbsd": [], "//src/conditions:darwin": [ ":with-jdk/install.sh", ":without-jdk/install.sh", @@ -120,8 +123,6 @@ genrule( cmd = "mkdir -p $$(dirname $@); cp $< $@", ) -load(":self_extract_binary.bzl", "self_extract_binary") - [self_extract_binary( name = "%s/install.sh" % kind, flatten_resources = [ diff --git a/scripts/packages/template_bin.sh b/scripts/packages/template_bin.sh index ca97e72d3ec52f..0c27a4df64beb0 100755 --- a/scripts/packages/template_bin.sh +++ b/scripts/packages/template_bin.sh @@ -115,6 +115,10 @@ if [ -z "${JAVA_HOME-}" ]; then JAVA_HOME="/usr/local/openjdk8" BASHRC="~/.bashrc" ;; + openbsd) + JAVA_HOME="/usr/local/jdk-1.8.0" + BASHRC="~/.bashrc" + ;; darwin) JAVA_HOME="$(/usr/libexec/java_home -v ${JAVA_VERSION}+ 2> /dev/null)" || true BASHRC="~/.bash_profile" diff --git a/src/BUILD b/src/BUILD index a85a9accfd5504..ab7c6a3216daf6 100644 --- a/src/BUILD +++ b/src/BUILD @@ -6,8 +6,9 @@ load(":rule_size_test.bzl", "rule_size_test") exports_files(["jdeps_modules.golden"]) -# Keep only the first 32 chars (a hex md5sum) and no trailing newline. -md5_cmd = "set -e -o pipefail && echo $(SRCS) | sort | xargs %s | %s | head -c 32 > $@" +# We avoid using the `head` tool's `-c` option, since it does not exist on +# OpenBSD; here we use `dd` instead. +md5_cmd = "set -e -o pipefail && echo $(SRCS) | sort | xargs %s | %s | dd bs=32 count=1 > $@" # TODO(bazel-team): find a better way to handle dylib extensions. filegroup( @@ -40,6 +41,7 @@ filegroup( "//src/conditions:darwin": md5_cmd % ("/sbin/md5", "/sbin/md5"), "//src/conditions:darwin_x86_64": md5_cmd % ("/sbin/md5", "/sbin/md5"), "//src/conditions:freebsd": md5_cmd % ("/sbin/md5", "/sbin/md5"), + "//src/conditions:openbsd": md5_cmd % ("/bin/md5", "/bin/md5"), "//conditions:default": md5_cmd % ("md5sum", "md5sum"), }), ) for suffix, embedded_tools_target in { @@ -354,8 +356,8 @@ filegroup( ], }), outs = ["package" + suffix + ".zip"], - cmd = "$(location :package-bazel.sh) $@ " + ("" if embed else "''") + " $(SRCS)", - tools = ["package-bazel.sh"], + cmd = "$(location :package_bazel_on_host_platform) $@ " + ("" if embed else "''") + " $(SRCS)", + tools = [":package_bazel_on_host_platform"], ) for suffix, embed in [ ("_jdk_allmodules", True), ("_jdk_minimal", True), @@ -364,6 +366,16 @@ filegroup( ("_nojdk", True), ]] +genrule( + name = "package_bazel_on_host_platform", + srcs = ["package-bazel.sh"], + outs = ["package-bazel-on-host-platform.sh"], + cmd = select({ + "//src/conditions:openbsd": "cat $(SRCS) | sed -e 's@#!/bin/bash@#!/usr/local/bin/bash@' > $@", + "//conditions:default": "cp $(SRCS) $@", + }), +) + genrule( name = "platforms_archive", srcs = ["@platforms//:srcs"], diff --git a/src/main/cpp/blaze_util_bsd.cc b/src/main/cpp/blaze_util_bsd.cc index 7b8da01484e303..954c8f0f1bec15 100644 --- a/src/main/cpp/blaze_util_bsd.cc +++ b/src/main/cpp/blaze_util_bsd.cc @@ -128,7 +128,7 @@ string GetSelfPath(const char* argv0) { // Otherwise, if argv[0] contains a slash, then it's a relative path. Prepend // the current directory to form an absolute path. if (argv0str.length() > 0 && argv0str.find('/') != std::string::npos) { - return GetCwd() + "/" + argv0str; + return blaze_util::GetCwd() + "/" + argv0str; } // TODO(aldersondrive): Try to find the executable by inspecting the PATH. diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE index 4ece3d9c5a26ed..8a0c823d8fd712 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE @@ -44,6 +44,11 @@ bind( actual = "@local_jdk//:jni_md_header-freebsd", ) +bind( + name = "jni_md_header-openbsd", + actual = "@local_jdk//:jni_md_header-openbsd", +) + bind( name = "java", actual = "@local_jdk//:java", diff --git a/src/main/tools/BUILD b/src/main/tools/BUILD index aa29cd43debbca..cb2548c8b7125a 100644 --- a/src/main/tools/BUILD +++ b/src/main/tools/BUILD @@ -1,7 +1,7 @@ -package(default_visibility = ["//src:__subpackages__"]) - load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") +package(default_visibility = ["//src:__subpackages__"]) + cc_binary( name = "daemonize", srcs = ["daemonize.c"], @@ -67,6 +67,7 @@ cc_binary( "//src/conditions:darwin": ["dummy-sandbox.c"], "//src/conditions:darwin_x86_64": ["dummy-sandbox.c"], "//src/conditions:freebsd": ["dummy-sandbox.c"], + "//src/conditions:openbsd": ["dummy-sandbox.c"], "//src/conditions:windows": ["dummy-sandbox.c"], "//conditions:default": [ "linux-sandbox.cc", @@ -82,6 +83,7 @@ cc_binary( "//src/conditions:darwin": [], "//src/conditions:darwin_x86_64": [], "//src/conditions:freebsd": [], + "//src/conditions:openbsd": [], "//src/conditions:windows": [], "//conditions:default": [ ":logging", diff --git a/src/main/tools/jdk.BUILD b/src/main/tools/jdk.BUILD index 47e3e0054bd9dc..0cd0b36157df74 100644 --- a/src/main/tools/jdk.BUILD +++ b/src/main/tools/jdk.BUILD @@ -32,6 +32,12 @@ filegroup( deprecation = DEPRECATION_MESSAGE, ) +filegroup( + name = "jni_md_header-openbsd", + srcs = ["include/openbsd/jni_md.h"], + deprecation = DEPRECATION_MESSAGE, +) + filegroup( name = "jni_md_header-windows", srcs = ["include/win32/jni_md.h"], diff --git a/src/main/tools/process-wrapper-legacy.h b/src/main/tools/process-wrapper-legacy.h index 5cea54717544b6..943c4c1e07cbba 100644 --- a/src/main/tools/process-wrapper-legacy.h +++ b/src/main/tools/process-wrapper-legacy.h @@ -21,7 +21,7 @@ // The process-wrapper implementation that was used until and including Bazel // 0.4.5. Waits for the wrapped process to exit and then kills its process // group. Works on all POSIX operating systems (tested on Linux, macOS, -// FreeBSD). +// FreeBSD, and OpenBSD). // // Caveats: // - Killing just the process group of the spawned child means that daemons or diff --git a/src/test/shell/bazel/remote/remote_execution_test.sh b/src/test/shell/bazel/remote/remote_execution_test.sh index 6cd04261a4937d..822900f1f1bef1 100755 --- a/src/test/shell/bazel/remote/remote_execution_test.sh +++ b/src/test/shell/bazel/remote/remote_execution_test.sh @@ -97,7 +97,7 @@ EOF function test_remote_grpc_via_unix_socket() { case "$PLATFORM" in - darwin|freebsd|linux) + darwin|freebsd|linux|openbsd) ;; *) return 0 diff --git a/src/test/shell/shell_utils.sh b/src/test/shell/shell_utils.sh index 8b0491badc316d..8997d875abbb31 100755 --- a/src/test/shell/shell_utils.sh +++ b/src/test/shell/shell_utils.sh @@ -110,7 +110,7 @@ function get_real_path() { function md5_file() { if [ $# -gt 0 ]; then local result="" - if [[ ${PLATFORM} == "darwin" ]] || [[ ${PLATFORM} == "freebsd" ]]; then + if [[ ${PLATFORM} == "darwin" ]] || [[ ${PLATFORM} == "freebsd" ]] || [[ ${PLATFORM} == "openbsd" ]]; then result=$(md5 -q $@ || echo) else result=$(md5sum $@ | awk '{print $1}' || echo) diff --git a/src/test/shell/unittest.bash b/src/test/shell/unittest.bash index d87d636044bd6b..dbefe6d27da2ba 100644 --- a/src/test/shell/unittest.bash +++ b/src/test/shell/unittest.bash @@ -734,9 +734,14 @@ if [ "$UNAME" = "linux" ] || [[ "$UNAME" =~ msys_nt* ]]; then function timestamp() { echo $(($(date +%s%N)/1000000)) } +elif [[ "$UNAME" = "openbsd" ]]; then + function timestamp() { + # OpenBSD does not have %N, so Python is the best we can do. + python3 -c 'import time; print(int(round(time.time() * 1000)))' + } else function timestamp() { - # OS X and FreeBSD do not have %N so python is the best we can do + # OS X and FreeBSD do not have %N, so Python is the best we can do. python -c 'import time; print(int(round(time.time() * 1000)))' } fi diff --git a/src/tools/singlejar/diag.h b/src/tools/singlejar/diag.h index c4ac36bd4eceac..9120911678b705 100644 --- a/src/tools/singlejar/diag.h +++ b/src/tools/singlejar/diag.h @@ -19,7 +19,8 @@ * Various useful diagnostics functions from Linux err.h file, wrapped * for portability. */ -#if defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) +#if defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) || \ + defined(__OpenBSD__) #include #define diag_err(...) err(__VA_ARGS__) diff --git a/src/tools/singlejar/mapped_file_posix.inc b/src/tools/singlejar/mapped_file_posix.inc index ae29a3c5f0ed9a..d59de009649950 100644 --- a/src/tools/singlejar/mapped_file_posix.inc +++ b/src/tools/singlejar/mapped_file_posix.inc @@ -24,8 +24,9 @@ #include "src/tools/singlejar/diag.h" -// The implementation is 64-bit Linux or OSX specific. -#if !((defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)) && \ +// The implementation is specific to 64-bit Linux / OS X / BSD. +#if !((defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \ + defined(__OpenBSD__)) && \ __SIZEOF_POINTER__ == 8) #error This code for 64 bit Unix. #endif diff --git a/src/tools/singlejar/zip_headers.h b/src/tools/singlejar/zip_headers.h index c76e15beba178d..e8cd3e14fa27e2 100644 --- a/src/tools/singlejar/zip_headers.h +++ b/src/tools/singlejar/zip_headers.h @@ -27,7 +27,7 @@ #if defined(__linux__) #include -#elif defined(__FreeBSD__) +#elif defined(__FreeBSD__) || defined(__OpenBSD__) #include #elif defined(__APPLE__) || defined(_WIN32) // Hopefully OSX and Windows will keep running solely on little endian CPUs, so: diff --git a/tools/jdk/default_java_toolchain.bzl b/tools/jdk/default_java_toolchain.bzl index 09961c269a1ab5..cf4a2fff0bcdc5 100644 --- a/tools/jdk/default_java_toolchain.bzl +++ b/tools/jdk/default_java_toolchain.bzl @@ -61,7 +61,10 @@ DEFAULT_TOOLCHAIN_CONFIGURATION = { "@bazel_tools//tools/jdk:jdk_compiler_jar", ], "javac_supports_workers": 1, - "jvm_opts": JDK9_JVM_OPTS, + "jvm_opts": select({ + "@bazel_tools//src/conditions:openbsd": JDK8_JVM_OPTS, + "//conditions:default": JDK9_JVM_OPTS, + }), "misc": DEFAULT_JAVACOPTS, "singlejar": ["@bazel_tools//tools/jdk:singlejar"], "bootclasspath": ["@bazel_tools//tools/jdk:platformclasspath"], diff --git a/tools/platforms/BUILD b/tools/platforms/BUILD index aedc392d11d1f3..e372d62e425c83 100644 --- a/tools/platforms/BUILD +++ b/tools/platforms/BUILD @@ -73,6 +73,11 @@ alias( actual = "@platforms//os:freebsd", ) +alias( + name = "openbsd", + actual = "@platforms//os:openbsd", +) + alias( name = "android", actual = "@platforms//os:android", @@ -109,6 +114,7 @@ platform( os_constraints = [ "@platforms//os:osx", "@platforms//os:freebsd", + "@platforms//os:openbsd", "@platforms//os:linux", "@platforms//os:windows", ], @@ -129,6 +135,7 @@ platform( os_constraints = [ "@platforms//os:osx", "@platforms//os:freebsd", + "@platforms//os:openbsd", "@platforms//os:linux", "@platforms//os:windows", ], diff --git a/tools/platforms/BUILD.tools b/tools/platforms/BUILD.tools index f7eece8bff33bc..73011dab678f48 100644 --- a/tools/platforms/BUILD.tools +++ b/tools/platforms/BUILD.tools @@ -61,6 +61,11 @@ alias( actual = "@platforms//os:freebsd", ) +alias( + name = "openbsd", + actual = "@platforms//os:openbsd", +) + alias( name = "android", actual = "@platforms//os:android", @@ -97,6 +102,7 @@ platform( os_constraints = [ "@platforms//os:osx", "@platforms//os:freebsd", + "@platforms//os:openbsd", "@platforms//os:linux", "@platforms//os:windows", ], @@ -117,6 +123,7 @@ platform( os_constraints = [ "@platforms//os:osx", "@platforms//os:freebsd", + "@platforms//os:openbsd", "@platforms//os:linux", "@platforms//os:windows", ],