Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraalVM native shared library for scala #5551

Open
burakakca opened this issue Dec 1, 2022 · 4 comments
Open

GraalVM native shared library for scala #5551

burakakca opened this issue Dec 1, 2022 · 4 comments
Assignees
Labels

Comments

@burakakca
Copy link

Feature request

Please include the following information:

I'm looking for a Native Shared Library and I want to achieve with scala

Is your feature request related to a problem? Please describe.

Error: Entry point method MyMath$.incrementFunctionScala is not static. Add a static modifier to the method.

import com.google.common.math.IntMath
import org.graalvm.nativeimage.IsolateThread
import org.graalvm.nativeimage.c.function.CEntryPoint


class MyMath {
  override def toString = "Hello"
}

object MyMath {
  var count = 0

  @CEntryPoint(name = "ceilingPowerOfTwoScala")
  def ceilingPowerOfTwoScala(thread: IsolateThread, x: Int): Int = {
    count = IntMath.ceilingPowerOfTwo(x)
    count
  }

  @CEntryPoint(name = "incrementFunctionScala")
  def incrementFunctionScala(thread: IsolateThread): Int = IntMath.checkedAdd(count, 1)
}
Task :app:nativeCompile
[native-image-plugin] GraalVM Toolchain detection is enabled
[native-image-plugin] GraalVM uses toolchain detection. Selected:
[native-image-plugin]    - language version: 17
[native-image-plugin]    - vendor: GraalVM Community
[native-image-plugin]    - runtime version: 17.0.5+8-jvmci-22.3-b08
[native-image-plugin] Native Image executable path: /home/burak/graalvm-ce-java17-22.3.0/lib/svm/bin/native-image
========================================================================================================================
GraalVM Native Image: Generating 'libmymath' (shared library)...
========================================================================================================================

[1/7] Initializing...                                                                                    (0.0s @ 0.14GB)
Error: Entry point method MyMath$.incrementFunctionScala is not static. Add a static modifier to the method.
Error: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception
------------------------------------------------------------------------------------------------------------------------
                        0.2s (8.7% of total time) in 10 GCs | Peak RSS: 0.63GB | CPU load: 4.03
========================================================================================================================
Failed generating 'libmymath' after 1.8s.

On another hand, without that example, I get the below section of output and there is no .h header file for my object

class MyMath {
  override def toString = "Hello"
}

object MyMath {
  def getFoo = "Foo"
}

Screenshot from 2022-12-01 14-33-23

Describe who do you think will benefit the most.
to reach scala environment

@oubidar-Abderrahim
Copy link
Member

Thank you for your report, tracked internally on GR 42873

@tarsa
Copy link

tarsa commented Dec 16, 2022

Error: Entry point method MyMath$.incrementFunctionScala is not static. Add a static modifier to the method.

import com.google.common.math.IntMath
import org.graalvm.nativeimage.IsolateThread
import org.graalvm.nativeimage.c.function.CEntryPoint


class MyMath {
  override def toString = "Hello"
}

object MyMath {
  var count = 0

  @CEntryPoint(name = "ceilingPowerOfTwoScala")
  def ceilingPowerOfTwoScala(thread: IsolateThread, x: Int): Int = {
    count = IntMath.ceilingPowerOfTwo(x)
    count
  }

  @CEntryPoint(name = "incrementFunctionScala")
  def incrementFunctionScala(thread: IsolateThread): Int = IntMath.checkedAdd(count, 1)
}

if you're using scala 3 then probably adding @static annotation could help. the final code would be something like:

import com.google.common.math.IntMath
import org.graalvm.nativeimage.IsolateThread
import org.graalvm.nativeimage.c.function.CEntryPoint

import scala.annotation.static

class MyMath {
  override def toString = "Hello"
}

object MyMath {
  @static // i'm not sure if it's needed here
  var count = 0

  @CEntryPoint(name = "ceilingPowerOfTwoScala")
  @static
  def ceilingPowerOfTwoScala(thread: IsolateThread, x: Int): Int = {
    count = IntMath.ceilingPowerOfTwo(x)
    count
  }

  @CEntryPoint(name = "incrementFunctionScala")
  @static
  def incrementFunctionScala(thread: IsolateThread): Int = IntMath.checkedAdd(count, 1)
}

simpler example: https://scastie.scala-lang.org/Wj7SuiilQRuXsKgmxXQlAA

@burakakca
Copy link
Author

Yes, It works in scala 3 but my codebase in scala 2

@tarsa
Copy link

tarsa commented Dec 18, 2022

Yes, It works in scala 3 but my codebase in scala 2

it seems that the problem is more with scala 2 than with native-image.

as a workaround, you can mix java and scala code, e.g. define an entrypoint in java that calls methods on scala singleton objects. theoretically, graalvm native-image could do that workaround automatically, but then it would need to be able to recognize singletons and treat their methods somewhat like static ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants