aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar carmi <carmi@google.com>2018-03-21 14:57:55 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-03-21 14:59:16 -0700
commitb12892e0ca50a436f1e629ec4391ee4068623dc6 (patch)
tree0f3b04839d6412526a286fee8c6c239e3b16bd39 /src
parent28a999cff60fa5a8c9ca045ab2b202d83a0e0583 (diff)
A tiny binary to print all rules in a package, after BUILD macro expansion.
RELNOTES: None PiperOrigin-RevId: 189973158
Diffstat (limited to 'src')
-rw-r--r--src/BUILD1
-rw-r--r--src/test/shell/bazel/BUILD1
-rw-r--r--src/tools/package_printer/java/com/google/devtools/build/packageprinter/BUILD39
-rw-r--r--src/tools/package_printer/java/com/google/devtools/build/packageprinter/BazelPackagePrinter.java82
-rw-r--r--src/tools/package_printer/java/com/google/devtools/build/packageprinter/Lib.java47
-rwxr-xr-xsrc/tools/package_printer/java/com/google/devtools/build/packageprinter/bazel_package_printer_test.sh43
6 files changed, 213 insertions, 0 deletions
diff --git a/src/BUILD b/src/BUILD
index 9ed84a4569..50cdd5f477 100644
--- a/src/BUILD
+++ b/src/BUILD
@@ -350,6 +350,7 @@ filegroup(
"//src/tools/android/java/com/google/devtools/build/android:srcs",
"//src/tools/launcher:srcs",
"//src/tools/runfiles:srcs",
+ "//src/tools/package_printer/java/com/google/devtools/build/packageprinter:srcs",
"//src/tools/skylark/java/com/google/devtools/skylark/skylint:srcs",
"//src/tools/skylark/javatests/com/google/devtools/skylark/skylint:srcs",
"//src/tools/xcode/actoolwrapper:srcs",
diff --git a/src/test/shell/bazel/BUILD b/src/test/shell/bazel/BUILD
index d271c43627..baf149d601 100644
--- a/src/test/shell/bazel/BUILD
+++ b/src/test/shell/bazel/BUILD
@@ -49,6 +49,7 @@ filegroup(
}),
visibility = [
"//src/test/shell:__subpackages__",
+ "//src/tools/package_printer/java/com/google/devtools/build/packageprinter:__pkg__",
],
)
diff --git a/src/tools/package_printer/java/com/google/devtools/build/packageprinter/BUILD b/src/tools/package_printer/java/com/google/devtools/build/packageprinter/BUILD
new file mode 100644
index 0000000000..ffada11a63
--- /dev/null
+++ b/src/tools/package_printer/java/com/google/devtools/build/packageprinter/BUILD
@@ -0,0 +1,39 @@
+licenses(["notice"]) # Apache 2.0
+
+filegroup(
+ name = "srcs",
+ srcs = glob(["**"]),
+ visibility = ["//src:__pkg__"],
+)
+
+java_binary(
+ name = "BazelPackagePrinter",
+ srcs = ["BazelPackagePrinter.java"],
+ deps = [
+ ":Lib",
+ "//src/main/java/com/google/devtools/build/lib:events",
+ "//src/main/java/com/google/devtools/build/lib/skyframe/packages",
+ "//src/main/java/com/google/devtools/build/lib/vfs",
+ "//third_party:guava",
+ ],
+)
+
+java_library(
+ name = "Lib",
+ srcs = ["Lib.java"],
+ deps = [
+ "//src/main/java/com/google/devtools/build/lib:packages",
+ "//src/main/java/com/google/devtools/build/lib/cmdline",
+ "//src/main/java/com/google/devtools/build/lib/skyframe/packages",
+ ],
+)
+
+sh_test(
+ name = "bazel_package_printer_test",
+ size = "medium",
+ srcs = ["bazel_package_printer_test.sh"],
+ data = [
+ ":BazelPackagePrinter",
+ "//src/test/shell/bazel:test-deps",
+ ],
+)
diff --git a/src/tools/package_printer/java/com/google/devtools/build/packageprinter/BazelPackagePrinter.java b/src/tools/package_printer/java/com/google/devtools/build/packageprinter/BazelPackagePrinter.java
new file mode 100644
index 0000000000..b9a1c55cf4
--- /dev/null
+++ b/src/tools/package_printer/java/com/google/devtools/build/packageprinter/BazelPackagePrinter.java
@@ -0,0 +1,82 @@
+// Copyright 2018 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.packageprinter;
+
+import com.google.common.eventbus.EventBus;
+import com.google.devtools.build.lib.events.PrintingEventHandler;
+import com.google.devtools.build.lib.events.Reporter;
+import com.google.devtools.build.lib.skyframe.packages.BazelPackageLoader;
+import com.google.devtools.build.lib.skyframe.packages.PackageLoader;
+import com.google.devtools.build.lib.vfs.FileSystem;
+import com.google.devtools.build.lib.vfs.JavaIoFileSystem;
+import com.google.devtools.build.lib.vfs.Path;
+import java.nio.file.Paths;
+
+/**
+ * PackagePrinter prints the list of rules in a Bazel package.
+ *
+ * <p>That is, it prints the names of the rules in a BUILD file after macro expansion.
+ */
+public class BazelPackagePrinter {
+ public static void main(String[] args) throws Exception {
+ if (!verifyArgs(args)) {
+ System.err.println(
+ "Usage example: BazelPackagePrinter "
+ + "--workspace_root=. "
+ + "--install_base=$(bazel info install_base) "
+ + "--output_base=$(bazel info output_base) "
+ + "package/to/print");
+ System.exit(1);
+ }
+ FileSystem fileSystem = new JavaIoFileSystem();
+ PackageLoader loader =
+ newPackageLoader(
+ fileSystem.getPath(getAbsPathFlag(args[0], "--workspace_root=")),
+ fileSystem.getPath(getAbsPathFlag(args[1], "--install_base=")),
+ fileSystem.getPath(getAbsPathFlag(args[2], "--output_base=")));
+
+ Lib.printPackageContents(loader, args[3]);
+ }
+
+ /** newPackageLoader returns a new PackageLoader. */
+ static PackageLoader newPackageLoader(Path workspaceDir, Path installBase, Path outputBase) {
+ return BazelPackageLoader.builder(workspaceDir, installBase, outputBase)
+ .useDefaultSkylarkSemantics()
+ .setReporter(new Reporter(new EventBus(), PrintingEventHandler.ERRORS_TO_STDERR))
+ .setLegacyGlobbingThreads(400)
+ .setSkyframeThreads(300)
+ .build();
+ }
+
+ static boolean verifyArgs(String[] args) {
+ if (args.length != 4) {
+ return false;
+ }
+ if (!args[0].startsWith("--workspace_root=")) {
+ return false;
+ }
+ if (!args[1].startsWith("--install_base=")) {
+ return false;
+ }
+ if (!args[2].startsWith("--output_base=")) {
+ return false;
+ }
+ return true;
+ }
+
+ static String getAbsPathFlag(String flagName, String arg) {
+ return Paths.get(flagName.substring(arg.length())).toAbsolutePath().toString();
+ }
+}
diff --git a/src/tools/package_printer/java/com/google/devtools/build/packageprinter/Lib.java b/src/tools/package_printer/java/com/google/devtools/build/packageprinter/Lib.java
new file mode 100644
index 0000000000..516082789c
--- /dev/null
+++ b/src/tools/package_printer/java/com/google/devtools/build/packageprinter/Lib.java
@@ -0,0 +1,47 @@
+// Copyright 2018 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.packageprinter;
+
+import com.google.devtools.build.lib.cmdline.PackageIdentifier;
+import com.google.devtools.build.lib.packages.NoSuchPackageException;
+import com.google.devtools.build.lib.packages.Package;
+import com.google.devtools.build.lib.packages.Rule;
+import com.google.devtools.build.lib.packages.Target;
+import com.google.devtools.build.lib.skyframe.packages.PackageLoader;
+import java.util.logging.Logger;
+
+/**
+ * Lib prints the list of rules in a Bazel package.
+ *
+ * <p>That is, it prints the names of the rules in a BUILD file after macro expansion.
+ */
+public class Lib {
+ private static final Logger logger = Logger.getLogger(Lib.class.getName());
+
+ public static void printPackageContents(PackageLoader loader, String packageName)
+ throws NoSuchPackageException, InterruptedException {
+ logger.info("Start of 'load'");
+
+ Package pkg = loader.loadPackage(PackageIdentifier.createInMainRepo(packageName));
+
+ for (Target target : pkg.getTargets().values()) {
+ if (target instanceof Rule) {
+ System.out.println(((Rule) target).getLabel());
+ }
+ }
+
+ logger.info("End of 'load'");
+ }
+}
diff --git a/src/tools/package_printer/java/com/google/devtools/build/packageprinter/bazel_package_printer_test.sh b/src/tools/package_printer/java/com/google/devtools/build/packageprinter/bazel_package_printer_test.sh
new file mode 100755
index 0000000000..babecfed30
--- /dev/null
+++ b/src/tools/package_printer/java/com/google/devtools/build/packageprinter/bazel_package_printer_test.sh
@@ -0,0 +1,43 @@
+#!/bin/bash
+#
+# Copyright 2018 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.
+
+# Set up
+source "src/test/shell/integration_test_setup.sh" \
+ || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
+
+#### TESTS #############################################################
+
+function test_basic() {
+ rm -rf peach
+ mkdir -p peach
+ cat > peach/BUILD <<EOF
+sh_library(name='brighton', deps=[':harken'])
+sh_library(name='harken')
+EOF
+ touch WORKSPACE
+
+ bazel info > /dev/null # unpack and create install and output roots.
+ ${BAZEL_RUNFILES}/src/tools/package_printer/java/com/google/devtools/build/packageprinter/BazelPackagePrinter --workspace_root=. --install_base=$(bazel info install_base) --output_base=$(bazel info output_base) peach > $TEST_log
+
+ expect_log "//peach:brighton"
+ expect_log "//peach:harken"
+}
+
+function tear_down() {
+ bazel shutdown
+}
+
+run_suite "BazelPackagePrinter tests"