Skip to content
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
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SHELL := /bin/bash -euo pipefail
GOPATH := $(shell pwd)/go:$(JIRI_ROOT)/release/go
export GOPATH := $(shell pwd)/go:$(JIRI_ROOT)/release/go

.DELETE_ON_ERROR:

Expand Down Expand Up @@ -30,9 +30,16 @@ c-main-shared: c/main.c c/lib.h build-shared
gcc -Wall -o c/main $< c/golib.so

.PHONY: c-main-archive
c-main-archive: c/main.c c/lib.h build-archive
gcc -Wall -o c/main $< c/golib.a
gcc -Wall -o c/main $< c/golib.a -lm -lpthread

.PHONY: clean
clean:
rm -rf c/golib* c/main c/lib.h

.PHONY: java
java: build-shared
mkdir -p java/Cgo/src/native/go
cp c/golib.h go/src/cgo/lib.h java/Cgo/src/native/go
cp c/golib.so java/Cgo/src/native
$(MAKE) -C java/Cgo/src/native
cd java/Cgo && ./gradlew cleanTest generateJniHeaders build
1 change: 1 addition & 0 deletions go/src/cgo/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

/*
#cgo LDFLAGS: -lm
#include <math.h>
#include "lib.h"

Expand Down
2 changes: 1 addition & 1 deletion go/src/cgo/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ typedef struct {
// currying RefMap handles to Swift closures.
// https://forums.developer.apple.com/message/15725#15725

typedef int x_Handle;
typedef uint64_t x_Handle;

typedef struct {
x_Handle h;
Expand Down
27 changes: 27 additions & 0 deletions java/Cgo/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'c'

repositories {
mavenCentral()
}

task generateJniHeaders(type:Exec) {
def classpath = sourceSets.main.output.classesDir
commandLine 'javah', '-d', 'src/native/include', '-classpath', classpath, rootProject.name

dependsOn classes
}

tasks.withType(Test) {
systemProperty 'java.library.path', '/usr/local/google/home/razvanm/github/asadovsky/cgotest/java/Cgo/src/native'
}

test {
environment 'LD_LIBRARY_PATH', '/usr/local/google/home/razvanm/github/asadovsky/cgotest/java/Cgo/src/native'
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
Binary file added java/Cgo/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions java/Cgo/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Fri May 06 12:38:56 PDT 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-bin.zip
160 changes: 160 additions & 0 deletions java/Cgo/gradlew

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

90 changes: 90 additions & 0 deletions java/Cgo/gradlew.bat

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

2 changes: 2 additions & 0 deletions java/Cgo/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'Cgo'

41 changes: 41 additions & 0 deletions java/Cgo/src/main/java/Cgo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public class Cgo {
public static native int add(int a, int b);

static class AddAndSubResult {
int add;
int sub;
}

public static native AddAndSubResult addAndSub(int a, int b);
public static native void addAndSub(int a, int b, AddAndSubResult result);
public static native int div(int a, int b) throws ArithmeticException;
public static native int divPtrs(int a, int b) throws ArithmeticException;
public static native double sqrt(double x);
public static native String echo(String s);

static class Foo {
String str;
byte[] arr;
int num;
}
public static native Foo echoFoo(Foo foo) throws Exception;

interface IntCallback {
void onValue(int value);
}
// Blocking function that will call the handler.onValue for all the values in the stream.
public static native void streamInts(int x, IntCallback callback);

interface IntStreamCallbacks {
void onValue(int value);
void onDone();
}
// Non-blocking equivalent of the function from above.
public static native void streamInts(int x, IntStreamCallbacks callbacks);

public static native void addAsync(int a, int b, IntCallback callback);

static {
System.loadLibrary("glue");
}
}
Loading