Skip to content

Commit c34a3aa

Browse files
committed
add build system, CI integration, and fix minor errors
1 parent b796c4a commit c34a3aa

File tree

5 files changed

+206
-18
lines changed

5 files changed

+206
-18
lines changed

.circleci/config.yml

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
version: 2
2+
3+
jobs:
4+
build:
5+
docker:
6+
- image: mbgl/jnihpp:df002510f3-linux
7+
working_directory: /src
8+
steps:
9+
- checkout
10+
11+
# Note: jni.hpp does not work with GCC 5 and earlier due to compiler bugs.
12+
13+
- run:
14+
name: Test GCC 6 with Android JNI
15+
when: always
16+
environment:
17+
CXX: g++-6
18+
CC: gcc-6
19+
VARIANT: android
20+
command: make clean ; make test examples
21+
- run:
22+
name: Test GCC 6 with OpenJDK JNI
23+
when: always
24+
environment:
25+
CXX: g++-6
26+
CC: gcc-6
27+
VARIANT: openjdk
28+
command: make clean ; make test examples
29+
30+
- run:
31+
name: Test GCC 7 with Android JNI
32+
when: always
33+
environment:
34+
CXX: g++-7
35+
CC: gcc-7
36+
VARIANT: android
37+
command: make clean ; make test examples
38+
- run:
39+
name: Test GCC 7 with OpenJDK JNI
40+
when: always
41+
environment:
42+
CXX: g++-7
43+
CC: gcc-7
44+
VARIANT: openjdk
45+
command: make clean ; make test examples
46+
47+
48+
- run:
49+
name: Test Clang 3.8 with Android JNI
50+
when: always
51+
environment:
52+
CXX: clang++-3.8
53+
CC: clang-3.8
54+
VARIANT: android
55+
command: make clean ; make test examples
56+
- run:
57+
name: Test Clang 3.8 with OpenJDK JNI
58+
when: always
59+
environment:
60+
CXX: clang++-3.8
61+
CC: clang-3.8
62+
VARIANT: openjdk
63+
command: make clean ; make test examples
64+
65+
- run:
66+
name: Test Clang 3.9 with Android JNI
67+
when: always
68+
environment:
69+
CXX: clang++-3.9
70+
CC: clang-3.9
71+
VARIANT: android
72+
command: make clean ; make test examples
73+
- run:
74+
name: Test Clang 3.9 with OpenJDK JNI
75+
when: always
76+
environment:
77+
CXX: clang++-3.9
78+
CC: clang-3.9
79+
VARIANT: openjdk
80+
command: make clean ; make test examples
81+
82+
- run:
83+
name: Test Clang 4.0 with Android JNI
84+
when: always
85+
environment:
86+
CXX: clang++-4.0
87+
CC: clang-4.0
88+
VARIANT: android
89+
command: make clean ; make test examples
90+
- run:
91+
name: Test Clang 4.0 with OpenJDK JNI
92+
when: always
93+
environment:
94+
CXX: clang++-4.0
95+
CC: clang-4.0
96+
VARIANT: openjdk
97+
command: make clean ; make test examples
98+
99+
- run:
100+
name: Test Clang 5.0 with Android JNI
101+
when: always
102+
environment:
103+
CXX: clang++-5.0
104+
CC: clang-5.0
105+
VARIANT: android
106+
command: make clean ; make test examples
107+
- run:
108+
name: Test Clang 5.0 with OpenJDK JNI
109+
when: always
110+
environment:
111+
CXX: clang++-5.0
112+
CC: clang-5.0
113+
VARIANT: openjdk
114+
command: make clean ; make test examples

