Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions .github/workflows/build-native.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
name: Build Native Image

on:
push:
branches:
- '*'
- '!refs/tags/.*'
tags-ignore:
- '*'
pull_request:
types: [opened, reopened, synchronize]
branches:
- '*'
- '!refs/tags/.*'
tags-ignore:
- '*'

env:
GRAALVM_VERSION: '21'
GRAALVM_DISTRIBUTION: 'graalvm'
JAVA_VERSION: '21'

jobs:
build:
name: Build ${{ matrix.platform }}
runs-on: ${{ matrix.os }}
if: "!contains(github.event.head_commit.message, '[skip ci]')"
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
platform: linux-amd64
- os: ubuntu-24.04-arm
platform: linux-arm64
- os: macos-latest-large
platform: macos-intel
- os: macos-latest-xlarge
platform: macos-silicon

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup GraalVM
uses: graalvm/setup-graalvm@v1
with:
java-version: ${{ env.JAVA_VERSION }}
distribution: ${{ env.GRAALVM_DISTRIBUTION }}
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4

- name: Build native image
run: |
chmod +x ./gradlew ./lsp-simulator.sh ./build-native.sh
./build-native.sh

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: nf-language-server-${{ matrix.platform }}
path: build/native/nativeCompile/nlsp
retention-days: 7

release:
name: Create Release
needs: build
runs-on: ubuntu-latest
if: contains(github.event.head_commit.message, '[release]')
permissions:
contents: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: artifacts

- name: Prepare release assets
run: |
mkdir -p release
find artifacts -name "*.tar.gz" -exec cp {} release/ \;
cd release
sha256sum *.tar.gz > checksums.txt
cat checksums.txt

