Skip to content

Commit

Permalink
Java changes
Browse files Browse the repository at this point in the history
  • Loading branch information
akrmn committed Aug 24, 2023
1 parent cb36094 commit f13b96d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 157 deletions.
16 changes: 2 additions & 14 deletions .bazelrc
Original file line number Diff line number Diff line change
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 @@ -912,7 +912,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 @@ -561,7 +562,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
144 changes: 22 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,25 @@ 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 +54,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 +83,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
16 changes: 2 additions & 14 deletions compatibility/.bazelrc
Original file line number Diff line number Diff line change
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 @@ -58,6 +58,7 @@ load(
"@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl",
"nixpkgs_cc_configure",
"nixpkgs_local_repository",
"nixpkgs_java_configure",
"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
8 changes: 4 additions & 4 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ rules_haskell_patches = [
# Remove this patch once that's available.
"@com_github_digital_asset_daml//bazel_tools:haskell-opt.patch",
]
rules_nixpkgs_version = "210d30a81cedde04b4281fd163428722278fddfb"
rules_nixpkgs_sha256 = "61b24e273821a15146f9ae7577e64b53f6aa332d5a7056abe8221ae2c346fdbd"
rules_nixpkgs_version = "0.9.0"
rules_nixpkgs_sha256 = "b01f170580f646ee3cde1ea4c117d00e561afaf3c59eda604cf09194a824ff10"
rules_nixpkgs_patches = [
]

Expand Down Expand Up @@ -119,7 +119,7 @@ def daml_deps():
http_archive(
name = "io_tweag_rules_nixpkgs",
strip_prefix = strip_prefix,
urls = ["https://github.com/tweag/rules_nixpkgs/archive/%s.tar.gz" % rules_nixpkgs_version],
urls = ["https://github.com/tweag/rules_nixpkgs/archive/v%s.tar.gz" % rules_nixpkgs_version],
sha256 = rules_nixpkgs_sha256,
patches = rules_nixpkgs_patches,
patch_args = ["-p1"],
Expand All @@ -128,7 +128,7 @@ def daml_deps():
http_archive(
name = "rules_nixpkgs_core",
strip_prefix = strip_prefix + "/core",
urls = ["https://github.com/tweag/rules_nixpkgs/archive/%s.tar.gz" % rules_nixpkgs_version],
urls = ["https://github.com/tweag/rules_nixpkgs/archive/v%s.tar.gz" % rules_nixpkgs_version],
sha256 = rules_nixpkgs_sha256,
patches = rules_nixpkgs_patches,
patch_args = ["-p2"],
Expand Down

0 comments on commit f13b96d

Please sign in to comment.