.gitignore

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
tst*
2-
examples/*.class
3-
examples/*.log
4-
examples/*.jnilib
1+
/examples/*.class
2+
/examples/*.log
3+
/build

Makefile

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,87 @@
1-
CFLAGS = $(env CFLAGS) -Iinclude --std=c++14 -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-exit-time-destructors -Werror
1+
VARIANT ?= android
2+
BUILD := build/$(VARIANT)
23

3-
test:
4-
$(CXX) -o tst $(CFLAGS) -Itest/android -g -Wno-padded test/low_level.cpp && ./tst
5-
$(CXX) -o tst $(CFLAGS) -Itest/android -g -Wno-padded test/high_level.cpp && ./tst
6-
$(CXX) -o tst $(CFLAGS) -Itest/openjdk -g -Wno-padded -Wno-reserved-id-macro test/low_level.cpp && ./tst
7-
$(CXX) -o tst $(CFLAGS) -Itest/openjdk -g -Wno-padded -Wno-reserved-id-macro test/high_level.cpp && ./tst
4+
COMPILER := $(shell CXX="${CXX}" misc/compiler.sh)
5+
ifeq ($(COMPILER), clang)
6+
CXXFLAGS_WARNINGS := -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-exit-time-destructors
7+
else ifeq ($(COMPILER), gcc)
8+
CXXFLAGS_WARNINGS := -Wall -Wextra -pedantic -Wno-unused-but-set-variable
9+
endif
810

9-
.PHONY: test
11+
CXXFLAGS := $(CXXFLAGS) --std=c++14 -fPIC -Iinclude $(CXXFLAGS_WARNINGS) -Werror
12+
CXXFLAGS_system := -g
13+
CXXFLAGS_android := -g -Itest/android -Wno-padded
14+
CXXFLAGS_openjdk := -g -Itest/openjdk -Wno-padded -Wno-reserved-id-macro
15+
16+
UNAME := $(shell uname -s)
17+
ifeq ($(UNAME), Darwin)
18+
dylib := jnilib
19+
LDFLAGS_shared := -dynamiclib
20+
else ifeq ($(UNAME), Linux)
21+
dylib := so
22+
LDFLAGS_shared := -shared
23+
else
24+
$(error Cannot determine host platform)
25+
endif
26+
27+
TARGETS += low_level
28+
low_level_SOURCES := test/low_level.cpp
29+
30+
TARGETS += high_level
31+
high_level_SOURCES := test/high_level.cpp
1032

11-
examples:
12-
$(CXX) -dynamiclib -o examples/libhello.jnilib $(CFLAGS) -Wno-shadow -Wno-padded -Itest examples/hello.cpp
13-
javac examples/Hello.java
14-
cd examples && java -Xcheck:jni Hello $(shell whoami)
33+
TARGETS += libhello.$(dylib)
34+
libhello.$(dylib)_SOURCES = examples/hello.cpp
35+
CXXFLAGS__examples/hello.cpp = -Wno-shadow
36+
libhello.$(dylib)_LDFLAGS = $(LDFLAGS_shared)
1537

16-
$(CXX) -dynamiclib -o examples/libpeer.jnilib $(CFLAGS) -Wno-shadow -Wno-padded -Itest examples/native_peer.cpp
17-
javac examples/NativePeer.java
18-
cd examples && java -Xcheck:jni NativePeer
38+
TARGETS += libpeer.$(dylib)
39+
libpeer.$(dylib)_SOURCES = examples/native_peer.cpp
40+
CXXFLAGS__examples/native_peer.cpp = -Wno-shadow
41+
libpeer.$(dylib)_LDFLAGS = $(LDFLAGS_shared)
42+
43+
.PHONY: all
44+
all: $(TARGETS)
45+
46+
.PHONY: test
47+
test: low_level high_level
48+
$(BUILD)/low_level
49+
$(BUILD)/high_level
1950

2051
.PHONY: examples
52+
examples: libhello.$(dylib) examples/Hello.class libpeer.$(dylib) examples/NativePeer.class
53+
java -Djava.library.path=$(BUILD) -Xcheck:jni -cp examples Hello $(shell whoami)
54+
java -Djava.library.path=$(BUILD) -Xcheck:jni -cp examples NativePeer
55+
56+
# --------------------------------------------------------------------------------------------------
57+
58+
define TARGET_template
59+
-include $(patsubst %,$(BUILD)/%.d,$$($(1)_SOURCES))
60+
$(BUILD)/$(1): LDFLAGS = $(LDFLAGS) $$($(1)_LDFLAGS)
61+
$(BUILD)/$(1): $(patsubst %,$(BUILD)/%.o,$$($(1)_SOURCES))
62+
$(1): $(BUILD)/$(1)
63+
endef
64+
65+
$(foreach target,$(TARGETS),$(eval $(call TARGET_template,$(target))))
66+
67+
# Link binaries
68+
$(patsubst %,$(BUILD)/%,$(TARGETS)):
69+
$(CXX) $(CXXFLAGS) $(CXXFLAGS_$(VARIANT)) $(LDFLAGS) -o $@ $^
70+
71+
# Compile C++ files
72+
$(BUILD)/%.cpp.o: %.cpp $(BUILD)/%.d
73+
@mkdir -p $(dir $@)
74+
$(CXX) -x c++ -MMD -MF $(BUILD)/$*.cpp.d $(CXXFLAGS) $(CXXFLAGS_$(VARIANT)) $(CXXFLAGS__$*.cpp) -c -o $@ $<
75+
76+
# Compile Java files
77+
%.class: %.java
78+
javac $<
79+
80+
clean:
81+
-rm -rf build
82+
-rm -rf examples/*.class
83+
84+
# Dependency tracking
85+
.PRECIOUS = $(BUILD)/%.d
86+
$(BUILD)/%.d: ;
87+
-include $(DEPS)

examples/native_peer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*)
99
static constexpr auto Name() { return "Calculator"; }
1010

1111
Calculator(JNIEnv&) { std::cout << "Native peer initialized" << std::endl; }
12+
Calculator(const Calculator&) = delete; // noncopyable
1213
~Calculator() { std::cout << "Native peer finalized" << std::endl; }
1314

1415
jni::jlong Add(jni::JNIEnv&, jni::jlong a, jni::jlong b) { return a + b; }

misc/compiler.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
case "$(${CXX} -v 2>&1)" in
4+
*clang*) echo clang ;;
5+
*gcc*) echo gcc ;;
6+
*) echo unknown ;;
7+
esac

0 commit comments

Comments
 (0)