Skip to content

Commit

Permalink
Extract jextract task
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Oct 6, 2024
1 parent 7ad2732 commit 6813cdf
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 47 deletions.
82 changes: 35 additions & 47 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,6 @@ testing {
}
}

task downloadJextract(type: DownloadTask) {
def getOs = {
if(currentOs.isMacOsX()) {
return "macos"
}
if(currentOs.isLinux()) {
return "linux"
}
throw new IllegalStateException("Unsupported operating system: ${currentOs}")
}
def getArch = {
def arch = System.getProperty("os.arch")
switch(arch) {
case "amd64":
return "x64"
case "x86_64":
return "x64"
case "aarch64":
return "aarch64"
default:
throw new IllegalStateException("Unsupported architecture: ${arch}")
}
}
sourceUrl = "https://download.java.net/java/early_access/jextract/22/5/openjdk-22-jextract+5-33_${getOs()}-${getArch()}_bin.tar.gz"
target = file("${project.buildDir}/jextract.tar.gz")
}

task unpackJextract(type: Copy, dependsOn: [tasks.downloadJextract]) {
from tarTree(resources.gzip(tasks.downloadJextract.outputs.files[0]))
into file("${project.buildDir}/jextract/")
}

task buildLua(type: Exec) {
def getMakeGoal = {
if(currentOs.isLinux()) {
Expand Down Expand Up @@ -142,22 +110,42 @@ task cleanLua(type: Exec) {

clean.dependsOn(tasks.cleanLua)

task generateNativeInterface(type: Exec, dependsOn: [tasks.unpackJextract]) {
def jextractBinary = "${tasks.unpackJextract.outputs.files[0]}/jextract-22/bin/jextract"
def includeDir = "${project.rootDir}/lua/src"
def generatedSrc = "${project.buildDir}/generated/sources/jextract/"
commandLine jextractBinary,
'--include-dir', includeDir,
'--output', generatedSrc,
'--target-package', 'org.itsallcode.luava.ffi',
'--library', 'lua',
'--header-class-name', 'Lua',
"$includeDir/all_lua.h"
inputs.dir(includeDir)
outputs.dir(generatedSrc)
doLast {

task downloadJextract(type: DownloadTask) {
def getOs = {
if(currentOs.isMacOsX()) {
return "macos"
}
if(currentOs.isLinux()) {
return "linux"
}
throw new IllegalStateException("Unsupported operating system: ${currentOs}")
}
def getArch = {
def arch = System.getProperty("os.arch")
switch(arch) {
case "amd64":
return "x64"
case "x86_64":
return "x64"
case "aarch64":
return "aarch64"
default:
throw new IllegalStateException("Unsupported architecture: ${arch}")
}
}
sourceUrl = "https://download.java.net/java/early_access/jextract/22/5/openjdk-22-jextract+5-33_${getOs()}-${getArch()}_bin.tar.gz"
target = file("${project.buildDir}/jextract.tar.gz")
}

task unpackJextract(type: Copy, dependsOn: [tasks.downloadJextract]) {
from tarTree(resources.gzip(tasks.downloadJextract.outputs.files[0]))
into file("${project.buildDir}/jextract/")
}

task generateNativeInterface(type: JextractTask, dependsOn: [tasks.unpackJextract]) {
jextractBinary = new File(tasks.unpackJextract.outputs.files[0], "jextract-22/bin/jextract")
includeDir = new File(project.rootDir, "lua/src")
outputDir = new File(project.buildDir, "generated/sources/jextract/")
}

compileJava {
Expand Down
37 changes: 37 additions & 0 deletions buildSrc/src/main/groovy/JextractTask.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity

@CacheableTask
class JextractTask extends DefaultTask {
@InputFile @PathSensitive(PathSensitivity.RELATIVE) File jextractBinary

@InputDirectory @PathSensitive(PathSensitivity.RELATIVE) File includeDir

@OutputDirectory File outputDir

@TaskAction
void generate() {
logger.info("Generating using binary ${jextractBinary} from ${includeDir} to ${outputDir}...")
def arguments = [
'--include-dir', includeDir,
'--output', outputDir,
'--target-package', 'org.itsallcode.luava.ffi',
'--library', 'lua',
'--header-class-name', 'Lua',
"$includeDir/all_lua.h"
]
project.exec {
workingDir project.rootDir
executable jextractBinary
args arguments
}
}
}

0 comments on commit 6813cdf

Please sign in to comment.