mike-neck / graalvm-native-image-plugin

A Gradle plugin which creates a native executable via GraalVM's native-image. This is a thin wrapper of the native-image command.
Apache License 2.0
89 stars 14 forks source link

Allow building shared libraries #145

Closed tomfi closed 3 years ago

tomfi commented 3 years ago

Is it possible to add the option / flag to build a shared library with this plugin?

When adding the --shared flag to GraalVM it should not require a main-class anymore (https://www.graalvm.org/reference-manual/native-image/ImplementingNativeMethodsInJavaWithSVM/#create-a-shared-library)

At the moment the maven plugin support it quite well but i could not find a good gradle plugin that supports it.

As a workaround i did the following configuration:

nativeImage {
  mainClass = "demo.MainKt"
  executableName = project.name
  arguments(
    "--shared",
  )
}

Where demo.MainKt is a stub:

package demo

fun main() {
}

This results in building a shared library by graalvm as expected.

It would be great if the mainClass and executableName were optional to allow building shared libraries with this plugin without workarounds.

Thanks in advance, this plugin is great 👍

mike-neck commented 3 years ago

The mainClass property cannot be optional, because nativeImage task and generateNativeImageConfig task are sharing the same mainClass property. Without setting mainClass property, the later task(generateNativeImageConfig) will fail. This means that we needs refactoring this plugin for realizing this feature. We have to introduce a convention object with the same interface to nativeImage task.

mike-neck commented 3 years ago

It seems that the shared library feature is limited to Java11 or later.

mike-neck commented 3 years ago

In PR #147

Example configuration of building shared library

nativeImage {
  buildType { BuildTypeSelector build ->
    build.sharedLibrary
  }
}

Example configuration of building executable

nativeImage {
  buildType { BuildTypeSelector build ->
    build.executable { main = 'com.example.Main' }
  }
}
mike-neck commented 3 years ago

The feature will be available at v1.4.0.

tomfi commented 3 years ago

@mike-neck thank you very much :)