NeuronRobotics / nrjavaserial

A Java Serial Port system. This is a fork of the RXTX project that uses in jar loading of the native code.
Other
345 stars 143 forks source link

Native build: `JAVA_HOME` auto-detection yields double-quoted value #244

Open MrDOS opened 1 year ago

MrDOS commented 1 year ago

When attempting to compile the native libraries in a Windows environment, I get a “No such file or directory” error:

C:\Users\scoleman\Projects\nrjavaserial>mingw32-make -C src/main/c windows
mingw32-make: Entering directory 'C:/Users/scoleman/Projects/nrjavaserial/src/main/c'
JAVA_HOME.mk:112: The JAVA_HOME environment variable has not been defined.
Based on the location of the `java` binary, I've guessed it's:

    "C:\Program Files\Eclipse Adoptium\jdk-17.0.8.7-hotspot"

If that's not correct, or to suppress this message, explicitly set the
JAVA_HOME environment variable to the location of your preferred Java
installation.
mingw32-make -f natives.mk
mingw32-make[1]: Entering directory 'C:/Users/scoleman/Projects/nrjavaserial/src/main/c'
x86_64-w64-mingw32-gcc -I""C:\Program Files\Eclipse Adoptium\jdk-17.0.8.7-hotspot"/include" -I./include -I./include/target -O3 -fPIC -c -Wall -I./include/windows -I./include/windows/win32 -m32 src/fixup.c -o build/windows/x86_32/fixup.o
x86_64-w64-mingw32-gcc: error: Files\Eclipse: No such file or directory
x86_64-w64-mingw32-gcc: error: Adoptium\jdk-17.0.8.7-hotspot/include: No such file or directory
natives.mk:15: recipe for target 'build/windows/x86_32/fixup.o' failed
mingw32-make[1]: *** [build/windows/x86_32/fixup.o] Error 1
mingw32-make[1]: Leaving directory 'C:/Users/scoleman/Projects/nrjavaserial/src/main/c'
Makefile:292: recipe for target 'windows32' failed
mingw32-make: *** [windows32] Error 2
mingw32-make: Leaving directory 'C:/Users/scoleman/Projects/nrjavaserial/src/main/c'

I'm using GNU Make 3.82.90 as packaged with TDM-GCC 10.3.0-2. The detected Java path is correct. The problem appears to be double-quoting in the include path:

-I""C:\Program Files\Eclipse Adoptium\jdk-17.0.8.7-hotspot"/include"

The doubling-up of the first quotation marks causes this to be interpreted as three arguments, not one.

The inner set of quotes are put there by the path detection:

https://github.com/NeuronRobotics/nrjavaserial/blob/0df8b60485a56d7698b71183237b5615d02a8194/src/main/c/JAVA_HOME.mk#L111

This is not a Windows-specific issue. The double-quoting also occurs on Linux:

$ make -C src/main/c linux32
make: Entering directory '/home/scoleman/Projects/nrjavaserial/src/main/c'
JAVA_HOME.mk:112: The JAVA_HOME environment variable has not been defined.
Based on the location of the `java` binary, I've guessed it's:

    "/usr/lib/jvm/java-17-openjdk-amd64"

...
i686-linux-gnu-gcc -I""/usr/lib/jvm/java-17-openjdk-amd64"/include" -I./include -I./include/target -O3 -fPIC -c -Wall -I"/usr/lib/jvm/java-17-openjdk-amd64"/include/linux -U_FORTIFY_SOURCE -m32 src/SerialImp.c -o build/linux/x86_32/SerialImp.o
...

...but it doesn't cause a problem there, because the Java home doesn't contain spaces.

The outer quotes were added in https://github.com/NeuronRobotics/nrjavaserial/pull/231 because @fwolter ran into issues with their absence. I suspect that Fabian had/has the JAVA_HOME environment variable explicitly defined in his environment; because the value was not quoted at time of use, he got much the same error as I'm getting now. The root cause here is that I must've neglected to test building with JAVA_HOME explicitly set to a path containing spaces while refactoring the build process in https://github.com/NeuronRobotics/nrjavaserial/pull/189. The full solution is to remove the inner quotes added during path detection, and to make sure $(JAVA_HOME) is quoted wherever it's used in the main Makefile.