From 36a7dfade57573b657f54ec1efe1c74ddf2ac6a9 Mon Sep 17 00:00:00 2001 From: Jingwiw Date: Fri, 25 Nov 2022 22:28:57 +0800 Subject: [PATCH] fix riscv64 build error and add qemu user support --- 01-fix-invalid-jni_md-select.patch | 15 +++ 02-use-riscv64-jdk.patch | 64 ++++++++++ 03-set-default-nojdk.patch | 13 ++ bazel.spec | 24 +++- linux-bazel-path-from-getauxval.patch | 175 ++++++++++++++++++++++++++ 5 files changed, 289 insertions(+), 2 deletions(-) create mode 100644 01-fix-invalid-jni_md-select.patch create mode 100644 02-use-riscv64-jdk.patch create mode 100644 03-set-default-nojdk.patch create mode 100644 linux-bazel-path-from-getauxval.patch diff --git a/01-fix-invalid-jni_md-select.patch b/01-fix-invalid-jni_md-select.patch new file mode 100644 index 0000000..c23fa24 --- /dev/null +++ b/01-fix-invalid-jni_md-select.patch @@ -0,0 +1,15 @@ +diff --git a/tools/jdk/BUILD.tools b/tools/jdk/BUILD.tools +index 85cb7b0..50d1b4e 100755 +--- a/tools/jdk/BUILD.tools ++++ b/tools/jdk/BUILD.tools +@@ -106,8 +106,8 @@ cc_library( + "//src/conditions:linux_aarch64": ["include/linux"], + "//src/conditions:linux_ppc64le": ["include/linux"], + "//src/conditions:linux_s390x": ["include/linux"], +- "//src/conditions:linux_mips64": [":include/linux"], +- "//src/conditions:linux_riscv64": [":include/linux"], ++ "//src/conditions:linux_mips64": ["include/linux"], ++ "//src/conditions:linux_riscv64": ["include/linux"], + "//src/conditions:linux_x86_64": ["include/linux"], + "//src/conditions:darwin": ["include/darwin"], + "//src/conditions:freebsd": ["include/freebsd"], diff --git a/02-use-riscv64-jdk.patch b/02-use-riscv64-jdk.patch new file mode 100644 index 0000000..58b16a9 --- /dev/null +++ b/02-use-riscv64-jdk.patch @@ -0,0 +1,64 @@ +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 a507f04..3be7b84 100755 +--- 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 +@@ -83,6 +83,21 @@ maybe( + version = "11", + ) + ++# This must be kept in sync with the top-level WORKSPACE file. ++maybe( ++ remote_java_repository, ++ name = "remotejdk11_linux_riscv64", ++ exec_compatible_with = [ ++ "@platforms//os:linux", ++ "@platforms//cpu:riscv64", ++ ], ++ strip_prefix = "java-11-openjdk", ++ urls = [ ++ "file:///build/bazel/src/remotejdk11_linux_riscv64.tar.gz", ++ ], ++ version = "11", ++) ++ + # This must be kept in sync with the top-level WORKSPACE file. + maybe( + remote_java_repository, +diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.tmpl b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.tmpl +index 7040eae..b783308 100755 +--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.tmpl ++++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.tmpl +@@ -66,6 +66,21 @@ maybe( + version = "11", + ) + ++# This must be kept in sync with the top-level WORKSPACE file. ++maybe( ++ remote_java_repository, ++ name = "remotejdk11_linux_riscv64", ++ exec_compatible_with = [ ++ "@platforms//os:linux", ++ "@platforms//cpu:riscv64", ++ ], ++ strip_prefix = "java-11-openjdk", ++ urls = [ ++ "file:///build/bazel/src/remotejdk11_linux_riscv64.tar.gz", ++ ], ++ version = "11", ++) ++ + # This must be kept in sync with the top-level WORKSPACE file. + maybe( + remote_java_repository, +diff --git a/tools/jdk/BUILD.tools b/tools/jdk/BUILD.tools +index ecddfe0..85cb7b0 100755 +--- a/tools/jdk/BUILD.tools ++++ b/tools/jdk/BUILD.tools +@@ -447,6 +447,7 @@ _JDKS = [ + "remotejdk11_linux", + "remotejdk11_linux_ppc64le", + "remotejdk11_linux_s390x", ++ "remotejdk11_linux_riscv64", + "remotejdk15_macos", + "remotejdk15_macos_aarch64", + "remotejdk15_win", diff --git a/03-set-default-nojdk.patch b/03-set-default-nojdk.patch new file mode 100644 index 0000000..621d0a8 --- /dev/null +++ b/03-set-default-nojdk.patch @@ -0,0 +1,13 @@ +diff --git a/src/BUILD b/src/BUILD +index 1873857..3983c08 100755 +--- a/src/BUILD ++++ b/src/BUILD +@@ -376,7 +376,7 @@ genrule( + ], + ) for suffix, jdk in [ + ("-dev", "_jdk_allmodules"), +- ("", "_jdk_minimal"), ++ ("", "_nojdk"), + ("_jdk_allmodules", "_jdk_allmodules"), + ("_jdk_minimal", "_jdk_minimal"), + ("_dev_jdk", "_dev_jdk"), diff --git a/bazel.spec b/bazel.spec index 71c55f3..3e026f7 100644 --- a/bazel.spec +++ b/bazel.spec @@ -2,12 +2,15 @@ Name: bazel Version: 5.1.1 -Release: 0 +Release: 1 Summary: Correct, reproducible, and fast builds for everyone. License: Apache License 2.0 URL: http://bazel.io/ Source0: https://github.com/bazelbuild/bazel/releases/download/%{version}/bazel-%{version}-dist.zip - +Patch0: 01-fix-invalid-jni_md-select.patch +Patch1: 02-use-riscv64-jdk.patch +Patch2: 03-set-default-nojdk.patch +Patch3: linux-bazel-path-from-getauxval.patch # for folks with 'bazel' v1 package installed Conflicts: bazel Conflicts: bazel2 @@ -26,6 +29,10 @@ Correct, reproducible, and fast builds for everyone. %prep %setup -q -c -n bazel-%{version} +%patch0 -p1 +%patch1 -p1 +%patch2 -p1 +%patch3 -p1 %build find . -type f -regextype posix-extended -iregex '.*(sh|txt|py|_stub|stub_.*|bazel|get_workspace_status|protobuf_support|_so)' -exec %{__sed} -i -e '1s|^#!/usr/bin/env python$|#!/usr/bin/env python3|' "{}" \; @@ -41,6 +48,10 @@ export EXTRA_BAZEL_ARGS="${EXTRA_BAZEL_ARGS} --nokeep_state_after_build --notrac %else %endif +%ifarch aarch64 %arm riscv64 +export BAZEL_JAVAC_OPTS="-J-Xmx2g -J-Xms200m" +%endif + %ifarch s390x # increase heap size to addess s390x build failures export BAZEL_JAVAC_OPTS="-J-Xmx4g -J-Xms512m" @@ -66,6 +77,9 @@ env ./scripts/generate_bash_completion.sh --bazel=output/bazel --output=output/b %{__mkdir_p} %{buildroot}/%{_bindir} %{__mkdir_p} %{buildroot}/%{bashcompdir} %{__cp} output/bazel %{buildroot}/%{_bindir}/bazel-real +%ifarch riscv64 +%{__cp} output/bazel %{buildroot}/%{_bindir}/bazel-%{version}-linux-riscv64 +%endif %{__cp} ./scripts/packages/bazel.sh %{buildroot}/%{_bindir}/bazel %{__cp} output/bazel-complete.bash %{buildroot}/%{bashcompdir}/bazel @@ -75,10 +89,16 @@ env ./scripts/generate_bash_completion.sh --bazel=output/bazel --output=output/b %files %defattr(-,root,root) %attr(0755,root,root) %{_bindir}/bazel +%ifarch riscv64 +%attr(0755,root,root) %{_bindir}/bazel-%{version}-linux-riscv64 +%endif %attr(0755,root,root) %{_bindir}/bazel-real %attr(0755,root,root) %{bashcompdir}/bazel %changelog +* Fri Jun 09 2023 Jingwiw - 5.1.1-1 +- fix riscv64 build error and add qemu user support + * Tue Nov 8 2022 Jincheng Miao - 5.1.1-0 - Update version to 5.1.1 for TF-2.10.0 build diff --git a/linux-bazel-path-from-getauxval.patch b/linux-bazel-path-from-getauxval.patch new file mode 100644 index 0000000..a68ed34 --- /dev/null +++ b/linux-bazel-path-from-getauxval.patch @@ -0,0 +1,175 @@ +diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc +index a7cbde7..cf32dec 100755 +--- a/src/main/cpp/blaze.cc ++++ b/src/main/cpp/blaze.cc +@@ -1586,12 +1586,6 @@ int Main(int argc, const char *const *argv, WorkspaceLayout *workspace_layout, + new blaze_util::BazelLogHandler()); + blaze_util::SetLogHandler(std::move(default_handler)); + +- const string self_path = GetSelfPath(argv[0]); +- +- if (argc == 2 && strcmp(argv[1], "--version") == 0) { +- PrintVersionInfo(self_path, option_processor->GetLowercaseProductName()); +- return blaze_exit_code::SUCCESS; +- } + + string cwd = GetCanonicalCwd(); + LoggingInfo logging_info(CheckAndGetBinaryPath(cwd, argv[0]), start_time); +@@ -1622,6 +1616,12 @@ int Main(int argc, const char *const *argv, WorkspaceLayout *workspace_layout, + ParseOptionsOrDie(cwd, workspace, *option_processor, argc, argv); + StartupOptions *startup_options = option_processor->GetParsedStartupOptions(); + startup_options->MaybeLogStartupOptionWarnings(); ++ const string self_path = GetSelfPath(argv[0], *startup_options); ++ ++ if (argc == 2 && strcmp(argv[1], "--version") == 0) { ++ PrintVersionInfo(self_path, option_processor->GetLowercaseProductName()); ++ return blaze_exit_code::SUCCESS; ++ } + + SetDebugLog(startup_options->client_debug); + // If client_debug was false, this is ignored, so it's accurate. +diff --git a/src/main/cpp/blaze_util_bsd.cc b/src/main/cpp/blaze_util_bsd.cc +index a9b81df..0aebf8f 100755 +--- a/src/main/cpp/blaze_util_bsd.cc ++++ b/src/main/cpp/blaze_util_bsd.cc +@@ -46,6 +46,7 @@ + + #include "src/main/cpp/blaze_util.h" + #include "src/main/cpp/blaze_util_platform.h" ++#include "src/main/cpp/startup_options.h" + #include "src/main/cpp/util/errors.h" + #include "src/main/cpp/util/exit_code.h" + #include "src/main/cpp/util/file.h" +@@ -89,7 +90,7 @@ void WarnFilesystemType(const blaze_util::Path &output_base) { + } + } + +-string GetSelfPath(const char* argv0) { ++string GetSelfPath(const char* argv0, const StartupOptions &options) { + #if defined(__FreeBSD__) + char buffer[PATH_MAX] = {}; + auto pid = getpid(); +diff --git a/src/main/cpp/blaze_util_darwin.cc b/src/main/cpp/blaze_util_darwin.cc +index c2e8e1f..33d1a96 100755 +--- a/src/main/cpp/blaze_util_darwin.cc ++++ b/src/main/cpp/blaze_util_darwin.cc +@@ -124,7 +124,7 @@ void WarnFilesystemType(const blaze_util::Path &output_base) { + } + } + +-string GetSelfPath(const char* argv0) { ++string GetSelfPath(const char* argv0, const StartupOptions &options) { + char pathbuf[PROC_PIDPATHINFO_MAXSIZE] = {}; + int len = proc_pidpath(getpid(), pathbuf, sizeof(pathbuf)); + if (len == 0) { +diff --git a/src/main/cpp/blaze_util_linux.cc b/src/main/cpp/blaze_util_linux.cc +index 2183d43..3e75ef0 100755 +--- a/src/main/cpp/blaze_util_linux.cc ++++ b/src/main/cpp/blaze_util_linux.cc +@@ -21,6 +21,7 @@ + #include + #include + #include // strerror ++#include + #include + #include + #include +@@ -29,6 +30,7 @@ + + #include "src/main/cpp/blaze_util.h" + #include "src/main/cpp/blaze_util_platform.h" ++#include "src/main/cpp/startup_options.h" + #include "src/main/cpp/util/errors.h" + #include "src/main/cpp/util/exit_code.h" + #include "src/main/cpp/util/file.h" +@@ -82,11 +84,13 @@ void WarnFilesystemType(const blaze_util::Path &output_base) { + } + } + +-string GetSelfPath(const char* argv0) { +- // The file to which this symlink points could change contents or go missing +- // concurrent with execution of the Bazel client, so we don't eagerly resolve +- // it. +- return "/proc/self/exe"; ++string GetSelfPath(const char* argv0, const StartupOptions &options) { ++ // Sometimes /proc/self/exec isn't valid (binfmt_misc + qemu) ++ // so we provide an alternate API. e.g. Linux aarch64 running ++ // bazel-x86_64-linux ++ if (options.linux_bazel_path_from_getauxval) { ++ return reinterpret_cast(getauxval(AT_EXECFN)); ++ } + } + + uint64_t GetMillisecondsMonotonic() { +diff --git a/src/main/cpp/blaze_util_platform.h b/src/main/cpp/blaze_util_platform.h +index 42075f1..54afa8e 100755 +--- a/src/main/cpp/blaze_util_platform.h ++++ b/src/main/cpp/blaze_util_platform.h +@@ -22,6 +22,7 @@ + #include + + #include "src/main/cpp/blaze_util.h" ++#include "src/main/cpp/startup_options.h" + #include "src/main/cpp/server_process_info.h" + #include "src/main/cpp/util/path.h" + #include "src/main/cpp/util/port.h" +@@ -113,7 +114,7 @@ std::string Which(const std::string& executable); + + // Gets an absolute path to the binary being executed that is guaranteed to be + // readable. +-std::string GetSelfPath(const char* argv0); ++std::string GetSelfPath(const char* argv0, const StartupOptions &options); + + // Returns the directory Bazel can use to store output. + std::string GetOutputRoot(); +diff --git a/src/main/cpp/blaze_util_windows.cc b/src/main/cpp/blaze_util_windows.cc +index 7cd1881..e84f92c 100755 +--- a/src/main/cpp/blaze_util_windows.cc ++++ b/src/main/cpp/blaze_util_windows.cc +@@ -383,7 +383,7 @@ string GetProcessIdAsString() { + return blaze_util::ToString(GetCurrentProcessId()); + } + +-string GetSelfPath(const char* argv0) { ++string GetSelfPath(const char* argv0, const StartupOptions &options) { + WCHAR buffer[kWindowsPathBufferSize] = {0}; + if (!GetModuleFileNameW(0, buffer, kWindowsPathBufferSize)) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) +diff --git a/src/main/cpp/startup_options.cc b/src/main/cpp/startup_options.cc +index d1e2f08..5112755 100755 +--- a/src/main/cpp/startup_options.cc ++++ b/src/main/cpp/startup_options.cc +@@ -97,7 +97,8 @@ StartupOptions::StartupOptions(const string &product_name, + macos_qos_class(QOS_CLASS_UNSPECIFIED), + #endif + unlimit_coredumps(false), +- windows_enable_symlinks(false) { ++ windows_enable_symlinks(false), ++ linux_bazel_path_from_getauxval(false) { + if (blaze::IsRunningWithinTest()) { + output_root = blaze_util::MakeAbsolute(blaze::GetPathEnv("TEST_TMPDIR")); + max_idle_secs = 15; +@@ -148,6 +149,8 @@ StartupOptions::StartupOptions(const string &product_name, + RegisterNullaryStartupFlag("write_command_log", &write_command_log); + RegisterNullaryStartupFlag("windows_enable_symlinks", + &windows_enable_symlinks); ++ RegisterNullaryStartupFlag("linux_bazel_path_from_getauxval", ++ &linux_bazel_path_from_getauxval); + RegisterUnaryStartupFlag("command_port"); + RegisterUnaryStartupFlag("connect_timeout_secs"); + RegisterUnaryStartupFlag("local_startup_timeout_secs"); +diff --git a/src/main/cpp/startup_options.h b/src/main/cpp/startup_options.h +index d6c8d02..2c60ffd 100755 +--- a/src/main/cpp/startup_options.h ++++ b/src/main/cpp/startup_options.h +@@ -279,6 +279,10 @@ class StartupOptions { + // developer mode to be enabled. + bool windows_enable_symlinks; + ++ // Accomodate bazel running via Linux's binfmt_misc which ++ // defeats /proc/self/exe path-finding ++ bool linux_bazel_path_from_getauxval; ++ + protected: + // Constructor for subclasses only so that site-specific extensions of this + // class can override the product name. The product_name must be the -- Gitee