Skip to content

Commit

Permalink
Merge pull request #320 from tweag/bzlmod-java
Browse files Browse the repository at this point in the history
Separate toolchains/java tests and run with bzlmod enabled
  • Loading branch information
mergify[bot] authored Feb 13, 2023
2 parents f2c9324 + 195daaa commit 69346fe
Show file tree
Hide file tree
Showing 26 changed files with 176 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build:bzlmod --enable_bzlmod

build --host_platform=@rules_nixpkgs_core//platforms:host

# Using toolchain resolution can lead to spurious dependencies on
Expand Down
5 changes: 4 additions & 1 deletion .github/build-and-test
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ trap on_exit EXIT
if [[ ${BZLMOD_ENABLED-} = true ]]; then
declare -ra build_dirs=(
core
toolchains/java
)
declare -ra test_dirs=(
testing/core
testing/java
)
declare -ra extra_flags=(
--enable_bzlmod
--config=bzlmod
)
else
declare -ra build_dirs=(
Expand All @@ -48,6 +50,7 @@ else
toolchains/rust
toolchains/nodejs
testing/core
testing/java
)
declare -ra extra_flags=(
)
Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,8 @@ Additional arguments to forward to the underlying repository rule.
<pre>
nixpkgs_java_configure(<a href="#nixpkgs_java_configure-name">name</a>, <a href="#nixpkgs_java_configure-attribute_path">attribute_path</a>, <a href="#nixpkgs_java_configure-java_home_path">java_home_path</a>, <a href="#nixpkgs_java_configure-repository">repository</a>, <a href="#nixpkgs_java_configure-repositories">repositories</a>, <a href="#nixpkgs_java_configure-nix_file">nix_file</a>,
<a href="#nixpkgs_java_configure-nix_file_content">nix_file_content</a>, <a href="#nixpkgs_java_configure-nix_file_deps">nix_file_deps</a>, <a href="#nixpkgs_java_configure-nixopts">nixopts</a>, <a href="#nixpkgs_java_configure-fail_not_supported">fail_not_supported</a>, <a href="#nixpkgs_java_configure-quiet">quiet</a>, <a href="#nixpkgs_java_configure-toolchain">toolchain</a>,
<a href="#nixpkgs_java_configure-toolchain_name">toolchain_name</a>, <a href="#nixpkgs_java_configure-toolchain_version">toolchain_version</a>, <a href="#nixpkgs_java_configure-exec_constraints">exec_constraints</a>, <a href="#nixpkgs_java_configure-target_constraints">target_constraints</a>)
<a href="#nixpkgs_java_configure-register">register</a>, <a href="#nixpkgs_java_configure-toolchain_name">toolchain_name</a>, <a href="#nixpkgs_java_configure-toolchain_version">toolchain_version</a>, <a href="#nixpkgs_java_configure-exec_constraints">exec_constraints</a>,
<a href="#nixpkgs_java_configure-target_constraints">target_constraints</a>)
</pre>

Define a Java runtime provided by nixpkgs.
Expand Down Expand Up @@ -840,7 +841,21 @@ default is <code>False</code>

<p>

Create & register a Bazel toolchain based on the Java runtime.
Create a Bazel toolchain based on the Java runtime.

</p>
</td>
</tr>
<tr id="nixpkgs_java_configure-register">
<td><code>register</code></td>
<td>

optional.
default is <code>None</code>

<p>

Register the created toolchain. Requires `toolchain` to be `True`. Defaults to the value of `toolchain`.

</p>
</td>
Expand Down
2 changes: 2 additions & 0 deletions core/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build:bzlmod --enable_bzlmod

build --host_platform=@rules_nixpkgs_core//platforms:host

# User Configuration
Expand Down
10 changes: 10 additions & 0 deletions registry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Local Bazel Registry for rules\_nixpkgs modules

This [local Bazel registry][registry] is only intended for testing and local
development purposes. The rules\_nixpkgs repository is split into multiple
Bazel modules that may depend on each other. Unfortunately, overrides such as
`local_path_override` are only allowed in the main module, and command-line
configuration like `--override_module` is difficult to configure correctly
since it does not support relative paths. Instead, we use a local registry.

