Skip to content

Commit

Permalink
Switch to Bazel 5 (digital-asset#12935)
Browse files Browse the repository at this point in the history
* Switch to Bazel 5

changelog_begin
changelog_end

* .

* .

changelog_begin
changelog_end
  • Loading branch information
cocreature authored Feb 16, 2022
1 parent 3d54d14 commit 4c0118d
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 190 deletions.
20 changes: 4 additions & 16 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_http_cache=https://bazel-cache.da-ext.net
build:linux --remote_http_cache=https://bazel-cache.da-ext.net/ubuntu_20_04
build:darwin --remote_cache=https://bazel-cache.da-ext.net
build:linux --remote_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,20 +62,8 @@ test --strategy=Scalac=worker
# https://github.com/grpc/grpc/pull/13929/files
build --copt -DGRPC_BAZEL_BUILD

# 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
build --java_language_version=11
build --java_runtime_version=11

# 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`, `resources` and `resource_jars`.
`scalacopts` and `resources`.
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: 2 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ 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 @@ -574,7 +575,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", "nixpkgs_java_configure")
load("//bazel_tools:java.bzl", "dadew_java_configure")

dadew_java_configure(
name = "dadew_java_runtime",
Expand Down
143 changes: 21 additions & 122 deletions bazel_tools/java.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,125 +8,6 @@ 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 @@ -138,11 +19,24 @@ 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 @@ -159,13 +53,19 @@ 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 @@ -182,7 +82,6 @@ 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: 57 additions & 0 deletions bazel_tools/nixpkgs-java-toolchain.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl
index 061170e..10dcac6 100644
--- a/nixpkgs/nixpkgs.bzl
+++ b/nixpkgs/nixpkgs.bzl
@@ -925,6 +925,40 @@ pkgs.runCommand "bazel-nixpkgs-java-runtime"
''
"""

+def _nixpkgs_java_toolchain_impl(repository_ctx):
+ cpu = get_cpu_value(repository_ctx)
+ exec_constraints, target_constraints = ensure_constraints(repository_ctx)
+
+ repository_ctx.file(
+ "BUILD.bazel",
+ executable = False,
+ content = """\
+package(default_visibility = ["//visibility:public"])
+
+toolchain(
+ name = "java-toolchain-{cpu}",
+ toolchain = "@{runtime}//:runtime",
+ toolchain_type = "@bazel_tools//tools/jdk:runtime_toolchain_type",
+ exec_compatible_with = {exec_constraints},
+ target_compatible_with = {target_constraints},
+)
+""".format(
+ runtime = repository_ctx.attr.runtime,
+ cpu = cpu,
+ exec_constraints = exec_constraints,
+ target_constraints = target_constraints,
+ ),
+ )
+
+_nixpkgs_java_toolchain = repository_rule(
+ _nixpkgs_java_toolchain_impl,
+ attrs = {
+ "runtime": attr.string(),
+ "exec_constraints": attr.string_list(),
+ "target_constraints": attr.string_list(),
+ },
+)
+
def nixpkgs_java_configure(
name = "nixpkgs_java_runtime",
attribute_path = None,
@@ -1014,6 +1048,11 @@ def nixpkgs_java_configure(
fail_not_supported = fail_not_supported,
quiet = quiet,
)
+ _nixpkgs_java_toolchain(
+ name = "{}_toolchain".format(name),
+ runtime = name,
+ )
+ native.register_toolchains("@{}_toolchain//:all".format(name))

def _nixpkgs_python_toolchain_impl(repository_ctx):
exec_constraints, target_constraints = ensure_constraints(repository_ctx)
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_http_cache=https://bazel-cache.da-ext.net/$CACHE_SUFFIX" >> .bazelrc.local
echo "build:windows-ci --remote_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_http_cache=$CACHE_URL --remote_upload_local_results=true --google_credentials=${GOOGLE_APPLICATION_CREDENTIALS}" >> .bazelrc.local
echo "build --remote_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: 4.2.1
bazel_base_version: 5.0.0
pool:
vmImage: windows-2019
vmImage: windows-2022
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 src/main/cpp:client src:package-zip_jdk_minimal -c opt --stamp --embed_label $(bazel_base_version)-patched-$(cache_key)
$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)
# 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: 4 additions & 16 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_http_cache=https://bazel-cache.da-ext.net
build:linux --remote_http_cache=https://bazel-cache.da-ext.net/ubuntu_20_04
build:darwin --remote_cache=https://bazel-cache.da-ext.net
build:linux --remote_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,20 +62,8 @@ test --strategy=Scalac=worker
# https://github.com/grpc/grpc/pull/13929/files
build --copt -DGRPC_BAZEL_BUILD

# 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
build --java_language_version=11
build --java_runtime_version=11

# 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: 2 additions & 1 deletion compatibility/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ 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 @@ -305,7 +306,7 @@ daml_sdk_head(
if ver != "0.0.0"
]

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

dadew_java_configure(
name = "dadew_java_runtime",
Expand Down
Loading

0 comments on commit 4c0118d

Please sign in to comment.