yglukhov / jnim

Nim - Java bridge
MIT License
184 stars 13 forks source link

Way to disable jvm_finder? #52

Closed akavel closed 5 years ago

akavel commented 5 years ago

Hi! I want to use jnim to compile JNI code, but I don't have (and don't want) JVM installed. In particular, I'm using a cross-compiler from the Android NDK, to compile some code to be used in an .apk on an Android device. I copied just the jni_wrapper.nim and commented out the jvm_finder import and the code fragments using it — and this let me build a .so that worked on the device. I would prefer to use the full proper jni package instead of the single copied file; could it be possible to somehow make jnim work correctly when a JVM is not installed? At least those parts of it that don't require JVM?

yglukhov commented 5 years ago

Iirc jnim-0.5 branch should be good for you.

akavel commented 5 years ago

I tried installing the branch with nimble, but got an error message like below:

C:\prog\dali>nimble install jnim@#jnim-0.5
Downloading https://github.com/yglukhov/jnim using git
       Tip: 13 messages have been suppressed, use --verbose to show them.
     Error: Could not read package info file in C:\Users\Mateusz\AppData\Local\Temp\nimble_4360\githubcom_yglukhovjnim_#jnim-0.5\jnim.nimble;
        ...   Reading as ini file failed with:
        ...     Invalid section: .
        ...   Evaluating as NimScript file failed with:
        ...     C:\dnload\nim-etc\nim\lib\windows\winlean.nim(549, 3) Error: cannot 'importc' variable at compile time.

Do you have any idea what this might mean, and if I could get it resolved somehow?

My Nim version:

C:\prog\dali>nim --version
Nim Compiler Version 0.19.4 [Windows: amd64]
Compiled at 2019-02-01
Copyright (c) 2006-2018 by Andreas Rumpf

git hash: b6d96cafc8bcad1f3d32f2910b25cd11a93f7751
active boot switches: -d:release
yglukhov commented 5 years ago

Please try the latest master. With the latest nim :)

akavel commented 5 years ago

Looks like it worked, cool, thanks! :) :heart: Will this feature get into the next version release? edit: Ok, IIUC, this is already part of v0.5, and seems to be working when imported as such; awesome, thanks! :)

akavel commented 5 years ago

Or not? :/ I'm confused, not sure if it works or not in the end :/ must check some more... :/

akavel commented 5 years ago