[registry]: https://bazel.build/external/registry
4 changes: 4 additions & 0 deletions registry/bazel_registry.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"mirrors": [],
"module_base_path": ".."
}
1 change: 1 addition & 0 deletions registry/modules/rules_nixpkgs_core/0.9.0/MODULE.bazel
4 changes: 4 additions & 0 deletions registry/modules/rules_nixpkgs_core/0.9.0/source.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "local_path",
"path": "core"
}
6 changes: 6 additions & 0 deletions registry/modules/rules_nixpkgs_core/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"homepage": "",
"maintainers": [],
"versions": ["0.9.0"],
"yanked_versions": []
}
1 change: 1 addition & 0 deletions testing/java/.bazelrc
Empty file added testing/java/BUILD.bazel
Empty file.
29 changes: 29 additions & 0 deletions testing/java/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module(name = "rules_nixpkgs_java_testing")

bazel_dep(name = "rules_nixpkgs_java")
local_path_override(
module_name = "rules_nixpkgs_java",
path = "../../toolchains/java",
)

bazel_dep(name = "rules_nixpkgs_core")
local_path_override(
module_name = "rules_nixpkgs_core",
path = "../../core",
)

bazel_dep(name = "bazel_skylib", version = "1.0.3")

# TODO[AH] Remove these transitive dependencies once nixpkgs_java_configure has
# become a module extension in rules_nixpkgs_java.
bazel_dep(name = "platforms", version = "0.0.4")
bazel_dep(name = "rules_java", version = "4.0.0")

java_toolchains = use_extension("@rules_java//java:extensions.bzl", "toolchains")
use_repo(java_toolchains, "remote_java_tools")

non_module_deps = use_extension("//:non_module_deps.bzl", "non_module_deps")
use_repo(non_module_deps, "nixpkgs")
use_repo(non_module_deps, "nixpkgs_java_runtime")
use_repo(non_module_deps, "nixpkgs_java_runtime_toolchain")
register_toolchains("@nixpkgs_java_runtime_toolchain//:all")
33 changes: 33 additions & 0 deletions testing/java/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
workspace(name = "rules_nixpkgs_java_testing")

local_repository(
name = "rules_nixpkgs_java",
path = "../../toolchains/java",
)

local_repository(
name = "rules_nixpkgs_core",
path = "../../core",
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "bazel_skylib",
sha256 = "f24ab666394232f834f74d19e2ff142b0af17466ea0c69a3f4c276ee75f6efce",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.4.0/bazel-skylib-1.4.0.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.0/bazel-skylib-1.4.0.tar.gz",
],
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

load(
"//tests:nixpkgs_repositories.bzl",
"nixpkgs_repositories",
)

nixpkgs_repositories(bzlmod = False)
Empty file added testing/java/WORKSPACE.bzlmod
Empty file.
1 change: 1 addition & 0 deletions testing/java/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions testing/java/nixpkgs.nix
11 changes: 11 additions & 0 deletions testing/java/non_module_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load(
"//tests:nixpkgs_repositories.bzl",
"nixpkgs_repositories",
)

def _non_module_deps_impl(ctx):
nixpkgs_repositories(bzlmod = True)

