diff --git a/dist/dist-osx/native-osx/call-swift-security-bookmarks.md b/dist/dist-osx/native-osx/call-swift-security-bookmarks.md new file mode 100644 index 00000000..797d876e --- /dev/null +++ b/dist/dist-osx/native-osx/call-swift-security-bookmarks.md @@ -0,0 +1,26 @@ +# Manage File access + +Since Mac App Store apps are sandboxed, file access is restricted and needs special consideration. + +LogoRRR naturally needs access to files, and as such it poses a problem for the whole application. + +In order to grant LogoRRR access to files this module has been put into place. + +Direct access from Java to the necessary swift code and Mac OsX APIs is only possible via calling it via C code. + +Thus as an Java developer, you have to + +- call native code via JNI +- from this intermediate layer, call the OsX API + +This is far from pretty and quite involved, but at the moment it is the best I can come up with. + +Of course, this includes additional complexities in building the code and deployment, more libraries which have to be taken care of and subtle bugs which can easily creep in. + +Sadly, it is another road block for JavaFX development; only few developers will bite the bullet and go this path; but since you are reading this you belong to this elitist circle ;-) + +## Links + +- https://stackoverflow.com/questions/27628385/write-call-swift-code-using-java-s-jni - how to call swift code from Java +- https://developer.apple.com/documentation/security/app_sandbox/accessing_files_from_the_macos_app_sandbox - API to call +- https://schlining.medium.com/a-simple-java-native-interface-jni-example-in-java-and-scala-68fdafe76f5f example on how to use scala & java and JNI \ No newline at end of file diff --git a/dist/dist-osx/native-osx/pom.xml b/dist/dist-osx/native-osx/pom.xml new file mode 100644 index 00000000..c1f94475 --- /dev/null +++ b/dist/dist-osx/native-osx/pom.xml @@ -0,0 +1,117 @@ + + + 4.0.0 + + + app.logorrr.dist.osx + dist-osx + ${revision}${changelist} + + + native-osx + app.logorrr.dist.osx.native + + + + org.scala-lang + scala-library + + + org.scalatest + scalatest_${scala.major.version} + + + org.scalacheck + scalacheck_${scala.major.version} + + + org.scalatestplus + scalacheck-1-15_${scala.major.version} + + + + + + org.codehaus.mojo + exec-maven-plugin + + + + + + compile-swift-code + compile + + exec + + + ${osx.swiftc} + ${project.basedir}/src/main/c/ + + + ${project.basedir}/src/main/c/SwiftCode.swift + -emit-library + -o + ${project.build.directory}/classes/libSwiftCode.dylib + -Xlinker + -install_name + -Xlinker + ${project.build.directory}/classes/libSwiftCode.dylib + + + + + + compile-c-code + compile + + exec + + + ${osx.gcc} + ${project.build.directory} + + + + -I${jdk.home}/include + -I${jdk.home}/include/darwin/ + -o + ${project.build.directory}/classes/libSwiftHelloWorld.dylib + -dynamiclib + ${project.basedir}/src/main/c/SwiftHelloWorld.c + ${project.build.directory}/classes/libSwiftCode.dylib + + + + + + + net.alchim31.maven + scala-maven-plugin + + + org.scalatest + scalatest-maven-plugin + + + + \ No newline at end of file diff --git a/dist/dist-osx/native-osx/src/main/c/SwiftCode.swift b/dist/dist-osx/native-osx/src/main/c/SwiftCode.swift new file mode 100644 index 00000000..cc7a97bf --- /dev/null +++ b/dist/dist-osx/native-osx/src/main/c/SwiftCode.swift @@ -0,0 +1,8 @@ +import Foundation + +// force the function to have a name callable by the c code +@_silgen_name("swiftHelloWorld") +public func swiftHelloWorld(number: Int) -> Int { + print("Hello world from Swift: \(number)") + return 69 +} diff --git a/dist/dist-osx/native-osx/src/main/c/SwiftHelloWorld.c b/dist/dist-osx/native-osx/src/main/c/SwiftHelloWorld.c new file mode 100644 index 00000000..ed30bf47 --- /dev/null +++ b/dist/dist-osx/native-osx/src/main/c/SwiftHelloWorld.c @@ -0,0 +1,12 @@ +#include +#include +#include "app_logorrr_SwiftHelloWorld.h" +#include "app_logorrr_SwiftHelloWorld_swift.h" + +JNIEXPORT void JNICALL Java_app_logorrr_SwiftHelloWorld_printHelloWorldImpl + (JNIEnv *env, jclass clazz) { + + int result = swiftHelloWorld(42); + printf("%s%i%s", "Hello World from JNI! ", result, "\n"); + +} diff --git a/dist/dist-osx/native-osx/src/main/c/app_logorrr_SwiftHelloWorld.h b/dist/dist-osx/native-osx/src/main/c/app_logorrr_SwiftHelloWorld.h new file mode 100644 index 00000000..e1e3bde6 --- /dev/null +++ b/dist/dist-osx/native-osx/src/main/c/app_logorrr_SwiftHelloWorld.h @@ -0,0 +1,21 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class app_logorrr_SwiftHelloWorld */ + +#ifndef _Included_app_logorrr_SwiftHelloWorld +#define _Included_app_logorrr_SwiftHelloWorld +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: app_logorrr_SwiftHelloWorld + * Method: printHelloWorldImpl + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_app_logorrr_SwiftHelloWorld_printHelloWorldImpl + (JNIEnv *, jclass); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/dist/dist-osx/native-osx/src/main/c/app_logorrr_SwiftHelloWorld_swift.h b/dist/dist-osx/native-osx/src/main/c/app_logorrr_SwiftHelloWorld_swift.h new file mode 100644 index 00000000..50fdfdd1 --- /dev/null +++ b/dist/dist-osx/native-osx/src/main/c/app_logorrr_SwiftHelloWorld_swift.h @@ -0,0 +1 @@ +int swiftHelloWorld(int); \ No newline at end of file diff --git a/dist/dist-osx/native-osx/src/main/java/app/logorrr/SwiftHelloWorld.java b/dist/dist-osx/native-osx/src/main/java/app/logorrr/SwiftHelloWorld.java new file mode 100644 index 00000000..c6bc5405 --- /dev/null +++ b/dist/dist-osx/native-osx/src/main/java/app/logorrr/SwiftHelloWorld.java @@ -0,0 +1,15 @@ +package app.logorrr; + +public class SwiftHelloWorld { + + static { + System.loadLibrary("SwiftHelloWorld"); + } + + public static native void printHelloWorldImpl(); + + public static void main(final String[] args) { + printHelloWorldImpl(); + } + +} diff --git a/dist/dist-osx/native-osx/src/main/scala/app/logorrr/OsxBridge.scala b/dist/dist-osx/native-osx/src/main/scala/app/logorrr/OsxBridge.scala new file mode 100644 index 00000000..f7bdb4f7 --- /dev/null +++ b/dist/dist-osx/native-osx/src/main/scala/app/logorrr/OsxBridge.scala @@ -0,0 +1,12 @@ +package app.logorrr + +object OsxBridge { + + @native def printHelloWorldImpl() : Unit + + def main(args: Array[String]): Unit = { + System.loadLibrary("OsxBridge") + printHelloWorldImpl() + } + +} diff --git a/dist/dist-osx/pom.xml b/dist/dist-osx/pom.xml index dacd1761..909772ba 100644 --- a/dist/dist-osx/pom.xml +++ b/dist/dist-osx/pom.xml @@ -17,7 +17,8 @@ + native-osx installer-osx - \ No newline at end of file + diff --git a/pom.xml b/pom.xml index 1ae48406..b3727abf 100644 --- a/pom.xml +++ b/pom.xml @@ -73,6 +73,7 @@ ${osx.jdk.home.aarch64} + ${jdk.home}/bin/javac @@ -90,7 +91,8 @@ mac.signingkey.username You must set property 'mac.signingkey.username' to create - a signed pkg file. A valid entry has to have the following form: 'John Doe (ABCDEFGHIJ)' + a signed pkg file. A valid entry has to have the following form: 'John + Doe (ABCDEFGHIJ)'