Skip to content

Commit

Permalink
Add java_current_repository rule
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Sep 15, 2022
1 parent 5d1856e commit 7dea6a3
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 0 deletions.
162 changes: 162 additions & 0 deletions src/test/shell/integration/java_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -883,4 +883,166 @@ EOF
|| fail "aspect Args do not contain both params files"
}

function test_java_current_repository() {
cat >> WORKSPACE <<'EOF'
local_repository(
name = "other_repo",
path = "other_repo",
)
EOF

cat > BUILD.bazel <<'EOF'
load("@bazel_tools//tools/java/runfiles:java_current_repository.bzl", "java_current_repository")
java_current_repository(
name = "java_current_repository",
tags = ["some_tag"],
)
EOF

mkdir -p pkg
cat > pkg/BUILD.bazel <<'EOF'
java_library(
name = "library",
srcs = ["Library.java"],
visibility = ["//visibility:public"],
deps = ["//:java_current_repository"],
)
java_binary(
name = "binary",
srcs = ["Binary.java"],
main_class = "com.example.Binary",
deps = [
":library",
"//:java_current_repository",
],
)
java_test(
name = "test",
srcs = ["Test.java"],
main_class = "com.example.Test",
use_testrunner = False,
deps = [
":library",
"//:java_current_repository",
],
)
EOF

cat > pkg/Library.java <<'EOF'
package com.example;
import static com.google.devtools.build.runfiles.RunfilesConstants.CURRENT_REPOSITORY;
public class Library {
public static void printRepositoryName() {
System.out.printf("in pkg/Library.java: '%s'%n", CURRENT_REPOSITORY);
}
}
EOF

cat > pkg/Binary.java <<'EOF'
package com.example;
import com.google.devtools.build.runfiles.RunfilesConstants;
public class Binary {
public static void main(String[] args) {
System.out.printf("in pkg/Binary.java: '%s'%n", RunfilesConstants.CURRENT_REPOSITORY);
Library.printRepositoryName();
}
}
EOF

cat > pkg/Test.java <<'EOF'
package com.example;
import com.google.devtools.build.runfiles.RunfilesConstants;
public class Test {
public static void main(String[] args) {
System.out.printf("in pkg/Test.java: '%s'%n", RunfilesConstants.CURRENT_REPOSITORY);
Library.printRepositoryName();
}
}
EOF

mkdir -p other_repo
touch other_repo/WORKSPACE

cat > other_repo/BUILD.bazel <<'EOF'
load("@bazel_tools//tools/java/runfiles:java_current_repository.bzl", "java_current_repository")
java_current_repository(
name = "java_current_repository",
)
EOF
#
mkdir -p other_repo/pkg
cat > other_repo/pkg/BUILD.bazel <<'EOF'
java_binary(
name = "binary",
srcs = ["Binary.java"],
main_class = "com.example.Binary",
deps = [
"//:java_current_repository",
"@//pkg:library",
],
)
java_test(
name = "test",
srcs = ["Test.java"],
main_class = "com.example.Test",
use_testrunner = False,
deps = [
"//:java_current_repository",
"@//pkg:library",
],
)
EOF

cat > other_repo/pkg/Binary.java <<'EOF'
package com.example;
import com.google.devtools.build.runfiles.RunfilesConstants;
public class Binary {
public static void main(String[] args) {
System.out.printf("in external/other_repo/pkg/Binary.java: '%s'%n", RunfilesConstants.CURRENT_REPOSITORY);
Library.printRepositoryName();
}
}
EOF

cat > other_repo/pkg/Test.java <<'EOF'
package com.example;
import com.google.devtools.build.runfiles.RunfilesConstants;
public class Test {
public static void main(String[] args) {
System.out.printf("in external/other_repo/pkg/Test.java: '%s'%n", RunfilesConstants.CURRENT_REPOSITORY);
Library.printRepositoryName();
}
}
EOF

bazel run //pkg:binary &>"$TEST_log" || fail "Run should succeed"
expect_log "in pkg/Binary.java: ''"
expect_log "in pkg/Library.java: ''"

bazel test --test_output=streamed //pkg:test &>"$TEST_log" || fail "Test should succeed"
expect_log "in pkg/Test.java: ''"
expect_log "in pkg/Library.java: ''"

bazel run @other_repo//pkg:binary &>"$TEST_log" || fail "Run should succeed"
expect_log "in external/other_repo/pkg/Binary.java: 'other_repo'"
expect_log "in pkg/Library.java: ''"

bazel test --test_output=streamed \
@other_repo//pkg:test &>"$TEST_log" || fail "Test should succeed"
expect_log "in external/other_repo/pkg/Test.java: 'other_repo'"
expect_log "in pkg/Library.java: ''"
}

