Skip to content

Commit

Permalink
#145: initial jni setup and build (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
rladstaetter committed Aug 9, 2023
1 parent fe1d6f9 commit 5abadb5
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package app.logorrr;

public class SwiftHelloWorld {

static {
System.loadLibrary("SwiftHelloWorld");
}
public class OsxBridge {

public static native void printHelloWorldImpl();

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/scala/app/logorrr/LogoRRRApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import app.logorrr.util.CanLog
import app.logorrr.views.main.LogoRRRStage
import javafx.stage.Stage

import java.nio.file.Paths

object LogoRRRApp {

/** LogoRRRs own log formatting string */
Expand All @@ -21,6 +23,8 @@ object LogoRRRApp {

class LogoRRRApp extends javafx.application.Application with CanLog {

logInfo("I'm in" + Paths.get("").toAbsolutePath)

def start(stage: Stage): Unit = {
logInfo(s"Started " + AppMeta.fullAppNameWithVersion)
val settings: Settings = SettingsIO.fromFile()
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/scala/app/logorrr/LogoRRRAppLauncher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ object LogoRRRAppLauncher {

/** launcher for macos installer to circumvent module loading mechanism by javafx (dirty hack) */
def main(args: Array[String]): Unit = {

test()
LogoRRRApp.main(args)
}

private def test(): Unit = {
System.loadLibrary("LogoRRRSwift")
System.loadLibrary("LogoRRR")
OsxBridge.printHelloWorldImpl()
}
}
24 changes: 24 additions & 0 deletions dist/dist-osx/installer-osx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
<groupId>app.logorrr</groupId>
<artifactId>build</artifactId>
</dependency>
<dependency>
<groupId>app.logorrr.native</groupId>
<artifactId>native-osx</artifactId>
<classifier>dylibs</classifier>
<type>zip</type>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -84,6 +90,24 @@
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>compile</includeScope>
<includeScope>runtime</includeScope>
<excludeArtifactIds>native-osx</excludeArtifactIds>
</configuration>
</execution>
<execution>
<id>unpack-native</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<!-- libs are needed by jpackage later on -->
<includeArtifactIds>native-osx</includeArtifactIds>
<outputDirectory>${project.build.directory}/libs/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>compile</includeScope>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@
--jlink-options "--strip-native-commands --no-header-files --no-man-pages --compress=2 --strip-debug --include-locales=en"
--app-version ${revision}
--main-class ${main.launcher.class}
--java-options -Xmx4096m
--java-options -Djava.library.path=$APPDIR/.
--main-jar app-${project.version}.jar
21 changes: 0 additions & 21 deletions dist/dist-osx/native-osx/src/main/c/app_logorrr_SwiftHelloWorld.h

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion dist/dist-osx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<modules>
<!-- atm there is no graalvm build active on macosx-->
<!-- <module>binary-osx</module> -->
<module>native-osx</module>
<module>installer-osx</module>
</modules>

Expand Down
19 changes: 19 additions & 0 deletions native/native-osx/dylibs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>dylibs</id>
<formats>
<format>zip</format>
</formats>
<baseDirectory>/</baseDirectory>
<fileSets>
<fileSet>
<directory>target/</directory>
<includes>
<include>*.dylib</include>
</includes>
<outputDirectory/>
</fileSet>
</fileSets>
</assembly>
96 changes: 55 additions & 41 deletions dist/dist-osx/native-osx/pom.xml → native/native-osx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,22 @@
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>app.logorrr.dist.osx</groupId>
<artifactId>dist-osx</artifactId>
<groupId>app.logorrr.native</groupId>
<artifactId>native</artifactId>
<version>${revision}${changelist}</version>
</parent>

<artifactId>native-osx</artifactId>
<name>app.logorrr.dist.osx.native</name>
<name>app.logorrr.native.osx</name>
<packaging>pom</packaging>

<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_${scala.major.version}</artifactId>
</dependency>
<dependency>
<groupId>org.scalacheck</groupId>
<artifactId>scalacheck_${scala.major.version}</artifactId>
</dependency>
<dependency>
<groupId>org.scalatestplus</groupId>
<artifactId>scalacheck-1-15_${scala.major.version}</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<!-- generate header file if you change the signature of the native methods -->
<!--
<!-- generate header file when methods declared as native change -->
<execution>
<id>generate-header-file</id>
<phase>generate-sources</phase>
Expand All @@ -47,15 +29,14 @@
</goals>
<configuration>
<executable>${jdk.javac}</executable>
<workingDirectory>${project.build.directory}</workingDirectory>
<workingDirectory>target</workingDirectory>
<arguments>
<argument>-h</argument>
<argument>${project.basedir}/src/main/c</argument>
<argument>${project.basedir}/src/main/java/app/logorrr/SwiftHelloworld.java</argument>
<argument>${project.basedir}/../../app/src/main/java/app/logorrr/OsxBridge.java</argument>
</arguments>
</configuration>
</execution>
-->
<!-- compile swift code -->
<execution>
<id>compile-swift-code</id>
Expand All @@ -65,17 +46,16 @@
</goals>
<configuration>
<executable>${osx.swiftc}</executable>
<workingDirectory>${project.basedir}/src/main/c/</workingDirectory>
<workingDirectory>src/main/c</workingDirectory>
<arguments>
<!-- SwiftCode.swift -emit-library -o libSwiftCode.dylib -Xlinker -install_name -Xlinker libSwiftCode.dylib -->
<argument>${project.basedir}/src/main/c/SwiftCode.swift</argument>
<argument>${project.basedir}/src/main/c/LogoRRRSwift.swift</argument>
<argument>-emit-library</argument>
<argument>-o</argument>
<argument>${project.build.directory}/classes/libSwiftCode.dylib</argument>
<argument>${project.build.directory}/libLogoRRRSwift.dylib</argument>
<argument>-Xlinker</argument>
<argument>-install_name</argument>
<argument>-Xlinker</argument>
<argument>${project.build.directory}/classes/libSwiftCode.dylib</argument>
<argument>${project.build.directory}/libLogoRRRSwift.dylib</argument>
</arguments>
</configuration>
</execution>
Expand All @@ -88,29 +68,63 @@
</goals>
<configuration>
<executable>${osx.gcc}</executable>
<workingDirectory>${project.build.directory}</workingDirectory>
<workingDirectory>target</workingDirectory>
<arguments>
<!-- -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/darwin/" -o libSwiftHelloWorld.dylib -dynamiclib helloworld_SwiftHelloWorld.c libSwiftCode.dylib -->

<argument>-I${jdk.home}/include</argument>
<argument>-I${jdk.home}/include/darwin/</argument>
<argument>-o</argument>
<argument>${project.build.directory}/classes/libSwiftHelloWorld.dylib</argument>
<argument>${project.build.directory}/libLogoRRR.dylib</argument>
<argument>-dynamiclib</argument>
<argument>${project.basedir}/src/main/c/SwiftHelloWorld.c</argument>
<argument>${project.build.directory}/classes/libSwiftCode.dylib</argument>
<argument>${project.basedir}/src/main/c/OsxBridge.c</argument>
<argument>${project.build.directory}/libLogoRRRSwift.dylib</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<executions>
<!-- get rid of generated class files in src/main/java -->
<execution>
<id>clean-class-file</id>
<phase>process-sources</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<filesets>
<fileset>
<directory>../../app/src/main/java/</directory>
<includes>
<include>**/*.class</include>
</includes>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>create-distribution</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>dylibs.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <jni.h>
#include <stdio.h>
#include "app_logorrr_SwiftHelloWorld.h"
#include "app_logorrr_SwiftHelloWorld_swift.h"
#include "app_logorrr_OsxBridge.h"
#include "app_logorrr_OsxBridge_swift.h"

JNIEXPORT void JNICALL Java_app_logorrr_SwiftHelloWorld_printHelloWorldImpl
JNIEXPORT void JNICALL Java_app_logorrr_OsxBridge_printHelloWorldImpl
(JNIEnv *env, jclass clazz) {

int result = swiftHelloWorld(42);
Expand Down
21 changes: 21 additions & 0 deletions native/native-osx/src/main/c/app_logorrr_OsxBridge.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions native/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>app.logorrr</groupId>
<artifactId>logorrr</artifactId>
<version>${revision}${changelist}</version>
</parent>
<groupId>app.logorrr.native</groupId>
<artifactId>native</artifactId>
<name>app.logorrr.native</name>
<packaging>pom</packaging>

<profiles>
<profile>
<id>OS.osx</id>
<activation>
<os>
<family>unix</family>
<name>Mac OS X</name>
</os>
</activation>
<modules>
<module>native-osx</module>
</modules>
</profile>
</profiles>

</project>
Loading

0 comments on commit 5abadb5

Please sign in to comment.