Skip to content

Commit

Permalink
Use jbuilder; camljava
Browse files Browse the repository at this point in the history
  • Loading branch information
Julow committed May 1, 2018
1 parent 25200e3 commit 5027ba6
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 216 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1 +1,2 @@
bin/*
_build/*
**/.merlin
4 changes: 4 additions & 0 deletions Dockerfile
Expand Up @@ -18,7 +18,11 @@ RUN apt-get install -y openjdk-8-jdk

#

RUN opam install -y ppx_tools_versioned

ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64

ENV LD_LIBRARY_PATH ${JAVA_HOME}/jre/lib/amd64/server/

RUN mkdir /app
WORKDIR /app
100 changes: 0 additions & 100 deletions cmxa.Makefile

This file was deleted.

Empty file modified container.sh 100644 → 100755
Empty file.
37 changes: 0 additions & 37 deletions ppx.Makefile

This file was deleted.

8 changes: 8 additions & 0 deletions ppx/jbuild
@@ -0,0 +1,8 @@
(jbuild_version 1)
(library
((name ppx)
(libraries (compiler-libs.common ocaml-migrate-parsetree ppx_tools_versioned))
(kind ppx_rewriter)
(ppx_runtime_libraries (java))
(preprocess (pps (ppx_tools_versioned.metaquot_406)))
(flags (:standard -open Migrate_parsetree -open Ast_406))))
2 changes: 1 addition & 1 deletion ppx/mapper.ml → ppx/ppx.ml
Expand Up @@ -212,4 +212,4 @@ let mapper _ _ = { default_mapper with structure_item }

let () =
Driver.register ~name:"ocaml-java-ppx" (module OCaml_406) mapper;
Driver.run_as_ppx_rewriter ()
Driver.run_main ()
11 changes: 11 additions & 0 deletions srcs/c/config/discover.ml
@@ -0,0 +1,11 @@
let () =
let w file_name fmt =
Printf.kfprintf close_out (open_out file_name) fmt
in
match Sys.getenv "JAVA_HOME" with
| exception Not_found ->
failwith "Environment variable JAVA_HOME is not set."
| java_home ->
w "c_flags.sexp" "(-I \"%s/include\" -I \"%s/include/linux\")" java_home java_home;
let lpath = java_home ^ "/jre/lib/amd64/server" in
w "c_library_flags.sexp" "(-dllpath \"%s/libjvm.so\" \"-L%s\")" lpath lpath
2 changes: 2 additions & 0 deletions srcs/c/config/jbuild
@@ -0,0 +1,2 @@
(jbuild_version 1)
(executable ((name discover)))
11 changes: 11 additions & 0 deletions srcs/c/jbuild
@@ -0,0 +1,11 @@
(jbuild_version 1)
(library
((name stubs)
(c_names (classes java_stubs string_convertions caml))
(c_flags (:standard (:include c_flags.sexp) -DTARGET_CAMLJAVA))
(c_library_flags (:standard (:include c_library_flags.sexp) -ljvm))))

(rule
((targets (c_flags.sexp c_library_flags.sexp))
(deps (config/discover.exe))
(action (run ${<}))))
24 changes: 24 additions & 0 deletions srcs/java/Makefile
@@ -0,0 +1,24 @@
BUILD_DIR = bin

JAVAC = javac

