Skip to content

Commit 4a43465

Browse files
kevinjeon-gmattgodbolt
authored andcommitted
Support OpenJDK output for Android Java (compiler-explorer#7122)
This change adds OpenJDK as an output compiler for Android Java, beginning with 11.0.2. This change also updates java.ts to allow for input libraries, by parsing library paths and passing them into the javac invocation as '-cp \<path\>:\<path\>:...'. This is intended to simplify the process of inspecting javac output when Android Java is already selected so that a user doesn't have to switch to the Java compiler option. This also allows a user to view javac output with Android libraries.
1 parent 3b6a659 commit 4a43465

File tree

2 files changed

+154
-2
lines changed

2 files changed

+154
-2
lines changed

etc/config/android-java.amazon.properties

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
compilers=&android-d8:&android-r8:&dex2oat
1+
compilers=&android-d8:&android-r8:&dex2oat:&android-openjdk
22

33
group.android-d8.compilers=java-d8-8156:java-d8-8172:java-d8-8233:java-d8-8242:java-d8-8247:java-d8-8336:java-d8-8337:java-d8-8510:java-d8-8527:java-d8-8535
44
group.android-d8.compilerType=android-d8
@@ -139,6 +139,105 @@ compiler.java-dex2oat-3310.objdumper=/opt/compiler-explorer/dex2oat-33.10/x86_64
139139
compiler.java-dex2oat-3310.d8Id=java-d8-8242
140140
compiler.java-dex2oat-3310.profmanPath=/opt/compiler-explorer/dex2oat-33.10/x86_64/bin/profman
141141

142+
group.android-openjdk.compilers=android-openjdk-1102:android-openjdk-1201:android-openjdk-1202:android-openjdk-1302:android-openjdk-1402:android-openjdk-1502:android-openjdk-1601:android-openjdk-1700:android-openjdk-1702:android-openjdk-1800:android-openjdk-1802:android-openjdk-1902:android-openjdk-2000:android-openjdk-2002:android-openjdk-2100:android-openjdk-2102:android-openjdk-2200
143+
group.android-openjdk.compilerType=java
144+
group.android-openjdk.versionFlag=-version
145+
group.android-openjdk.groupName=OpenJDK
146+
group.android-openjdk.demangler=
147+
group.android-openjdk.postProcess=
148+
group.android-openjdk.options=
149+
group.android-openjdk.supportsBinary=false
150+
group.android-openjdk.needsMulti=false
151+
group.android-openjdk.supportsExecute=true
152+
group.android-openjdk.interpreted=true
153+
group.android-openjdk.isSemVer=true
154+
group.android-openjdk.baseName=jdk
155+
156+
compiler.android-openjdk-2200.exe=/opt/compiler-explorer/jdk-22.0.0/bin/javac
157+
compiler.android-openjdk-2200.objdumper=/opt/compiler-explorer/jdk-22.0.0/bin/javap
158+
compiler.android-openjdk-2200.runtime=/opt/compiler-explorer/jdk-22.0.0/bin/java
159+
compiler.android-openjdk-2200.semver=22.0.0
160+
161+
compiler.android-openjdk-2102.exe=/opt/compiler-explorer/jdk-21.0.2/bin/javac
162+
compiler.android-openjdk-2102.objdumper=/opt/compiler-explorer/jdk-21.0.2/bin/javap
163+
compiler.android-openjdk-2102.runtime=/opt/compiler-explorer/jdk-21.0.2/bin/java
164+
compiler.android-openjdk-2102.semver=21.0.2
165+
166+
compiler.android-openjdk-2100.exe=/opt/compiler-explorer/jdk-21.0.0/bin/javac
167+
compiler.android-openjdk-2100.objdumper=/opt/compiler-explorer/jdk-21.0.0/bin/javap
168+
compiler.android-openjdk-2100.runtime=/opt/compiler-explorer/jdk-21.0.0/bin/java
169+
compiler.android-openjdk-2100.semver=21.0.0
170+
171+
compiler.android-openjdk-2002.exe=/opt/compiler-explorer/jdk-20.0.2/bin/javac
172+
compiler.android-openjdk-2002.objdumper=/opt/compiler-explorer/jdk-20.0.2/bin/javap
173+
compiler.android-openjdk-2002.runtime=/opt/compiler-explorer/jdk-20.0.2/bin/java
174+
compiler.android-openjdk-2002.semver=20.0.2
175+
176+
compiler.android-openjdk-2000.exe=/opt/compiler-explorer/jdk-20.0.0/bin/javac
177+
compiler.android-openjdk-2000.objdumper=/opt/compiler-explorer/jdk-20.0.0/bin/javap
178+
compiler.android-openjdk-2000.runtime=/opt/compiler-explorer/jdk-20.0.0/bin/java
179+
compiler.android-openjdk-2000.semver=20.0.0
180+
181+
compiler.android-openjdk-1902.exe=/opt/compiler-explorer/jdk-19.0.2/bin/javac
182+
compiler.android-openjdk-1902.objdumper=/opt/compiler-explorer/jdk-19.0.2/bin/javap
183+
compiler.android-openjdk-1902.runtime=/opt/compiler-explorer/jdk-19.0.2/bin/java
184+
compiler.android-openjdk-1902.semver=19.0.2
185+
186+
compiler.android-openjdk-1802.exe=/opt/compiler-explorer/jdk-18.0.2/bin/javac
187+
compiler.android-openjdk-1802.objdumper=/opt/compiler-explorer/jdk-18.0.2/bin/javap
188+
compiler.android-openjdk-1802.runtime=/opt/compiler-explorer/jdk-18.0.2/bin/java
189+
compiler.android-openjdk-1802.semver=18.0.2
190+
191+
compiler.android-openjdk-1800.exe=/opt/compiler-explorer/jdk-18.0.0/bin/javac
192+
compiler.android-openjdk-1800.objdumper=/opt/compiler-explorer/jdk-18.0.0/bin/javap
193+
compiler.android-openjdk-1800.runtime=/opt/compiler-explorer/jdk-18.0.0/bin/java
194+
compiler.android-openjdk-1800.semver=18.0.0
195+
196+
compiler.android-openjdk-1702.exe=/opt/compiler-explorer/jdk-17.0.2/bin/javac
197+
compiler.android-openjdk-1702.objdumper=/opt/compiler-explorer/jdk-17.0.2/bin/javap
198+
compiler.android-openjdk-1702.runtime=/opt/compiler-explorer/jdk-17.0.2/bin/java
199+
compiler.android-openjdk-1702.semver=17.0.2
200+
201+
compiler.android-openjdk-1700.exe=/opt/compiler-explorer/jdk-17.0.0/bin/javac
202+
compiler.android-openjdk-1700.objdumper=/opt/compiler-explorer/jdk-17.0.0/bin/javap
203+
compiler.android-openjdk-1700.runtime=/opt/compiler-explorer/jdk-17.0.0/bin/java
204+
compiler.android-openjdk-1700.semver=17.0.0
205+
206+
compiler.android-openjdk-1601.exe=/opt/compiler-explorer/jdk-16.0.1/bin/javac
207+
compiler.android-openjdk-1601.objdumper=/opt/compiler-explorer/jdk-16.0.1/bin/javap
208+
compiler.android-openjdk-1601.runtime=/opt/compiler-explorer/jdk-16.0.1/bin/java
209+
compiler.android-openjdk-1601.semver=16.0.1
210+
211+
compiler.android-openjdk-1502.exe=/opt/compiler-explorer/jdk-15.0.2/bin/javac
212+
compiler.android-openjdk-1502.objdumper=/opt/compiler-explorer/jdk-15.0.2/bin/javap
213+
compiler.android-openjdk-1502.runtime=/opt/compiler-explorer/jdk-15.0.2/bin/java
214+
compiler.android-openjdk-1502.semver=15.0.2
215+
216+
compiler.android-openjdk-1402.exe=/opt/compiler-explorer/jdk-14.0.2/bin/javac
217+
compiler.android-openjdk-1402.objdumper=/opt/compiler-explorer/jdk-14.0.2/bin/javap
218+
compiler.android-openjdk-1402.runtime=/opt/compiler-explorer/jdk-14.0.2/bin/java
219+
compiler.android-openjdk-1402.semver=14.0.2
220+
221+
compiler.android-openjdk-1302.exe=/opt/compiler-explorer/jdk-13.0.2/bin/javac
222+
compiler.android-openjdk-1302.objdumper=/opt/compiler-explorer/jdk-13.0.2/bin/javap
223+
compiler.android-openjdk-1302.runtime=/opt/compiler-explorer/jdk-13.0.2/bin/java
224+
compiler.android-openjdk-1302.semver=13.0.2
225+
226+
compiler.android-openjdk-1202.exe=/opt/compiler-explorer/jdk-12.0.2/bin/javac
227+
compiler.android-openjdk-1202.objdumper=/opt/compiler-explorer/jdk-12.0.2/bin/javap
228+
compiler.android-openjdk-1202.runtime=/opt/compiler-explorer/jdk-12.0.2/bin/java
229+
compiler.android-openjdk-1202.semver=12.0.2
230+
231+
compiler.android-openjdk-1201.exe=/opt/compiler-explorer/jdk-12.0.1/bin/javac
232+
compiler.android-openjdk-1201.objdumper=/opt/compiler-explorer/jdk-12.0.1/bin/javap
233+
compiler.android-openjdk-1201.runtime=/opt/compiler-explorer/jdk-12.0.1/bin/java
234+
compiler.android-openjdk-1201.semver=12.0.1
235+
236+
compiler.android-openjdk-1102.exe=/opt/compiler-explorer/jdk-11.0.2/bin/javac
237+
compiler.android-openjdk-1102.objdumper=/opt/compiler-explorer/jdk-11.0.2/bin/javap
238+
compiler.android-openjdk-1102.runtime=/opt/compiler-explorer/jdk-11.0.2/bin/java
239+
compiler.android-openjdk-1102.semver=11.0.2
240+
142241
defaultCompiler=java-dex2oat-latest
143242

144243
libs=android-api-stubs:r8-keepanno:androidx-annotation

lib/compilers/java.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,19 @@ import path from 'path';
2626

2727
import fs from 'fs-extra';
2828
import Semver from 'semver';
29+
import _ from 'underscore';
2930

3031
import type {ParsedAsmResult, ParsedAsmResultLine} from '../../types/asmresult/asmresult.interfaces.js';
31-
import {BypassCache, CacheKey, CompilationResult} from '../../types/compilation/compilation.interfaces.js';
32+
import {
33+
BypassCache,
34+
CacheKey,
35+
CompilationResult,
36+
ExecutionOptionsWithEnv,
37+
} from '../../types/compilation/compilation.interfaces.js';
3238
import type {PreliminaryCompilerInfo} from '../../types/compiler.interfaces.js';
3339
import {ExecutableExecutionOptions} from '../../types/execution/execution.interfaces.js';
3440
import type {ParseFiltersAndOutputOptions} from '../../types/features/filters.interfaces.js';
41+
import type {SelectedLibraryVersion} from '../../types/libraries/libraries.interfaces.js';
3542
import {assert, unwrap} from '../assert.js';
3643
import {BaseCompiler, SimpleOutputFilenameCompiler} from '../base-compiler.js';
3744
import {CompilationEnvironment} from '../compilation-env.js';
@@ -48,6 +55,8 @@ export class JavaCompiler extends BaseCompiler implements SimpleOutputFilenameCo
4855
javaRuntime: string;
4956
mainRegex: RegExp;
5057

58+
libPaths: string[];
59+
5160
constructor(compilerInfo: PreliminaryCompilerInfo, env: CompilationEnvironment) {
5261
super(
5362
{
@@ -59,12 +68,42 @@ export class JavaCompiler extends BaseCompiler implements SimpleOutputFilenameCo
5968
);
6069
this.javaRuntime = this.compilerProps<string>(`compiler.${this.compiler.id}.runtime`);
6170
this.mainRegex = /public static ?(.*?) void main\(java\.lang\.String\[]\)/;
71+
this.libPaths = [];
6272
}
6373

6474
override getSharedLibraryPathsAsArguments() {
6575
return [];
6676
}
6777

78+
override async runCompiler(
79+
compiler: string,
80+
options: string[],
81+
inputFilename: string,
82+
execOptions: ExecutionOptionsWithEnv,
83+
filters?: ParseFiltersAndOutputOptions,
84+
): Promise<CompilationResult> {
85+
if (!execOptions) {
86+
execOptions = this.getDefaultExecOptions();
87+
}
88+
89+
if (!execOptions.customCwd) {
90+
execOptions.customCwd = path.dirname(inputFilename);
91+
}
92+
93+
// The items in 'options' before the source file are user inputs.
94+
const sourceFileOptionIndex = options.findIndex(option => {
95+
return option.endsWith('.java');
96+
});
97+
const userOptions = options.slice(0, sourceFileOptionIndex);
98+
const javaOptions = _.compact([...this.getClasspathArgument(), ...userOptions, inputFilename]);
99+
const result = await this.exec(compiler, javaOptions, execOptions);
100+
return {
101+
...this.transformToCompilationResult(result, inputFilename),
102+
languageId: this.getCompilerResultLanguageId(filters),
103+
instructionSet: this.getInstructionSetFromCompilerArgs(options),
104+
};
105+
}
106+
68107
override async objdump(outputFilename: string, result: any, maxSize: number) {
69108
const dirPath = path.dirname(outputFilename);
70109
const files = await fs.readdir(dirPath);
@@ -417,4 +456,18 @@ export class JavaCompiler extends BaseCompiler implements SimpleOutputFilenameCo
417456
textsBeforeMethod,
418457
};
419458
}
459+
460+
getClasspathArgument(): string[] {
461+
const libString = this.libPaths.join(':');
462+
return libString ? ['-cp', libString] : [''];
463+
}
464+
465+
override getIncludeArguments(libraries: SelectedLibraryVersion[], dirPath: string): string[] {
466+
this.libPaths = libraries.flatMap(selectedLib => {
467+
const foundVersion = this.findLibVersion(selectedLib);
468+
if (!foundVersion) return [];
469+
return foundVersion.path;
470+
});
471+
return this.libPaths;
472+
}
420473
}

0 commit comments

Comments
 (0)