Skip to content

Commit d721f0f

Browse files
committed
[MCOMPILER-542] rework log and conditions to run
1 parent 8420d58 commit d721f0f

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

Diff for: src/it/MCOMPILER-542/pom.xml

+38-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,46 @@
4848
<groupId>org.apache.maven.plugins</groupId>
4949
<artifactId>maven-compiler-plugin</artifactId>
5050
<version>@project.version@</version>
51-
<configuration>
52-
<release>${java.specification.version}</release>
53-
</configuration>
5451
</plugin>
5552
</plugins>
5653
</pluginManagement>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-compiler-plugin</artifactId>
58+
<executions>
59+
<execution>
60+
<id>default-compile</id>
61+
<goals>
62+
<goal>compile</goal>
63+
</goals>
64+
<configuration>
65+
<release>${java.specification.version}</release>
66+
</configuration>
67+
</execution>
68+
<execution>
69+
<id>compile-release-9</id>
70+
<goals>
71+
<goal>compile</goal>
72+
</goals>
73+
<configuration>
74+
<release>9</release>
75+
<outputDirectory>${project.build.outputDirectory}-9</outputDirectory>
76+
</configuration>
77+
</execution>
78+
<execution>
79+
<id>compile-target</id>
80+
<goals>
81+
<goal>compile</goal>
82+
</goals>
83+
<configuration>
84+
<source>${java.specification.version}</source>
85+
<target>${java.specification.version}</target>
86+
<outputDirectory>${project.build.outputDirectory}-target</outputDirectory>
87+
</configuration>
88+
</execution>
89+
</executions>
90+
</plugin>
91+
</plugins>
5792
</build>
5893
</project>

Diff for: src/it/MCOMPILER-542/verify.groovy

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ if (javaVersion != javaSpecVersion) { // handle the case when is the first relea
4747
}
4848
assert out.contains('// ' + javaSpecVersion) : "java specification version not found in module descriptor"
4949

50-
// Additional validation that the checksum is always the same
50+
// Additional validation that the checksum is always the same: useful because constant pool reordering happens when
51+
// transforming bytecode, then we need to check results precisely
5152
def checksumMap = [
5253
'21': 'SHA-256 checksum ccc6515c8fc1bf4e675e205b2a5200d02545b06014b304c292eeddc68cffee8d',
5354
'17': 'SHA-256 checksum 102f24c71aff97210d66ef791b7d56f8a25ff8692d2c97b21682bc7170aaca9c',

Diff for: src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,16 @@ public abstract class AbstractCompilerMojo extends AbstractMojo {
598598

599599
@Parameter(defaultValue = "false", property = "maven.compiler.showCompilationChanges")
600600
private boolean showCompilationChanges = false;
601+
602+
/**
603+
* Timestamp for reproducible output archive entries, either formatted as ISO 8601
604+
* <code>yyyy-MM-dd'T'HH:mm:ssXXX</code> or as an int representing seconds since the epoch (like
605+
* <a href="https://reproducible-builds.org/docs/source-date-epoch/">SOURCE_DATE_EPOCH</a>).
606+
* @since 3.12.0
607+
*/
608+
@Parameter(defaultValue = "${project.build.outputTimestamp}")
609+
private String outputTimestamp;
610+
601611
/**
602612
* Resolves the artifacts needed.
603613
*/
@@ -1186,7 +1196,10 @@ public void execute() throws MojoExecutionException, CompilationFailureException
11861196
}
11871197
}
11881198

1189-
patchJdkModuleVersion(compilerResult, sources);
1199+
if (outputTimestamp != null && (outputTimestamp.length() > 1 || Character.isDigit(outputTimestamp.charAt(0)))) {
1200+
// if Reproducible Builds mode, apply workaround
1201+
patchJdkModuleVersion(compilerResult, sources);
1202+
}
11901203

11911204
if (useIncrementalCompilation) {
11921205
if (incrementalBuildHelperRequest.getOutputDirectory().exists()) {
@@ -1811,7 +1824,7 @@ final String getImplicit() {
18111824
}
18121825

18131826
/**
1814-
* Patch module-info.class to set the java release version for java/jdk modules.
1827+
* JDK-8318913 workaround: Patch module-info.class to set the java release version for java/jdk modules.
18151828
*
18161829
* @param compilerResult should succeed.
18171830
* @param sources the list of the source files to check for the "module-info.java"
@@ -1827,7 +1840,9 @@ private void patchJdkModuleVersion(CompilerResult compilerResult, Set<File> sour
18271840
final byte[] descriptorOriginal = Files.readAllBytes(moduleDescriptor);
18281841
final byte[] descriptorMod =
18291842
ModuleInfoTransformer.transform(descriptorOriginal, getRelease(), getLog());
1830-
Files.write(moduleDescriptor, descriptorMod);
1843+
if (descriptorMod != null) {
1844+
Files.write(moduleDescriptor, descriptorMod);
1845+
}
18311846
} catch (IOException ex) {
18321847
throw new MojoExecutionException("Error reading or writing module-info.class", ex);
18331848
}

Diff for: src/main/java/org/apache/maven/plugin/compiler/ModuleInfoTransformer.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
package org.apache.maven.plugin.compiler;
2020

2121
import java.util.ArrayList;
22+
import java.util.HashSet;
2223
import java.util.List;
24+
import java.util.Set;
2325

2426
import org.apache.maven.plugin.logging.Log;
2527
import org.objectweb.asm.ClassReader;
@@ -28,12 +30,18 @@
2830
import org.objectweb.asm.ModuleVisitor;
2931
import org.objectweb.asm.Opcodes;
3032

33+
/**
34+
* {@code module-info.class} bytecode transformer for
35+
* <a href="https://issues.apache.org/jira/browse/MCOMPILER-542">MCOMPILER-542</a>:
36+
* drops detailed JDK version.
37+
*/
3138
final class ModuleInfoTransformer {
3239

3340
private ModuleInfoTransformer() {}
3441

3542
static byte[] transform(byte[] originalBytecode, String javaVersion, Log log) {
3643
List<String> modulesModified = new ArrayList<>();
44+
Set<String> foundVersions = new HashSet<>();
3745
ClassReader reader = new ClassReader(originalBytecode);
3846
ClassWriter writer = new ClassWriter(0);
3947

@@ -45,10 +53,11 @@ public ModuleVisitor visitModule(String name, int access, String version) {
4553
@Override
4654
public void visitRequire(String module, int access, String version) {
4755
// Check if the module name matches the java/jdk modules
48-
if (module.startsWith("java.") || module.startsWith("jdk.")) {
56+
if (version != null && (module.startsWith("java.") || module.startsWith("jdk."))) {
4957
// Patch the version from the java.* and jdk.* modules
5058
// with the --release N version.
5159
super.visitRequire(module, access, javaVersion);
60+
foundVersions.add(version);
5261
modulesModified.add(module);
5362
} else {
5463
// Keep the original require statement
@@ -61,7 +70,14 @@ public void visitRequire(String module, int access, String version) {
6170

6271
reader.accept(classVisitor, 0);
6372

64-
log.info(String.format("Patch module-info.class %s with version %s", modulesModified, javaVersion));
73+
if (modulesModified.isEmpty()) {
74+
return null;
75+
}
76+
77+
log.info(String.format(
78+
"JDK-8318913 workaround: patched module-info.class requires version from %s to [%s] on %d JDK modules %s",
79+
foundVersions, javaVersion, modulesModified.size(), modulesModified));
80+
6581
return writer.toByteArray();
6682
}
6783
}

0 commit comments

Comments
 (0)