Skip to content

Commit

Permalink
When compiling source jars, use the jar entry for each source file, n…
Browse files Browse the repository at this point in the history
…ot the full jar URL path (ie, "jar:file:jarfile-path!entry"). This makes the header comment less verbose, and simplifies use by indexing tools.

PiperOrigin-RevId: 561992973
  • Loading branch information
tomball authored and copybara-github committed Sep 1, 2023
1 parent 7200a2b commit aac6f99
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private enum State {

@VisibleForTesting
public GenerationUnit(String sourceName, Options options) {
this.sourceName = sourceName;
this.sourceName = relativePath(sourceName);
this.options = options;
}

Expand Down Expand Up @@ -258,6 +258,19 @@ public String getOutputPath() {
return outputPath;
}

/**
* If path is for a jar file entry ("jar:file:jar-file-path!entry-path"), return just the entry's
* relative path. Otherwise, return the specified path as is.
*/
private static final String relativePath(String path) {
int jarPathOffset = path.indexOf("!");
if (jarPathOffset != -1) {
return path.substring(jarPathOffset + 1);
} else {
return path;
}
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,18 +250,13 @@ private static class RelativeSourcePathGenerator extends ObjectiveCHeaderGenerat

@Override
public final void generate() {
// Jar file paths consist of the jar file's path + "!" + source's jar file entry path,
// so this extracts just the jar entry path.
String relativeSourcePath = getGenerationUnit().getSourceName().replace(".java", getSuffix());
int jarPathOffset = relativeSourcePath.indexOf("!");
if (jarPathOffset != -1) {
relativeSourcePath = relativeSourcePath.substring(jarPathOffset + 1);
}
// Create the source path for the header file being generated.
String sourcePath = getGenerationUnit().getSourceName().replace(".java", getSuffix());

// Don't generate if the relative source path header points to itself, which will happen
// with sources generated by annotation processors.
String outputPath = getOutputPath();
if (outputPath.equals(relativeSourcePath)) {
if (outputPath.equals(sourcePath)) {
return;
}

Expand All @@ -272,7 +267,7 @@ public final void generate() {
}

printf("#include \"%s\"\n", getOutputPath());
save(relativeSourcePath, getGenerationUnit().options().fileUtil().getHeaderOutputDirectory());
save(sourcePath, getGenerationUnit().options().fileUtil().getHeaderOutputDirectory());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,10 @@ private void makeAssertionsForJar() throws Exception {
String packageInfoH = getTranslatedFile("com/google/test/package-info.h");
String packageInfoM = getTranslatedFile("com/google/test/package-info.m");
// Test the file header comments.
assertTranslation(exampleH, "source: jar:file:");
assertTranslation(exampleM, "source: jar:file:");
assertTranslation(packageInfoH, "source: jar:file:");
assertTranslation(packageInfoM, "source: jar:file:");
assertTranslation(exampleH, jarPath);
assertTranslation(exampleM, jarPath);
assertTranslation(packageInfoH, jarPath);
assertTranslation(packageInfoM, jarPath);
assertTranslation(exampleH, "com/google/test/Example.java");
assertTranslation(exampleM, "com/google/test/Example.java");
assertTranslation(packageInfoH, "com/google/test/package-info.java");
assertTranslation(packageInfoM, "com/google/test/package-info.java");
assertTranslation(exampleH, "source: com/google/test/Example.java");
assertTranslation(exampleM, "source: com/google/test/Example.java");
assertTranslation(packageInfoH, "source: com/google/test/package-info.java");
assertTranslation(packageInfoM, "source: com/google/test/package-info.java");
assertNotInTranslation(packageInfoH, "J2ObjCTempDir");
assertNotInTranslation(packageInfoM, "J2ObjCTempDir");
// Test the includes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -962,4 +962,26 @@ public void testKytheMetadataMappings() throws IOException {
assertTranslation(kytheMetadata, "kythe0");
assertTranslation(kytheMetadata, "{\"type\":\"anchor_anchor\"");
}

// Verifies that properly encoded Kythe metadata and associated pragmas are generated when
// using the Kythe mapping flag with a source jar.
public void testKytheMetadataMappingsWithSourceJar() throws Exception {
options.setEmitKytheMappings(true);
addJarFile(
"some/path/test.jar", "foo/Test.java", "package foo; class Test { public void test() {}}");
runPipeline("some/path/test.jar");

String translation = getTranslatedFile("foo/Test.h");
assertTranslation(translation, "#ifdef KYTHE_IS_RUNNING");
assertTranslation(
translation, "#pragma kythe_inline_metadata" + " \"This file contains Kythe metadata.\"");
assertTranslation(translation, "/* This file contains Kythe metadata.");

// Verify metadata paths don't contain jar URL paths ...
String kytheMetadata = extractKytheMetadata(translation);
assertNotInTranslation(kytheMetadata, "jar:file:some/path/test.jar");

// ... just the source file entry in that jar file.
assertTranslation(kytheMetadata, "\"path\":\"foo/Test.java\"");
}
}

0 comments on commit aac6f99

Please sign in to comment.