Skip to content

Commit

Permalink
Accept --target_label, --injecting_rule_kind in Turbine.
Browse files Browse the repository at this point in the history
The values (if present) are written into the manifest with this format:

Target-Label: <label>
Injecting-Rule-Kind: <kind>

In the future, JavaBuilder will make sure of this instead of command
line arguments to find owners for jars for its add_dep commands.

MOE_MIGRATED_REVID=185008500
  • Loading branch information
tomlu authored and cushon committed Feb 8, 2018
1 parent 5c62aeb commit 2c7d119
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
40 changes: 38 additions & 2 deletions java/com/google/turbine/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.turbine.tree.Tree.CompUnit;
import com.google.turbine.zip.Zip;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
Expand All @@ -47,15 +48,25 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;

/** Main entry point for the turbine CLI. */
public class Main {

private static final int BUFFER_SIZE = 65536;

// These attributes are used by JavaBuilder, Turbine, and ijar.
// They must all be kept in sync.
static final String MANIFEST_DIR = "META-INF/";
static final String MANIFEST_NAME = JarFile.MANIFEST_NAME;
static final Attributes.Name TARGET_LABEL = new Attributes.Name("Target-Label");
static final Attributes.Name INJECTING_RULE_KIND = new Attributes.Name("Injecting-Rule-Kind");

public static void main(String[] args) throws IOException {
compile(args);
}
Expand Down Expand Up @@ -96,7 +107,7 @@ public static boolean compile(TurbineOptions options) throws IOException {
}
}

writeOutput(Paths.get(options.outputFile()), lowered.bytes(), transitive);
writeOutput(options, lowered.bytes(), transitive);
return true;
}

Expand Down Expand Up @@ -154,7 +165,9 @@ private static ImmutableList<CompUnit> parseAll(TurbineOptions options) throws I

/** Write bytecode to the output jar. */
private static void writeOutput(
Path path, Map<String, byte[]> lowered, Map<String, byte[]> transitive) throws IOException {
TurbineOptions options, Map<String, byte[]> lowered, Map<String, byte[]> transitive)
throws IOException {
Path path = Paths.get(options.outputFile());
try (OutputStream os = Files.newOutputStream(path);
BufferedOutputStream bos = new BufferedOutputStream(os, BUFFER_SIZE);
JarOutputStream jos = new JarOutputStream(bos)) {
Expand All @@ -165,6 +178,10 @@ private static void writeOutput(
addEntry(
jos, ClassPathBinder.TRANSITIVE_PREFIX + entry.getKey() + ".class", entry.getValue());
}
if (options.targetLabel().isPresent()) {
addEntry(jos, MANIFEST_DIR, new byte[] {});
addEntry(jos, MANIFEST_NAME, manifestContent(options));
}
}
}

Expand All @@ -178,6 +195,25 @@ private static void addEntry(JarOutputStream jos, String name, byte[] bytes) thr
jos.write(bytes);
}

