Skip to content

Commit

Permalink
Introduce native-image-musl feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Jan 21, 2022
1 parent b1f6593 commit bc24649
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 18 deletions.
21 changes: 20 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,27 @@ jobs:
java --version
native-image.cmd --version
if: runner.os == 'Windows'
test-native-image-musl:
name: native-image-musl on ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run setup-graalvm action
uses: ./
with:
version: 'latest'
java-version: '17'
components: 'native-image'
features: 'native-image-musl'
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Build static HelloWorld image with Native Image and musl
run: |
echo 'public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }' > HelloWorld.java
javac HelloWorld.java
native-image --static --libc=musl HelloWorld
./helloworld
test-additional:
name: Extensive tests on ubuntu-latest
name: extensive tests on ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,18 @@ jobs:
| `version`<br>*(required)* | n/a | `X.Y.Z` (e.g., `22.0.0`) for a specific [GraalVM release][releases]<br>`latest` for [latest stable release][stable],<br>`dev` for [latest dev build][dev-build],<br>`mandrel-X.Y.Z` (e.g., `mandrel-21.3.0.0-Final`) for a specific [Mandrel release][mandrel-releases],<br>`mandrel-latest` for [latest Mandrel stable release][mandrel-stable]. |
| `java-version`<br>*(required)* | n/a | `'11'` or `'17'` for a specific Java version.<br>(`'8'` and `'16'` are supported for GraalVM 21.2 and earlier.) |
| `components` | `''` | Comma-spearated list of GraalVM components (e.g., `native-image` or `ruby,nodejs`) that will be installed by the [GraalVM Updater][gu]. |
| `features` | `''` | Comma-separated list of [optional features](#optional-features). |
| `github-token` | `''` | Token for communication with the GitHub API. Please set to `${{ secrets.GITHUB_TOKEN }}` (see [templates](#templates)) to allow the action to authenticate with the GitHub API, which helps to reduce rate limiting issues. |
| `set-java-home` | `'true'` | If set to `'true'`, instructs the action to set `$JAVA_HOME` to the path of the GraalVM installation. |


## Optional Features

| Name | Description |
|---------------------|-------------|
| `native-image-musl` | Sets up [musl] for building [static images][native-image-static] with GraalVM Native Image *(Linux only)*. [Example usage][native-image-musl-build] (be sure to replace `uses: ./` with `uses: graalvm/setup-graalvm@v1`). |


## Contributing

We welcome code contributions. To get started, you will need to sign the [Oracle Contributor Agreement][oca] (OCA).
Expand All @@ -108,7 +117,10 @@ Only pull requests from committers that can be verified as having signed the OCA
[mandrel]: https://github.com/graalvm/mandrel
[mandrel-releases]: https://github.com/graalvm/mandrel/releases
[mandrel-stable]: https://github.com/graalvm/mandrel/releases/latest
[musl]: https://musl.libc.org/
[native-image]: https://www.graalvm.org/native-image/
[native-image-musl-build]: https://github.com/graalvm/setup-graalvm/blob/main/.github/workflows/test.yml
[native-image-static]: https://github.com/oracle/graal/blob/fa6f4a974dedacf4688dcc430dd100849d9882f2/docs/reference-manual/native-image/StaticImages.md
[oca]: https://oca.opensource.oracle.com
[releases]: https://github.com/graalvm/graalvm-ce-builds/releases
[repo]: https://github.com/oracle/graal
Expand Down
10 changes: 7 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ inputs:
description: 'Java version (11 or 17, 8 or 16 for older releases).'
components:
required: false
description: 'Comma-separated list of GraalVM components to be installed'
description: 'Comma-separated list of GraalVM components to be installed.'
default: ''
features:
required: false
description: 'Comma-separated list of optional features (see README.md).'
default: ''
github-token:
required: false
description: 'Set it to secrets.GITHUB_TOKEN to increase rate limits when accessing the GitHub API'
description: 'Set it to secrets.GITHUB_TOKEN to increase rate limits when accessing the GitHub API.'
default: ''
set-java-home:
required: false
description: 'Set $JAVA_HOME to the GraalVM installation. Default: true'
description: 'Set $JAVA_HOME to the GraalVM installation. Default: true.'
default: 'true'
runs:
using: 'node12'
Expand Down
114 changes: 108 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as otypes from '@octokit/types'
import {homedir} from 'os'
import {join} from 'path'

export const IS_LINUX = process.platform === 'linux'
export const IS_MACOS = process.platform === 'darwin'
export const IS_WINDOWS = process.platform === 'win32'

Expand Down
64 changes: 64 additions & 0 deletions src/features.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as core from '@actions/core'
import * as tc from '@actions/tool-cache'
import {IS_LINUX} from './constants'
import {exec} from '@actions/exec'
import {homedir} from 'os'
import {join} from 'path'
import {mkdirP} from '@actions/io'

export async function enableFeatures(features: string[]): Promise<void> {
for (const feature of features) {
await enableFeature(feature)
}
}

async function enableFeature(feature: string): Promise<void> {
switch (feature) {
case 'native-image-musl':
await enableNativeImageMusl()
break
default:
core.warning(`Unknown feature requested: ${feature}`)
break
}
}

async function enableNativeImageMusl(): Promise<void> {
if (!IS_LINUX) {
core.warning('musl is only supported on Linux')
return
}
core.startGroup(`Setting up musl for GraalVM Native Image...`)
const basePath = join(homedir(), '.musl_feature')
await mkdirP(basePath)

const muslName = 'x86_64-linux-musl-native'
const muslDownloadPath = await tc.downloadTool(
`http://more.musl.cc/10/x86_64-linux-musl/${muslName}.tgz`
)
await tc.extractTar(muslDownloadPath, basePath)
const muslPath = join(basePath, muslName)
core.addPath(join(muslPath, 'bin'))

const zlibVersion = '1.2.11'
const zlibDownloadPath = await tc.downloadTool(
`https://zlib.net/zlib-${zlibVersion}.tar.gz`
)
await tc.extractTar(zlibDownloadPath, basePath)
const zlibPath = join(basePath, `zlib-${zlibVersion}`)
const zlibBuildOptions = {
cwd: zlibPath,
env: {
...process.env,
CC: join(muslPath, 'bin', 'gcc')
}
}
await exec(
'./configure',
[`--prefix=${muslPath}`, '--static'],
zlibBuildOptions
)
await exec('make', [], zlibBuildOptions)
await exec('make', ['install'], {cwd: zlibPath})
core.endGroup()
}
18 changes: 11 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as c from './constants'
import * as core from '@actions/core'
import * as graalvm from './graalvm'
import {enableFeatures} from './features'
import {extractCommaSeparatedList} from './utils'
import {join} from 'path'
import {mkdirP} from '@actions/io'
import {setUpDependencies} from './dependencies'
Expand All @@ -10,17 +12,17 @@ import {setUpWindowsEnvironment} from './msvc'

async function run(): Promise<void> {
try {
const graalvmVersion: string = core.getInput('version', {required: true})
const javaVersion: string = core.getInput('java-version', {required: true})
const componentsString: string = core.getInput('components')
const components: string[] =
componentsString.length > 0 ? componentsString.split(',') : []
const graalvmVersion = core.getInput('version', {required: true})
const javaVersion = core.getInput('java-version', {required: true})
const components = extractCommaSeparatedList(core.getInput('components'))
const features = extractCommaSeparatedList(core.getInput('features'))
const setJavaHome = core.getInput('set-java-home') === 'true'

if (c.IS_WINDOWS) {
setUpWindowsEnvironment()
}
setUpDependencies(components)
await setUpDependencies(components)
await enableFeatures(features)

await mkdirP(c.GRAALVM_BASE)

Expand Down Expand Up @@ -57,7 +59,9 @@ async function run(): Promise<void> {
if (components.length > 0) {
if (graalvmVersion.startsWith(c.MANDREL_NAMESPACE)) {
core.warning(
`Mandrel does not support GraalVM components: ${componentsString}`
`Mandrel does not support GraalVM components: ${core.getInput(
'components'
)}`
)
} else {
await setUpGUComponents(graalVMHome, components)
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ export function findJavaHomeInSubfolder(searchPath: string): string {
)
}
}

export function extractCommaSeparatedList(list: string): string[] {
return list.length > 0 ? list.split(',') : []
}

0 comments on commit bc24649

Please sign in to comment.