pud: wireformat-java: improve java detection
[olsrd.git] / lib / pud / wireformat-java / Makefile
1 include ../wireformat/Makefile.inc
2 WIREFORMAT_PROJECT := $(PROJECT)
3
4 include Makefile.inc
5
6 include ../Makefile.inc
7 OLSRD_PLUGIN = false
8
9 TOPDIR = ../../..
10 include $(TOPDIR)/Makefile.inc
11
12 LDFLAGS = -shared -Wl,--warn-common -fPIC
13
14 # 32/64 cross compilation
15 ifdef M32
16 CFLAGS +=       -m32
17 LDFLAGS +=      -m32
18 endif
19 ifdef M64
20 CFLAGS +=       -m64
21 LDFLAGS +=      -m64
22 endif
23
24 ######################
25 #
26 # Settings
27 #
28 ######################
29
30 ifeq ($(DEBUG),1)
31 DEBUGBOOLEAN = true
32 else
33 DEBUGBOOLEAN = false
34 endif
35
36
37 DESTDIR ?=
38 USRDIR ?= $(DESTDIR)/usr
39 LIBDIR ?= $(USRDIR)/lib
40 SHAREDIR ?= $(USRDIR)/share
41 SHAREJAVADIR ?= $(SHAREDIR)/java/$(EXENAME)
42
43
44 MACHINE=$(shell uname -m)
45 ARCH=
46 ARCH2=-i386
47 ifeq ($(strip $(MACHINE)),x86_64)
48   ARCH=.$(MACHINE)
49   ARCH2=-amd64
50 endif
51
52
53 RESOURCES_DIR = resources
54 BUILD_DIR = build
55 LIBDIR_BUILD = lib
56
57 INC_DIR_WIREFORMAT = ../wireformat/include
58 INC_DIR_WIREFORMAT_FULL = $(INC_DIR_WIREFORMAT)/$(WIREFORMAT_PROJECT)
59
60
61 JAVAHOMEREGEX = ^[[:space:]]*java\.home[[:space:]]*=[[:space:]]*(.+)[[:space:]]*$$
62 JAVAHOME = $(shell java -XshowSettings 2>&1 | grep -E '$(JAVAHOMEREGEX)' | sed -r 's/$(JAVAHOMEREGEX)/\1/')
63
64 ifeq ($(JAVAHOME),)
65   $(error No java detected)
66 endif
67
68 INC_DIR_JVM = $(JAVAHOME)/../include
69
70 ifeq ($(wildcard $(INC_DIR_JVM)),)
71   $(error No java include directory detected at $(INC_DIR_JVM))
72 endif
73
74 ifeq ($(OS),android)
75   INC_DIR_JVM_OS = $(INC_DIR_JVM)/linux
76 else
77   INC_DIR_JVM_OS = $(INC_DIR_JVM)/$(OS)
78 endif
79
80 ifeq ($(wildcard $(INC_DIR_JVM_OS)),)
81   $(error No java OS include directory detected at $(INC_DIR_JVM_OS))
82 endif
83
84
85 SRC_DIR_C = src/main/c
86
87 INCLUDES = -I "$(INC_DIR_WIREFORMAT)" -I "$(INC_DIR_JVM)" -I "$(INC_DIR_JVM_OS)"
88 LIBRARIES = -L ../wireformat/lib -l$(WIREFORMAT_PROJECT)
89
90
91 BUILD_DIR_JNI = $(BUILD_DIR)/jni
92
93 JAVASRC = src/main/java
94 JAVAVERSIONSRC = 1.6
95 JAVAVERSIONDST = 1.6
96 JAVABOOTCLASSPATH = resources/ee.j2se/$(JAVAVERSIONSRC)/ee.j2se.jar
97 JAVA_PKG = org.olsr.plugin.pud
98 JAVA_PKG_DIR = $(subst .,/,$(JAVA_PKG))
99 JAVA_PKG_UNDER = $(subst .,_,$(JAVA_PKG))
100
101
102 ifneq ($(OS),linux)
103
104 .PHONY: all default_target install clean
105
106 all: default_target
107
108 default_target install clean:
109         @echo "*** only supported on Linux"
110
111 else
112
113 GIT_SHA = $(shell git describe 2> /dev/null || echo "Unknown")
114
115 .PHONY: all default_target java prepare clean doc doc-clean install uninstall
116
117 #
118 # Targets
119 #
120
121 all: default_target
122
123 default_target: prepare $(LIBDIR_BUILD)/$(SONAME) java
124
125 .gitignore:
126 ifeq ($(VERBOSE),0)
127         @echo "[$@]"
128 endif
129         $(MAKECMDPREFIX)echo "/$(BUILD_DIR)/" > $@
130         $(MAKECMDPREFIX)echo "/$(LIBDIR_BUILD)/" >> $@
131         $(MAKECMDPREFIX)echo "/build.xml" >> $@
132         $(MAKECMDPREFIX)echo "/$(SRC_DIR_C)/$(JAVA_PKG_UNDER)_PositionUpdate.h" >> $@
133         $(MAKECMDPREFIX)echo "/$(SRC_DIR_C)/$(JAVA_PKG_UNDER)_ClusterLeader.h" >> $@
134         $(MAKECMDPREFIX)echo "/$(SRC_DIR_C)/$(JAVA_PKG_UNDER)_UplinkMessage.h" >> $@
135         $(MAKECMDPREFIX)echo "/$(JAVASRC)/$(JAVA_PKG_DIR)/WireFormatConstants.java" >> $@
136         $(MAKECMDPREFIX)$(MAKE) -C doc $@
137
138 $(LIBDIR_BUILD)/$(SONAME): $(BUILD_DIR)/$(JAVA_PKG_UNDER)_UplinkMessage.o \
139                            $(BUILD_DIR)/$(JAVA_PKG_UNDER)_ClusterLeader.o \
140                            $(BUILD_DIR)/$(JAVA_PKG_UNDER)_PositionUpdate.o
141 ifeq ($(VERBOSE),0)
142         @echo "[LD] $@"
143 endif
144         $(MAKECMDPREFIX)$(CC) $(LDFLAGS) -Wl,-soname=$(SONAME) $(LIBRARIES) -o "$@" \
145       $(BUILD_DIR)/$(JAVA_PKG_UNDER)_UplinkMessage.o \
146       $(BUILD_DIR)/$(JAVA_PKG_UNDER)_ClusterLeader.o \
147       $(BUILD_DIR)/$(JAVA_PKG_UNDER)_PositionUpdate.o
148
149 # PositionUpdate
150 $(BUILD_DIR)/$(JAVA_PKG_UNDER)_PositionUpdate.o: $(SRC_DIR_C)/$(JAVA_PKG_UNDER)_PositionUpdate.c \
151                                                  $(SRC_DIR_C)/$(JAVA_PKG_UNDER)_PositionUpdate.h \
152                                                  $(INC_DIR_WIREFORMAT_FULL)/wireFormat.h \
153                                                  $(INC_DIR_WIREFORMAT_FULL)/nodeIdConversion.h
154 ifeq ($(VERBOSE),0)
155         @echo "[CC] $<"
156 endif
157         $(MAKECMDPREFIX)$(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDES) -c -o "$@" "$<"
158
159 $(SRC_DIR_C)/$(JAVA_PKG_UNDER)_PositionUpdate.h: $(BUILD_DIR_JNI)/$(JAVA_PKG_DIR)/PositionUpdate.class
160 ifeq ($(VERBOSE),0)
161         @echo "[JavaH] $<"
162 endif
163         $(MAKECMDPREFIX)rm -f "$@"
164         $(MAKECMDPREFIX)javah -jni -classpath "$(BUILD_DIR_JNI)" -o "$@" "$(JAVA_PKG).PositionUpdate"
165
166 $(BUILD_DIR_JNI)/$(JAVA_PKG_DIR)/PositionUpdate.class: $(JAVASRC)/$(JAVA_PKG_DIR)/PositionUpdate.java \
167                                                        $(JAVASRC)/$(JAVA_PKG_DIR)/LibraryLoader.java \
168                                                        $(JAVASRC)/$(JAVA_PKG_DIR)/WireFormatConstants.java
169 ifeq ($(VERBOSE),0)
170         @echo "[JavaC] $<"
171 endif
172         $(MAKECMDPREFIX)javac -bootclasspath $(JAVABOOTCLASSPATH) -classpath $(JAVASRC) -source 1.6 -target 1.6 -Xlint -d "$(BUILD_DIR_JNI)" "$<"
173
174 # ClusterLeader
175 $(BUILD_DIR)/$(JAVA_PKG_UNDER)_ClusterLeader.o: $(SRC_DIR_C)/$(JAVA_PKG_UNDER)_ClusterLeader.c \
176                                                  $(SRC_DIR_C)/$(JAVA_PKG_UNDER)_ClusterLeader.h \
177                                                  $(INC_DIR_WIREFORMAT_FULL)/wireFormat.h \
178                                                  $(INC_DIR_WIREFORMAT_FULL)/nodeIdConversion.h
179 ifeq ($(VERBOSE),0)
180         @echo "[CC] $<"
181 endif
182         $(MAKECMDPREFIX)$(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDES) -c -o "$@" "$<"
183
184 $(SRC_DIR_C)/$(JAVA_PKG_UNDER)_ClusterLeader.h: $(BUILD_DIR_JNI)/$(JAVA_PKG_DIR)/ClusterLeader.class
185 ifeq ($(VERBOSE),0)
186         @echo "[JavaH] $<"
187 endif
188         $(MAKECMDPREFIX)rm -f "$@"
189         $(MAKECMDPREFIX)javah -jni -classpath "$(BUILD_DIR_JNI)" -o "$@" "$(JAVA_PKG).ClusterLeader"
190
191 $(BUILD_DIR_JNI)/$(JAVA_PKG_DIR)/ClusterLeader.class: $(JAVASRC)/$(JAVA_PKG_DIR)/ClusterLeader.java \
192                                                       $(JAVASRC)/$(JAVA_PKG_DIR)/LibraryLoader.java \
193                                                       $(JAVASRC)/$(JAVA_PKG_DIR)/WireFormatConstants.java
194 ifeq ($(VERBOSE),0)
195         @echo "[JavaC] $<"
196 endif
197         $(MAKECMDPREFIX)javac -bootclasspath $(JAVABOOTCLASSPATH) -classpath $(JAVASRC) -source 1.6 -target 1.6 -Xlint -d "$(BUILD_DIR_JNI)" "$<"
198
199 # UplinkMessage
200 $(BUILD_DIR)/$(JAVA_PKG_UNDER)_UplinkMessage.o: $(SRC_DIR_C)/$(JAVA_PKG_UNDER)_UplinkMessage.c \
201                                                  $(SRC_DIR_C)/$(JAVA_PKG_UNDER)_UplinkMessage.h \
202                                                  $(INC_DIR_WIREFORMAT_FULL)/wireFormat.h \
203                                                  $(INC_DIR_WIREFORMAT_FULL)/nodeIdConversion.h
204 ifeq ($(VERBOSE),0)
205         @echo "[CC] $<"
206 endif
207         $(MAKECMDPREFIX)$(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDES) -c -o "$@" "$<"
208
209 $(SRC_DIR_C)/$(JAVA_PKG_UNDER)_UplinkMessage.h: $(BUILD_DIR_JNI)/$(JAVA_PKG_DIR)/UplinkMessage.class
210 ifeq ($(VERBOSE),0)
211         @echo "[JavaH] $<"
212 endif
213         $(MAKECMDPREFIX)rm -f "$@"
214         $(MAKECMDPREFIX)javah -jni -classpath "$(BUILD_DIR_JNI)" -o "$@" "$(JAVA_PKG).UplinkMessage"
215
216 $(BUILD_DIR_JNI)/$(JAVA_PKG_DIR)/UplinkMessage.class: $(JAVASRC)/$(JAVA_PKG_DIR)/UplinkMessage.java \
217                                                       $(JAVASRC)/$(JAVA_PKG_DIR)/LibraryLoader.java \
218                                                       $(JAVASRC)/$(JAVA_PKG_DIR)/WireFormatConstants.java
219 ifeq ($(VERBOSE),0)
220         @echo "[JavaC] $<"
221 endif
222         $(MAKECMDPREFIX)javac -bootclasspath $(JAVABOOTCLASSPATH) -classpath $(JAVASRC) -source 1.6 -target 1.6 -Xlint -d "$(BUILD_DIR_JNI)" "$<"
223
224
225 #
226 # java
227 #
228
229 java: prepare \
230      build.xml \
231      $(JAVASRC)/$(JAVA_PKG_DIR)/PositionUpdate.java \
232      $(JAVASRC)/$(JAVA_PKG_DIR)/WireFormatConstants.java
233         $(MAKECMDPREFIX)ant -f build.xml release
234
235 build.xml: $(RESOURCES_DIR)/build.xml
236 ifeq ($(VERBOSE),0)
237         @echo "[generate] $@"
238 endif
239         $(MAKECMDPREFIX)sed -r -e "s#PLUGIN_VER#$(PLUGIN_VER)#g" \
240                 -e "s#PROJECT#$(PROJECT)#g" \
241                 -e "s#BUILD_DIR#$(BUILD_DIR)#g" \
242                 -e "s#LIBDIR_BUILD#$(LIBDIR_BUILD)#g" \
243                 -e "s#JAVASRC#$(JAVASRC)#g" \
244                 -e "s#JAVAVERSIONSRC#$(JAVAVERSIONSRC)#g" \
245                 -e "s#JAVAVERSIONDST#$(JAVAVERSIONDST)#g" \
246                 -e "s#DEBUG#$(DEBUGBOOLEAN)#g" \
247                 "$<" > "$@"
248
249
250 #
251 # generate java classes
252 #
253
254 $(JAVASRC)/$(JAVA_PKG_DIR)/WireFormatConstants.java: \
255       $(RESOURCES_DIR)/WireFormatConstants.java.h \
256       $(BUILD_DIR_JNI)/c/library.h \
257       $(BUILD_DIR_JNI)/c/wireFormat.h
258 ifeq ($(VERBOSE),0)
259         @echo "[generate] $@"
260 endif
261         $(MAKECMDPREFIX)$(CPP) -I "$(BUILD_DIR_JNI)/c" -DPLUGIN_VER=\"$(PLUGIN_VER)\" -DGIT_SHA=\"$(GIT_SHA)\" "$<" | \
262           sed -r -e '/^[[:space:]]*#.*$$/ d' \
263                  -e '/^[[:space:]]*$$/ d' \
264                  > "$@"
265
266
267 #
268 # generate jni includes
269 #
270
271 $(BUILD_DIR_JNI)/c/library.h: Makefile.inc
272 ifeq ($(VERBOSE),0)
273         @echo "[generate] $@"
274 endif
275         $(MAKECMDPREFIX)echo "#define PUD_WIRE_FORMAT_LIBRARYNAME \"$(PROJECT)\"" > "$@"
276
277 $(BUILD_DIR_JNI)/c/wireFormat.h: $(INC_DIR_WIREFORMAT_FULL)/wireFormat.h
278 ifeq ($(VERBOSE),0)
279         @echo "[generate] $@"
280 endif
281         $(MAKECMDPREFIX)grep -E '^[[:space:]]*#define[[:space:]]+' "$<" > "$@"
282
283
284 #
285 # Phony Targets
286 #
287
288 prepare:
289         $(MAKECMDPREFIX)mkdir -p "$(BUILD_DIR)" \
290                   "$(LIBDIR_BUILD)" \
291                   "$(BUILD_DIR_JNI)/c"
292
293
294 clean: doc-clean
295 ifeq ($(VERBOSE),0)
296         @echo "[$@]"
297 endif
298         $(MAKECMDPREFIX)rm -fr "$(BUILD_DIR)" "$(LIBDIR_BUILD)" \
299                 build.xml \
300                 "$(SRC_DIR_C)/$(JAVA_PKG_UNDER)_PositionUpdate.h" \
301                 "$(SRC_DIR_C)/$(JAVA_PKG_UNDER)_ClusterLeader.h" \
302                 "$(SRC_DIR_C)/$(JAVA_PKG_UNDER)_UplinkMessage.h" \
303                 "$(JAVASRC)/$(JAVA_PKG_DIR)/WireFormatConstants.java"
304
305 doc: build.xml
306         $(MAKECMDPREFIX)$(MAKE) -C doc all
307         $(MAKECMDPREFIX)ant -f build.xml javadoc
308
309 doc-clean:
310         $(MAKECMDPREFIX)$(MAKE) -C doc clean
311
312 install: all
313         mkdir -v -p "$(LIBDIR)"
314         cp "$(LIBDIR_BUILD)/$(SONAME)" "$(LIBDIR)/$(SONAME).$(PLUGIN_VER)"
315         $(STRIP) "$(LIBDIR)/$(SONAME).$(PLUGIN_VER)"
316         /sbin/ldconfig -n "$(LIBDIR)"
317         mkdir -v -p "$(SHAREJAVADIR)"
318         cp -t "$(SHAREJAVADIR)" \
319       "$(LIBDIR_BUILD)/$(PROJECT).jar" \
320       "$(LIBDIR_BUILD)/$(PROJECT)-src.zip" \
321       "$(LIBDIR_BUILD)/$(PROJECT)-doc.zip"
322
323 uninstall:
324         rm -f "$(LIBDIR)/$(SONAME)" \
325               "$(LIBDIR)/$(SONAME).$(PLUGIN_VER)"
326         /sbin/ldconfig -n "$(LIBDIR)"
327         rmdir -v -p --ignore-fail-on-non-empty "$(LIBDIR)"
328         rm -f "$(SHAREJAVADIR)/$(PROJECT).jar" \
329           "$(SHAREJAVADIR)/$(PROJECT)-src.zip" \
330           "$(SHAREJAVADIR)/$(PROJECT)-doc.zip"
331         rmdir -v -p --ignore-fail-on-non-empty "$(SHAREJAVADIR)"
332
333 endif