JAVA_FILES = $(wildcard juloo/javacaml/*.java)
CLASS_FILES_REL = $(JAVA_FILES:%.java=%.class)
CLASS_FILES = $(addprefix $(BUILD_DIR)/,$(CLASS_FILES_REL))

all: $(BUILD_DIR)/ocaml-java.jar

$(BUILD_DIR)/ocaml-java.jar: $(CLASS_FILES) | $(BUILD_DIR)
cd $(@D); jar cf $(@F) $(CLASS_FILES_REL)

$(BUILD_DIR)/%.class: %.java | $(BUILD_DIR)
$(JAVAC) -sourcepath . -d $(BUILD_DIR) $<

$(BUILD_DIR):
mkdir -p $@

clean:
rm -f $(BUILD_DIR)/ocaml-java.jar
rm -f $(CLASS_FILES)

.PHONY: all clean
4 changes: 4 additions & 0 deletions srcs/java/jbuild
@@ -0,0 +1,4 @@
(jbuild_version 1)
(rule ((targets (ocaml-java.jar))
(deps (Makefile (glob_files juloo/javacaml/*.java)))
(action (run make BUILD_DIR=. ${@}))))
5 changes: 5 additions & 0 deletions srcs/ml/jbuild
@@ -0,0 +1,5 @@
(jbuild_version 1)
(library
((name java)
(wrapped false)
(libraries (stubs))))
7 changes: 0 additions & 7 deletions tests/.gitignore

This file was deleted.

48 changes: 1 addition & 47 deletions tests/Makefile
@@ -1,49 +1,3 @@
# -

OCAMLJAVA_DIR = ..

OCAMLJAVA_JAR = $(OCAMLJAVA_DIR)/bin/ocaml-java.jar

ML_FILES := test_caml.ml test_java.ml test_ppx.ml test.ml

run: camljava javacaml

camljava: test_camljava test_javacaml.jar
LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(JAVA_HOME)/jre/lib/amd64/server \
OCAMLRUNPARAM=b \
./test_camljava

test_camljava: $(ML_FILES) | libs
ocamlopt \
-ppx $(OCAMLJAVA_DIR)/bin/ocaml-java-ppx \
-I $(OCAMLJAVA_DIR)/bin/camljava camljava.cmxa \
unix.cmxa -g -o $@ $^

javacaml: libtest_javacaml.so test_javacaml.jar
CP=test_javacaml.jar:$(OCAMLJAVA_JAR); \
LD_LIBRARY_PATH=. java -ea -cp "$$CP" ocamljava.test.TestJava

libtest_javacaml.so: $(ML_FILES) | libs
ocamlfind ocamlopt -linkpkg -output-obj \
-ppx $(OCAMLJAVA_DIR)/bin/ocaml-java-ppx \
-runtime-variant _pic \
-ccopt "-shared" \
-I $(OCAMLJAVA_DIR)/bin/javacaml javacaml.cmxa \
unix.cmxa -I . -g -o $@ $^

test_javacaml.jar: $(wildcard ocamljava/test/*.java)
javac -encoding UTF8 -cp $(OCAMLJAVA_JAR) $^
javac -encoding UTF8 -cp "$(CLASS_PATH)" $^
jar cf $@ ocamljava/test/*.class

libs:
make -C $(OCAMLJAVA_DIR)

clean:
rm -f $(wildcard *.o *.cmo *.cmi *.cmx ocamljava/test/*.class)
rm -f test_camljava libtest_javacaml.so test_javacaml.jar
make -C $(OCAMLJAVA_DIR) clean

re: clean
make run

.PHONY: run camljava javacaml libs clean re
14 changes: 14 additions & 0 deletions tests/jbuild
@@ -0,0 +1,14 @@
(jbuild_version 1)
(executable
((name test)
(libraries (java unix))
(preprocess (pps (ppx)))))

(alias
((name runtest)
(deps (../srcs/java/ocaml-java.jar test_javacaml.jar))
(action (run ${exe:test.exe} ${^}))))

(rule ((targets (test_javacaml.jar))
(deps (Makefile (glob_files ocamljava/test/*.java)))
(action (run make CLASS_PATH=${path:../srcs/java/ocaml-java.jar} ${@}))))
45 changes: 22 additions & 23 deletions tests/test.ml
@@ -1,25 +1,24 @@
let jar_files = [
"test_javacaml.jar";
"../bin/ocaml-java.jar"
]

let () = Printexc.record_backtrace true
let run () =
let class_path = match Array.to_list Sys.argv with
| _ :: (_ :: _ as cp) -> String.concat ":" cp
| _ -> failwith "Missing argument: class path"
in
Java.init [|
"-Djava.class.path=" ^ class_path;
"-ea";
"-Xcheck:jni"
|];
try
for i = 0 to 10 do
Test_caml.run ();
Test_java.run ();
Test_ppx.run ()
done
with Java.Exception e ->
Jthrowable.print_stack_trace e;
failwith ""

let () =
(* quick check if executed from javacaml's tests *)
if Sys.argv.(0) <> "" then begin
Java.init [|
"-Djava.class.path=" ^ String.concat ":" jar_files;
"-ea";
"-Xcheck:jni"
|];
try
for i = 0 to 10 do
Test_caml.run ();
Test_java.run ();
Test_ppx.run ()
done
with Java.Exception e ->
Jthrowable.print_stack_trace e;
failwith ""
end
Printexc.record_backtrace true;
(* quick check if executed from java *)
(if Sys.argv.(0) <> "" then run ())

0 comments on commit 5027ba6

Please sign in to comment.