run_suite "Java integration tests"
1 change: 1 addition & 0 deletions tools/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ filegroup(
"//tools/build_rules:bzl_srcs",
"//tools/config:common_settings.bzl",
"//tools/cpp:bzl_srcs",
"//tools/java:bzl_srcs",
"//tools/jdk:bzl_srcs",
"//tools/osx:bzl_srcs",
"//tools/python:bzl_srcs",
Expand Down
6 changes: 6 additions & 0 deletions tools/java/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ filegroup(
visibility = ["//tools:__pkg__"],
)

filegroup(
name = "bzl_srcs",
srcs = ["//tools/java/runfiles:bzl_srcs"],
visibility = ["//tools:__pkg__"],
)

genrule(
name = "copy_java_stub_template",
srcs = [
Expand Down
6 changes: 6 additions & 0 deletions tools/java/BUILD.tools
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
filegroup(
name = "bzl_srcs",
srcs = ["//tools/java/runfiles:bzl_srcs"],
visibility = ["//tools:__pkg__"],
)

exports_files(["java_stub_template.txt"])
8 changes: 8 additions & 0 deletions tools/java/runfiles/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ filegroup(
visibility = ["//tools/java:__pkg__"],
)

filegroup(
name = "bzl_srcs",
srcs = glob(["*.bzl"]),
visibility = ["//tools:__pkg__"],
)

filegroup(
name = "embedded_tools",
srcs = [
"BUILD.tools",
"RunfilesConstants.java.tpl",
"java_current_repository.bzl",
":java-srcs",
],
visibility = ["//tools/java:__pkg__"],
Expand Down
8 changes: 8 additions & 0 deletions tools/java/runfiles/BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ java_library(
],
visibility = ["//visibility:public"],
)

filegroup(
name = "bzl_srcs",
srcs = glob(["*.bzl"]),
visibility = ["//tools:__pkg__"],
)

exports_files(["RunfilesConstants.java.tpl"])
25 changes: 25 additions & 0 deletions tools/java/runfiles/RunfilesConstants.java.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Automatically generated by the java_current_repository target:
// __LABEL__

package com.google.devtools.build.runfiles;

public final class RunfilesConstants {
public static final String CURRENT_REPOSITORY = "__CURRENT_REPOSITORY__";
private RunfilesConstants() {}
}
65 changes: 65 additions & 0 deletions tools/java/runfiles/java_current_repository.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def _java_current_repository_impl(ctx):
java_file = ctx.actions.declare_file("_java_current_repository/RunfilesConstants.java")
ctx.actions.expand_template(
template = ctx.file._template,
output = java_file,
substitutions = {
"__LABEL__": str(ctx.label),
"__CURRENT_REPOSITORY__": ctx.label.workspace_name,
},
)

jar_file = ctx.actions.declare_file("_java_current_repository/runfiles_constants.jar")
java_info = java_common.compile(
ctx,
java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"].java,
# The JLS guarantees that constants are inlined. Since the generated code only contains
# constants, we can remove it from the runtime classpath. This also serves to prevent
# confusion as different repositories will compile against different versions of the
# generated class, with an essentially arbitrary one appearing on the runtime classpath.
neverlink = True,
output = jar_file,
source_files = [java_file],
)

return [
DefaultInfo(files = depset([jar_file])),
java_info,
]

_java_current_repository = rule(
attrs = {
"_template": attr.label(
default = "RunfilesConstants.java.tpl",
allow_single_file = True,
),
},
implementation = _java_current_repository_impl,
provides = [JavaInfo],
fragments = ["java"],
toolchains = [config_common.toolchain_type("@bazel_tools//tools/jdk:toolchain_type", mandatory = True)],
)

def java_current_repository(name, visibility = None, **kwargs):
if visibility:
fail("java_current_repository targets are automatically visible in all subpackages and should not be used from other repositories.", attr = "visibility")

_java_current_repository(
name = name,
visibility = [":__subpackages__"],
**kwargs
)

0 comments on commit 7dea6a3

Please sign in to comment.