|
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) |
2 | 3 |
|
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 |
8 | 10 |
|
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 |
10 | 32 |
|
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) |
15 | 37 |
|
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 |
19 | 50 |
|
20 | 51 | .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) |
0 commit comments