Skip to content

Commit

Permalink
Update to use static libpasta, include native libs.
Browse files Browse the repository at this point in the history
With this, Java build in libpasta-java repo should be working.
  • Loading branch information
samscott89 committed Nov 14, 2017
1 parent 22a50c5 commit 25a1e59
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 34 deletions.
35 changes: 26 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
targets = java python php5 ruby

LIBS = -l pasta -L.
LIBS = ./libpasta-ffi/target/${BUILD_TYPE}/libpasta.a
NATIVE_LIBS = -lcrypto -lc -lm -lc -lutil -lutil -ldl -lrt -lpthread -lgcc_s -lc -lm -lrt -lpthread -lutil
SWIG = swig
OUTPUT_NAME = pasta.so
CC = gcc
Expand All @@ -14,6 +15,7 @@ PYTHON_VERSION ?= python2.7

go_SWIG_ARGS = -cgo -intgosize 64 -use-shlib -soname libpasta.so
javascript_SWIG_ARGS = -c++ -v8
java_SWIG_ARGS = -package io.github.libpasta
java_DIR = $(shell java -XshowSettings 2>&1 | grep java.home | grep '/usr/.*/' -o)
java_INCLUDES = -I$(java_DIR)include/ -I$(java_DIR)include/linux/
javascript_INCLUDES = -I/usr/include/node/
Expand All @@ -24,30 +26,45 @@ ruby_INCLUDES = -I$(shell ruby -rrbconfig -e 'puts RbConfig::CONFIG[%q{rubyhdr

all: $(targets)

java: OUTPUT_DIR = META_INF/lib/linux_64
java: OUTPUT_NAME = libpasta_jni.so
javascript: CC = g++
python: OUTPUT_NAME = _pasta.so

$(targets): pasta.i libpasta
$(SWIG) -$@ $($@_SWIG_ARGS) -outdir $@ pasta.i
mkdir -p $@/$(OUTPUT_DIR)
$(SWIG) -$@ $($@_SWIG_ARGS) -outdir $@ -o $@/pasta_wrap.c pasta.i
$(CC) $@/pasta_wrap.c -fPIC -c -g $($@_INCLUDES) -o $@/pasta_wrap.o
$(CC) -shared $@/pasta_wrap.o $(LIBS) -o $@/$(OUTPUT_NAME)
$(CC) -shared $@/pasta_wrap.o $(LIBS) -L/usr/lib/ $(NATIVE_LIBS) -o $@/$(OUTPUT_DIR)/$(OUTPUT_NAME)

clean:
rm -rf $(targets)
rm -f tests/test.class
cd libpasta-ffi && cargo clean
rm libpasta.jar

force: clean all

test: all
make -C tests
make -C tests/ c $(targets)

libpasta: libpasta-ffi/Cargo.toml libpasta-ffi/src/lib.rs
cd libpasta-ffi/ && cargo build --${BUILD_TYPE}
cp libpasta-ffi/target/${BUILD_TYPE}/libpasta.so ../

libpasta-ffi/Cargo.toml:
git submodule update && git submodule sync

git submodule update --remote && git submodule sync


PHP_INI=$(shell $(PHP_BIN) -i | grep -o '/etc/.*/php.ini')
PHPENMOD := $(shell command -v php${PHP_VERSION}enmod 2> /dev/null)

install_php: libpasta php5
sudo cp bindings/php5/pasta.so $(shell $(PHP_CONFIG) --extension-dir)/pasta.so
ifdef PHPENMOD
sudo $(PHPENMOD) pasta
else
(grep -qi pasta.so $(PHP_INI) && sudo sed -i 's/;extension=pasta.so/extension=pasta/' $(PHP_INI)) || \
(echo "extension=pasta.so" | sudo tee --append $(PHP_INI) > /dev/null)
endif


.PHONY: all clean force test
.PHONY: all clean force test libpasta-ffi/Cargo.toml
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Cross-language bindings for libpasta
====================================

This library is intended for developers wishing to extend or improve the
existing language bindings for [libpasta](https://libpasta.github.io/).

For existing language bindings, and ways to install in those languages,
please see the information
[here](https://libpasta.github.io/other-languages/overview/).

SWIG and libpasta
------------------

The language bindings produced here are through using
[SWIG](http://www.swig.org/).

The following logic is used to produce the language bindings:

We define the C header file in [pasta.h](./pasta.h) which corresponds to the
Rust definitions from the
[libpasta-ffi](https://github.com/libpasta/libpasta-ffi) crate (included as a
submodule for convenience).

This header file is now compatible with using SWIG, and the [pasta.i](./pasta.i)
file produces basic bindings with just the `%include <pasta.h>` line.

The rest of the [pasta.i](./pasta.i) file is dedicated to language-specific
requirements, and convenience code, such as automatically deallocating
the Rust `String` objects required to call the library.

We produce code for each support language using the [Makefile](./Makefile).
In general, this runs SWIG over the definition file to produce wrapper code,
and compiles it into a single `pasta.so` file (name depending on the language
and system preferences), and language-specific code to use this library.

Current Status
--------------

The entire libpasta project is still in an early phase. These bindings are
designed for ease of use, and early testing.

For now, libpasta is compiled dynamically, and included as a dependency in the
produced wrapper, which means linking all the required shared libraries when
building the wrappers. This step is potentially troublesome. In the future, we
would like libpasta to be a shared system library.

Currently these bindings are designed for `x86_64-unknown-linux-gnu`
(as per
[Rust platform support](https://forge.rust-lang.org/platform-support.html)),
but we are trying to increase support to other platforms.
2 changes: 1 addition & 1 deletion libpasta-ffi
Submodule libpasta-ffi updated 1 files
+2 −2 Cargo.toml
Binary file removed libpasta.jar
Binary file not shown.
17 changes: 13 additions & 4 deletions pasta.i
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,22 @@
%newobject read_password;



%pragma(java) jniclassimports=%{
import org.scijava.nativelib.*;
%}

%pragma(java) jniclasscode=%{
static {
try {
System.loadLibrary("pasta_jni");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);
NativeLoader.loadLibrary("pasta_jni");
} catch (Exception e) {
try {
NativeLibraryUtil.loadNativeLibrary(pastaJNI.class, "pasta_jni");
} catch (Exception e2) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);
}
}
}
%}
Expand Down
35 changes: 22 additions & 13 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
LD_LIBRARY_PATH="../"
test:
CGO_LDFLAGS="-l pasta -L../"
NATIVE_LIBS=-lcrypto -lc -lm -lc -lutil -lutil -ldl -lrt -lpthread -lgcc_s -lc -lm -lrt -lpthread -lutil

all: c java javascript go php python ruby

c:
CGO_LDFLAGS="../libpasta-ffi/target/release/libpasta.a"

@printf "\nTest C:\n"
gcc -Wall test.c -lpasta -I../ -o test
gcc -Wall test.c ../libpasta-ffi/target/release/libpasta.a -I../ $(NATIVE_LIBS) -o test
chmod +x test
LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
./test


java:
@printf "\nTest Java:\n"
javac -cp .:../libpasta.jar test.java
java -cp .:../libpasta.jar test
@echo -e "\e[1;33mJava tests should be performed using the libpasta-java repo.\e[0m"

# @printf "\nTest Javascript (node.js)\n"
# cd ../javacsript && node-gyp build
javascript:
@printf "\nTest Javascript (node.js)\n"
cd ../javascript && node-gyp build

@# printf "\nTest Go:\n"
@# go test
go:
@printf "\nTest Go:\n"
go test

php5 php56 php:
@printf "\nTest PHP:\n"
LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
$(PHP_BIN) test.php
@$(PHP_BIN) test.php || \
echo -e "\e[1;33mPHP requires either installing libpasta (try: make install_php), or enabling dynamically loaded extensions.\e[0m"


python:
@printf "\nTest Python:\n"
LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
python2 test.py

ruby:
@printf "\nTest Ruby\n"
LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
ruby test.rb

.PHONY: test
.PHONY: all c java javascript go php python ruby
7 changes: 0 additions & 7 deletions tests/test.java

This file was deleted.

0 comments on commit 25a1e59

Please sign in to comment.