Skip to content

Commit

Permalink
Implement dump-includes
Browse files Browse the repository at this point in the history
  • Loading branch information
tamaracha committed Sep 26, 2024
1 parent a30fe12 commit 5d975c3
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import de.infolektuell.gradle.download.tasks.DownloadTask
import de.infolektuell.gradle.jextract.extensions.JextractExtension
import de.infolektuell.gradle.jextract.extensions.ResourceHandler
import de.infolektuell.gradle.download.tasks.ExtractTask
import de.infolektuell.gradle.jextract.tasks.DumpIncludesTask
import de.infolektuell.gradle.jextract.tasks.GenerateBindingsTask
import org.gradle.api.JavaVersion
import org.gradle.api.NamedDomainObjectContainer
Expand Down Expand Up @@ -36,28 +37,31 @@ abstract class GradleJextractPlugin : Plugin<Project> {
task.source.set(downloadTask.get().target)
task.target.set(userOutput.dir("jextract").dir(resource.integrity.checksum.map { it.substring(0, 8) }))
}
extension.generator.local.convention(extractTask.flatMap { it.target })

extension.libraries.all { lib ->
lib.useSystemLoadLibrary.convention(false)
project.tasks.register("${lib.name}Jextract", GenerateBindingsTask::class.java) { task ->
task.run {
group = "Build"
outputDirectory.convention(project.layout.buildDirectory.dir("generated/sources/jextract/main/java"))
description = "Generates bindings for the ${lib.name} library using Jextract"
if (extension.generator.local.isPresent){
generator.set(extension.generator.local)
} else {
generator.set(extractTask.get().target)
}
header.set(lib.header)
definedMacros.set(lib.definedMacros)
whitelist.set(lib.whitelist.mapProvider)
targetPackage.set(lib.targetPackage)
headerClassName.set(lib.headerClassName)
includes.set(lib.includes)
libraries.set(lib.libraries)
useSystemLoadLibrary.set(lib.useSystemLoadLibrary)
}
task.group = "Build"
task.description = "Generates bindings for the ${lib.name} library using Jextract"
task.outputDirectory.convention(project.layout.buildDirectory.dir("generated/sources/jextract/main/java"))
task.generator.location.set(extension.generator.local)
task.header.set(lib.header)
task.definedMacros.set(lib.definedMacros)
task.whitelist.set(lib.whitelist.mapProvider)
task.argFile.set(lib.whitelist.argFile)
task.targetPackage.set(lib.targetPackage)
task.headerClassName.set(lib.headerClassName)
task.includes.set(lib.includes)
task.libraries.set(lib.libraries)
task.useSystemLoadLibrary.set(lib.useSystemLoadLibrary)
}
project.tasks.register("${lib.name}DumpIncludes", DumpIncludesTask::class.java) { task ->
task.group = "documentation"
task.description = "Generates a dump of all symbols encountered in a header file"
task.generator.location.set(extension.generator.local)
task.header.set(lib.header)
task.argFile.convention(project.layout.buildDirectory.file("reports/jextract/${lib.name}-includes.txt"))
}
}
project.plugins.withType(JavaPlugin::class.java) { _ ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.infolektuell.gradle.jextract.extensions

import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
Expand All @@ -19,6 +20,8 @@ abstract class WhitelistHandler @Inject constructor(providers: ProviderFactory)
abstract val unions: ListProperty<String>
/** Global variables to include */
abstract val variables: ListProperty<String>
/** A file containing all includes as args, maybe generated by --dump-includes */
abstract val argFile: RegularFileProperty
/** All included definitions in a map provider, simplifies usage by task */
val mapProvider: Provider<Map<String, List<String>>> = providers.provider {
mapOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package de.infolektuell.gradle.jextract.tasks

import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.*
import org.gradle.api.tasks.options.Option

abstract class DumpIncludesTask : DefaultTask() {
@get:Nested
abstract val generator: Generator
@get:InputFile
abstract val header: RegularFileProperty
@get:OutputFile
@get:Option(option = "arg-file", description = "The file to dump includes to")
abstract val argFile: RegularFileProperty
@TaskAction
fun dump() {
generator.execute { spec ->
spec.args("--dump-includes", argFile.get().asFile.absolutePath)
spec.args(header.get().asFile.absolutePath)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ package de.infolektuell.gradle.jextract.tasks
import org.gradle.api.DefaultTask
import org.gradle.api.file.Directory
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileSystemOperations
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
import org.gradle.process.ExecOperations
import javax.inject.Inject

abstract class GenerateBindingsTask @Inject constructor(private var execOperations: ExecOperations) : DefaultTask() {
@get:InputDirectory
abstract val generator: DirectoryProperty
abstract class GenerateBindingsTask @Inject constructor(private val fileSystemOperations: FileSystemOperations) : DefaultTask() {
@get:Nested
abstract val generator: Generator
@get:InputFile
abstract val header: RegularFileProperty
@get:Optional
Expand All @@ -31,6 +30,9 @@ abstract class GenerateBindingsTask @Inject constructor(private var execOperatio

@get:Input
abstract val whitelist: MapProperty<String, List<String>>
@get:Optional
@get:InputFile
abstract val argFile: RegularFileProperty

@get:Input
abstract val libraries: ListProperty<String>
Expand All @@ -40,12 +42,10 @@ abstract class GenerateBindingsTask @Inject constructor(private var execOperatio
abstract val outputDirectory: DirectoryProperty
@TaskAction
fun execute() {
val cmdFile = generator.asFileTree.matching { spec ->
val fileName = if (DefaultNativePlatform.getCurrentOperatingSystem().isWindows) "jextract.bat" else "jextract"
spec.include("**/bin/$fileName")
}.singleFile
execOperations.exec { spec ->
spec.executable(cmdFile.absolutePath)
fileSystemOperations.delete { spec ->
spec.delete(outputDirectory)
}
generator.execute { spec ->
spec.args("--output", outputDirectory.get())
targetPackage.orNull?.let { spec.args("-t", it) }
headerClassName.orNull?.let { spec.args("--header-class-name", it) }
Expand All @@ -57,6 +57,7 @@ abstract class GenerateBindingsTask @Inject constructor(private var execOperatio
}
libraries.get().forEach { spec.args("-l", it) }
if (useSystemLoadLibrary.get()) spec.args("--use-system-load-library")
argFile.orNull?.let { spec.args("@$it") }
spec.args(header.get())
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.infolektuell.gradle.jextract.tasks

import org.gradle.api.Action
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.Internal
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
import org.gradle.process.ExecOperations
import org.gradle.process.ExecSpec
import java.io.File
import javax.inject.Inject

abstract class Generator @Inject constructor(objects: ObjectFactory, private val execOperations: ExecOperations) {
@get:InputDirectory
val location: DirectoryProperty = objects.directoryProperty()
@get:Internal
val executable: Provider<File> = location.map { g ->
g.asFileTree.matching { spec ->
val fileName = if (DefaultNativePlatform.getCurrentOperatingSystem().isWindows) "jextract.bat" else "jextract"
spec.include("**/bin/$fileName")
}.singleFile
}
fun execute(action: Action<in ExecSpec>) {
execOperations.exec { spec ->
spec.executable(executable.get())
action.execute(spec)
}
}
}

0 comments on commit 5d975c3

Please sign in to comment.