Skip to content

Commit ee8b81f

Browse files
committed
Use NATIVE_IMAGE_OPTIONS if possible.
1 parent 3e1010f commit ee8b81f

File tree

4 files changed

+97
-39
lines changed

4 files changed

+97
-39
lines changed

dist/cleanup/index.js

Lines changed: 29 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main/index.js

Lines changed: 30 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/features/reports.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import * as c from '../constants'
22
import * as core from '@actions/core'
33
import * as fs from 'fs'
44
import * as github from '@actions/github'
5+
import * as semver from 'semver'
56
import {join} from 'path'
67
import {tmpdir} from 'os'
78
import {createPRComment, isPREvent, toSemVer} from '../utils'
8-
import {gte} from 'semver'
99

1010
const BUILD_OUTPUT_JSON_PATH = join(tmpdir(), 'native-image-build-output.json')
1111
const BYTES_TO_KiB = 1024
@@ -19,6 +19,7 @@ const NATIVE_IMAGE_CONFIG_FILE = join(
1919
tmpdir(),
2020
'native-image-options.properties'
2121
)
22+
const NATIVE_IMAGE_OPTIONS_ENV = 'NATIVE_IMAGE_OPTIONS'
2223
const NATIVE_IMAGE_CONFIG_FILE_ENV = 'NATIVE_IMAGE_CONFIG_FILE'
2324

2425
interface AnalysisResult {
@@ -91,6 +92,7 @@ interface BuildOutput {
9192

9293
export async function setUpNativeImageBuildReports(
9394
isGraalVMforJDK17OrLater: boolean,
95+
javaVersionOrDev: string,
9496
graalVMVersion: string
9597
): Promise<void> {
9698
const isRequired = areJobReportsEnabled() || arePRReportsEnabled()
@@ -102,14 +104,15 @@ export async function setUpNativeImageBuildReports(
102104
graalVMVersion === c.VERSION_LATEST ||
103105
graalVMVersion === c.VERSION_DEV ||
104106
(!graalVMVersion.startsWith(c.MANDREL_NAMESPACE) &&
105-
gte(toSemVer(graalVMVersion), '22.2.0'))
107+
semver.gte(toSemVer(graalVMVersion), '22.2.0'))
106108
if (!isSupported) {
107109
core.warning(
108110
`Build reports for PRs and job summaries are only available in GraalVM 22.2.0 or later. This build job uses GraalVM ${graalVMVersion}.`
109111
)
110112
return
111113
}
112114
setNativeImageOption(
115+
javaVersionOrDev,
113116
`-H:BuildOutputJSONFile=${BUILD_OUTPUT_JSON_PATH.replace(/\\/g, '\\\\')}`
114117
) // Escape backslashes for Windows
115118
}
@@ -144,6 +147,34 @@ function arePRReportsEnabled(): boolean {
144147
return isPREvent() && core.getInput(INPUT_NI_PR_REPORTS) === 'true'
145148
}
146149

150+
function setNativeImageOption(
151+
javaVersionOrDev: string,
152+
optionValue: string
153+
): void {
154+
const coercedJavaVersionOrDev = semver.coerce(javaVersionOrDev)
155+
if (
156+
(coercedJavaVersionOrDev &&
157+
semver.gte(coercedJavaVersionOrDev, '22.0.0')) ||
158+
javaVersionOrDev === c.VERSION_DEV ||
159+
javaVersionOrDev.endsWith('-ea')
160+
) {
161+
/* NATIVE_IMAGE_OPTIONS was introduced in GraalVM for JDK 22 (so were EA builds). */
162+
let newOptionValue = optionValue
163+
const existingOptions = process.env[NATIVE_IMAGE_OPTIONS_ENV]
164+
if (existingOptions) {
165+
newOptionValue = `${existingOptions} ${newOptionValue}`
166+
}
167+
core.exportVariable(NATIVE_IMAGE_OPTIONS_ENV, newOptionValue)
168+
} else {
169+
const optionsFile = getNativeImageOptionsFile()
170+
if (fs.existsSync(optionsFile)) {
171+
fs.appendFileSync(optionsFile, ` ${optionValue}`)
172+
} else {
173+
fs.writeFileSync(optionsFile, `NativeImageArgs = ${optionValue}`)
174+
}
175+
}
176+
}
177+
147178
function getNativeImageOptionsFile(): string {
148179
let optionsFile = process.env[NATIVE_IMAGE_CONFIG_FILE_ENV]
149180
if (optionsFile === undefined) {
@@ -153,15 +184,6 @@ function getNativeImageOptionsFile(): string {
153184
return optionsFile
154185
}
155186

156-
function setNativeImageOption(value: string): void {
157-
const optionsFile = getNativeImageOptionsFile()
158-
if (fs.existsSync(optionsFile)) {
159-
fs.appendFileSync(optionsFile, ` ${value}`)
160-
} else {
161-
fs.writeFileSync(optionsFile, `NativeImageArgs = ${value}`)
162-
}
163-
}
164-
165187
function createReport(data: BuildOutput): string {
166188
const context = github.context
167189
const info = data.general_info

src/main.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ async function run(): Promise<void> {
160160
if (cache && isCacheAvailable()) {
161161
await restore(cache)
162162
}
163-
setUpNativeImageBuildReports(isGraalVMforJDK17OrLater, graalVMVersion)
163+
setUpNativeImageBuildReports(
164+
isGraalVMforJDK17OrLater,
165+
javaVersion,
166+
graalVMVersion
167+
)
164168

165169
core.startGroup(`Successfully set up '${basename(graalVMHome)}'`)
166170
await exec(join(graalVMHome, 'bin', `java${c.EXECUTABLE_SUFFIX}`), [

0 commit comments

Comments
 (0)