non_module_deps = module_extension(
implementation = _non_module_deps_impl,
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions testing/java/tests/nixpkgs_repositories.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_local_repository")
load("@rules_nixpkgs_java//:java.bzl", "nixpkgs_java_configure")

def nixpkgs_repositories(*, bzlmod):
nixpkgs_local_repository(
name = "nixpkgs",
nix_file = "//:nixpkgs.nix",
nix_file_deps = ["//:flake.lock"],
)

nixpkgs_java_configure(
name = "nixpkgs_java_runtime",
attribute_path = "jdk11.home",
repository = "@nixpkgs",
toolchain = True,
register = not bzlmod,
toolchain_name = "nixpkgs_java",
toolchain_version = "11",
)
6 changes: 6 additions & 0 deletions toolchains/java/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
build:bzlmod --enable_bzlmod
build:bzlmod --registry=file://%workspace%/../../registry
build:bzlmod --registry=https://bcr.bazel.build
# Skip docs directories in bzlmod mode. Stardoc is not yet bzlmod compatible.
build:bzlmod --deleted_packages=docs

build --host_platform=@rules_nixpkgs_core//platforms:host

# Using toolchain resolution can lead to spurious dependencies on
Expand Down
1 change: 1 addition & 0 deletions toolchains/java/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ module(

bazel_dep(name = "rules_nixpkgs_core", version = "0.9.0")
bazel_dep(name = "rules_java", version = "4.0.0")
bazel_dep(name = "bazel_skylib", version = "1.0.3")
19 changes: 17 additions & 2 deletions toolchains/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
<pre>
nixpkgs_java_configure(<a href="#nixpkgs_java_configure-name">name</a>, <a href="#nixpkgs_java_configure-attribute_path">attribute_path</a>, <a href="#nixpkgs_java_configure-java_home_path">java_home_path</a>, <a href="#nixpkgs_java_configure-repository">repository</a>, <a href="#nixpkgs_java_configure-repositories">repositories</a>, <a href="#nixpkgs_java_configure-nix_file">nix_file</a>,
<a href="#nixpkgs_java_configure-nix_file_content">nix_file_content</a>, <a href="#nixpkgs_java_configure-nix_file_deps">nix_file_deps</a>, <a href="#nixpkgs_java_configure-nixopts">nixopts</a>, <a href="#nixpkgs_java_configure-fail_not_supported">fail_not_supported</a>, <a href="#nixpkgs_java_configure-quiet">quiet</a>, <a href="#nixpkgs_java_configure-toolchain">toolchain</a>,
<a href="#nixpkgs_java_configure-toolchain_name">toolchain_name</a>, <a href="#nixpkgs_java_configure-toolchain_version">toolchain_version</a>, <a href="#nixpkgs_java_configure-exec_constraints">exec_constraints</a>, <a href="#nixpkgs_java_configure-target_constraints">target_constraints</a>)
<a href="#nixpkgs_java_configure-register">register</a>, <a href="#nixpkgs_java_configure-toolchain_name">toolchain_name</a>, <a href="#nixpkgs_java_configure-toolchain_version">toolchain_version</a>, <a href="#nixpkgs_java_configure-exec_constraints">exec_constraints</a>,
<a href="#nixpkgs_java_configure-target_constraints">target_constraints</a>)
</pre>

Define a Java runtime provided by nixpkgs.
Expand Down Expand Up @@ -244,7 +245,21 @@ default is <code>False</code>

<p>

Create & register a Bazel toolchain based on the Java runtime.
Create a Bazel toolchain based on the Java runtime.

</p>
</td>
</tr>
<tr id="nixpkgs_java_configure-register">
<td><code>register</code></td>
<td>

optional.
default is <code>None</code>

<p>

Register the created toolchain. Requires `toolchain` to be `True`. Defaults to the value of `toolchain`.

</p>
</td>
Expand Down
8 changes: 0 additions & 8 deletions toolchains/java/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,3 @@ load("@rules_nixpkgs_docs//:dependencies_2.bzl", "docs_dependencies_2")
docs_dependencies_2()

### end generic dependencies for rendering documentation

### dependencies for tests

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

### end dependencies for tests
9 changes: 7 additions & 2 deletions toolchains/java/java.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def nixpkgs_java_configure(
fail_not_supported = True,
quiet = False,
toolchain = False,
register = None,
toolchain_name = None,
toolchain_version = None,
exec_constraints = None,
Expand Down Expand Up @@ -181,7 +182,8 @@ def nixpkgs_java_configure(
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).
toolchain: Create & register a Bazel toolchain based on the Java runtime.
toolchain: Create a Bazel toolchain based on the Java runtime.
register: Register the created toolchain. Requires `toolchain` to be `True`. Defaults to the value of `toolchain`.
toolchain_name: The name of the toolchain that can be used in --java_runtime_version.
toolchain_version: The version of the toolchain that can be used in --java_runtime_version.
exec_constraints: Constraints for the execution platform.
Expand Down Expand Up @@ -233,4 +235,7 @@ def nixpkgs_java_configure(
exec_constraints = exec_constraints,
target_constraints = target_constraints,
)
native.register_toolchains("@{}_toolchain//:all".format(name))
if register or register == None:
native.register_toolchains("@{}_toolchain//:all".format(name))
elif register:
fail("toolchain must be True if register is set to True.")

0 comments on commit 69346fe

Please sign in to comment.