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

Copy over runfiles library from Bazel #239

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
# Ignore jekyll build output.
/production
/.sass-cache
# Ignore MODULE.bazel.lock as this is a library project.
MODULE.bazel.lock
9 changes: 6 additions & 3 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@ REMOTE_JDK_REPOS = [(("remote_jdk" if version == "8" else "remotejdk") + version

[register_toolchains("@" + name + "_toolchain_config_repo//:all") for name in REMOTE_JDK_REPOS]

# Compatibility layer
compat = use_extension("//java:extensions.bzl", "compatibility_proxy")
use_repo(compat, "compatibility_proxy")

# Dev dependencies
bazel_dep(name = "rules_pkg", version = "0.9.1", dev_dependency = True)
bazel_dep(name = "stardoc", version = "0.7.1", dev_dependency = True)
bazel_dep(name = "rules_shell", version = "0.2.0", dev_dependency = True)

# Compatibility layer
compat = use_extension("//java:extensions.bzl", "compatibility_proxy")
use_repo(compat, "compatibility_proxy")
test_repositories = use_extension("//test:repositories.bzl", "test_repositories_ext", dev_dependency = True)
use_repo(test_repositories, "guava", "truth")
4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ rules_java_toolchains()
load("@stardoc//:setup.bzl", "stardoc_repositories")

stardoc_repositories()

load("//test:repositories.bzl", "test_repositories")

test_repositories()
5 changes: 5 additions & 0 deletions java/runfiles/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alias(
name = "runfiles",
actual = "//java/runfiles/src/main/java/com/google/devtools/build/runfiles",
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.

package com.google.devtools.build.runfiles;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotating a class {@code Fooer} with this annotation generates a class {@code
* AutoBazelRepository_Fooer} defining a {@link String} constant {@code NAME} containing the
* canonical name of the repository containing the Bazel target that compiled the annotated class.
*/
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface AutoBazelRepository {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// 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.

package com.google.devtools.build.runfiles;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedOptions;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic.Kind;

/** Processor for {@link AutoBazelRepository}. */
@SupportedAnnotationTypes("com.google.devtools.build.runfiles.AutoBazelRepository")
@SupportedOptions(AutoBazelRepositoryProcessor.BAZEL_REPOSITORY_OPTION)
public final class AutoBazelRepositoryProcessor extends AbstractProcessor {

static final String BAZEL_REPOSITORY_OPTION = "bazel.repository";

@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
annotations.stream()
.flatMap(element -> roundEnv.getElementsAnnotatedWith(element).stream())
.map(element -> (TypeElement) element)
.forEach(this::emitClass);
return false;
}

private void emitClass(TypeElement annotatedClass) {
// This option is always provided by the Java rule implementations.
if (!processingEnv.getOptions().containsKey(BAZEL_REPOSITORY_OPTION)) {
processingEnv
.getMessager()
.printMessage(
Kind.ERROR,
String.format(
"The %1$s annotation processor option is not set. To use this annotation"
+ " processor, provide the canonical repository name of the current target as"
+ " the value of the -A%1$s flag.",
BAZEL_REPOSITORY_OPTION),
annotatedClass);
return;
}
String repositoryName = processingEnv.getOptions().get(BAZEL_REPOSITORY_OPTION);
if (repositoryName == null) {
// javac translates '-Abazel.repository=' into a null value.
// https://github.com/openjdk/jdk/blob/7a49c9baa1d4ad7df90e7ca626ec48ba76881822/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java#L651
repositoryName = "";
}

// For a nested class Outer.Middle.Inner, generate a class with simple name
// AutoBazelRepository_Outer_Middle_Inner.
// Note: There can be collisions when local classes are involved, but since the definition of a
// class depends only on the containing Bazel target, this does not result in ambiguity.
Deque<String> classNameSegments = new ArrayDeque<>();
Element element = annotatedClass;
while (element instanceof TypeElement) {
classNameSegments.addFirst(element.getSimpleName().toString());
element = element.getEnclosingElement();
}
classNameSegments.addFirst("AutoBazelRepository");
String generatedClassSimpleName = String.join("_", classNameSegments);

String generatedClassPackage =
processingEnv.getElementUtils().getPackageOf(annotatedClass).getQualifiedName().toString();

String generatedClassName =
generatedClassPackage.isEmpty()
? generatedClassSimpleName
: generatedClassPackage + "." + generatedClassSimpleName;

try (PrintWriter out =
new PrintWriter(
processingEnv.getFiler().createSourceFile(generatedClassName).openWriter())) {
if (!generatedClassPackage.isEmpty()) {
// This annotation may exist on a class which is at the root package
out.printf("package %s;\n", generatedClassPackage);
}
out.printf("\n");
out.printf("class %s {\n", generatedClassSimpleName);
out.printf(" /**\n");
out.printf(" * The canonical name of the repository containing the Bazel target that\n");
out.printf(" * compiled {@link %s}.\n", annotatedClass.getQualifiedName().toString());
out.printf(" */\n");
out.printf(" static final String NAME = \"%s\";\n", repositoryName);
out.printf("\n");
out.printf(" private %s() {}\n", generatedClassSimpleName);
out.printf("}\n");
} catch (IOException e) {
processingEnv
.getMessager()
.printMessage(
Kind.ERROR,
String.format("Failed to generate %s: %s", generatedClassName, e.getMessage()),
annotatedClass);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@rules_java//java:defs.bzl", "java_library", "java_plugin")

java_library(
name = "runfiles",
srcs = [
"Runfiles.java",
"Util.java",
],
exported_plugins = [":auto_bazel_repository_processor"],
visibility = ["//java/runfiles:__pkg__"],
exports = [":auto_bazel_repository"],
)

java_library(
name = "auto_bazel_repository",
srcs = ["AutoBazelRepository.java"],
)

java_plugin(
name = "auto_bazel_repository_processor",
srcs = ["AutoBazelRepositoryProcessor.java"],
processor_class = "com.google.devtools.build.runfiles.AutoBazelRepositoryProcessor",
)
Loading