No, not working :( it seemed to work in a different project, which was compiling as a library for Android. But in an executable, still doesn't work, apparently — see below:

C:\prog\jnim_no_jvm>git clone https://github.com/akavel/jnim_no_jvm .

C:\prog\jnim_no_jvm>nim --version
Nim Compiler Version 0.20.2 [Windows: i386]
Compiled at 2019-07-17
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: 88a0edba4b1a3d535b54336fd589746add54e937
active boot switches: -d:release

C:\prog\jnim_no_jvm>nimble install jnim@0.5
Downloading https://github.com/yglukhov/jnim using git
  Verifying dependencies for jnim@0.5.0
 Installing jnim@0.5.0
    Prompt: jnim@0.5.0 already exists. Overwrite? [y/N]
    Answer: y
   Success: jnim installed successfully.

C:\prog\jnim_no_jvm>nimble build
  Verifying dependencies for jnim_no_jvm@0.1.0
      Info: Dependency on jnim@0.5 already satisfied
  Verifying dependencies for jnim@0.5.0
   Building jnim_no_jvm/jnim_no_jvm.exe using c backend
       Tip: 2 messages have been suppressed, use --verbose to show them.
     Error: Build failed for package: jnim_no_jvm
        ... Details:
        ... Execution failed with exit code 1
        ... Command: "C:\Users\Mateusz\.nimble\bin\nim.exe" c --noBabelPath --path:"C:\Users\Mateusz\.nimble\pkgs\jnim-0.5.0"  -o:"C:\prog\jnim_no_jvm\jnim_no_jvm.exe" "C:\prog\jnim_no_jvm\src\jnim_no_jvm.nim"
        ... Output: Hint: used config file 'C:\Users\Mateusz\.choosenim\toolchains\nim-0.20.2\config\nim.cfg' [Conf]
        ... Hint: system [Processing]
        ... Hint: widestrs [Processing]
        ... Hint: io [Processing]
        ... Hint: jnim_no_jvm [Processing]
        ... Hint: jnim [Processing]
        ... Hint: jvm_finder [Processing]
        ... Hint: sugar [Processing]
        ... Hint: macros [Processing]
        ... Hint: os [Processing]
        ... Hint: strutils [Processing]
        ... Hint: parseutils [Processing]
        ... Hint: math [Processing]
        ... Hint: bitops [Processing]
        ... Hint: algorithm [Processing]
        ... Hint: unicode [Processing]
        ... Hint: pathnorm [Processing]
        ... Hint: osseps [Processing]
        ... Hint: winlean [Processing]
        ... Hint: dynlib [Processing]
        ... Hint: times [Processing]
        ... Hint: options [Processing]
        ... Hint: typetraits [Processing]
        ... Hint: time_t [Processing]
        ... Hint: osproc [Processing]
        ... Hint: strtabs [Processing]
        ... Hint: hashes [Processing]
        ... Hint: streams [Processing]
        ... Hint: cpuinfo [Processing]
        ... stack trace: (most recent call last)
        ... C:\Users\Mateusz\.nimble\pkgs\jnim-0.5.0\jnim\private\jvm_finder.nim(103, 26)
        ... C:\Users\Mateusz\.nimble\pkgs\jnim-0.5.0\jnim\private\jvm_finder.nim(100, 11) findCtJVM
        ... C:\Users\Mateusz\.choosenim\toolchains\nim-0.20.2\lib\system\assertions.nim(27, 20) failedAssertImpl
        ... C:\Users\Mateusz\.choosenim\toolchains\nim-0.20.2\lib\system\assertions.nim(20, 11) raiseAssert
        ... C:\Users\Mateusz\.choosenim\toolchains\nim-0.20.2\lib\system\fatal.nim(39, 5) sysFatal
        ... C:\Users\Mateusz\.choosenim\toolchains\nim-0.20.2\lib\system\fatal.nim(39, 5) Error: unhandled exception: C:\Users\Mateusz\.nimble\pkgs\jnim-0.5.0\jnim\private\jvm_finder.nim(100, 10) `jvmO.isSome` JVM not found. Please set JAVA_HOME environment variable
yglukhov commented 5 years ago

Sorry, somehow forgot to commit it.

akavel commented 5 years ago

I'm afraid still not working :( after upgrading jnim_no_jvm to jnim 0.5.1, I'm getting:

C:\prog\jnim_no_jvm>nimble build
  Verifying dependencies for jnim_no_jvm@0.1.0
      Info: Dependency on jnim@0.5.1 already satisfied
  Verifying dependencies for jnim@0.5.1
   Building jnim_no_jvm/jnim_no_jvm.exe using c backend
       Tip: 2 messages have been suppressed, use --verbose to show them.
     Error: Build failed for package: jnim_no_jvm
        ... Details:
        ... Execution failed with exit code 1
        ... Command: "C:\Users\Mateusz\.nimble\bin\nim.exe" c --noBabelPath --path:"C:\Users\Mateusz\.nimble\pkgs\jnim-0.5.1"  -o:"C:\prog\jnim_no_jvm\jnim_no_jvm.exe" "C:\prog\jnim_no_jvm\src\jnim_no_jvm.nim"
        ... Output: Hint: used config file 'C:\Users\Mateusz\.choosenim\toolchains\nim-0.20.2\config\nim.cfg' [Conf]
        ... Hint: system [Processing]
        ... Hint: widestrs [Processing]
        ... Hint: io [Processing]
        ... Hint: jnim_no_jvm [Processing]
        ... Hint: jnim [Processing]
        ... Hint: jvm_finder [Processing]
        ... Hint: sugar [Processing]
        ... Hint: macros [Processing]
        ... Hint: os [Processing]
        ... Hint: strutils [Processing]
        ... Hint: parseutils [Processing]
        ... Hint: math [Processing]
        ... Hint: bitops [Processing]
        ... Hint: algorithm [Processing]
        ... Hint: unicode [Processing]
        ... Hint: pathnorm [Processing]
        ... Hint: osseps [Processing]
        ... Hint: winlean [Processing]
        ... Hint: dynlib [Processing]
        ... Hint: times [Processing]
        ... Hint: options [Processing]
        ... Hint: typetraits [Processing]
        ... Hint: time_t [Processing]
        ... Hint: osproc [Processing]
        ... Hint: strtabs [Processing]
        ... Hint: hashes [Processing]
        ... Hint: streams [Processing]
        ... Hint: cpuinfo [Processing]
        ... Hint: jni_wrapper [Processing]
        ... stack trace: (most recent call last)
        ... C:\Users\Mateusz\.nimble\pkgs\jnim-0.5.1\jnim\private\jvm_finder.nim(103, 60) linkWithJVMLib
        ... C:\Users\Mateusz\.nimble\pkgs\jnim-0.5.1\jnim\private\jvm_finder.nim(100, 11) findCtJVM
        ... C:\Users\Mateusz\.choosenim\toolchains\nim-0.20.2\lib\system\assertions.nim(27, 20) failedAssertImpl
        ... C:\Users\Mateusz\.choosenim\toolchains\nim-0.20.2\lib\system\assertions.nim(20, 11) raiseAssert
        ... C:\Users\Mateusz\.choosenim\toolchains\nim-0.20.2\lib\system\fatal.nim(39, 5) sysFatal
        ... C:\Users\Mateusz\.nimble\pkgs\jnim-0.5.1\jnim\private\jni_wrapper.nim(517, 24) template/generic instantiation of `CT_JVM` from here
        ... C:\Users\Mateusz\.choosenim\toolchains\nim-0.20.2\lib\system\fatal.nim(39, 5) Error: unhandled exception: C:\Users\Mateusz\.nimble\pkgs\jnim-0.5.1\jnim\private\jvm_finder.nim(100, 10) `jvmO.isSome` JVM not found. Please set JAVA_HOME environment variable

I would guess the CT_JVM.lib access even just in definition of linkWithJVMLib requires Nim to materialize the CT_JVM template already, which then triggers the static(findCtJVM()) block to be run at compile time anyway?...

(I would reopen this issue, but once you closed it, GitHub doesn't give me such an option anymore...)

yglukhov commented 5 years ago

Argh, please retry with latest master again.

akavel commented 5 years ago

Okkk, the jnim_no_jvm at last seems to build indeed. Huge thanks! :) will try with a bigger project some time later, big hopes it will work too ;)