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

support for building plain Java jars from Go projects #90

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bind/genjava.go
Original file line number Diff line number Diff line change
Expand Up @@ -1713,7 +1713,6 @@ import go.Seq;
//
// autogenerated by gobind %[1]s %[2]s

#include <android/log.h>
#include <stdint.h>
#include "seq.h"
#include "_cgo_export.h"
Expand Down
142 changes: 142 additions & 0 deletions bind/java/NativeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Class NativeUtils is published under the The MIT License:
*
* Copyright (c) 2012 Adam Heinrich <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package go;

import java.io.*;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.ProviderNotFoundException;
import java.nio.file.StandardCopyOption;

/**
* A simple library class which helps with loading dynamic libraries stored in the
* JAR archive. These libraries usually contain implementation of some methods in
* native code (using JNI - Java Native Interface).
*
* @see <a href="http://adamheinrich.com/blog/2012/how-to-load-native-jni-library-from-jar">http://adamheinrich.com/blog/2012/how-to-load-native-jni-library-from-jar</a>
* @see <a href="https://github.com/adamheinrich/native-utils">https://github.com/adamheinrich/native-utils</a>
*
*/
public class NativeUtils {

/**
* The minimum length a prefix for a file has to have according to {@link File#createTempFile(String, String)}}.
*/
private static final int MIN_PREFIX_LENGTH = 3;
public static final String NATIVE_FOLDER_PATH_PREFIX = "nativeutils";

/**
* Temporary directory which will contain the DLLs.
*/
private static File temporaryDir;

/**
* Private constructor - this class will never be instanced
*/
private NativeUtils() {
}

/**
* Loads library from current JAR archive
*
* The file from JAR is copied into system temporary directory and then loaded. The temporary file is deleted after
* exiting.
* Method uses String as filename because the pathname is "abstract", not system-dependent.
*
* @param path The path of file inside JAR as absolute path (beginning with '/'), e.g. /package/File.ext
* @throws IOException If temporary file creation or read/write operation fails
* @throws IllegalArgumentException If source file (param path) does not exist
* @throws IllegalArgumentException If the path is not absolute or if the filename is shorter than three characters
* (restriction of {@link File#createTempFile(java.lang.String, java.lang.String)}).
* @throws FileNotFoundException If the file could not be found inside the JAR.
*/
public static void loadLibraryFromJar(String path) throws IOException {

if (null == path || !path.startsWith("/")) {
throw new IllegalArgumentException("The path has to be absolute (start with '/').");
}

// Obtain filename from path
String[] parts = path.split("/");
String filename = (parts.length > 1) ? parts[parts.length - 1] : null;

// Check if the filename is okay
if (filename == null || filename.length() < MIN_PREFIX_LENGTH) {
throw new IllegalArgumentException("The filename has to be at least 3 characters long.");
}

// Prepare temporary file
if (temporaryDir == null) {
temporaryDir = createTempDirectory(NATIVE_FOLDER_PATH_PREFIX);
temporaryDir.deleteOnExit();
}

File temp = new File(temporaryDir, filename);

try (InputStream is = NativeUtils.class.getResourceAsStream(path)) {
Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
temp.delete();
throw e;
} catch (NullPointerException e) {
temp.delete();
throw new FileNotFoundException("File " + path + " was not found inside JAR.");
}

try {
System.load(temp.getAbsolutePath());
} finally {
if (isPosixCompliant()) {
// Assume POSIX compliant file system, can be deleted after loading
temp.delete();
} else {
// Assume non-POSIX, and don't delete until last file descriptor closed
temp.deleteOnExit();
}
}
}

private static boolean isPosixCompliant() {
try {
return FileSystems.getDefault()
.supportedFileAttributeViews()
.contains("posix");
} catch (FileSystemNotFoundException
| ProviderNotFoundException
| SecurityException e) {
return false;
}
}

private static File createTempDirectory(String prefix) throws IOException {
String tempDir = System.getProperty("java.io.tmpdir");
File generatedDir = new File(tempDir, prefix + System.nanoTime());

if (!generatedDir.mkdir())
throw new IOException("Failed to create temp directory " + generatedDir.getName());

return generatedDir;
}
}
25 changes: 20 additions & 5 deletions bind/java/Seq.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package go;

import android.content.Context;
// import android.content.Context;

