From 94261203955f8683dd26e3d0233cb45329e4545f Mon Sep 17 00:00:00 2001 From: Googler Date: Sat, 29 Jul 2017 02:15:04 +0200 Subject: Remove old intellij aspect code bundled with Bazel PiperOrigin-RevId: 163538636 --- src/BUILD | 1 - src/main/protobuf/BUILD | 2 - src/main/protobuf/intellij_ide_info.proto | 181 ----- src/main/protobuf/package_manifest.proto | 47 -- .../java/com/google/devtools/build/android/BUILD | 1 - .../ideinfo/ArtifactLocationConverterTest.java | 106 --- .../google/devtools/build/android/ideinfo/BUILD | 52 -- .../build/android/ideinfo/JarFilterTest.java | 255 ------- .../build/android/ideinfo/PackageParserTest.java | 281 -------- .../build/lib/analysis/mock/BazelAnalysisMock.java | 10 - src/test/shell/integration/BUILD | 18 - src/test/shell/integration/ide_info_generation.sh | 319 --------- .../java/com/google/devtools/build/android/BUILD | 2 - .../android/ideinfo/ArtifactLocationConverter.java | 69 -- .../ideinfo/ArtifactLocationListConverter.java | 45 -- .../google/devtools/build/android/ideinfo/BUILD | 80 --- .../devtools/build/android/ideinfo/BUILD.tools | 23 - .../devtools/build/android/ideinfo/JarFilter.java | 543 --------------- .../build/android/ideinfo/PackageParser.java | 243 ------- .../android/ideinfo/PackageParserIoProvider.java | 47 -- tools/BUILD | 2 - tools/android/BUILD.tools | 10 - tools/ide/BUILD | 28 - tools/ide/BUILD.tools | 0 tools/ide/intellij_info.bzl | 32 - tools/ide/intellij_info_impl.bzl | 731 --------------------- 26 files changed, 3128 deletions(-) delete mode 100644 src/main/protobuf/intellij_ide_info.proto delete mode 100644 src/main/protobuf/package_manifest.proto delete mode 100644 src/test/java/com/google/devtools/build/android/ideinfo/ArtifactLocationConverterTest.java delete mode 100644 src/test/java/com/google/devtools/build/android/ideinfo/BUILD delete mode 100644 src/test/java/com/google/devtools/build/android/ideinfo/JarFilterTest.java delete mode 100644 src/test/java/com/google/devtools/build/android/ideinfo/PackageParserTest.java delete mode 100755 src/test/shell/integration/ide_info_generation.sh delete mode 100644 src/tools/android/java/com/google/devtools/build/android/ideinfo/ArtifactLocationConverter.java delete mode 100644 src/tools/android/java/com/google/devtools/build/android/ideinfo/ArtifactLocationListConverter.java delete mode 100644 src/tools/android/java/com/google/devtools/build/android/ideinfo/BUILD delete mode 100644 src/tools/android/java/com/google/devtools/build/android/ideinfo/BUILD.tools delete mode 100644 src/tools/android/java/com/google/devtools/build/android/ideinfo/JarFilter.java delete mode 100644 src/tools/android/java/com/google/devtools/build/android/ideinfo/PackageParser.java delete mode 100644 src/tools/android/java/com/google/devtools/build/android/ideinfo/PackageParserIoProvider.java delete mode 100644 tools/ide/BUILD delete mode 100644 tools/ide/BUILD.tools delete mode 100644 tools/ide/intellij_info.bzl delete mode 100644 tools/ide/intellij_info_impl.bzl diff --git a/src/BUILD b/src/BUILD index 041d6903d3..15b7502910 100644 --- a/src/BUILD +++ b/src/BUILD @@ -143,7 +143,6 @@ py_binary( "//third_party/py/six:srcs", "//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper:srcs", "//src/tools/android/java/com/google/devtools/build/android:embedded_tools", - "//src/tools/android/java/com/google/devtools/build/android/ideinfo:embedded_tools", "//src/tools/android/java/com/google/devtools/build/android/idlclass:embedded_tools", "//src/tools/android/java/com/google/devtools/build/android/dexer:embedded_tools", "//src/tools/android/java/com/google/devtools/build/android/ziputils:embedded_tools", diff --git a/src/main/protobuf/BUILD b/src/main/protobuf/BUILD index 349880bdb8..14ce83b71d 100644 --- a/src/main/protobuf/BUILD +++ b/src/main/protobuf/BUILD @@ -14,10 +14,8 @@ FILES = [ "crosstool_config", "deps", "extra_actions_base", - "intellij_ide_info", "invocation_policy", "java_compilation", - "package_manifest", "plmerge", "test_status", "worker_protocol", diff --git a/src/main/protobuf/intellij_ide_info.proto b/src/main/protobuf/intellij_ide_info.proto deleted file mode 100644 index abc1d6709b..0000000000 --- a/src/main/protobuf/intellij_ide_info.proto +++ /dev/null @@ -1,181 +0,0 @@ -// Copyright 2015 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. - -syntax = "proto3"; - -package blaze; - - -option java_package = "com.google.devtools.intellij.ideinfo"; - -message ArtifactLocation { - string relative_path = 2; - bool is_source = 3; - - // path from the execution root to the actual root: - // exec_root + root_execution_path_fragment + relative_path = absolute_path - string root_execution_path_fragment = 4; - // whether this artifact comes from an external repository (bazel only) - bool is_external = 5; - - // The contents of relative_path and root_execution_path_fragment have changed - // for external workspaces. This is a temporary field to distinguish between - // the two versions. - bool is_new_external_version = 6; -} - -message LibraryArtifact { - ArtifactLocation jar = 1; - ArtifactLocation interface_jar = 2; - ArtifactLocation source_jar = 3; -} - -message JavaIdeInfo { - repeated LibraryArtifact jars = 1; - repeated LibraryArtifact generated_jars = 2; - ArtifactLocation package_manifest = 3; - repeated ArtifactLocation sources = 4; - ArtifactLocation jdeps = 5; - LibraryArtifact filtered_gen_jar = 6; - string main_class = 7; -} - -message CIdeInfo { - repeated ArtifactLocation source = 1; - - repeated string transitive_include_directory = 3; - repeated string transitive_quote_include_directory = 4; - repeated string transitive_define = 5; - repeated string transitive_system_include_directory = 6; - - repeated string target_copt = 7; - repeated string target_define = 8; - repeated string target_include = 9; - repeated ArtifactLocation header = 10; - repeated ArtifactLocation textual_header = 11; -} - -message AndroidIdeInfo { - repeated ArtifactLocation resources = 1; - ArtifactLocation apk = 3; - repeated ArtifactLocation dependency_apk = 4; - ArtifactLocation manifest = 5; - string java_package = 7; - bool has_idl_sources = 8; - LibraryArtifact idl_jar = 9; - bool generate_resource_class = 10; - string legacy_resources = 11; - LibraryArtifact resource_jar = 12; - string idl_import_root = 13; -} - -message AndroidSdkIdeInfo { - ArtifactLocation android_jar = 1; -} - -message PyIdeInfo { - repeated ArtifactLocation sources = 1; -} - -message CToolchainIdeInfo { - string target_name = 1; - repeated string base_compiler_option = 2; - repeated string cpp_option = 3; - repeated string c_option = 4; - string preprocessor_executable = 5; - string cpp_executable = 6; - repeated string link_option = 7; - repeated string built_in_include_directory = 8; - repeated string unfiltered_compiler_option = 9; -} - -message ProtoLibraryLegacyJavaIdeInfo { - enum ApiFlavor { - NONE = 0; - IMMUTABLE = 1; - MUTABLE = 2; - BOTH = 3; - } - - int32 api_version = 1; - ApiFlavor api_flavor = 2; - repeated LibraryArtifact jars1 = 3; - repeated LibraryArtifact jars_mutable = 4; - repeated LibraryArtifact jars_immutable = 5; -} - -message TestInfo { - string size = 1; -} - -message JavaToolchainIdeInfo { - string source_version = 1; - string target_version = 2; - ArtifactLocation javac_jar = 3; -} - -message TargetKey { - string label = 1; - repeated string aspect_ids = 3; -} - -message Dependency { - enum DependencyType { - COMPILE_TIME = 0; - RUNTIME = 1; - } - - TargetKey target = 1; - DependencyType dependency_type = 2; -} - -message TargetIdeInfo { - string label = 1 [deprecated = true]; - repeated string dependencies = 4 [deprecated = true]; - - // kind is one of {JAVA,ANDROID}_{LIBRARY,BINARY,TEST} and JAVA_IMPORT - JavaIdeInfo java_ide_info = 7; - AndroidIdeInfo android_ide_info = 8; - - repeated string tags = 9; - repeated string runtime_deps = 10 [deprecated = true]; - - ArtifactLocation build_file_artifact_location = 11; - - CIdeInfo c_ide_info = 12; - CToolchainIdeInfo c_toolchain_ide_info = 13; - - string kind_string = 14; - - TestInfo test_info = 15; - - ProtoLibraryLegacyJavaIdeInfo proto_library_legacy_java_ide_info = 16; - JavaToolchainIdeInfo java_toolchain_ide_info = 17; - - PyIdeInfo py_ide_info = 18; - - TargetKey key = 19; - - repeated Dependency deps = 20; - - reserved 21; - - AndroidSdkIdeInfo android_sdk_ide_info = 22; - - reserved 23; - - repeated string features = 24; - - // Next available: 25 -} diff --git a/src/main/protobuf/package_manifest.proto b/src/main/protobuf/package_manifest.proto deleted file mode 100644 index d5106a2bad..0000000000 --- a/src/main/protobuf/package_manifest.proto +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2015 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. - -syntax = "proto3"; - -package blaze; - - -option java_package = "com.google.devtools.build.lib.ideinfo.androidstudio"; - - -// TODO(bazel-team): support proto => proto dependencies and remove this duplication -message ArtifactLocation { - string relative_path = 2; - bool is_source = 3; - - // path from the execution root to the actual root: - // exec_root + root_execution_path_fragment + relative_path = absolute_path - string root_execution_path_fragment = 4; - // whether this artifact comes from an external repository (bazel only) - bool is_external = 5; - - // The contents of relative_path and root_execution_path_fragment have changed - // for external workspaces. This is a temporary field to distinguish between - // the two versions. - bool is_new_external_version = 6; -} - -message JavaSourcePackage { - string package_string = 2; - ArtifactLocation artifact_location = 3; -} - -message PackageManifest { - repeated JavaSourcePackage sources = 1; -} diff --git a/src/test/java/com/google/devtools/build/android/BUILD b/src/test/java/com/google/devtools/build/android/BUILD index 9c66a43b2a..77bb8dfa07 100644 --- a/src/test/java/com/google/devtools/build/android/BUILD +++ b/src/test/java/com/google/devtools/build/android/BUILD @@ -1,7 +1,6 @@ filegroup( name = "srcs", srcs = glob(["**"]) + [ - "//src/test/java/com/google/devtools/build/android/ideinfo:srcs", "//src/test/java/com/google/devtools/build/android/idlclass:srcs", "//src/test/java/com/google/devtools/build/android/dexer:srcs", "//src/test/java/com/google/devtools/build/android/junctions:srcs", diff --git a/src/test/java/com/google/devtools/build/android/ideinfo/ArtifactLocationConverterTest.java b/src/test/java/com/google/devtools/build/android/ideinfo/ArtifactLocationConverterTest.java deleted file mode 100644 index 9fac92c48c..0000000000 --- a/src/test/java/com/google/devtools/build/android/ideinfo/ArtifactLocationConverterTest.java +++ /dev/null @@ -1,106 +0,0 @@ -// Copyright 2016 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.android.ideinfo; - -import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.fail; - -import com.google.common.base.Joiner; -import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.ArtifactLocation; -import com.google.devtools.common.options.OptionsParsingException; -import java.nio.file.Paths; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests {@link ArtifactLocationConverter}. - */ -@RunWith(JUnit4.class) -public class ArtifactLocationConverterTest { - - private ArtifactLocationConverter converter; - - @Before - public final void init() throws Exception { - converter = new ArtifactLocationConverter(); - } - - @Test - public void testConverterSourceArtifact() throws Exception { - ArtifactLocation parsed = converter.convert( - Joiner.on(',').join("", "test.java") - ); - assertThat(parsed) - .isEqualTo( - ArtifactLocation.newBuilder() - .setRelativePath(Paths.get("test.java").toString()) - .setIsSource(true) - .build()); - } - - @Test - public void testConverterDerivedArtifact() throws Exception { - ArtifactLocation parsed = converter.convert( - Joiner.on(',').join("bin", "java/com/test.java") - ); - assertThat(parsed) - .isEqualTo( - ArtifactLocation.newBuilder() - .setRootExecutionPathFragment(Paths.get("bin").toString()) - .setRelativePath(Paths.get("java/com/test.java").toString()) - .setIsSource(false) - .build()); - } - - @Test - public void testConverterExternal() throws Exception { - ArtifactLocation externalArtifact = - converter.convert(Joiner.on(',').join("", "test.java", "1")); - assertThat(externalArtifact) - .isEqualTo( - ArtifactLocation.newBuilder() - .setRelativePath(Paths.get("test.java").toString()) - .setIsSource(true) - .setIsExternal(true) - .build()); - ArtifactLocation nonExternalArtifact = - converter.convert(Joiner.on(',').join("", "test.java", "0")); - assertThat(nonExternalArtifact) - .isEqualTo( - ArtifactLocation.newBuilder() - .setRelativePath(Paths.get("test.java").toString()) - .setIsSource(true) - .setIsExternal(false) - .build()); - } - - @Test - public void testInvalidFormatFails() throws Exception { - assertFails("/root", ArtifactLocationConverter.INVALID_FORMAT); - assertFails("/root,exec,rel,extra", ArtifactLocationConverter.INVALID_FORMAT); - } - - private void assertFails(String input, String expectedError) { - try { - new ArtifactLocationConverter().convert(input); - fail(); - } catch (OptionsParsingException e) { - assertThat(e).hasMessage(expectedError); - } - } -} - diff --git a/src/test/java/com/google/devtools/build/android/ideinfo/BUILD b/src/test/java/com/google/devtools/build/android/ideinfo/BUILD deleted file mode 100644 index 924ec5f28b..0000000000 --- a/src/test/java/com/google/devtools/build/android/ideinfo/BUILD +++ /dev/null @@ -1,52 +0,0 @@ -filegroup( - name = "srcs", - srcs = glob(["**"]), - visibility = ["//src/test/java/com/google/devtools/build/android:__pkg__"], -) - -java_test( - name = "JarFilterTest", - size = "small", - srcs = ["JarFilterTest.java"], - deps = [ - "//src/main/java/com/google/devtools/common/options", - "//src/main/protobuf:package_manifest_java_proto", - "//src/tools/android/java/com/google/devtools/build/android/ideinfo:jar_filter_lib", - "//third_party:guava", - "//third_party:jsr305", - "//third_party:junit4", - "//third_party:truth", - "//third_party/protobuf:protobuf_java", - ], -) - -java_test( - name = "PackageParserTest", - size = "small", - srcs = ["PackageParserTest.java"], - deps = [ - "//src/main/java/com/google/devtools/common/options", - "//src/main/protobuf:package_manifest_java_proto", - "//src/tools/android/java/com/google/devtools/build/android/ideinfo:package_parser_lib", - "//third_party:guava", - "//third_party:jsr305", - "//third_party:junit4", - "//third_party:truth", - "//third_party/protobuf:protobuf_java", - ], -) - -java_test( - name = "ArtifactLocationConverterTest", - size = "small", - srcs = ["ArtifactLocationConverterTest.java"], - deps = [ - "//src/main/java/com/google/devtools/common/options", - "//src/main/protobuf:package_manifest_java_proto", - "//src/tools/android/java/com/google/devtools/build/android/ideinfo:package_parser_lib", - "//third_party:guava", - "//third_party:junit4", - "//third_party:truth", - "//third_party/protobuf:protobuf_java", - ], -) diff --git a/src/test/java/com/google/devtools/build/android/ideinfo/JarFilterTest.java b/src/test/java/com/google/devtools/build/android/ideinfo/JarFilterTest.java deleted file mode 100644 index 2bb779f088..0000000000 --- a/src/test/java/com/google/devtools/build/android/ideinfo/JarFilterTest.java +++ /dev/null @@ -1,255 +0,0 @@ -// Copyright 2015 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.android.ideinfo; - -import static com.google.common.truth.Truth.assertThat; -import static java.nio.charset.StandardCharsets.UTF_8; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; -import com.google.common.io.Files; -import com.google.devtools.build.android.ideinfo.JarFilter.JarFilterOptions; -import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.ArtifactLocation; -import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.JavaSourcePackage; -import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.PackageManifest; -import java.io.File; -import java.io.FileOutputStream; -import java.util.Enumeration; -import java.util.List; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; -import java.util.zip.ZipOutputStream; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit tests for {@link JarFilter} */ -@RunWith(JUnit4.class) -public class JarFilterTest { - - @Rule public TemporaryFolder folder = new TemporaryFolder(); - - @Test - public void testFilterMethod() throws Exception { - List prefixes = - ImmutableList.of("com/google/foo/Foo", "com/google/bar/Bar", "com/google/baz/Baz"); - assertThat(JarFilter.shouldKeepClass(prefixes, "com/google/foo/Foo.class")).isTrue(); - assertThat(JarFilter.shouldKeepClass(prefixes, "com/google/foo/Foo$Inner.class")).isTrue(); - assertThat(JarFilter.shouldKeepClass(prefixes, "com/google/bar/Bar.class")).isTrue(); - assertThat(JarFilter.shouldKeepClass(prefixes, "com/google/foo/Foo/NotFoo.class")).isFalse(); - assertThat(JarFilter.shouldKeepClass(prefixes, "wrong/com/google/foo/Foo.class")).isFalse(); - } - - @Test - public void legacyIntegrationTest() throws Exception { - PackageManifest packageManifest = - PackageManifest.newBuilder() - .addSources( - JavaSourcePackage.newBuilder() - .setArtifactLocation( - ArtifactLocation.newBuilder() - .setIsSource(true) - .setRelativePath("com/google/foo/Foo.java")) - .setPackageString("com.google.foo")) - .addSources( - JavaSourcePackage.newBuilder() - .setArtifactLocation( - ArtifactLocation.newBuilder() - .setIsSource(true) - .setRelativePath("com/google/bar/Bar.java")) - .setPackageString("com.google.bar")) - .addSources( - JavaSourcePackage.newBuilder() - .setArtifactLocation( - ArtifactLocation.newBuilder() - .setIsSource(true) - .setRelativePath("some/path/Test.java")) - .setPackageString("com.google.test")) - .build(); - assertThat(JarFilter.parsePackageManifest(packageManifest)) - .containsExactly("com/google/foo/Foo", "com/google/bar/Bar", "com/google/test/Test"); - File manifest = folder.newFile("foo.manifest"); - try (FileOutputStream outputStream = new FileOutputStream(manifest)) { - packageManifest.writeTo(outputStream); - } - - File filterJar = folder.newFile("foo.jar"); - try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(filterJar))) { - zo.putNextEntry(new ZipEntry("com/google/foo/Foo.class")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("com/google/foo/Foo$Inner.class")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("com/google/bar/Bar.class")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("com/google/test/Test.class")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("com/google/foo/Foo2.class")); - zo.closeEntry(); - } - - File outputJar = folder.newFile("foo-filtered-gen.jar"); - - String[] args = - new String[] { - "--jar", - filterJar.getPath(), - "--output", - outputJar.getPath(), - "--manifest", - manifest.getPath() - }; - JarFilter.JarFilterOptions options = JarFilter.parseArgs(args); - JarFilter.main(options); - - List filteredJarNames = Lists.newArrayList(); - try (ZipFile zipFile = new ZipFile(outputJar)) { - Enumeration entries = zipFile.entries(); - while (entries.hasMoreElements()) { - ZipEntry zipEntry = entries.nextElement(); - filteredJarNames.add(zipEntry.getName()); - } - } - - assertThat(filteredJarNames) - .containsExactly( - "com/google/foo/Foo.class", - "com/google/foo/Foo$Inner.class", - "com/google/bar/Bar.class", - "com/google/test/Test.class"); - } - - @Test - public void fullIntegrationTest() throws Exception { - File fooJava = folder.newFile("Foo.java"); - Files.write("package com.google.foo; class Foo { class Inner {} }".getBytes(UTF_8), fooJava); - - File barJava = folder.newFile("Bar.java"); - Files.write("package com.google.foo.bar; class Bar {}".getBytes(UTF_8), barJava); - - File srcJar = folder.newFile("gen.srcjar"); - try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(srcJar))) { - zo.putNextEntry(new ZipEntry("com/google/foo/gen/Gen.java")); - zo.write("package gen; class Gen {}".getBytes(UTF_8)); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("com/google/foo/gen/Gen2.java")); - zo.write("package gen; class Gen2 {}".getBytes(UTF_8)); - zo.closeEntry(); - } - - File src3Jar = folder.newFile("gen3.srcjar"); - try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(src3Jar))) { - zo.putNextEntry(new ZipEntry("com/google/foo/gen/Gen3.java")); - zo.write("package gen; class Gen3 {}".getBytes(UTF_8)); - zo.closeEntry(); - } - - File filterJar = folder.newFile("foo.jar"); - try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(filterJar))) { - zo.putNextEntry(new ZipEntry("com/google/foo/Foo.class")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("com/google/foo/Foo$Inner.class")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("com/google/foo/bar/Bar.class")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("gen/Gen.class")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("gen/Gen2.class")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("gen/Gen3.class")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("com/google/foo/Foo2.class")); - zo.closeEntry(); - } - File filterSrcJar = folder.newFile("foo-src.jar"); - try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(filterSrcJar))) { - zo.putNextEntry(new ZipEntry("com/google/foo/Foo.java")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("com/google/foo/bar/Bar.java")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("gen/Gen.java")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("gen/Gen2.java")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("gen/Gen3.java")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("com/google/foo/Foo2.java")); - zo.closeEntry(); - zo.putNextEntry(new ZipEntry("com/google/foo/bar/Bar2.java")); - zo.closeEntry(); - } - - File filteredJar = folder.newFile("foo-filtered-gen.jar"); - File filteredSourceJar = folder.newFile("foo-filtered-gen-src.jar"); - - String[] args = - new String[] { - "--keep_java_file", - fooJava.getPath(), - "--keep_java_file", - barJava.getPath(), - "--keep_source_jar", - srcJar.getPath(), - "--keep_source_jar", - src3Jar.getPath(), - "--filter_jar", - filterJar.getPath(), - "--filter_source_jar", - filterSrcJar.getPath(), - "--filtered_jar", - filteredJar.getPath(), - "--filtered_source_jar", - filteredSourceJar.getPath() - }; - JarFilterOptions options = JarFilter.parseArgs(args); - JarFilter.main(options); - - List filteredJarNames = Lists.newArrayList(); - try (ZipFile zipFile = new ZipFile(filteredJar)) { - Enumeration entries = zipFile.entries(); - while (entries.hasMoreElements()) { - ZipEntry zipEntry = entries.nextElement(); - filteredJarNames.add(zipEntry.getName()); - } - } - - List filteredSourceJarNames = Lists.newArrayList(); - try (ZipFile zipFile = new ZipFile(filteredSourceJar)) { - Enumeration entries = zipFile.entries(); - while (entries.hasMoreElements()) { - ZipEntry zipEntry = entries.nextElement(); - filteredSourceJarNames.add(zipEntry.getName()); - } - } - - assertThat(filteredJarNames) - .containsExactly( - "com/google/foo/Foo.class", - "com/google/foo/Foo$Inner.class", - "com/google/foo/bar/Bar.class", - "gen/Gen.class", - "gen/Gen2.class", - "gen/Gen3.class"); - - assertThat(filteredSourceJarNames) - .containsExactly( - "com/google/foo/Foo.java", - "com/google/foo/bar/Bar.java", - "gen/Gen.java", - "gen/Gen2.java", - "gen/Gen3.java"); - } -} diff --git a/src/test/java/com/google/devtools/build/android/ideinfo/PackageParserTest.java b/src/test/java/com/google/devtools/build/android/ideinfo/PackageParserTest.java deleted file mode 100644 index e775ca36c7..0000000000 --- a/src/test/java/com/google/devtools/build/android/ideinfo/PackageParserTest.java +++ /dev/null @@ -1,281 +0,0 @@ -// Copyright 2015 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.android.ideinfo; - -import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.fail; - -import com.google.common.base.Joiner; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.ArtifactLocation; -import com.google.protobuf.MessageLite; -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.StringWriter; -import java.io.UnsupportedEncodingException; -import java.nio.charset.StandardCharsets; -import java.nio.file.InvalidPathException; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.List; -import java.util.Map; -import javax.annotation.Nonnull; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Unit tests for {@link PackageParser} - */ -@RunWith(JUnit4.class) -public class PackageParserTest { - - private static class MockPackageParserIoProvider extends PackageParserIoProvider { - private final Map sources = Maps.newHashMap(); - private final List sourceLocations = Lists.newArrayList(); - private StringWriter writer = new StringWriter(); - - public MockPackageParserIoProvider addSource(ArtifactLocation source, String javaSrc) { - try { - Path path = Paths.get(source.getRootExecutionPathFragment(), source.getRelativePath()); - sources.put(path, new ByteArrayInputStream(javaSrc.getBytes("UTF-8"))); - sourceLocations.add(source); - - } catch (UnsupportedEncodingException | InvalidPathException e) { - fail(e.getMessage()); - } - return this; - } - - public void reset() { - sources.clear(); - sourceLocations.clear(); - writer = new StringWriter(); - } - - public List getSourceLocations() { - return Lists.newArrayList(sourceLocations); - } - - @Nonnull - @Override - public BufferedReader getReader(Path file) throws IOException { - InputStream input = sources.get(file); - return new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)); - } - - @Override - public void writeProto(@Nonnull MessageLite message, @Nonnull Path file) throws IOException { - writer.write(message.toString()); - } - } - - private static final ArtifactLocation DUMMY_SOURCE_ARTIFACT = - ArtifactLocation.newBuilder() - .setRelativePath("java/com/google/Foo.java") - .setIsSource(true) - .build(); - - private static final ArtifactLocation DUMMY_DERIVED_ARTIFACT = - ArtifactLocation.newBuilder() - .setRootExecutionPathFragment("bin") - .setRelativePath("java/com/google/Bla.java") - .setIsSource(false) - .build(); - - private static final ArtifactLocation DUMMY_SCALA_SOURCE_ARTIFACT = - ArtifactLocation.newBuilder() - .setRelativePath("scala/com/google/Foo.scala") - .setIsSource(true) - .build(); - - private static final ArtifactLocation DUMMY_SCALA_DERIVED_ARTIFACT = - ArtifactLocation.newBuilder() - .setRootExecutionPathFragment("bin") - .setRelativePath("scala/com/google/Bla.scala") - .setIsSource(false) - .build(); - - private MockPackageParserIoProvider mockIoProvider; - private PackageParser parser; - - @Before - public void setUp() { - mockIoProvider = new MockPackageParserIoProvider(); - parser = new PackageParser(mockIoProvider); - } - - private Map parsePackageStrings() throws Exception { - List sources = mockIoProvider.getSourceLocations(); - return parser.parsePackageStrings(sources); - } - - @Test - public void testParseCommandLineArguments() throws Exception { - String[] args = new String[] { - "--output_manifest", - "/tmp/out.manifest", - "--sources", - Joiner.on(':').join( - ",java/com/google/Foo.java", - "bin/out,java/com/google/Bla.java") - }; - PackageParser.PackageParserOptions options = PackageParser.parseArgs(args); - assertThat(options.outputManifest.toString()) - .isEqualTo(Paths.get("/tmp/out.manifest").toString()); - assertThat(options.sources).hasSize(2); - assertThat(options.sources.get(0)) - .isEqualTo( - ArtifactLocation.newBuilder() - .setRelativePath(Paths.get("java/com/google/Foo.java").toString()) - .setIsSource(true) - .build()); - assertThat(options.sources.get(1)) - .isEqualTo( - ArtifactLocation.newBuilder() - .setRootExecutionPathFragment(Paths.get("bin/out").toString()) - .setRelativePath(Paths.get("java/com/google/Bla.java").toString()) - .setIsSource(false) - .build()); - } - - @Test - public void testReadNoSources() throws Exception { - Map map = parsePackageStrings(); - assertThat(map).isEmpty(); - } - - @Test - public void testSingleRead() throws Exception { - mockIoProvider - .addSource( - DUMMY_SOURCE_ARTIFACT, - "package com.google;\n public class Bla {}\""); - Map map = parsePackageStrings(); - assertThat(map).hasSize(1); - assertThat(map).containsEntry( - DUMMY_SOURCE_ARTIFACT, - "com.google"); - } - - @Test - public void testMultiRead() throws Exception { - mockIoProvider - .addSource(DUMMY_SOURCE_ARTIFACT, "package com.test;\n public class Foo {}\"") - .addSource(DUMMY_DERIVED_ARTIFACT, "package com.other;\n public class Bla {}\""); - Map map = parsePackageStrings(); - assertThat(map).hasSize(2); - assertThat(map).containsEntry(DUMMY_SOURCE_ARTIFACT, "com.test"); - assertThat(map).containsEntry(DUMMY_DERIVED_ARTIFACT, "com.other"); - } - - @Test - public void testReadSomeInvalid() throws Exception { - mockIoProvider - .addSource(DUMMY_SOURCE_ARTIFACT, "package %com.test;\n public class Foo {}\"") - .addSource(DUMMY_DERIVED_ARTIFACT, "package com.other;\n public class Bla {}\""); - Map map = parsePackageStrings(); - assertThat(map).hasSize(1); - assertThat(map).containsEntry(DUMMY_DERIVED_ARTIFACT, "com.other"); - } - - @Test - public void testReadAllInvalid() throws Exception { - mockIoProvider - .addSource(DUMMY_SOURCE_ARTIFACT, "#package com.test;\n public class Foo {}\"") - .addSource(DUMMY_DERIVED_ARTIFACT, "package %com.other\n public class Bla {}\""); - Map map = parsePackageStrings(); - assertThat(map).isEmpty(); - } - - @Test - public void testReadScala() throws Exception { - mockIoProvider - .addSource(DUMMY_SCALA_SOURCE_ARTIFACT, "package com.test\n class Foo {}\"") - .addSource(DUMMY_SCALA_DERIVED_ARTIFACT, "package com.other {}\n object Bla {}\""); - Map map = parsePackageStrings(); - assertThat(map).containsEntry(DUMMY_SCALA_SOURCE_ARTIFACT, "com.test"); - assertThat(map).containsEntry(DUMMY_SCALA_DERIVED_ARTIFACT, "com.other"); - } - - @Test - public void testWriteEmptyMap() throws Exception { - parser.writeManifest( - Maps. newHashMap(), - Paths.get("/java/com/google/test.manifest")); - assertThat(mockIoProvider.writer.toString()).isEmpty(); - } - - @Test - public void testWriteMap() throws Exception { - Map map = ImmutableMap.of( - DUMMY_SOURCE_ARTIFACT, - "com.google", - DUMMY_DERIVED_ARTIFACT, - "com.other" - ); - parser.writeManifest(map, Paths.get("/java/com/google/test.manifest")); - - String writtenString = mockIoProvider.writer.toString(); - assertThat(writtenString).contains(String.format( - "relative_path: \"%s\"", - DUMMY_SOURCE_ARTIFACT.getRelativePath())); - assertThat(writtenString).contains("package_string: \"com.google\""); - - assertThat(writtenString).contains(String.format( - "root_execution_path_fragment: \"%s\"", - DUMMY_DERIVED_ARTIFACT.getRootExecutionPathFragment())); - assertThat(writtenString).contains(String.format( - "relative_path: \"%s\"", - DUMMY_DERIVED_ARTIFACT.getRelativePath())); - assertThat(writtenString).contains("package_string: \"com.other\""); - } - - @Test - public void testHandlesOldFormat() throws Exception { - String[] args = new String[] { - "--output_manifest", - "/tmp/out.manifest", - "--sources", - Joiner.on(':').join( - ",java/com/google/Foo.java,/usr/local/google/code", - "bin,java/com/google/Bla.java,/usr/local/_tmp/code/bin" - )}; - PackageParser.PackageParserOptions options = PackageParser.parseArgs(args); - assertThat(options.outputManifest.toString()) - .isEqualTo(Paths.get("/tmp/out.manifest").toString()); - assertThat(options.sources).hasSize(2); - assertThat(options.sources.get(0)) - .isEqualTo( - ArtifactLocation.newBuilder() - .setRelativePath(Paths.get("java/com/google/Foo.java").toString()) - .setIsSource(true) - .build()); - assertThat(options.sources.get(1)) - .isEqualTo( - ArtifactLocation.newBuilder() - .setRootExecutionPathFragment(Paths.get("bin").toString()) - .setRelativePath(Paths.get("java/com/google/Bla.java").toString()) - .setIsSource(false) - .build()); - } - -} diff --git a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java index a8aaa9735d..55cc5e2045 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java @@ -211,16 +211,6 @@ public final class BazelAnalysisMock extends AnalysisMock { .add("sh_binary(name = 'strip_resources', srcs = ['empty.sh'])") .add("sh_binary(name = 'build_incremental_dexmanifest', srcs = ['empty.sh'])") .add("sh_binary(name = 'incremental_install', srcs = ['empty.sh'])") - .add("java_binary(name = 'JarFilter',") - .add(" runtime_deps = [ ':JarFilter_import'],") - .add(" main_class = 'com.google.devtools.build.android.ideinfo.JarFilter')") - .add("java_import(name = 'JarFilter_import',") - .add(" jars = [ 'jar_filter_deploy.jar' ])") - .add("java_binary(name = 'PackageParser',") - .add(" runtime_deps = [ ':PackageParser_import'],") - .add(" main_class = 'com.google.devtools.build.android.ideinfo.PackageParser')") - .add("java_import(name = 'PackageParser_import',") - .add(" jars = [ 'package_parser_deploy.jar' ])") .add("java_binary(name = 'IdlClass',") .add(" runtime_deps = [ ':idlclass_import' ],") .add(" main_class = 'com.google.devtools.build.android.idlclass.IdlClass')") diff --git a/src/test/shell/integration/BUILD b/src/test/shell/integration/BUILD index 857eab618d..edafc4ef5c 100644 --- a/src/test/shell/integration/BUILD +++ b/src/test/shell/integration/BUILD @@ -162,24 +162,6 @@ sh_test( data = [":test-deps"], ) -sh_test( - name = "ide_info_generation_skylark", - size = "large", - srcs = ["ide_info_generation.sh"], - args = [ - "@bazel_tools//tools/ide:intellij_info.bzl%intellij_info_aspect", - "intellij-info-text", - "intellij-info.txt", - "intellij-info-text", - "intellij-info.txt", - "intellij-resolve", - ], - data = [ - ":test-deps", - "//src/tools/android/java/com/google/devtools/build/android/ideinfo:PackageParser", - ], -) - sh_test( name = "skylark_flag_test", size = "small", diff --git a/src/test/shell/integration/ide_info_generation.sh b/src/test/shell/integration/ide_info_generation.sh deleted file mode 100755 index e2c116078b..0000000000 --- a/src/test/shell/integration/ide_info_generation.sh +++ /dev/null @@ -1,319 +0,0 @@ -#!/bin/bash -# -# Copyright 2016 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. -# -# Integration tests for IDE build info generation. - -ASPECT=$1 -BINARY_OUTPUT_GROUP=$2 -BINARY_OUTPUT=$3 -TEXT_OUTPUT_GROUP=$4 -TEXT_OUTPUT=$5 -RESOLVE_OUTPUT_GROUP=$6 - -# Load the test setup defined in the parent directory -CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -source "${CURRENT_DIR}/../integration_test_setup.sh" \ - || { echo "integration_test_setup.sh not found!" >&2; exit 1; } - -add_to_bazelrc "build --noshow_progress" - -function test_ide_build_file_generation() { - mkdir -p com/google/example/simple - cat > com/google/example/simple/Simple.java < com/google/example/complex/Complex.java < com/google/example/BUILD < com/google/example/simple/Simple.java < com/google/example/complex/Complex.java < com/google/example/BUILD < $TEST_log \ - || fail "Expected success" - SIMPLE_BUILD="${PRODUCT_NAME}-bin/com/google/example/simple.$BINARY_OUTPUT" - [ -e $SIMPLE_BUILD ] || fail "$SIMPLE_BUILD not found" - COMPLEX_BUILD="${PRODUCT_NAME}-bin/com/google/example/complex.$BINARY_OUTPUT" - [ -e $COMPLEX_BUILD ] || fail "$COMPLEX_BUILD not found" - - expect_log '^Build artifacts:' - expect_log "^>>>.*/com/google/example/complex.$BINARY_OUTPUT" - expect_log "^>>>.*/com/google/example/simple.$BINARY_OUTPUT" - - # second build; test that up-to-date artifacts are output. - bazel build //com/google/example:complex \ - --aspects $ASPECT --output_groups "$BINARY_OUTPUT_GROUP" \ - --experimental_show_artifacts 2> $TEST_log \ - || fail "Expected success" - expect_log '^Build artifacts:' - expect_log "^>>>.*/com/google/example/complex.$BINARY_OUTPUT" - expect_log "^>>>.*/com/google/example/simple.$BINARY_OUTPUT" -} - -function test_ide_resolve_output_group() { - mkdir -p com/google/example/simple - cat > com/google/example/simple/Simple.java < com/google/example/complex/Complex.java < com/google/example/BUILD < com/google/example/Test.java < com/google/example/BUILD < com/google/example/simple/Simple.java < com/google/example/complex/Complex.java < com/google/example/BUILD < com/google/example/simple/Simple.java < com/google/example/complex/Complex.java < com/google/example/BUILD < { - private static final Splitter SPLITTER = Splitter.on(','); - private static final PathConverter pathConverter = new PathConverter(); - static final String INVALID_FORMAT = - "Expected format rootExecutionPathFragment,relPath,isExternal"; - - @Override - public ArtifactLocation convert(String input) throws OptionsParsingException { - Iterator values = SPLITTER.split(input).iterator(); - try { - Path rootExecutionPathFragment = pathConverter.convert(values.next()); - Path relPath = pathConverter.convert(values.next()); - - // Last value is optional to maintain compatibility with the native aspect - boolean isExternal = false; - if (values.hasNext()) { - isExternal = values.next().equals("1"); - } - - if (values.hasNext()) { - throw new OptionsParsingException(INVALID_FORMAT); - } - - boolean isSource = rootExecutionPathFragment.toString().isEmpty(); - return ArtifactLocation.newBuilder() - .setRootExecutionPathFragment(rootExecutionPathFragment.toString()) - .setRelativePath(relPath.toString()) - .setIsSource(isSource) - .setIsExternal(isExternal) - .build(); - - } catch (OptionsParsingException | NoSuchElementException e) { - throw new OptionsParsingException(INVALID_FORMAT); - } - } - - @Override - public String getTypeDescription() { - return "Artifact location parser"; - } -} diff --git a/src/tools/android/java/com/google/devtools/build/android/ideinfo/ArtifactLocationListConverter.java b/src/tools/android/java/com/google/devtools/build/android/ideinfo/ArtifactLocationListConverter.java deleted file mode 100644 index 535c5fc342..0000000000 --- a/src/tools/android/java/com/google/devtools/build/android/ideinfo/ArtifactLocationListConverter.java +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2016 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.android.ideinfo; - -import com.google.common.collect.Lists; -import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.ArtifactLocation; -import com.google.devtools.common.options.Converter; -import com.google.devtools.common.options.OptionsParsingException; -import java.util.Collections; -import java.util.List; - -/** - * Parses list of colon-separated artifact locations - */ -public class ArtifactLocationListConverter implements Converter> { - final ArtifactLocationConverter baseConverter = new ArtifactLocationConverter(); - - @Override - public List convert(String input) throws OptionsParsingException { - List list = Lists.newArrayList(); - for (String piece : input.split(":")) { - if (!piece.isEmpty()) { - list.add(baseConverter.convert(piece)); - } - } - return Collections.unmodifiableList(list); - } - - @Override - public String getTypeDescription() { - return "a colon-separated list of artifact locations"; - } -} diff --git a/src/tools/android/java/com/google/devtools/build/android/ideinfo/BUILD b/src/tools/android/java/com/google/devtools/build/android/ideinfo/BUILD deleted file mode 100644 index c416043f45..0000000000 --- a/src/tools/android/java/com/google/devtools/build/android/ideinfo/BUILD +++ /dev/null @@ -1,80 +0,0 @@ -filegroup( - name = "embedded_tools", - srcs = [ - "BUILD.tools", - "jar_filter_classes_deploy.jar", - "package_parser_classes_deploy.jar", - ], - visibility = ["//src:__pkg__"], -) - -java_binary( - name = "jar_filter_classes", - main_class = "does.not.exist", - runtime_deps = [":jar_filter_lib"], -) - -java_binary( - name = "JarFilter", - main_class = "com.google.devtools.build.android.ideinfo.JarFilter", - visibility = ["//visibility:public"], - runtime_deps = [":jar_filter_lib"], -) - -java_library( - name = "jar_filter_lib", - srcs = ["JarFilter.java"], - visibility = [ - "//devtools/blaze/integration:__pkg__", - "//src/test/java/com/google/devtools/build/android/ideinfo:__pkg__", - ], - deps = [ - "//src/main/java/com/google/devtools/common/options", - "//src/main/protobuf:package_manifest_java_proto", - "//src/tools/android/java/com/google/devtools/build/android:android_builder_lib", - "//third_party:guava", - "//third_party:jsr305", - "//third_party/protobuf:protobuf_java", - ], -) - -java_binary( - name = "package_parser_classes", - main_class = "does.not.exist", - runtime_deps = [":package_parser_lib"], -) - -java_binary( - name = "PackageParser", - main_class = "com.google.devtools.build.android.ideinfo.PackageParser", - visibility = ["//visibility:public"], - runtime_deps = [":package_parser_lib"], -) - -java_library( - name = "package_parser_lib", - srcs = [ - "ArtifactLocationConverter.java", - "ArtifactLocationListConverter.java", - "PackageParser.java", - "PackageParserIoProvider.java", - ], - visibility = [ - "//devtools/blaze/integration:__pkg__", - "//src/test/java/com/google/devtools/build/android/ideinfo:__pkg__", - ], - deps = [ - "//src/main/java/com/google/devtools/common/options", - "//src/main/protobuf:package_manifest_java_proto", - "//src/tools/android/java/com/google/devtools/build/android:android_builder_lib", - "//third_party:guava", - "//third_party:jsr305", - "//third_party/protobuf:protobuf_java", - ], -) - -filegroup( - name = "srcs", - srcs = glob(["**"]), - visibility = ["//src/tools/android/java/com/google/devtools/build/android:__pkg__"], -) diff --git a/src/tools/android/java/com/google/devtools/build/android/ideinfo/BUILD.tools b/src/tools/android/java/com/google/devtools/build/android/ideinfo/BUILD.tools deleted file mode 100644 index e6515db7e7..0000000000 --- a/src/tools/android/java/com/google/devtools/build/android/ideinfo/BUILD.tools +++ /dev/null @@ -1,23 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -java_import( - name = "jar_filter_classes", - jars = [":jar_filter_classes_deploy.jar"], -) - -java_binary( - name = "JarFilter", - main_class = "com.google.devtools.build.android.ideinfo.JarFilter", - runtime_deps = [":jar_filter_classes"], -) - -java_import( - name = "package_parser_classes", - jars = [":package_parser_classes_deploy.jar"], -) - -java_binary( - name = "PackageParser", - main_class = "com.google.devtools.build.android.ideinfo.PackageParser", - runtime_deps = [":package_parser_classes"], -) diff --git a/src/tools/android/java/com/google/devtools/build/android/ideinfo/JarFilter.java b/src/tools/android/java/com/google/devtools/build/android/ideinfo/JarFilter.java deleted file mode 100644 index a1678a6d00..0000000000 --- a/src/tools/android/java/com/google/devtools/build/android/ideinfo/JarFilter.java +++ /dev/null @@ -1,543 +0,0 @@ -// Copyright 2015 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.android.ideinfo; - -import static java.nio.charset.StandardCharsets.UTF_8; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Preconditions; -import com.google.common.base.Predicate; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; -import com.google.common.io.Files; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.ListeningExecutorService; -import com.google.common.util.concurrent.MoreExecutors; -import com.google.devtools.build.android.Converters.PathConverter; -import com.google.devtools.build.android.Converters.PathListConverter; -import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.ArtifactLocation; -import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.JavaSourcePackage; -import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.PackageManifest; -import com.google.devtools.common.options.Option; -import com.google.devtools.common.options.OptionDocumentationCategory; -import com.google.devtools.common.options.OptionEffectTag; -import com.google.devtools.common.options.OptionMetadataTag; -import com.google.devtools.common.options.OptionsBase; -import com.google.devtools.common.options.OptionsParser; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.nio.file.Path; -import java.util.Enumeration; -import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; -import java.util.zip.ZipOutputStream; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** Filters a jar, keeping only the classes that are indicated. */ -public final class JarFilter { - - /** The options for a {@JarFilter} action. */ - public static final class JarFilterOptions extends OptionsBase { - @Option( - name = "filter_jar", - allowMultiple = true, - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "null", - converter = PathConverter.class, - category = "input", - help = - "Paths to target output jars to filter for generated sources. You may use this flag " - + "multiple times, specify each path with a separate instance of the flag." - ) - public List filterJars; - - // TODO(laszlocsomor): remove this flag after 2018-01-31 (about 6 months from now). Everyone - // should have updated to newer Bazel versions by then. - @Deprecated - @Option( - name = "filter_jars", - deprecationWarning = "Deprecated in favour of \"--filter_jar\"", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "null", - converter = PathListConverter.class, - category = "input", - help = "A list of the paths to target output jars to filter for generated sources.", - metadataTags = {OptionMetadataTag.DEPRECATED} - ) - public List deprecatedFilterJars; - - @Option( - name = "filter_source_jar", - allowMultiple = true, - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "null", - converter = PathConverter.class, - category = "input", - help = - "Paths to target output source jars to filter for generated sources. You may use this " - + "flag multiple times, specify each path with a separate instance of the flag." - ) - public List filterSourceJars; - - // TODO(laszlocsomor): remove this flag after 2018-01-31 (about 6 months from now). Everyone - // should have updated to newer Bazel versions by then. - @Deprecated - @Option( - name = "filter_source_jars", - deprecationWarning = "Deprecated in favour of \"--filter_source_jar\"", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "null", - converter = PathListConverter.class, - category = "input", - help = "A list of the paths to target output source jars to filter for generated sources.", - metadataTags = {OptionMetadataTag.DEPRECATED} - ) - public List deprecatedFilterSourceJars; - - @Option( - name = "keep_java_file", - allowMultiple = true, - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "null", - converter = PathConverter.class, - category = "input", - help = - "Path of target input java files to keep. You may use this flag multiple times, " - + "specify each path with a separate instance of the flag." - ) - public List keepJavaFiles; - - // TODO(laszlocsomor): remove this flag after 2018-01-31 (about 6 months from now). Everyone - // should have updated to newer Bazel versions by then. - @Deprecated - @Option( - name = "keep_java_files", - deprecationWarning = "Deprecated in favour of \"--keep_java_file\"", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "null", - converter = PathListConverter.class, - category = "input", - help = "A list of target input java files to keep.", - metadataTags = {OptionMetadataTag.DEPRECATED} - ) - public List deprecatedKeepJavaFiles; - - @Option( - name = "keep_source_jar", - allowMultiple = true, - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "null", - converter = PathConverter.class, - category = "input", - help = - "Path of target input .srcjar files to keep. You may use this flag multiple times, " - + "specify each path with a separate instance of the flag." - ) - public List keepSourceJars; - - // TODO(laszlocsomor): remove this flag after 2018-01-31 (about 6 months from now). Everyone - // should have updated to newer Bazel versions by then. - @Deprecated - @Option( - name = "keep_source_jars", - deprecationWarning = "Deprecated in favour of \"--keep_source_jar\"", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "null", - converter = PathListConverter.class, - category = "input", - help = "A list of target input .srcjar files to keep.", - metadataTags = {OptionMetadataTag.DEPRECATED} - ) - public List deprecatedKeepSourceJars; - - @Option( - name = "filtered_jar", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "null", - converter = PathConverter.class, - category = "output", - help = "The path to the jar to output." - ) - public Path filteredJar; - - @Option( - name = "filtered_source_jar", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "null", - converter = PathConverter.class, - category = "output", - help = "The path to the source jar to output." - ) - public Path filteredSourceJar; - - // Deprecated options -- only here to maintain command line backwards compatibility - // with the current blaze native IDE aspect - - @Deprecated - @Option( - name = "jar", - allowMultiple = true, - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "null", - converter = PathConverter.class, - category = "input", - help = "A list of the paths to jars to filter for generated sources." - ) - public List jars; - - // TODO(laszlocsomor): remove this flag after 2018-01-31 (about 6 months from now). Everyone - // should have updated to newer Bazel versions by then. - @Deprecated - @Option( - name = "jars", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "null", - converter = PathListConverter.class, - category = "input", - help = "A list of the paths to jars to filter for generated sources." - ) - public List deprecatedJars; - - @Deprecated - @Option( - name = "manifest", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "null", - converter = PathConverter.class, - category = "input", - help = "The path to a package manifest generated only from generated sources." - ) - public Path manifest; - - @Deprecated - @Option( - name = "output", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "null", - converter = PathConverter.class, - category = "output", - help = "The path to the jar to output." - ) - public Path output; - } - - private static final Logger logger = Logger.getLogger(JarFilter.class.getName()); - - private static final Pattern JAVA_PACKAGE_PATTERN = - Pattern.compile("^\\s*package\\s+([\\w\\.]+);"); - - public static void main(String[] args) throws Exception { - JarFilterOptions options = parseArgs(args); - try { - main(options); - } catch (Throwable e) { - logger.log(Level.SEVERE, "Error parsing package strings", e); - System.exit(1); - } - System.exit(0); - } - - @VisibleForTesting - static void main(JarFilterOptions options) throws Exception { - Preconditions.checkNotNull(options.filteredJar); - - if (options.filterJars == null) { - options.filterJars = ImmutableList.of(); - } - if (options.filterSourceJars == null) { - options.filterSourceJars = ImmutableList.of(); - } - - final List archiveFileNamePrefixes = Lists.newArrayList(); - if (options.manifest != null) { - archiveFileNamePrefixes.addAll(parsePackageManifest(options.manifest)); - } - if (options.keepJavaFiles != null) { - archiveFileNamePrefixes.addAll(parseJavaFiles(options.keepJavaFiles)); - } - if (options.keepSourceJars != null) { - archiveFileNamePrefixes.addAll(parseSrcJars(options.keepSourceJars)); - } - - filterJars( - options.filterJars, - options.filteredJar, - new Predicate() { - @Override - public boolean apply(@Nullable String s) { - return shouldKeepClass(archiveFileNamePrefixes, s); - } - }); - if (options.filteredSourceJar != null) { - filterJars( - options.filterSourceJars, - options.filteredSourceJar, - new Predicate() { - @Override - public boolean apply(@Nullable String s) { - return shouldKeepJavaFile(archiveFileNamePrefixes, s); - } - }); - } - } - - @VisibleForTesting - static JarFilterOptions parseArgs(String[] args) { - args = parseParamFileIfUsed(args); - OptionsParser optionsParser = OptionsParser.newOptionsParser(JarFilterOptions.class); - optionsParser.parseAndExitUponError(args); - JarFilterOptions options = optionsParser.getOptions(JarFilterOptions.class); - - options.filterJars = PathListConverter.concatLists( - options.filterJars, options.deprecatedFilterJars); - options.filterSourceJars = PathListConverter.concatLists( - options.filterSourceJars, options.deprecatedFilterSourceJars); - options.keepJavaFiles = PathListConverter.concatLists( - options.keepJavaFiles, options.deprecatedKeepJavaFiles); - options.keepSourceJars = PathListConverter.concatLists( - options.keepSourceJars, options.deprecatedKeepSourceJars); - options.jars = PathListConverter.concatLists( - options.jars, options.deprecatedJars); - // Migrate options from v1 jar filter - if (options.filterJars.isEmpty() && !options.jars.isEmpty()) { - options.filterJars = options.jars; - } - if (options.filteredJar == null && options.output != null) { - options.filteredJar = options.output; - } - return options; - } - - private static String[] parseParamFileIfUsed(@Nonnull String[] args) { - if (args.length != 1 || !args[0].startsWith("@")) { - return args; - } - File paramFile = new File(args[0].substring(1)); - try { - return Files.readLines(paramFile, StandardCharsets.UTF_8).toArray(new String[0]); - } catch (IOException e) { - throw new RuntimeException("Error parsing param file: " + args[0], e); - } - } - - /** Finds the expected jar archive file name prefixes for the java files. */ - static List parseJavaFiles(List javaFiles) throws IOException { - ListeningExecutorService executorService = - MoreExecutors.listeningDecorator( - Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())); - - List> futures = Lists.newArrayList(); - for (final Path javaFile : javaFiles) { - futures.add( - executorService.submit( - new Callable() { - @Override - public String call() throws Exception { - String packageString = getDeclaredPackageOfJavaFile(javaFile); - return packageString != null - ? getArchiveFileNamePrefix(javaFile.toString(), packageString) - : null; - } - })); - } - try { - List archiveFileNamePrefixes = Futures.allAsList(futures).get(); - List result = Lists.newArrayList(); - for (String archiveFileNamePrefix : archiveFileNamePrefixes) { - if (archiveFileNamePrefix != null) { - result.add(archiveFileNamePrefix); - } - } - return result; - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new IOException(e); - } catch (ExecutionException e) { - throw new IOException(e); - } - } - - static List parseSrcJars(List srcJars) throws IOException { - List result = Lists.newArrayList(); - for (Path srcJar : srcJars) { - try (ZipFile sourceZipFile = new ZipFile(srcJar.toFile())) { - Enumeration entries = sourceZipFile.entries(); - while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); - if (!entry.getName().endsWith(".java")) { - continue; - } - try (BufferedReader reader = - new BufferedReader( - new InputStreamReader(sourceZipFile.getInputStream(entry), UTF_8))) { - String packageString = parseDeclaredPackage(reader); - if (packageString != null) { - String archiveFileNamePrefix = - getArchiveFileNamePrefix(entry.getName(), packageString); - result.add(archiveFileNamePrefix); - } - } - } - } - } - return result; - } - - @Nullable - private static String getDeclaredPackageOfJavaFile(Path javaFile) { - try (BufferedReader reader = - java.nio.file.Files.newBufferedReader(javaFile, StandardCharsets.UTF_8)) { - return parseDeclaredPackage(reader); - - } catch (IOException e) { - logger.log(Level.WARNING, "Error parsing package string from java source: " + javaFile, e); - return null; - } - } - - @Nullable - private static String parseDeclaredPackage(BufferedReader reader) throws IOException { - String line; - while ((line = reader.readLine()) != null) { - Matcher packageMatch = JAVA_PACKAGE_PATTERN.matcher(line); - if (packageMatch.find()) { - return packageMatch.group(1); - } - } - return null; - } - - /** - * Computes the expected archive file name prefix of a java class. - * - *

Eg.: file java/com/google/foo/Foo.java, package com.google.foo -> com/google/foo/Foo - */ - private static String getArchiveFileNamePrefix(String javaFile, String packageString) { - int lastSlashIndex = javaFile.lastIndexOf('/'); - // On Windows, the separator could be '\\' - if (lastSlashIndex == -1) { - lastSlashIndex = javaFile.lastIndexOf('\\'); - } - String fileName = lastSlashIndex != -1 ? javaFile.substring(lastSlashIndex + 1) : javaFile; - String className = fileName.substring(0, fileName.length() - ".java".length()); - return packageString.replace('.', '/') + '/' + className; - } - - /** Reads the package manifest and computes a list of the expected jar archive file names. */ - private static List parsePackageManifest(Path manifest) throws IOException { - try (InputStream inputStream = java.nio.file.Files.newInputStream(manifest)) { - PackageManifest packageManifest = PackageManifest.parseFrom(inputStream); - return parsePackageManifest(packageManifest); - } - } - - @VisibleForTesting - static List parsePackageManifest(PackageManifest packageManifest) { - List result = Lists.newArrayList(); - for (JavaSourcePackage javaSourcePackage : packageManifest.getSourcesList()) { - ArtifactLocation artifactLocation = javaSourcePackage.getArtifactLocation(); - String packageString = javaSourcePackage.getPackageString(); - String archiveFileNamePrefix = - getArchiveFileNamePrefix(artifactLocation.getRelativePath(), packageString); - result.add(archiveFileNamePrefix); - } - return result; - } - - /** Filters a list of jars, keeping anything matching the passed predicate. */ - private static void filterJars(List jars, Path output, Predicate shouldKeep) - throws IOException { - final int bufferSize = 8 * 1024; - byte[] buffer = new byte[bufferSize]; - - try (ZipOutputStream outputStream = - new ZipOutputStream(new FileOutputStream(output.toFile()))) { - for (Path jar : jars) { - try (ZipFile sourceZipFile = new ZipFile(jar.toFile())) { - Enumeration entries = sourceZipFile.entries(); - while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); - if (!shouldKeep.apply(entry.getName())) { - continue; - } - - ZipEntry newEntry = new ZipEntry(entry.getName()); - outputStream.putNextEntry(newEntry); - try (InputStream inputStream = sourceZipFile.getInputStream(entry)) { - int len; - while ((len = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, len); - } - } - } - } - } - } - } - - @VisibleForTesting - static boolean shouldKeepClass(List archiveFileNamePrefixes, String name) { - if (!name.endsWith(".class")) { - return false; - } - for (String archiveFileNamePrefix : archiveFileNamePrefixes) { - if (name.startsWith(archiveFileNamePrefix) - && name.length() > archiveFileNamePrefix.length()) { - char c = name.charAt(archiveFileNamePrefix.length()); - if (c == '.' || c == '$') { - return true; - } - } - } - return false; - } - - private static boolean shouldKeepJavaFile(List archiveFileNamePrefixes, String name) { - if (!name.endsWith(".java")) { - return false; - } - String nameWithoutJava = name.substring(0, name.length() - ".java".length()); - return archiveFileNamePrefixes.contains(nameWithoutJava); - } -} diff --git a/src/tools/android/java/com/google/devtools/build/android/ideinfo/PackageParser.java b/src/tools/android/java/com/google/devtools/build/android/ideinfo/PackageParser.java deleted file mode 100644 index 9c3db72c6c..0000000000 --- a/src/tools/android/java/com/google/devtools/build/android/ideinfo/PackageParser.java +++ /dev/null @@ -1,243 +0,0 @@ -// Copyright 2015 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.android.ideinfo; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Preconditions; -import com.google.common.collect.Maps; -import com.google.common.io.Files; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.ListeningExecutorService; -import com.google.common.util.concurrent.MoreExecutors; -import com.google.devtools.build.android.Converters.PathConverter; -import com.google.devtools.build.android.Converters.PathListConverter; -import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.ArtifactLocation; -import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.JavaSourcePackage; -import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.PackageManifest; -import com.google.devtools.common.options.Option; -import com.google.devtools.common.options.OptionDocumentationCategory; -import com.google.devtools.common.options.OptionEffectTag; -import com.google.devtools.common.options.OptionMetadataTag; -import com.google.devtools.common.options.OptionsBase; -import com.google.devtools.common.options.OptionsParser; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * Parses the package string from each of the source .java files - */ -public class PackageParser { - - /** The options for a {@PackageParser} action. */ - public static final class PackageParserOptions extends OptionsBase { - @Option( - name = "sources", - defaultValue = "null", - converter = ArtifactLocationListConverter.class, - category = "input", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - help = - "The locations of the java source files. The expected format is a " - + "colon-separated list." - ) - public List sources; - - @Option( - name = "output_manifest", - defaultValue = "null", - converter = PathConverter.class, - category = "output", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - help = "The path to the manifest file this parser writes to." - ) - public Path outputManifest; - - @Option( - name = "sources_execution_path", - allowMultiple = true, - defaultValue = "null", - converter = PathConverter.class, - category = "input", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - help = - "The execution paths of the java source files. You may use this flag multiple times, " - + "each instance should specify one path." - ) - public List sourcesExecutionPaths; - - // TODO(laszlocsomor): remove this flag after 2018-01-31 (about 6 months from now). Everyone - // should have updated to newer Bazel versions by then. - @Deprecated - @Option( - name = "sources_execution_paths", - defaultValue = "null", - deprecationWarning = "Deprecated in favour of \"--sources_execution_path\"", - converter = PathListConverter.class, - category = "input", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - help = - "The execution paths of the java source files. The expected format is a " - + "colon-separated list.", - metadataTags = {OptionMetadataTag.DEPRECATED} - ) - public List deprecatedSourcesExecutionPaths; - } - - private static final Logger logger = Logger.getLogger(PackageParser.class.getName()); - - private static final Pattern PACKAGE_PATTERN = - Pattern.compile("^\\s*package\\s+([\\w\\.]+)"); - - public static void main(String[] args) throws Exception { - PackageParserOptions options = parseArgs(args); - Preconditions.checkNotNull(options.outputManifest); - options.sourcesExecutionPaths = PathListConverter.concatLists( - options.sourcesExecutionPaths, options.deprecatedSourcesExecutionPaths); - - try { - PackageParser parser = new PackageParser(PackageParserIoProvider.INSTANCE); - Map outputMap = parser.parsePackageStrings(options.sources); - parser.writeManifest(outputMap, options.outputManifest); - } catch (Throwable e) { - logger.log(Level.SEVERE, "Error parsing package strings", e); - System.exit(1); - } - System.exit(0); - } - - @Nonnull - private static Path getExecutionPath(@Nonnull ArtifactLocation location) { - return Paths.get(location.getRootExecutionPathFragment(), location.getRelativePath()); - } - - @VisibleForTesting - public static PackageParserOptions parseArgs(String[] args) { - args = parseParamFileIfUsed(args); - OptionsParser optionsParser = OptionsParser.newOptionsParser(PackageParserOptions.class); - optionsParser.parseAndExitUponError(args); - return optionsParser.getOptions(PackageParserOptions.class); - } - - private static String[] parseParamFileIfUsed(@Nonnull String[] args) { - if (args.length != 1 || !args[0].startsWith("@")) { - return args; - } - File paramFile = new File(args[0].substring(1)); - try { - return Files.readLines(paramFile, StandardCharsets.UTF_8).toArray(new String[0]); - } catch (IOException e) { - throw new RuntimeException("Error parsing param file: " + args[0], e); - } - } - - private final PackageParserIoProvider ioProvider; - - @VisibleForTesting - public PackageParser(@Nonnull PackageParserIoProvider ioProvider) { - this.ioProvider = ioProvider; - } - - @VisibleForTesting - public void writeManifest( - @Nonnull Map sourceToPackageMap, - Path outputFile) - throws IOException { - PackageManifest.Builder builder = PackageManifest.newBuilder(); - for (Entry entry : sourceToPackageMap.entrySet()) { - JavaSourcePackage.Builder srcBuilder = JavaSourcePackage.newBuilder() - .setPackageString(entry.getValue()) - .setArtifactLocation(entry.getKey()); - builder.addSources(srcBuilder.build()); - } - - try { - ioProvider.writeProto(builder.build(), outputFile); - } catch (IOException e) { - logger.log(Level.SEVERE, "Error writing package manifest", e); - throw e; - } - } - - @Nonnull - @VisibleForTesting - public Map parsePackageStrings(@Nonnull List sources) - throws Exception { - - ListeningExecutorService executorService = MoreExecutors.listeningDecorator( - Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())); - - Map> futures = Maps.newHashMap(); - for (final ArtifactLocation source : sources) { - futures.put(source, executorService.submit(new Callable() { - @Override - public String call() throws Exception { - return getDeclaredPackageOfJavaFile(source); - } - })); - } - Map map = Maps.newHashMap(); - for (Entry> entry : futures.entrySet()) { - String value = entry.getValue().get(); - if (value != null) { - map.put(entry.getKey(), value); - } - } - return map; - } - - @Nullable - private String getDeclaredPackageOfJavaFile(@Nonnull ArtifactLocation source) { - try (BufferedReader reader = ioProvider.getReader(getExecutionPath(source))) { - return parseDeclaredPackage(reader); - - } catch (IOException e) { - logger.log(Level.WARNING, "Error parsing package string from java source: " + source, e); - return null; - } - } - - @VisibleForTesting - @Nullable - public static String parseDeclaredPackage(@Nonnull BufferedReader reader) throws IOException { - String line; - while ((line = reader.readLine()) != null) { - Matcher packageMatch = PACKAGE_PATTERN.matcher(line); - if (packageMatch.find()) { - return packageMatch.group(1); - } - } - return null; - } - -} diff --git a/src/tools/android/java/com/google/devtools/build/android/ideinfo/PackageParserIoProvider.java b/src/tools/android/java/com/google/devtools/build/android/ideinfo/PackageParserIoProvider.java deleted file mode 100644 index 607ed9bdeb..0000000000 --- a/src/tools/android/java/com/google/devtools/build/android/ideinfo/PackageParserIoProvider.java +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2015 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.android.ideinfo; - -import com.google.common.annotations.VisibleForTesting; -import com.google.protobuf.MessageLite; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import javax.annotation.Nonnull; - -/** - * Provides a BufferedReader for the source java files, - * and a writer for the output proto - */ -@VisibleForTesting -public class PackageParserIoProvider { - - public static final PackageParserIoProvider INSTANCE = new PackageParserIoProvider(); - - public void writeProto(@Nonnull MessageLite message, @Nonnull Path file) throws IOException { - try (OutputStream out = Files.newOutputStream(file)) { - message.writeTo(out); - } - } - - @Nonnull - public BufferedReader getReader(Path file) throws IOException { - return Files.newBufferedReader(file, StandardCharsets.UTF_8); - } - -} diff --git a/tools/BUILD b/tools/BUILD index 49cd6290a7..9f89403ecc 100644 --- a/tools/BUILD +++ b/tools/BUILD @@ -17,7 +17,6 @@ filegroup( "//tools/build_defs/repo:srcs", "//tools/build_rules:srcs", "//tools/coverage:srcs", - "//tools/ide:srcs", "//tools/jdk:srcs", "//tools/launcher:srcs", "//tools/platforms:srcs", @@ -47,7 +46,6 @@ filegroup( "//tools/coverage:srcs", "//tools/cpp:srcs", "//tools/genrule:srcs", - "//tools/ide:embedded_tools_srcs", "//tools/j2objc:srcs", "//tools/jdk:package-srcs", "//tools/jdk:srcs", diff --git a/tools/android/BUILD.tools b/tools/android/BUILD.tools index 67b0d018f8..6828ddca36 100644 --- a/tools/android/BUILD.tools +++ b/tools/android/BUILD.tools @@ -86,16 +86,6 @@ alias( actual = "//src/tools/android/java/com/google/devtools/build/android/idlclass:IdlClass", ) -alias( - name = "JarFilter", - actual = "//src/tools/android/java/com/google/devtools/build/android/ideinfo:JarFilter", -) - -alias( - name = "PackageParser", - actual = "//src/tools/android/java/com/google/devtools/build/android/ideinfo:PackageParser", -) - py_binary( name = "merge_manifests", srcs = [ diff --git a/tools/ide/BUILD b/tools/ide/BUILD deleted file mode 100644 index 840adefc23..0000000000 --- a/tools/ide/BUILD +++ /dev/null @@ -1,28 +0,0 @@ -licenses(["notice"]) # Apache 2.0 - -filegroup( - name = "intellij_info", - srcs = [ - "intellij_info.bzl", - "intellij_info_impl.bzl", - ], - visibility = [ - "//src/test/java/com/google/devtools/build/lib:__pkg__", - ], -) - -filegroup( - name = "srcs", - srcs = glob(["**"]), - visibility = ["//tools:__pkg__"], -) - -filegroup( - name = "embedded_tools_srcs", - srcs = [ - "BUILD.tools", - "intellij_info.bzl", - "intellij_info_impl.bzl", - ], - visibility = ["//visibility:public"], -) diff --git a/tools/ide/BUILD.tools b/tools/ide/BUILD.tools deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/tools/ide/intellij_info.bzl b/tools/ide/intellij_info.bzl deleted file mode 100644 index 94247ed140..0000000000 --- a/tools/ide/intellij_info.bzl +++ /dev/null @@ -1,32 +0,0 @@ -# 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. - -"""Bazel-specific intellij aspect.""" - -load("@bazel_tools//tools/ide:intellij_info_impl.bzl", - "make_intellij_info_aspect", - "intellij_info_aspect_impl") - -def tool_label(label_str): - """Returns a label that points to a bazel tool.""" - return Label("@bazel_tools" + label_str) - -semantics = struct( - tool_label = tool_label, -) - -def _aspect_impl(target, ctx): - return intellij_info_aspect_impl(target, ctx, semantics) - -intellij_info_aspect = make_intellij_info_aspect(_aspect_impl, semantics) diff --git a/tools/ide/intellij_info_impl.bzl b/tools/ide/intellij_info_impl.bzl deleted file mode 100644 index 15b5f588b7..0000000000 --- a/tools/ide/intellij_info_impl.bzl +++ /dev/null @@ -1,731 +0,0 @@ -# Copyright 2016 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. - -"""Implementation of IntelliJ-specific information collecting aspect.""" - -# Compile-time dependency attributes, grouped by type. -DEPS = [ - "_cc_toolchain", # From C rules - "_java_toolchain", # From java rules - "deps", - "exports", - "_robolectric", # From android_robolectric_test - "_android_sdk", # from android rules - "aidl_lib", # from android_sdk - "_scala_toolchain", # From scala rules - # All other scala ones besides toolchain can be removed after - # https://github.com/bazelbuild/rules_scala/pull/241 - "_scalalib", # From scala rules - "_scalacompiler", # From scala rules - "_scalareflect", # From scala rules - "_scalatest", # From scala_test rules - "_scalatest_reporter", # From scala_test rules - "_scalaxml", # From scala_test rules -] - -# Run-time dependency attributes, grouped by type. -RUNTIME_DEPS = [ - "runtime_deps", -] - -PREREQUISITE_DEPS = [] - -# Dependency type enum -COMPILE_TIME = 0 -RUNTIME = 1 - -##### Helpers - -def struct_omit_none(**kwargs): - """A replacement for standard `struct` function that omits the fields with None value.""" - d = {name: kwargs[name] for name in kwargs if kwargs[name] != None} - return struct(**d) - -def artifact_location(f): - """Creates an ArtifactLocation proto from a File.""" - if f == None: - return None - - return to_artifact_location( - f.path, - f.root.path if not f.is_source else "", - f.is_source, - is_external_artifact(f.owner), - ) - -def to_artifact_location(exec_path, root_exec_path_fragment, is_source, is_external): - """Derives workspace path from other path fragments, and creates an ArtifactLocation proto.""" - # Bazel 0.4.4 has directory structure: - # exec_path = (root_fragment)? + (external/repo_name)? + relative_path - # Bazel 0.4.5 has planned directory structure: - # exec_path = (../repo_name)? + (root_fragment)? + relative_path - # Handle both cases by trying to strip the external workspace prefix before and after removing - # root_exec_path_fragment. - relative_path = strip_external_workspace_prefix(exec_path) - relative_path = strip_root_exec_path_fragment(relative_path, root_exec_path_fragment) - # Remove this line when Bazel 0.4.4 and earlier no longer need to be supported. - relative_path = strip_external_workspace_prefix(relative_path) - - root_exec_path_fragment = exec_path[:-(len("/" + relative_path))] - - return struct_omit_none( - relative_path = relative_path, - is_source = is_source, - is_external = is_external, - root_execution_path_fragment = root_exec_path_fragment, - is_new_external_version = True, - ) - -def strip_root_exec_path_fragment(path, root_fragment): - if root_fragment and path.startswith(root_fragment + "/"): - return path[len(root_fragment + "/"):] - return path - -def strip_external_workspace_prefix(path): - """Either 'external/workspace_name/' or '../workspace_name/'.""" - # Label.EXTERNAL_PATH_PREFIX is due to change from 'external' to '..' in Bazel 0.4.5. - # This code is for forwards and backwards compatibility. - # Remove the 'external/' check when Bazel 0.4.4 and earlier no longer need to be supported. - if path.startswith("../") or path.startswith("external/"): - return "/".join(path.split("/")[2:]) - return path - -def is_external_artifact(label): - """Determines whether a label corresponds to an external artifact.""" - # Label.EXTERNAL_PATH_PREFIX is due to change from 'external' to '..' in Bazel 0.4.5. - # This code is for forwards and backwards compatibility. - # Remove the 'external' check when Bazel 0.4.4 and earlier no longer need to be supported. - return label.workspace_root.startswith("external") or label.workspace_root.startswith("..") - -def source_directory_tuple(resource_file): - """Creates a tuple of (exec_path, root_exec_path_fragment, is_source, is_external).""" - relative_path = str(android_common.resource_source_directory(resource_file)) - root_exec_path_fragment = resource_file.root.path if not resource_file.is_source else None - return ( - relative_path if resource_file.is_source else root_exec_path_fragment + relative_path, - root_exec_path_fragment, - resource_file.is_source, - is_external_artifact(resource_file.owner) - ) - -def all_unique_source_directories(resources): - """Builds a list of unique ArtifactLocation protos.""" - # Sets can contain tuples, but cannot contain structs. - # Use set of tuples to unquify source directories. - source_directory_tuples = depset([source_directory_tuple(f) for f in resources]) - return [to_artifact_location( - exec_path, - root_path_fragment, - is_source, - is_external) - for (exec_path, root_path_fragment, is_source, is_external) in source_directory_tuples] - -def build_file_artifact_location(ctx): - """Creates an ArtifactLocation proto representing a location of a given BUILD file.""" - return to_artifact_location( - ctx.build_file_path, - ctx.build_file_path, - True, - is_external_artifact(ctx.label) - ) - -def get_source_jar(output): - if hasattr(output, "source_jar"): - return output.source_jar - return None - -def library_artifact(java_output): - """Creates a LibraryArtifact representing a given java_output.""" - if java_output == None or java_output.class_jar == None: - return None - return struct_omit_none( - jar = artifact_location(java_output.class_jar), - interface_jar = artifact_location(java_output.ijar), - source_jar = artifact_location(get_source_jar(java_output)), - ) - -def annotation_processing_jars(annotation_processing): - """Creates a LibraryArtifact representing Java annotation processing jars.""" - return struct_omit_none( - jar = artifact_location(annotation_processing.class_jar), - source_jar = artifact_location(annotation_processing.source_jar), - ) - -def jars_from_output(output): - """Collect jars for intellij-resolve-files from Java output.""" - if output == None: - return [] - return [jar - for jar in [output.class_jar, output.ijar, get_source_jar(output)] - if jar != None and not jar.is_source] - -# TODO(salguarnieri) Remove once skylark provides the path safe string from a PathFragment. -def replace_empty_path_with_dot(path): - return path or "." - -def sources_from_target(ctx): - """Get the list of sources from a target as artifact locations.""" - return artifacts_from_target_list_attr(ctx, "srcs") - -def artifacts_from_target_list_attr(ctx, attr_name): - """Converts a list of targets to a list of artifact locations.""" - return [artifact_location(f) - for target in getattr(ctx.rule.attr, attr_name, []) - for f in target.files] - -def _collect_target_from_attr(rule_attrs, attr_name, result): - """Collects the targets from the given attr into the result.""" - if not hasattr(rule_attrs, attr_name): - return - attr_value = getattr(rule_attrs, attr_name) - type_name = type(attr_value) - if type_name == "Target": - result.append(attr_value) - elif type_name == "list": - result.extend(attr_value) - -def collect_targets_from_attrs(rule_attrs, attrs): - """Returns a list of targets from the given attributes.""" - result = [] - for attr_name in attrs: - _collect_target_from_attr(rule_attrs, attr_name, result) - return [target for target in result if is_valid_aspect_target(target)] - -def targets_to_labels(targets): - """Returns a set of label strings for the given targets.""" - return depset([str(target.label) for target in targets]) - -def list_omit_none(value): - """Returns a list of the value, or the empty list if None.""" - return [value] if value else [] - -def is_valid_aspect_target(target): - """Returns whether the target has had the aspect run on it.""" - return hasattr(target, "intellij_info") - -def get_aspect_ids(ctx, target): - """Returns the all aspect ids, filtering out self.""" - aspect_ids = None - if hasattr(ctx, "aspect_ids"): - aspect_ids = ctx.aspect_ids - elif hasattr(target, "aspect_ids"): - aspect_ids = target.aspect_ids - else: - return None - return [aspect_id for aspect_id in aspect_ids if "intellij_info_aspect" not in aspect_id] - -def make_target_key(label, aspect_ids): - """Returns a TargetKey proto struct from a target.""" - return struct_omit_none( - label = str(label), - aspect_ids = tuple(aspect_ids) if aspect_ids else None - ) - -def make_dep(dep, dependency_type): - """Returns a Dependency proto struct.""" - return struct( - target = dep.intellij_info.target_key, - dependency_type = dependency_type, - ) - -def make_deps(deps, dependency_type): - """Returns a list of Dependency proto structs.""" - return [make_dep(dep, dependency_type) for dep in deps] - -def make_dep_from_label(label, dependency_type): - """Returns a Dependency proto struct from a label.""" - return struct( - target = struct(label = str(label)), - dependency_type = dependency_type, - ) - -##### Builders for individual parts of the aspect output - -def build_py_ide_info(target, ctx): - """Build PyIdeInfo.""" - if not hasattr(target, "py"): - return (None, depset()) - - sources = sources_from_target(ctx) - transitive_sources = target.py.transitive_sources - - py_ide_info = struct_omit_none( - sources = sources, - ) - return (py_ide_info, transitive_sources) - -def build_c_ide_info(target, ctx): - """Build CIdeInfo.""" - if not hasattr(target, "cc"): - return (None, depset()) - - sources = artifacts_from_target_list_attr(ctx, "srcs") - headers = artifacts_from_target_list_attr(ctx, "hdrs") - textual_headers = artifacts_from_target_list_attr(ctx, "textual_hdrs") - - target_includes = [] - if hasattr(ctx.rule.attr, "includes"): - target_includes = ctx.rule.attr.includes - target_defines = [] - if hasattr(ctx.rule.attr, "defines"): - target_defines = ctx.rule.attr.defines - target_copts = [] - if hasattr(ctx.rule.attr, "copts"): - target_copts = ctx.rule.attr.copts - - cc_provider = target.cc - - c_ide_info = struct_omit_none( - source = sources, - header = headers, - textual_header = textual_headers, - target_include = target_includes, - target_define = target_defines, - target_copt = target_copts, - transitive_include_directory = cc_provider.include_directories, - transitive_quote_include_directory = cc_provider.quote_include_directories, - transitive_define = cc_provider.defines, - transitive_system_include_directory = cc_provider.system_include_directories, - ) - intellij_resolve_files = cc_provider.transitive_headers - return (c_ide_info, intellij_resolve_files) - -def build_c_toolchain_ide_info(ctx): - """Build CToolchainIdeInfo.""" - - if ctx.rule.kind != "cc_toolchain": - return (None, depset()) - - # This should exist because we requested it in our aspect definition. - cc_fragment = ctx.fragments.cpp - - c_toolchain_ide_info = struct_omit_none( - target_name = cc_fragment.target_gnu_system_name, - base_compiler_option = cc_fragment.compiler_options(ctx.features), - c_option = cc_fragment.c_options, - cpp_option = cc_fragment.cxx_options(ctx.features), - link_option = cc_fragment.link_options, - unfiltered_compiler_option = cc_fragment.unfiltered_compiler_options(ctx.features), - preprocessor_executable = replace_empty_path_with_dot( - str(cc_fragment.preprocessor_executable)), - cpp_executable = str(cc_fragment.compiler_executable), - built_in_include_directory = [str(d) - for d in cc_fragment.built_in_include_directories], - ) - return (c_toolchain_ide_info, depset()) - -def get_java_provider(target): - if hasattr(target, "proto_java"): - return target.proto_java - if hasattr(target, "java"): - return target.java - if hasattr(target, "scala"): - return target.scala - return None - -def get_java_jars(outputs): - """Handle both Java (java.outputs.jars list) and Scala (single scala.outputs) targets.""" - if hasattr(outputs, "jars"): - return outputs.jars - if hasattr(outputs, "class_jar"): - return [outputs] - -def build_java_ide_info(target, ctx, semantics): - """Build JavaIdeInfo.""" - java = get_java_provider(target) - if not java: - return (None, depset(), depset(), depset()) - - java_semantics = semantics.java if hasattr(semantics, "java") else None - if java_semantics and java_semantics.skip_target(target, ctx): - return (None, depset(), depset(), depset()) - - ide_info_files = depset() - sources = sources_from_target(ctx) - java_jars = get_java_jars(java.outputs) - jars = [library_artifact(output) for output in java_jars] - class_jars = [output.class_jar for output in java_jars if output and output.class_jar] - output_jars = [jar for output in java_jars for jar in jars_from_output(output)] - intellij_resolve_files = depset(output_jars) - intellij_compile_files = depset(class_jars) - - gen_jars = [] - if (hasattr(java, "annotation_processing") and - java.annotation_processing and - java.annotation_processing.enabled): - gen_jars = [annotation_processing_jars(java.annotation_processing)] - intellij_resolve_files = intellij_resolve_files | depset([ - jar for jar in [java.annotation_processing.class_jar, - java.annotation_processing.source_jar] - if jar != None and not jar.is_source]) - intellij_compile_files = intellij_compile_files | depset([ - jar for jar in [java.annotation_processing.class_jar] - if jar != None and not jar.is_source]) - - jdeps = None - if hasattr(java.outputs, "jdeps"): - jdeps = artifact_location(java.outputs.jdeps) - - java_sources, gen_java_sources, srcjars = divide_java_sources(ctx) - - if java_semantics: - srcjars = java_semantics.filter_source_jars(target, ctx, srcjars) - - package_manifest = None - if java_sources: - package_manifest = build_java_package_manifest(ctx, target, java_sources, ".java-manifest") - ide_info_files = ide_info_files | depset([package_manifest]) - - filtered_gen_jar = None - if java_sources and (gen_java_sources or srcjars): - filtered_gen_jar, filtered_gen_resolve_files = build_filtered_gen_jar( - ctx, - target, - java, - gen_java_sources, - srcjars - ) - intellij_resolve_files = intellij_resolve_files | filtered_gen_resolve_files - - java_ide_info = struct_omit_none( - sources = sources, - jars = jars, - jdeps = jdeps, - generated_jars = gen_jars, - package_manifest = artifact_location(package_manifest), - filtered_gen_jar = filtered_gen_jar, - main_class = ctx.rule.attr.main_class if hasattr(ctx.rule.attr, "main_class") else None, - ) - return (java_ide_info, ide_info_files, intellij_resolve_files, intellij_compile_files) - -def _package_manifest_file_argument(f): - artifact = artifact_location(f) - is_external = "1" if is_external_artifact(f.owner) else "0" - return artifact.root_execution_path_fragment + "," + artifact.relative_path + "," + is_external - -def build_java_package_manifest(ctx, target, source_files, suffix): - """Builds the java package manifest for the given source files.""" - output = ctx.new_file(target.label.name + suffix) - - args = [] - args += ["--output_manifest", output.path] - args += ["--sources"] - args += [":".join([_package_manifest_file_argument(f) for f in source_files])] - argfile = ctx.new_file(ctx.configuration.bin_dir, - target.label.name + suffix + ".params") - ctx.file_action(output=argfile, content="\n".join(args)) - - ctx.action( - inputs = source_files + [argfile], - outputs = [output], - executable = ctx.executable._package_parser, - arguments = ["@" + argfile.path], - mnemonic = "JavaPackageManifest", - progress_message = "Parsing java package strings for " + str(target.label), - ) - return output - -def build_filtered_gen_jar(ctx, target, java, gen_java_sources, srcjars): - """Filters the passed jar to contain only classes from the given manifest.""" - jar_artifacts = [] - source_jar_artifacts = [] - for jar in java.outputs.jars: - if jar.ijar: - jar_artifacts.append(jar.ijar) - elif jar.class_jar: - jar_artifacts.append(jar.class_jar) - if jar.source_jar: - source_jar_artifacts.append(jar.source_jar) - - filtered_jar = ctx.new_file(target.label.name + "-filtered-gen.jar") - filtered_source_jar = ctx.new_file(target.label.name + "-filtered-gen-src.jar") - args = [] - for jar in jar_artifacts: - args += ["--filter_jar", jar.path] - for jar in source_jar_artifacts: - args += ["--filter_source_jar", jar.path] - args += ["--filtered_jar", filtered_jar.path] - args += ["--filtered_source_jar", filtered_source_jar.path] - if gen_java_sources: - for java_file in gen_java_sources: - args += ["--keep_java_file", java_file.path] - if srcjars: - for source_jar in srcjars: - args += ["--keep_source_jar", source_jar.path] - ctx.action( - inputs = jar_artifacts + source_jar_artifacts + gen_java_sources + srcjars, - outputs = [filtered_jar, filtered_source_jar], - executable = ctx.executable._jar_filter, - arguments = args, - mnemonic = "JarFilter", - progress_message = "Filtering generated code for " + str(target.label), - ) - output_jar = struct( - jar=artifact_location(filtered_jar), - source_jar=artifact_location(filtered_source_jar), - ) - intellij_resolve_files = depset([filtered_jar, filtered_source_jar]) - return output_jar, intellij_resolve_files - -def divide_java_sources(ctx): - """Divide sources into plain java, generated java, and srcjars.""" - - java_sources = [] - gen_java_sources = [] - srcjars = [] - if hasattr(ctx.rule.attr, "srcs"): - srcs = ctx.rule.attr.srcs - for src in srcs: - for f in src.files: - if f.basename.endswith(".java"): - if f.is_source: - java_sources.append(f) - else: - gen_java_sources.append(f) - elif f.basename.endswith(".srcjar"): - srcjars.append(f) - - return java_sources, gen_java_sources, srcjars - -def build_android_ide_info(target, ctx, semantics): - """Build AndroidIdeInfo.""" - if not hasattr(target, "android"): - return (None, depset()) - - android_semantics = semantics.android if hasattr(semantics, "android") else None - extra_ide_info = android_semantics.extra_ide_info(target, ctx) if android_semantics else {} - - android = target.android - android_ide_info = struct_omit_none( - java_package = android.java_package, - idl_import_root = android.idl.import_root if hasattr(android.idl, "import_root") else None, - manifest = artifact_location(android.manifest), - apk = artifact_location(android.apk), - dependency_apk = [artifact_location(apk) for apk in android.apks_under_test], - has_idl_sources = android.idl.output != None, - idl_jar = library_artifact(android.idl.output), - generate_resource_class = android.defines_resources, - resources = all_unique_source_directories(android.resources), - resource_jar = library_artifact(android.resource_jar), - **extra_ide_info - ) - intellij_resolve_files = depset(jars_from_output(android.idl.output)) - - if android.manifest and not android.manifest.is_source: - intellij_resolve_files = intellij_resolve_files | depset([android.manifest]) - - return (android_ide_info, intellij_resolve_files) - -def build_android_sdk_ide_info(ctx): - if ctx.rule.kind != "android_sdk": - return None - android_jar_file = list(ctx.rule.attr.android_jar.files)[0] - return struct( - android_jar = artifact_location(android_jar_file), - ) - -def build_test_info(ctx): - """Build TestInfo.""" - if not is_test_rule(ctx): - return None - return struct_omit_none( - size = ctx.rule.attr.size, - ) - -def is_test_rule(ctx): - kind_string = ctx.rule.kind - return kind_string.endswith("_test") - -def build_java_toolchain_ide_info(target): - """Build JavaToolchainIdeInfo.""" - if not hasattr(target, "java_toolchain"): - return None - toolchain_info = target.java_toolchain - javac_jar_file = toolchain_info.javac_jar if hasattr(toolchain_info, "javac_jar") else None - return struct_omit_none( - source_version = toolchain_info.source_version, - target_version = toolchain_info.target_version, - javac_jar = artifact_location(javac_jar_file), - ) - -##### Main aspect function - -def intellij_info_aspect_impl(target, ctx, semantics): - """Aspect implementation function.""" - tags = ctx.rule.attr.tags - if "no-ide" in tags: - return struct() - - rule_attrs = ctx.rule.attr - - # Collect direct dependencies - direct_dep_targets = collect_targets_from_attrs( - rule_attrs, semantics_extra_deps(DEPS, semantics, "extra_deps")) - direct_deps = make_deps(direct_dep_targets, COMPILE_TIME) - - # Add exports from direct dependencies - exported_deps_from_deps = [] - for dep in direct_dep_targets: - exported_deps_from_deps = exported_deps_from_deps + dep.intellij_info.export_deps - - # Combine into all compile time deps - compiletime_deps = direct_deps + exported_deps_from_deps - - # Propagate my own exports - export_deps = [] - if hasattr(target, "java"): - transitive_exports = target.java.transitive_exports - export_deps = [make_dep_from_label(label, COMPILE_TIME) for label in transitive_exports] - # Empty android libraries export all their dependencies. - if ctx.rule.kind == "android_library": - if not hasattr(rule_attrs, "srcs") or not ctx.rule.attr.srcs: - export_deps = export_deps + compiletime_deps - export_deps = list(depset(export_deps)) - - # runtime_deps - runtime_dep_targets = collect_targets_from_attrs( - rule_attrs, semantics_extra_deps(RUNTIME_DEPS, semantics, "extra_runtime_deps")) - runtime_deps = make_deps(runtime_dep_targets, RUNTIME) - all_deps = list(depset(compiletime_deps + runtime_deps)) - - # extra prerequisites - extra_prerequisite_targets = collect_targets_from_attrs( - rule_attrs, semantics_extra_deps(PREREQUISITE_DEPS, semantics, "extra_prerequisites")) - - # Roll up files from my prerequisites - prerequisites = direct_dep_targets + runtime_dep_targets + extra_prerequisite_targets - intellij_info_text = depset() - intellij_resolve_files = depset() - intellij_compile_files = target.output_group("files_to_compile_INTERNAL_") - for dep in prerequisites: - intellij_info_text = intellij_info_text | dep.intellij_info.intellij_info_text - intellij_resolve_files = intellij_resolve_files | dep.intellij_info.intellij_resolve_files - intellij_compile_files = intellij_compile_files | dep.intellij_info.intellij_compile_files - - # Collect python-specific information - (py_ide_info, py_resolve_files) = build_py_ide_info(target, ctx) - intellij_resolve_files = intellij_resolve_files | py_resolve_files - - # Collect C-specific information - (c_ide_info, c_resolve_files) = build_c_ide_info(target, ctx) - intellij_resolve_files = intellij_resolve_files | c_resolve_files - - (c_toolchain_ide_info, c_toolchain_resolve_files) = build_c_toolchain_ide_info(ctx) - intellij_resolve_files = intellij_resolve_files | c_toolchain_resolve_files - - # Collect Java-specific information - (java_ide_info, java_ide_info_files, java_resolve_files, - java_compile_files) = build_java_ide_info(target, ctx, semantics) - intellij_info_text = intellij_info_text | java_ide_info_files - intellij_resolve_files = intellij_resolve_files | java_resolve_files - intellij_compile_files = intellij_compile_files | java_compile_files - - # Collect Android-specific information - (android_ide_info, android_resolve_files) = build_android_ide_info( - target, ctx, semantics) - intellij_resolve_files = intellij_resolve_files | android_resolve_files - android_sdk_ide_info = build_android_sdk_ide_info(ctx) - - # java_toolchain - java_toolchain_ide_info = build_java_toolchain_ide_info(target) - - # Collect test info - test_info = build_test_info(ctx) - - file_name = target.label.name - aspect_ids = get_aspect_ids(ctx, target) - if aspect_ids: - aspect_hash = hash(".".join(aspect_ids)) - file_name = file_name + "-" + str(aspect_hash) - file_name = file_name + ".intellij-info.txt" - - # Any extra ide info - extra_ide_info = {} - if hasattr(semantics, "extra_ide_info"): - extra_ide_info = semantics.extra_ide_info(target, ctx) - - # Build TargetIdeInfo proto - target_key = make_target_key(target.label, aspect_ids) - info = struct_omit_none( - key = target_key, - kind_string = ctx.rule.kind, - deps = list(all_deps), - build_file_artifact_location = build_file_artifact_location(ctx), - c_ide_info = c_ide_info, - c_toolchain_ide_info = c_toolchain_ide_info, - java_ide_info = java_ide_info, - android_ide_info = android_ide_info, - android_sdk_ide_info = android_sdk_ide_info, - tags = tags, - test_info = test_info, - java_toolchain_ide_info = java_toolchain_ide_info, - py_ide_info = py_ide_info, - features = ctx.features, - **extra_ide_info - ) - - # Output the ide information file. - output = ctx.new_file(file_name) - ctx.file_action(output, info.to_proto()) - intellij_info_text = intellij_info_text | depset([output]) - - # Return providers. - return struct_omit_none( - output_groups = { - "intellij-info-text" : intellij_info_text, - "intellij-resolve" : intellij_resolve_files, - "intellij-compile": intellij_compile_files, - }, - intellij_info = struct( - target_key = target_key, - intellij_info_text = intellij_info_text, - intellij_resolve_files = intellij_resolve_files, - intellij_compile_files = intellij_compile_files, - export_deps = export_deps, - ), - ) - -def semantics_extra_deps(base, semantics, name): - if not hasattr(semantics, name): - return base - extra_deps = getattr(semantics, name) - return base + extra_deps - -def make_intellij_info_aspect(aspect_impl, semantics): - """Creates the aspect given the semantics.""" - tool_label = semantics.tool_label - deps = semantics_extra_deps(DEPS, semantics, "extra_deps") - runtime_deps = semantics_extra_deps(RUNTIME_DEPS, semantics, "extra_runtime_deps") - prerequisite_deps = semantics_extra_deps(PREREQUISITE_DEPS, semantics, "extra_prerequisites") - - attr_aspects = deps + runtime_deps + prerequisite_deps - - return aspect( - attrs = { - "_package_parser": attr.label( - default = tool_label("//tools/android:PackageParser"), - cfg = "host", - executable = True, - allow_files = True), - "_jar_filter": attr.label( - default = tool_label("//tools/android:JarFilter"), - cfg = "host", - executable = True, - allow_files = True), - }, - attr_aspects = attr_aspects, - fragments = ["cpp"], - implementation = aspect_impl, - required_aspect_providers = ["proto_java"], - ) -- cgit v1.2.3