private static byte[] manifestContent(TurbineOptions turbineOptions) throws IOException {
Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
Attributes.Name createdBy = new Attributes.Name("Created-By");
if (attributes.getValue(createdBy) == null) {
attributes.put(createdBy, "bazel");
}
if (turbineOptions.targetLabel().isPresent()) {
attributes.put(TARGET_LABEL, turbineOptions.targetLabel().get());
}
if (turbineOptions.injectingRuleKind().isPresent()) {
attributes.put(INJECTING_RULE_KIND, turbineOptions.injectingRuleKind().get());
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
manifest.write(out);
return out.toByteArray();
}

private static ImmutableList<Path> toPaths(Iterable<String> paths) {
ImmutableList.Builder<Path> result = ImmutableList.builder();
for (String path : paths) {
Expand Down
19 changes: 19 additions & 0 deletions java/com/google/turbine/options/TurbineOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class TurbineOptions {
private final ImmutableMap<String, String> jarToTarget;
private final ImmutableSet<String> directJars;
private final Optional<String> targetLabel;
private final Optional<String> injectingRuleKind;
private final ImmutableList<String> depsArtifacts;
private final Optional<String> ruleKind;
private final boolean javacFallback;
Expand All @@ -61,6 +62,7 @@ private TurbineOptions(
ImmutableMap<String, String> jarToTarget,
ImmutableSet<String> directJars,
@Nullable String targetLabel,
@Nullable String injectingRuleKind,
ImmutableList<String> depsArtifacts,
@Nullable String ruleKind,
boolean javacFallback,
Expand All @@ -79,6 +81,7 @@ private TurbineOptions(
this.jarToTarget = checkNotNull(jarToTarget, "jarToTarget must not be null");
this.directJars = checkNotNull(directJars, "directJars must not be null");
this.targetLabel = Optional.fromNullable(targetLabel);
this.injectingRuleKind = Optional.fromNullable(injectingRuleKind);
this.depsArtifacts = checkNotNull(depsArtifacts, "depsArtifacts must not be null");
this.ruleKind = Optional.fromNullable(ruleKind);
this.javacFallback = javacFallback;
Expand Down Expand Up @@ -175,6 +178,15 @@ public Optional<String> targetLabel() {
return targetLabel;
}

/**
* If present, the name of the rule that injected an aspect that compiles this target.
*
* <p>Note that this rule will have a completely different label to {@link #targetLabel} above.
*/
public Optional<String> injectingRuleKind() {
return injectingRuleKind;
}

/** The .jdeps artifacts for direct dependencies. */
public ImmutableList<String> depsArtifacts() {
return depsArtifacts;
Expand Down Expand Up @@ -220,6 +232,7 @@ public static class Builder {
private final ImmutableMap.Builder<String, String> jarToTarget = ImmutableMap.builder();
private final ImmutableSet.Builder<String> directJars = ImmutableSet.builder();
@Nullable private String targetLabel;
@Nullable private String injectingRuleKind;
private final ImmutableList.Builder<String> depsArtifacts = ImmutableList.builder();
@Nullable private String ruleKind;
private boolean javacFallback = true;
Expand All @@ -241,6 +254,7 @@ public TurbineOptions build() {
jarToTarget.build(),
directJars.build(),
targetLabel,
injectingRuleKind,
depsArtifacts.build(),
ruleKind,
javacFallback,
Expand Down Expand Up @@ -321,6 +335,11 @@ public Builder setTargetLabel(String targetLabel) {
return this;
}

public Builder setInjectingRuleKind(String injectingRuleKind) {
this.injectingRuleKind = injectingRuleKind;
return this;
}

public Builder addAllDepsArtifacts(Iterable<String> depsArtifacts) {
this.depsArtifacts.addAll(depsArtifacts);
return this;
Expand Down
3 changes: 3 additions & 0 deletions java/com/google/turbine/options/TurbineOptionsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ private static void parse(TurbineOptions.Builder builder, Deque<String> argument
case "--target_label":
builder.setTargetLabel(readOne(argumentDeque));
break;
case "--injecting_rule_kind":
builder.setInjectingRuleKind(readOne(argumentDeque));
break;
case "--rule_kind":
builder.setRuleKind(readOne(argumentDeque));
break;
Expand Down
32 changes: 32 additions & 0 deletions javatests/com/google/turbine/main/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -158,4 +162,32 @@ public void moduleInfos() throws IOException {
assertThat(data.keySet())
.containsExactly("foo/module-info.class", "bar/module-info.class", "baz/module-info.class");
}

@Test
public void testManifest() throws IOException {
Path src = temporaryFolder.newFile("Foo.java").toPath();
Files.write(src, "class Foo {}".getBytes(UTF_8));

Path output = temporaryFolder.newFile("output.jar").toPath();

boolean ok =
Main.compile(
optionsWithBootclasspath()
.addSources(ImmutableList.of(src.toString()))
.setTargetLabel("//foo:foo")
.setInjectingRuleKind("foo_library")
.setOutput(output.toString())
.build());
assertThat(ok).isTrue();

try (JarFile jarFile = new JarFile(output.toFile())) {
Manifest manifest = jarFile.getManifest();
Attributes attributes = manifest.getMainAttributes();
assertThat(attributes.getValue("Target-Label")).isEqualTo("//foo:foo");
assertThat(attributes.getValue("Injecting-Rule-Kind")).isEqualTo("foo_library");
assertThat(jarFile.getEntry(JarFile.MANIFEST_NAME).getLastModifiedTime().toInstant())
.isEqualTo(
Instant.ofEpochMilli(new GregorianCalendar(1980, 0, 1, 0, 0, 0).getTimeInMillis()));
}
}
}
4 changes: 4 additions & 0 deletions javatests/com/google/turbine/options/TurbineOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public void exhaustiveArgs() throws Exception {
"out.jdeps",
"--target_label",
"//java/com/google/test",
"--injecting_rule_kind",
"foo_library",
"--rule_kind",
"java_library",
};
Expand All @@ -102,6 +104,7 @@ public void exhaustiveArgs() throws Exception {
assertThat(options.sources()).containsExactly("Source1.java", "Source2.java");
assertThat(options.outputDeps()).hasValue("out.jdeps");
assertThat(options.targetLabel()).hasValue("//java/com/google/test");
assertThat(options.injectingRuleKind()).hasValue("foo_library");
assertThat(options.ruleKind()).hasValue("java_library");
}

Expand Down Expand Up @@ -259,6 +262,7 @@ public void optionalTargetLabelAndRuleKind() throws Exception {

assertThat(options.ruleKind()).isAbsent();
assertThat(options.targetLabel()).isAbsent();
assertThat(options.injectingRuleKind()).isAbsent();
}

@Test
Expand Down

0 comments on commit 2c7d119

Please sign in to comment.