import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
Expand Down Expand Up @@ -34,15 +34,30 @@ public class Seq {
private static final GoRefQueue goRefQueue = new GoRefQueue();

static {
System.loadLibrary("gojni");
if ("The Android Project".equals(System.getProperty("java.vendor"))) {
System.loadLibrary("gojni");
} else {
String arch = System.getProperty("os.arch");
if ("aarch64".equals(arch)) {
arch = "arm64";
} else if ("x86_64".equals(arch)) {
arch = "amd64";
}
try {
NativeUtils.loadLibraryFromJar("/jniLibs/" + arch + "/libgojni.so");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
init();
Universe.touch();
}

// TODO we probably need to make this file go templatable, to support plain JVM and Android better.
// setContext sets the context in the go-library to be used in RunOnJvm.
public static void setContext(Context context) {
setContext((java.lang.Object)context);
}
// public static void setContext(Context context) {
// setContext((java.lang.Object)context);
// }

private static native void init();

Expand Down
2 changes: 1 addition & 1 deletion bind/java/context_android.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.

#include <jni.h>
#include "seq_android.h"
#include "seq_java.h"
#include "_cgo_export.h"

JNIEXPORT void JNICALL
Expand Down
9 changes: 7 additions & 2 deletions bind/java/seq_android.c.support
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// generated gomobile_bind package and compiled along with the
// generated binding files.

#include <android/log.h>
#include <errno.h>
#include <jni.h>
#include <stdint.h>
Expand All @@ -19,6 +18,12 @@

#define NULL_REFNUM 41

#ifndef __GOBIND_ANDROID__
#define ACT_ENV_CAST (void**)
#else
#define ACT_ENV_CAST
#endif

// initClasses are only exported from Go if reverse bindings are used.
// If they're not, weakly define a no-op function.
__attribute__((weak)) void initClasses(void) { }
Expand Down Expand Up @@ -55,7 +60,7 @@ static JNIEnv *go_seq_get_thread_env(void) {
if (ret != JNI_EDETACHED) {
LOG_FATAL("failed to get thread env");
}
if ((*jvm)->AttachCurrentThread(jvm, &env, NULL) != JNI_OK) {
if ((*jvm)->AttachCurrentThread(jvm, ACT_ENV_CAST &env, NULL) != JNI_OK) {
LOG_FATAL("failed to attach current thread");
}
pthread_setspecific(jnienvs, env);
Expand Down
4 changes: 2 additions & 2 deletions bind/java/seq_android.go.support
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ package main
// files.

//#cgo CFLAGS: -Werror
//#cgo LDFLAGS: -llog
//#cgo android LDFLAGS: -llog
//#include <jni.h>
//#include <stdint.h>
//#include <stdlib.h>
//#include "seq_android.h"
//#include "seq_java.h"
import "C"
import (
"unsafe"
Expand Down
16 changes: 15 additions & 1 deletion bind/java/seq_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,32 @@
#define __GO_SEQ_ANDROID_HDR__

#include <stdint.h>
#include <android/log.h>
// For abort()
#include <stdlib.h>
#include <jni.h>

#ifdef __GOBIND_ANDROID__

#include <android/log.h>

#define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, "go/Seq", __VA_ARGS__)
#define LOG_FATAL(...) \
{ \
__android_log_print(ANDROID_LOG_FATAL, "go/Seq", __VA_ARGS__); \
abort(); \
}

#else

#define LOG_INFO(...) printf(__VA_ARGS__)
#define LOG_FATAL(...) \
{ \
fprintf(stderr, __VA_ARGS__); \
abort(); \
}

#endif

// Platform specific types
typedef struct nstring {
// UTF16 or UTF8 Encoded string. When converting from Java string to Go
Expand Down
1 change: 1 addition & 0 deletions bind/seq.go.support
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package main
// with the bindings.

// #cgo android CFLAGS: -D__GOBIND_ANDROID__
// #cgo java CFLAGS: -D__GOBIND_JAVA__
// #cgo darwin CFLAGS: -D__GOBIND_DARWIN__
// #include <stdlib.h>
// #include "seq.h"
Expand Down
22 changes: 11 additions & 11 deletions cmd/gobind/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ func genPkg(lang string, p *types.Package, astFiles []*ast.File, allPkg []*types
closer()
}
buf.Reset()
w, closer = writer(filepath.Join("src", "gobind", pname+"_android.c"))
w, closer = writer(filepath.Join("src", "gobind", pname+"_java.c"))
processErr(g.GenC())
io.Copy(w, &buf)
closer()
buf.Reset()
w, closer = writer(filepath.Join("src", "gobind", pname+"_android.h"))
w, closer = writer(filepath.Join("src", "gobind", pname+"_java.h"))
processErr(g.GenH())
io.Copy(w, &buf)
closer()
Expand All @@ -86,7 +86,7 @@ func genPkg(lang string, p *types.Package, astFiles []*ast.File, allPkg []*types
return
}
repo := filepath.Clean(filepath.Join(dir, "..")) // golang.org/x/mobile directory.
for _, javaFile := range []string{"Seq.java"} {
for _, javaFile := range []string{"Seq.java", "NativeUtils.java"} {
src := filepath.Join(repo, "bind/java/"+javaFile)
in, err := os.Open(src)
if err != nil {
Expand All @@ -110,9 +110,9 @@ func genPkg(lang string, p *types.Package, astFiles []*ast.File, allPkg []*types
errorf("unable to import bind/java: %v", err)
return
}
copyFile(filepath.Join("src", "gobind", "seq_android.c"), filepath.Join(javaDir, "seq_android.c.support"))
copyFile(filepath.Join("src", "gobind", "seq_android.go"), filepath.Join(javaDir, "seq_android.go.support"))
copyFile(filepath.Join("src", "gobind", "seq_android.h"), filepath.Join(javaDir, "seq_android.h"))
copyFile(filepath.Join("src", "gobind", "seq_java.c"), filepath.Join(javaDir, "seq_android.c.support"))
copyFile(filepath.Join("src", "gobind", "seq_java.go"), filepath.Join(javaDir, "seq_android.go.support"))
copyFile(filepath.Join("src", "gobind", "seq_java.h"), filepath.Join(javaDir, "seq_android.h"))
}
case "go":
w, closer := writer(filepath.Join("src", "gobind", fname))
Expand Down Expand Up @@ -174,10 +174,10 @@ func genPkg(lang string, p *types.Package, astFiles []*ast.File, allPkg []*types
func genPkgH(w io.Writer, pname string) {
fmt.Fprintf(w, `// Code generated by gobind. DO NOT EDIT.

#ifdef __GOBIND_ANDROID__
#include "%[1]s_android.h"
#ifdef __GOBIND_JAVA__
#include "%[1]s_java.h"
#endif
#ifdef __GOBIND_DARWIN__
#if defined(__GOBIND_DARWIN__) && !defined(__GOBIND_JAVA__)
#include "%[1]s_darwin.h"
#endif`, pname)
}
Expand Down Expand Up @@ -275,7 +275,7 @@ func genJavaPackages(dir string, classes []*java.Class, embedders []importers.St
}
buf.Reset()
cg.GenGo()
if err := ioutil.WriteFile(filepath.Join(goBase, "classes_android.go"), buf.Bytes(), 0600); err != nil {
if err := ioutil.WriteFile(filepath.Join(goBase, "classes_java.go"), buf.Bytes(), 0600); err != nil {
return err
}
buf.Reset()
Expand All @@ -285,7 +285,7 @@ func genJavaPackages(dir string, classes []*java.Class, embedders []importers.St
}
buf.Reset()
cg.GenC()
if err := ioutil.WriteFile(filepath.Join(goBase, "classes_android.c"), buf.Bytes(), 0600); err != nil {
if err := ioutil.WriteFile(filepath.Join(goBase, "classes_java.c"), buf.Bytes(), 0600); err != nil {
return err
}
return nil
Expand Down
4 changes: 3 additions & 1 deletion cmd/gomobile/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
var cmdBind = &command{
run: runBind,
Name: "bind",
Usage: "[-target android|" + strings.Join(applePlatforms, "|") + "] [-bootclasspath <path>] [-classpath <path>] [-o output] [build flags] [package]",
Usage: "[-target android|" + strings.Join(applePlatforms, "|") + "|java] [-bootclasspath <path>] [-classpath <path>] [-o output] [build flags] [package]",
Short: "build a library for Android and iOS",
Long: `
Bind generates language bindings for the package named by the import
Expand Down Expand Up @@ -133,6 +133,8 @@ func runBind(cmd *command) error {
return fmt.Errorf("-target=%q requires Xcode", buildTarget)
}
return goAppleBind(gobind, pkgs, targets)
case isJavaPlatform(targets[0].platform):
return goJavaBind(gobind, pkgs, targets)
default:
return fmt.Errorf(`invalid -target=%q`, buildTarget)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/gomobile/bind_androidapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func buildAAR(srcDir, androidDir string, pkgs []*packages.Package, targets []tar
if err != nil {
return err
}
if err := buildJar(w, srcDir); err != nil {
if err := buildAndroidJar(w, srcDir); err != nil {
return err
}

Expand Down Expand Up @@ -249,7 +249,7 @@ const (
minAndroidAPI = 16
)

func buildJar(w io.Writer, srcDir string) error {
func buildAndroidJar(w io.Writer, srcDir string) error {
var srcFiles []string
if buildN {
srcFiles = []string{"*.java"}
Expand Down
Loading