Skip to content

Commit

Permalink
Update j2objc scripts to be py_binary targets.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 436904987
  • Loading branch information
Googler authored and Copybara-Service committed Mar 24, 2022
1 parent c07e66d commit 041f7ce
Show file tree
Hide file tree
Showing 12 changed files with 1,249 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,6 @@ private void registerJ2ObjcDeadCodeRemovalActions(
J2ObjcMappingFileProvider j2ObjcMappingFileProvider,
J2ObjcEntryClassProvider j2ObjcEntryClassProvider) {
NestedSet<String> entryClasses = j2ObjcEntryClassProvider.getEntryClasses();
Artifact pruner = ruleContext.getPrerequisiteArtifact("$j2objc_dead_code_pruner");
NestedSet<Artifact> j2ObjcDependencyMappingFiles =
j2ObjcMappingFileProvider.getDependencyMappingFiles();
NestedSet<Artifact> j2ObjcHeaderMappingFiles =
Expand Down Expand Up @@ -903,9 +902,8 @@ private void registerJ2ObjcDeadCodeRemovalActions(
XcodeConfigInfo.fromRuleContext(ruleContext),
appleConfiguration.getSingleArchPlatform())
.setMnemonic("DummyPruner")
.setExecutable(pruner)
.setExecutable(ruleContext.getExecutablePrerequisite("$j2objc_dead_code_pruner"))
.addInput(dummyArchive)
.addInput(pruner)
.addInput(j2objcArchive)
.addInput(xcrunwrapper(ruleContext).getExecutable())
.addTransitiveInputs(j2ObjcDependencyMappingFiles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant;
import com.google.devtools.build.lib.util.FileType;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.io.Serializable;
import java.util.ArrayList;
Expand Down Expand Up @@ -172,22 +171,20 @@ public AspectDefinition getDefinition(AspectParameters aspectParameters) {
toolsRepository + "//tools/j2objc:j2objc_deploy.jar")))
.add(
attr("$j2objc_wrapper", LABEL)
.allowedFileTypes(FileType.of(".py"))
.cfg(ExecutionTransitionFactory.create())
.exec()
.singleArtifact()
.legacyAllowAnyFileType()
.value(
Label.parseAbsoluteUnchecked(
toolsRepository + "//tools/j2objc:j2objc_wrapper")))
toolsRepository + "//tools/j2objc:j2objc_wrapper_binary")))
.add(
attr("$j2objc_header_map", LABEL)
.allowedFileTypes(FileType.of(".py"))
.cfg(ExecutionTransitionFactory.create())
.exec()
.singleArtifact()
.legacyAllowAnyFileType()
.value(
Label.parseAbsoluteUnchecked(
toolsRepository + "//tools/j2objc:j2objc_header_map")))
toolsRepository + "//tools/j2objc:j2objc_header_map_binary")))
.add(
attr("$jre_emul_jar", LABEL)
.cfg(ExecutionTransitionFactory.create())
Expand Down Expand Up @@ -584,8 +581,7 @@ private static J2ObjcMappingFileProvider createJ2ObjcTranspilationAction(
SpawnAction.Builder transpilationAction =
new SpawnAction.Builder()
.setMnemonic("TranspilingJ2objc")
.setExecutable(ruleContext.getPrerequisiteArtifact("$j2objc_wrapper"))
.addInput(ruleContext.getPrerequisiteArtifact("$j2objc_wrapper"))
.setExecutable(ruleContext.getExecutablePrerequisite("$j2objc_wrapper"))
.addInput(j2ObjcDeployJar)
.addInput(bootclasspathJar)
.addInputs(moduleFiles)
Expand Down Expand Up @@ -627,8 +623,7 @@ private static J2ObjcMappingFileProvider createJ2ObjcTranspilationAction(
ruleContext.registerAction(
new SpawnAction.Builder()
.setMnemonic("GenerateJ2objcHeaderMap")
.setExecutable(ruleContext.getPrerequisiteArtifact("$j2objc_header_map"))
.addInput(ruleContext.getPrerequisiteArtifact("$j2objc_header_map"))
.setExecutable(ruleContext.getExecutablePrerequisite("$j2objc_header_map"))
.addInputs(sources)
.addInputs(sourceJars)
.addCommandLine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,12 +664,15 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
.direct_compile_time_input()
.allowedFileTypes(FileTypeSet.ANY_FILE))
.add(
// This attribute definition must be kept in sync with
// third_party/bazel_rules/rules_apple/apple/internal/rule_factory.bzl
attr("$j2objc_dead_code_pruner", LABEL)
.allowedFileTypes(FileType.of(".py"))
.cfg(ExecutionTransitionFactory.create())
.exec()
.singleArtifact()
.value(env.getToolsLabel("//tools/objc:j2objc_dead_code_pruner")))
// Allow arbitrary executable files; this gives more flexibility for the
// implementation of the underlying tool.
.legacyAllowAnyFileType()
.value(env.getToolsLabel("//tools/objc:j2objc_dead_code_pruner_binary")))
.add(attr("$dummy_lib", LABEL).value(env.getToolsLabel("//tools/objc:dummy_lib")))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@
import com.google.devtools.build.lib.testutil.TestConstants;
import java.io.IOException;

/**
* Creates mock BUILD files required for J2Objc.
*/
/** Creates mock BUILD files required for J2Objc. */
public final class MockJ2ObjcSupport {
/**
* Setup the support for building with J2ObjC.
*/
/** Setup the support for building with J2ObjC. */
public static void setup(MockToolsConfig config) throws IOException {
config.create(TestConstants.TOOLS_REPOSITORY_SCRATCH + "third_party/java/j2objc/jre_emul.jar");
config.create(TestConstants.TOOLS_REPOSITORY_SCRATCH + "third_party/java/j2objc/mod/release");
Expand Down Expand Up @@ -79,12 +75,14 @@ public static void setup(MockToolsConfig config) throws IOException {
TestConstants.LOAD_PROTO_LANG_TOOLCHAIN,
"package(default_visibility=['//visibility:public'])",
"licenses(['notice'])",
"filegroup(",
" name = 'j2objc_wrapper',",
" srcs = ['j2objc_wrapper.py'])",
"filegroup(",
" name = 'j2objc_header_map',",
" srcs = ['j2objc_header_map.py'])",
"py_binary(",
" name = 'j2objc_wrapper_binary',",
" srcs = ['j2objc_wrapper_binary.py'],",
")",
"py_binary(",
" name = 'j2objc_header_map_binary',",
" srcs = ['j2objc_header_map_binary.py'],",
")",
"proto_lang_toolchain(",
" name = 'j2objc_proto_toolchain',",
" blacklisted_protos = [':j2objc_proto_blacklist'],",
Expand All @@ -94,7 +92,9 @@ public static void setup(MockToolsConfig config) throws IOException {
" plugin = '//third_party/java/j2objc:proto_plugin',",
" runtime = '//third_party/java/j2objc:proto_runtime',",
")",
"exports_files(['j2objc_deploy.jar'])",
"exports_files([",
" 'j2objc_deploy.jar',",
"])",
"proto_library(",
" name = 'j2objc_proto_blacklist',",
" deps = [",
Expand All @@ -112,12 +112,15 @@ public static void setup(MockToolsConfig config) throws IOException {

if (config.isRealFileSystem()) {
config.linkTool(TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/j2objc/j2objc_deploy.jar");
config.linkTool(TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/j2objc/j2objc_wrapper.py");
config.linkTool(TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/j2objc/j2objc_header_map.py");
config.linkTool(
TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/j2objc/j2objc_wrapper_binary");
config.linkTool(
TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/j2objc/j2objc_header_map_binary");
} else {
config.create(TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/j2objc/j2objc_deploy.jar");
config.create(TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/j2objc/j2objc_wrapper.py");
config.create(TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/j2objc/j2objc_header_map.py");
config.create(TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/j2objc/j2objc_wrapper_binary");
config.create(
TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/j2objc/j2objc_header_map_binary");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ public static void setup(MockToolsConfig config) throws IOException {
"filegroup(name = 'default_provisioning_profile', srcs = ['foo.mobileprovision'])",
"sh_binary(name = 'xcrunwrapper', srcs = ['xcrunwrapper.sh'])",
"filegroup(name = 'xctest_infoplist', srcs = ['xctest.plist'])",
"filegroup(name = 'j2objc_dead_code_pruner', srcs = ['j2objc_dead_code_pruner.py'])",
"py_binary(",
" name = 'j2objc_dead_code_pruner_binary',",
" srcs = ['j2objc_dead_code_pruner_binary.py']",
")",
"xcode_config(name = 'host_xcodes',",
" default = ':version7_3_1',",
" versions = [':version7_3_1', ':version5_0', ':version7_3', ':version5_8', ':version5'])",
Expand Down Expand Up @@ -208,7 +211,6 @@ public static void setup(MockToolsConfig config) throws IOException {
config.create(
TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/objc/foo.mobileprovision", "No such luck");
config.create(TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/objc/xctest.plist");
config.create(TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/objc/j2objc_dead_code_pruner.py");
setupCcToolchainConfig(config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,16 +529,17 @@ public void testJ2ObjcHeaderMappingAction() throws Exception {
getConfiguration(target).getBinDirectory(RepositoryName.MAIN).getExecPath() + "/";
assertThat(baseArtifactNames(headerMappingAction.getInputs()))
.containsAtLeast("libOne.java", "jar.srcjar");
assertThat(headerMappingAction.getArguments())
.containsExactly(
TestConstants.TOOLS_REPOSITORY_PATH_PREFIX + "tools/j2objc/j2objc_header_map.py",
"--source_files",
"java/com/google/transpile/libOne.java",
"--source_jars",
"java/com/google/transpile/jar.srcjar",
"--output_mapping_file",
execPath + "java/com/google/transpile/lib1.mapping.j2objc")
.inOrder();
assertThat(headerMappingAction.getArguments().get(0))
.contains("tools/j2objc/j2objc_header_map_binary");
assertThat(headerMappingAction.getArguments().get(1)).isEqualTo("--source_files");
assertThat(headerMappingAction.getArguments().get(2))
.isEqualTo("java/com/google/transpile/libOne.java");
assertThat(headerMappingAction.getArguments().get(3)).isEqualTo("--source_jars");
assertThat(headerMappingAction.getArguments().get(4))
.isEqualTo("java/com/google/transpile/jar.srcjar");
assertThat(headerMappingAction.getArguments().get(5)).isEqualTo("--output_mapping_file");
assertThat(headerMappingAction.getArguments().get(6))
.isEqualTo(execPath + "java/com/google/transpile/lib1.mapping.j2objc");
}

protected void checkObjcCompileActions(
Expand Down Expand Up @@ -1045,17 +1046,17 @@ public void testProtoToolchainForJ2ObjcFlag() throws Exception {
TestConstants.LOAD_PROTO_LANG_TOOLCHAIN,
"package(default_visibility=['//visibility:public'])",
"exports_files(['j2objc_deploy.jar'])",
"filegroup(",
" name = 'j2objc_wrapper',",
" srcs = ['j2objc_wrapper.py'],",
"py_binary(",
" name = 'j2objc_wrapper_binary',",
" srcs = ['j2objc_wrapper_binary.py'],",
")",
"proto_library(",
" name = 'excluded_protos',",
" srcs = ['proto_to_exclude.proto'],",
")",
"filegroup(",
" name = 'j2objc_header_map',",
" srcs = ['j2objc_header_map.py'],",
"py_binary(",
" name = 'j2objc_header_map_binary',",
" srcs = ['j2objc_header_map_binary.py'],",
")",
"proto_lang_toolchain(",
" name = 'alt_j2objc_proto_toolchain',",
Expand Down Expand Up @@ -1162,13 +1163,8 @@ public void testJ2ObjcDeadCodeRemovalActionWithOptFlag() throws Exception {
"com.google.app.test.test"));

SpawnAction deadCodeRemovalAction = (SpawnAction) getGeneratingAction(prunedArchive);
assertContainsSublist(
deadCodeRemovalAction.getArguments(),
new ImmutableList.Builder<String>()
.add(
TestConstants.TOOLS_REPOSITORY_PATH_PREFIX
+ "tools/objc/j2objc_dead_code_pruner.py")
.build());
assertThat(deadCodeRemovalAction.getArguments().get(0))
.contains("tools/objc/j2objc_dead_code_pruner_binary");
assertThat(deadCodeRemovalAction.getOutputs()).containsExactly(prunedArchive);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,10 @@ protected static void addAppleBinaryStarlarkRule(Scratch scratch) throws Excepti
" default = Label('" + toolsRepo + "//tools/cpp:grep-includes'),",
" ),",
" '_j2objc_dead_code_pruner': attr.label(",
" default = Label('" + toolsLoc + ":j2objc_dead_code_pruner'),),",
" executable = True,",
" allow_files=True,",
" cfg = 'exec',",
" default = Label('" + toolsLoc + ":j2objc_dead_code_pruner_binary'),),",
" '_xcode_config': attr.label(",
" default=configuration_field(",
" fragment='apple', name='xcode_config_label'),),",
Expand Down
13 changes: 13 additions & 0 deletions tools/j2objc/BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ java_binary(
runtime_deps = ["@bazel_j2objc//:j2objc"],
)

# TODO(b/225174999): Remove filegroups once blaze update is rolled out.
filegroup(
name = "j2objc_wrapper",
srcs = ["j2objc_wrapper.py"],
Expand All @@ -21,6 +22,18 @@ filegroup(
srcs = ["j2objc_header_map.py"],
)

py_binary(
name = "j2objc_wrapper_binary",
srcs = ["j2objc_wrapper_binary.py"],
python_version = "PY3",
)

py_binary(
name = "j2objc_header_map_binary",
srcs = ["j2objc_header_map_binary.py"],
python_version = "PY3",
)

proto_lang_toolchain(
name = "j2objc_proto_toolchain",
blacklisted_protos = [],
Expand Down
Loading

0 comments on commit 041f7ce

Please sign in to comment.