- name: Extract version from tag
id: version
run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
name: Release ${{ steps.version.outputs.VERSION }}
draft: false
prerelease: ${{ contains(github.ref, '-rc') || contains(github.ref, '-beta') || contains(github.ref, '-alpha') }}
generate_release_notes: true
files: |
release/*.tar.gz
release/checksums.txt
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.gradle/
build/
.idea/
build/
185 changes: 185 additions & 0 deletions build-native.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#!/bin/bash
set -euo pipefail

#
# Build script for GraalVM native image of nf-language-server
# Supports: linux/amd64, linux/arm64, macos/amd64, macos/arm64 (Apple Silicon)
#
# Usage:
# ./build-native.sh [options]
#
# Options:
# --skip-test Skip testing the native binary
# --help, -h Show this help message
#

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

# Colors for output (disabled in CI)
if [[ -t 1 ]] && [[ -z "${CI:-}" ]]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
else
RED=''
GREEN=''
YELLOW=''
NC=''
fi

log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}

# Check for required tools
check_requirements() {
log_info "Checking requirements..."

# Check for Java
if ! command -v java &> /dev/null; then
log_error "Java is not installed or not in PATH"
exit 1
fi

JAVA_VERSION=$(java -version 2>&1 | head -1 | cut -d'"' -f2 | cut -d'.' -f1)
if [[ "$JAVA_VERSION" -lt 17 ]]; then
log_error "Java 17+ is required, found version $JAVA_VERSION"
exit 1
fi

# Check for native-image
if ! command -v native-image &> /dev/null; then
# Try to find it in JAVA_HOME
if [[ -n "${JAVA_HOME:-}" ]] && [[ -x "$JAVA_HOME/bin/native-image" ]]; then
export PATH="$JAVA_HOME/bin:$PATH"
elif [[ -n "${GRAALVM_HOME:-}" ]] && [[ -x "$GRAALVM_HOME/bin/native-image" ]]; then
export PATH="$GRAALVM_HOME/bin:$PATH"
export JAVA_HOME="$GRAALVM_HOME"
else
log_error "native-image not found. Please install GraalVM with native-image support."
log_error "You can use SDKMAN: sdk install java 21.0.1-graal && sdk use java 21.0.1-graal"
exit 1
fi
fi

# Verify native-image works
if ! native-image --version &> /dev/null; then
log_error "native-image is not working correctly"
exit 1
fi

log_info "Using Java: $(java -version 2>&1 | head -1)"
log_info "Using native-image: $(native-image --version 2>&1 | head -1)"
}

# Detect platform
detect_platform() {
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"

case "$OS" in
linux|darwin)
BINARY_EXT=""
;;
mingw*|msys*|cygwin*)
BINARY_EXT=".exe"
;;
*)
log_error "Unsupported OS: $OS"
exit 1
;;
esac
}

# Build native image (includes shadow jar, tracing agent, and native compilation)
build_native_image() {
log_info "Building native image (this includes JAR build and tracing agent)..."
./gradlew nativeCompile --no-configuration-cache --no-daemon

local BINARY_PATH="build/native/nativeCompile/nlsp${BINARY_EXT}"
if [[ -f "$BINARY_PATH" ]]; then
log_info "Native image built successfully: $BINARY_PATH"
ls -lh "$BINARY_PATH"
else
log_error "Native image build failed - binary not found"
exit 1
fi
}

# Test the native binary
test_native_binary() {
log_info "Testing native binary..."

local BINARY_PATH="build/native/nativeCompile/nlsp${BINARY_EXT}"
local OUTPUT

OUTPUT=$(./lsp-simulator.sh | "$BINARY_PATH" 2>&1 | head -20)

if echo "$OUTPUT" | grep -q '"id":1,"result"'; then
log_info "Native binary test passed - LSP initialize succeeded"
else
log_error "Native binary test failed"
echo "$OUTPUT"
exit 1
fi
}


# Main build process
main() {
local SKIP_TEST=false

# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--skip-test)
SKIP_TEST=true
shift
;;
--help|-h)
echo "Usage: $0 [options]"
echo ""
echo "Build GraalVM native image for nf-language-server"
echo ""
echo "Options:"
echo " --skip-test Skip testing the native binary"
echo " --help, -h Show this help message"
echo ""
echo "Requirements:"
echo " - GraalVM 21+ with native-image"
echo " - Use SDKMAN: sdk install java 21.0.1-graal"
exit 0
;;
*)
log_error "Unknown option: $1"
exit 1
;;
esac
done

log_info "Starting native image build..."
echo ""

check_requirements
detect_platform
build_native_image

if [[ "$SKIP_TEST" != "true" ]]; then
test_native_binary
fi

echo ""
log_info "Build completed successfully!"
log_info "Binary: build/native/nativeCompile/nlsp${BINARY_EXT}"
}

main "$@"
68 changes: 68 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'com.gradleup.shadow' version '8.3.5'
id 'org.graalvm.buildtools.native' version '0.10.4'
id 'application'
id 'groovy'
id 'java'
Expand Down Expand Up @@ -93,6 +94,7 @@ processResources {

jar {
dependsOn buildSpec
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from("$buildDir/generated") {
include 'definitions.json'
into 'spec'
Expand All @@ -106,3 +108,69 @@ application {
shadowJar {
archiveVersion = ''
}

task generateNativeImageConfig(type: Exec) {
description = 'Generate native-image configuration using tracing agent'
group = 'native'

dependsOn shadowJar

def agentOutputDir = file("$buildDir/native-image-agent")
def simulateScript = file("$projectDir/lsp-simulator.sh")

inputs.file(shadowJar.archiveFile)
inputs.file(simulateScript)
outputs.dir(agentOutputDir)

doFirst {
agentOutputDir.mkdirs()
}

commandLine 'bash', '-c', """
${simulateScript.absolutePath} | java \\
-agentlib:native-image-agent=config-output-dir=${agentOutputDir.absolutePath} \\
-cp ${shadowJar.archiveFile.get().asFile.absolutePath} \\
nextflow.lsp.NextflowLanguageServer
"""
}

tasks.named('nativeCompile') {
dependsOn generateNativeImageConfig
}

graalvmNative {
agent {
defaultMode = 'standard'
builtinCallerFilter = true
builtinHeuristicFilter = true
enableExperimentalPredefinedClasses = false
enableExperimentalUnsafeAllocationTracing = false
trackReflectionMetadata = true
metadataCopy {
inputTaskNames.add('run')
outputDirectories.add("$buildDir/native-image-agent")
mergeWithExisting = true
}
}
binaries {
main {
imageName = 'nlsp'
mainClass = 'nextflow.lsp.NextflowLanguageServer'
buildArgs.addAll([
'--no-fallback',
'--enable-url-protocols=http,https',
'-H:+ReportExceptionStackTraces',
'--report-unsupported-elements-at-runtime',
'--initialize-at-build-time=org.slf4j',
'--initialize-at-build-time=org.apache.groovy',
'--initialize-at-build-time=groovy',
'--initialize-at-build-time=org.codehaus.groovy',
'--initialize-at-build-time=groovyjarjarantlr4',
'--initialize-at-build-time=java.beans',
'--initialize-at-build-time=com.sun.beans',
"-H:ConfigurationFileDirectories=$buildDir/native-image-agent,$projectDir/conf/native-image"
])
}
}
toolchainDetection = false
}
12 changes: 12 additions & 0 deletions conf/native-image/reflect-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"name": "[Ljava.lang.reflect.AccessFlag;",
"allPublicMethods": true,
"allPublicFields": true
},
{
"name": "java.lang.reflect.AccessFlag",
"allPublicMethods": true,
"allPublicFields": true
}
]
Loading