Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Switch to Bazel 5 (#12935)" #12974

Merged
merged 1 commit into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Bazel distributed cache, can be temporarily disabled by passing the following
# flag: --noremote_accept_cached
build:darwin --remote_cache=https://bazel-cache.da-ext.net
build:linux --remote_cache=https://bazel-cache.da-ext.net/ubuntu_20_04
build:darwin --remote_http_cache=https://bazel-cache.da-ext.net
build:linux --remote_http_cache=https://bazel-cache.da-ext.net/ubuntu_20_04
# Don't push local build results to the remote cache.
build --remote_upload_local_results=false
# Do still push local build results to the local disk cache.
Expand Down Expand Up @@ -62,8 +62,20 @@ test --strategy=Scalac=worker
# https://github.com/grpc/grpc/pull/13929/files
build --copt -DGRPC_BAZEL_BUILD

build --java_language_version=11
build --java_runtime_version=11
# Bazel defaults to JDK9. This makes sure we run Nix provided JDK, and
# avoid dreaded "Unrecognized VM option 'CompactStrings'" error
build:linux --javabase=@nixpkgs_java_runtime//:runtime
build:linux --host_javabase=@nixpkgs_java_runtime//:runtime
build:linux --java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
build:linux --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
build:darwin --javabase=@nixpkgs_java_runtime//:runtime
build:darwin --host_javabase=@nixpkgs_java_runtime//:runtime
build:darwin --java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
build:darwin --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
build:windows --javabase=@dadew_java_runtime//:runtime
build:windows --host_javabase=@dadew_java_runtime//:runtime
build:windows --java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
build:windows --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java11

# Do not use a distinct configuration for "host", that is, binaries used
# at build time should be the same as release binaries.
Expand Down
2 changes: 1 addition & 1 deletion BAZEL.md
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ be defined using `da_scala_test`. It is preferable to always use
Scala benchmarks based on the JMH toolkit can be defined using the
`scala_benchmark_jmh` macro provided by `rules_scala`. It supports a restricted
subset of the attributes of `da_scala_binary`, namely: `name`, `deps`, `srcs`,
`scalacopts` and `resources`.
`scalacopts`, `resources` and `resource_jars`.

The end result of building the benchmark is a Scala binary of the same name,
which can be executed with `bazel run`.
Expand Down
3 changes: 1 addition & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ load("//bazel_tools/dev_env_tool:dev_env_tool.bzl", "dadew", "dev_env_tool")
load(
"@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl",
"nixpkgs_cc_configure",
"nixpkgs_java_configure",
"nixpkgs_local_repository",
"nixpkgs_package",
"nixpkgs_python_configure",
Expand Down Expand Up @@ -575,7 +574,7 @@ load("@rules_haskell//tools:repositories.bzl", "rules_haskell_worker_dependencie
# Call this after `daml_haskell_deps` to ensure that the right `stack` is used.
rules_haskell_worker_dependencies()

load("//bazel_tools:java.bzl", "dadew_java_configure")
load("//bazel_tools:java.bzl", "dadew_java_configure", "nixpkgs_java_configure")

dadew_java_configure(
name = "dadew_java_runtime",
Expand Down
143 changes: 122 additions & 21 deletions bazel_tools/java.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,125 @@ load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_package")
load("//bazel_tools:pkg.bzl", "pkg_empty_zip")
load("//bazel_tools/dev_env_tool:dev_env_tool.bzl", "dadew_tool_home", "dadew_where")

_java_nix_file_content = """
let
pkgs = import <nixpkgs> { config = {}; overlays = []; };
in

{ attrPath
, attrSet
, filePath
}:

let
javaHome =
if attrSet == null then
pkgs.lib.attrByPath (pkgs.lib.splitString "." attrPath) null pkgs
else
pkgs.lib.attrByPath (pkgs.lib.splitString "." attrPath) null attrSet
;
javaHomePath =
if filePath == "" then
"${javaHome}"
else
"${javaHome}/${filePath}"
;
in

assert javaHome != null;

pkgs.runCommand "bazel-nixpkgs-java-runtime"
{ executable = false;
# Pointless to do this on a remote machine.
preferLocalBuild = true;
allowSubstitutes = false;
}
''
n=$out/BUILD.bazel
mkdir -p "$(dirname "$n")"

cat >>$n <<EOF
load("@rules_java//java:defs.bzl", "java_runtime")
java_runtime(
name = "runtime",
java_home = r"${javaHomePath}",
visibility = ["//visibility:public"],
)
EOF
''
"""

def nixpkgs_java_configure(
name = "nixpkgs_java_runtime",
attribute_path = None,
java_home_path = "",
repository = None,
repositories = {},
nix_file = None,
nix_file_content = "",
nix_file_deps = None,
nixopts = [],
fail_not_supported = True,
quiet = False):
"""Define a Java runtime provided by nixpkgs.

Creates a `nixpkgs_package` for a `java_runtime` instance.

Args:
name: The name-prefix for the created external repositories.
attribute_path: string, The nixpkgs attribute path for `jdk.home`.
java_home_path: optional, string, The path to `JAVA_HOME` within the package.
repository: See [`nixpkgs_package`](#nixpkgs_package-repository).
repositories: See [`nixpkgs_package`](#nixpkgs_package-repositories).
nix_file: optional, Label, Obtain the runtime from the Nix expression defined in this file. Specify only one of `nix_file` or `nix_file_content`.
nix_file_content: optional, string, Obtain the runtime from the given Nix expression. Specify only one of `nix_file` or `nix_file_content`.
nix_file_deps: See [`nixpkgs_package`](#nixpkgs_package-nix_file_deps).
nixopts: See [`nixpkgs_package`](#nixpkgs_package-nixopts).
fail_not_supported: See [`nixpkgs_package`](#nixpkgs_package-fail_not_supported).
quiet: See [`nixpkgs_package`](#nixpkgs_package-quiet).
"""
if attribute_path == None:
fail("'attribute_path' is required.", "attribute_path")

nix_expr = None
if nix_file and nix_file_content:
fail("Cannot specify both 'nix_file' and 'nix_file_content'.")
elif nix_file:
nix_expr = "import $(location {}) {{}}".format(nix_file)
nix_file_deps = depset(direct = [nix_file] + nix_file_deps).to_list()
elif nix_file_content:
nix_expr = nix_file_content
else:
nix_expr = "null"

nixopts = list(nixopts)
nixopts.extend([
"--argstr",
"attrPath",
attribute_path,
"--arg",
"attrSet",
nix_expr,
"--argstr",
"filePath",
java_home_path,
])

kwargs = dict(
repository = repository,
repositories = repositories,
nix_file_deps = nix_file_deps,
nixopts = nixopts,
fail_not_supported = fail_not_supported,
quiet = quiet,
)
java_runtime = "@%s//:runtime" % name
nixpkgs_package(
name = name,
nix_file_content = _java_nix_file_content,
**kwargs
)

def _dadew_java_configure_impl(repository_ctx):
ps = repository_ctx.which("powershell")
dadew = dadew_where(repository_ctx, ps)
Expand All @@ -19,24 +138,11 @@ java_runtime(
java_home = r"{java_home}",
visibility = ["//visibility:public"],
)
toolchain(
name = "toolchain",
toolchain = ":runtime",
toolchain_type = "@bazel_tools//tools/jdk:runtime_toolchain_type",
exec_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:windows",
],
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:windows",
],
)
""".format(
java_home = java_home.replace("\\", "/"),
))

_dadew_java_configure = repository_rule(
dadew_java_configure = repository_rule(
implementation = _dadew_java_configure_impl,
attrs = {
"dadew_path": attr.string(
Expand All @@ -53,19 +159,13 @@ Creates a `java_runtime` that uses the JDK installed by dadew.
""",
)

def dadew_java_configure(name, dadew_path):
_dadew_java_configure(
name = name,
dadew_path = dadew_path,
)
native.register_toolchains("@{}//:toolchain".format(name))

def da_java_library(
name,
deps,
srcs,
data = [],
resources = [],
resource_jars = [],
resource_strip_prefix = None,
tags = [],
visibility = None,
Expand All @@ -82,6 +182,7 @@ def da_java_library(
srcs = srcs,
data = data,
resources = resources,
resource_jars = resource_jars,
resource_strip_prefix = resource_strip_prefix,
tags = tags,
visibility = visibility,
Expand Down
57 changes: 0 additions & 57 deletions bazel_tools/nixpkgs-java-toolchain.patch

This file was deleted.

4 changes: 2 additions & 2 deletions ci/configure-bazel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ if is_windows; then
# We include an extra version at the end that we can bump manually.
CACHE_SUFFIX="$SUFFIX-v11"
CACHE_URL="$CACHE_URL/$CACHE_SUFFIX"
echo "build:windows-ci --remote_cache=https://bazel-cache.da-ext.net/$CACHE_SUFFIX" >> .bazelrc.local
echo "build:windows-ci --remote_http_cache=https://bazel-cache.da-ext.net/$CACHE_SUFFIX" >> .bazelrc.local
fi

# sets up write access to the shared remote cache if the branch is not a fork
Expand All @@ -86,5 +86,5 @@ if [[ "${IS_FORK}" = False ]]; then
echo "$GOOGLE_APPLICATION_CREDENTIALS_CONTENT" > "$GOOGLE_APPLICATION_CREDENTIALS"
unset GOOGLE_APPLICATION_CREDENTIALS_CONTENT
export GOOGLE_APPLICATION_CREDENTIALS
echo "build --remote_cache=$CACHE_URL --remote_upload_local_results=true --google_credentials=${GOOGLE_APPLICATION_CREDENTIALS}" >> .bazelrc.local
echo "build --remote_http_cache=$CACHE_URL --remote_upload_local_results=true --google_credentials=${GOOGLE_APPLICATION_CREDENTIALS}" >> .bazelrc.local
fi
6 changes: 3 additions & 3 deletions ci/patch_bazel_windows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ jobs:
variables:
cache_key: $[ dependencies.patch_bazel_pre_check.outputs['out.cache_key'] ]
should_run: $[ dependencies.patch_bazel_pre_check.outputs['out.should_run'] ]
bazel_base_version: 5.0.0
bazel_base_version: 4.2.1
pool:
vmImage: windows-2022
vmImage: windows-2019
steps:
- checkout: self
condition: eq(variables.should_run, 'true')
Expand All @@ -65,7 +65,7 @@ jobs:
BAZEL=$(mktemp)
curl -sL https://github.com/bazelbuild/bazel/releases/download/$(bazel_base_version)/bazel-$(bazel_base_version)-windows-x86_64.exe > $BAZEL
cd bazel
$BAZEL build --java_language_version=11 --java_runtime_version=11 src/main/cpp:client src:package-zip_jdk_minimal -c opt --stamp --embed_label $(bazel_base_version)-patched-$(cache_key)
$BAZEL build src/main/cpp:client src:package-zip_jdk_minimal -c opt --stamp --embed_label $(bazel_base_version)-patched-$(cache_key)
# Note (MK) For some reason, the `zip` from chocolatey seems to result in
# a “zip file structure invalid” error. I’ve tried adding msys to PATH so the Bazel
# rules pick up `zip` from msys but that broke other things. So for now
Expand Down
20 changes: 16 additions & 4 deletions compatibility/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Bazel distributed cache, can be temporarily disabled by passing the following
# flag: --noremote_accept_cached
build:darwin --remote_cache=https://bazel-cache.da-ext.net
build:linux --remote_cache=https://bazel-cache.da-ext.net/ubuntu_20_04
build:darwin --remote_http_cache=https://bazel-cache.da-ext.net
build:linux --remote_http_cache=https://bazel-cache.da-ext.net/ubuntu_20_04
# Don't push local build results to the remote cache.
build --remote_upload_local_results=false
# Do still push local build results to the local disk cache.
Expand Down Expand Up @@ -62,8 +62,20 @@ test --strategy=Scalac=worker
# https://github.com/grpc/grpc/pull/13929/files
build --copt -DGRPC_BAZEL_BUILD

build --java_language_version=11
build --java_runtime_version=11
# Bazel defaults to JDK9. This makes sure we run Nix provided JDK, and
# avoid dreaded "Unrecognized VM option 'CompactStrings'" error
build:linux --javabase=@nixpkgs_java_runtime//:runtime
build:linux --host_javabase=@nixpkgs_java_runtime//:runtime
build:linux --java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
build:linux --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
build:darwin --javabase=@nixpkgs_java_runtime//:runtime
build:darwin --host_javabase=@nixpkgs_java_runtime//:runtime
build:darwin --java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
build:darwin --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
build:windows --javabase=@dadew_java_runtime//:runtime
build:windows --host_javabase=@dadew_java_runtime//:runtime
build:windows --java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
build:windows --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java11

# Do not use a distinct configuration for "host", that is, binaries used
# at build time should be the same as release binaries.
Expand Down
3 changes: 1 addition & 2 deletions compatibility/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ load(
load(
"@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl",
"nixpkgs_cc_configure",
"nixpkgs_java_configure",
"nixpkgs_local_repository",
"nixpkgs_package",
"nixpkgs_python_configure",
Expand Down Expand Up @@ -306,7 +305,7 @@ daml_sdk_head(
if ver != "0.0.0"
]

load("@daml//bazel_tools:java.bzl", "dadew_java_configure")
load("@daml//bazel_tools:java.bzl", "dadew_java_configure", "nixpkgs_java_configure")

dadew_java_configure(
name = "dadew_java_runtime",
Expand Down
Loading