2 # Copyright (C) 2019,2022 Michael Zucchi
4 # This is the copyright for java.make
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # General purpose modular java makefile that supports native library
21 # compilation directly. Non-recrusve implementation.
23 # Uses metamake programming with some file conventions to implement
24 # auto-make-like features.
28 # java_MODULES list of java modules to compile. The sources must
29 # exist in src/<module>/classes. Resource files are
30 # stored in src/<module>/classes. Source-code
31 # generators must exist in src/<module>/gen. Native
32 # libraries must exist in src/<module>/jni.
34 # native_MODULES list of native-only "modules".
39 # JAVA_HOME location of jdk.
40 # JAVAC java compiler to use. Default is 'javac' on the path.
41 # JAVACFLAGS javac flags applied to all invocations.
45 # JMODFLAGS jmod flags.
46 # JAVAFLAGS java flags for run targets
48 # Module specific variables
50 # <module>_JDEPMOD Lists modules which this one depends on.
52 # <module>_JAVACFLAGS Extra module-specific flags for each command.
56 # <module>_JAVA Java sources. If not set it is found from src/<module>/classes/(*.java)
57 # <module>_RESOURCES .jar resources. If not set it is found from src/<module>/classes/(not *.java)
58 # <module>_JAVA_GENERATED Java generated sources. These must be relative to the package name.
60 # Variables for use in fragments
62 # gen.make and jni.make can additionally make use of these variables
64 # <module>_gendir Location for files used in Java generation process (per project).
65 # <module>_genjavadir Location where _JAVA_GENERATED .java files will be created (per project).
66 # <module>_objdir Location for c objects (per target).
67 # <module>_incdir Location for output includes, .jmod staging.
68 # <module>_libdir Location for output libraries, .jmod staging. May point to _bindir.
69 # <module>_bindir Location for output commands, .jmod staging.
74 # Each module can define one or more native libraries.
76 # These are compiled after the java sources have been compiled as that
77 # process also generates any native binding headers.
79 # <module>_NATIVE_LIBRARIES list of libraries to build.
80 # library names match System.loadLibrary().
91 # SO shared library suffix
92 # LIB shared library prefix
96 # $(call library-path,<module>,<libname>) will resolve to the library file name.
98 # Per library variables.
100 # <library>_SOURCES .c source files for library. Paths are relative to src/<module>/native.
101 # <library>_CXXSOURCES .c source files for library. Paths are relative to src/<module>/native.
102 # <library>_HEADERS header files for install/jmod
103 # <library>_COMMANDS commands/bin/scripts for install/jmod
105 # <library>_LDFLAGS link flags
106 # <library>_LIBADD extra objects to add to link line
107 # <library>_LDLIBS link libraries
108 # <library>_CPPFLAGS c and c++ pre-processor flags. "-Isrc/<module>/jni -Ibin/include/<module>" is implicit.
109 # <library>_CCFLAGS c compiler flags
110 # <library>_CXXFLAGS c++ compiler flags
112 # <library>_DEPENDENCIES A list of other objects on which this library depends before linking.
114 # .c and .cc files have dependencies automatically generated
119 # make gen only generate java sources
120 # make clean rm -rf bin
121 # make dist create dist tar in bin/
122 # make | make jar make all jars and jmods
123 # make bin make everything but jars and mods
128 # All intermediate and output files are written to bin/
130 # This layout is enforced by javac
131 # bin/include/<module>/ .h files from javac -h
132 # bin/modules/<module>/ .class files from javac
134 # This layout is convenient for netbeans
135 # bin/gen/<module>/gen/ .c, exe files for generator free use
136 # bin/gen/<module>/classes/ .java files from generator <module>_JAVA_GENERATED
139 # bin/status/ marker files for makefile
141 # bin/<module>/<target>/lib .so librareies for jmod <module>_LIBRARIES = libname
142 # bin/<module>/<target>/obj .o, .d files for library <libname>_SOURCES
143 # bin/<module>/<target>/include .h files for jmod <libname>_HEADERS
144 # bin/<module>/<target>/<module>.jmod .jmod module
147 # bin/<target>/lib/ modular jar files and shared libraries for GNU/linux dev
148 # bin/<target>/include/ header files for exported shared libraries
149 # bin/<target>/bin/ shared libraries for microsoft dev
150 # bin/<target>/jmods/ jmod files for 'jlink' use.
152 # ######################################################################
157 # All modules with native code
158 java_JMODS=$(foreach module,$(java_MODULES),$(if $(wildcard src/$(module)/jni/jni.make),$(module)))
159 # Only modules with no native code
160 java_JARS=$(foreach module,$(java_MODULES),$(if $(wildcard src/$(module)/jni/jni.make),,$(module)))
161 # Modules with generated java source
162 java_JGEN=$(foreach module,$(java_MODULES),$(if $(wildcard src/$(module)/gen/gen.make),$(module)))
164 # Define some useful variables before including fragments
165 define common_variables=
166 $(1)_gendir:=bin/gen/$(1)/gen
167 $(1)_genjavadir:=bin/gen/$(1)/classes
168 $(1)_objdir:=bin/$(1)/$(TARGET)/obj
169 $(1)_incdir:=bin/$(1)/$(TARGET)/include
170 $(1)_libdir:=$$(if $$(filter windows-%,$(TARGET)),bin/$(1)/$(TARGET)/bin,bin/$(1)/$(TARGET)/lib)
171 $(1)_bindir:=bin/$(1)/$(TARGET)/bin
173 $(1)_SCRIPTS := $$(shell find src/$(1)/bin src/$(1)/$(TARGET)/bin -type f 2>/dev/null)
176 $(1)_DATA := $$(shell find src/$(1)/lib src/$(1)/$(TARGET)/lib -type f 2>/dev/null)
180 define java_variables=
182 $(1)_JAVA := $$(shell find src/$(1)/classes -type f -name '*.java')
184 ifndef $(1)_RESOURCES
185 $(1)_RESOURCES := $$(shell find src/$(1)/classes -type f \! -name '*.java')
189 java_libdir:=$(if $(filter windows-%,$(TARGET)),bin/$(TARGET)/bin,bin/$(TARGET)/lib)
190 java_bindir:=bin/$(TARGET)/bin
191 java_jardir:=bin/$(TARGET)/lib
192 java_incdir:=bin/$(TARGET)/include
193 java_jmoddir:=bin/$(TARGET)/jmods
195 $(foreach module,$(java_MODULES),$(eval $(call java_variables,$(module))))
196 $(foreach module,$(java_MODULES) $(native_MODULES),$(eval $(call common_variables,$(module))))
198 # ######################################################################
205 .PHONY: all clean jar bin gen $(java_MODULES)
209 include $(foreach module,$(java_MODULES),$(wildcard src/$(module)/gen/gen.make))
210 include $(foreach module,$(java_MODULES) $(native_MODULES),$(wildcard src/$(module)/native/native.make))
212 # staging only, not for modules, not sure here?
213 define bin_files_targets=
214 bin $1: $(patsubst src/$1/bin/%,bin/$(TARGET)/bin/%,$($1_SCRIPTS))
215 bin/$(TARGET)/bin/%: src/$1/bin/%
216 install -vD -m 0755 $$< $$@
218 define lib_files_targets=
219 bin $1: $(patsubst src/$1/lib/%,bin/$(TARGET)/lib/%,$($1_DATA))
220 bin/$(TARGET)/lib/%: src/$1/lib/%
221 install -vD -m 0644 $$< $$@
224 #$(foreach module,$(java_MODULES),$(if $($(module)_SCRIPTS),$(info $(call bin_files_targets,$(module)))))
225 #$(foreach module,$(java_MODULES),$(if $($(module)_DATA),$(info $(call lib_files_targets,$(module)))))
227 $(foreach module,$(java_MODULES),$(if $($(module)_SCRIPTS),$(eval $(call bin_files_targets,$(module)))))
228 $(foreach module,$(java_MODULES),$(if $($(module)_DATA),$(eval $(call lib_files_targets,$(module)))))
232 # ######################################################################
234 # ######################################################################
237 # Rules for module $(1)
238 $(1)_JAVA_generated = $$(addprefix $$($1_genjavadir)/,$$($1_JAVA_GENERATED))
240 bin/status/$1.classes: $(patsubst %,bin/status/%.classes,$(filter $($1_JDEPMOD), $(java_MODULES))) $$($1_JAVA) $$($1_JAVA_generated)
242 jar $1: $(java_jardir)/$1.jar $(java_jmoddir)/$1.jmod
243 $1: $(filter $($1_JDEPMOD), $(native_MODULES))
244 bin: bin/status/$(1).classes $$($1_resources_files)
245 sources: $(java_jardir)/$(1)-sources.zip
246 gen: $$($(1)_JAVA_generated)
248 $(java_jardir)/$1.jar $(java_jmoddir)/$1.jmod: bin/status/$1.classes $(patsubst src/$1/classes/%,%,$($1_RESOURCES))
251 $(java_jardir)/$1.jar:
254 $(JARFLAGS) $$($(1)_JARFLAGS) \
255 -C bin/modules/$(1) .
258 $(java_jmoddir)/$1.jmod:
262 $$(JMODFLAGS) $$($(1)_JMODFLAGS) \
263 --target-platform $(TARGET) \
264 --class-path bin/modules/$(1) \
265 $$(if $$(wildcard bin/$(1)/$(TARGET)/include),--header-files bin/$(1)/$(TARGET)/include) \
266 $$(if $$(wildcard src/$(1)/legal),--legal-notices src/$(1)/legal) \
267 $$(if $$(wildcard bin/$(1)/$(TARGET)/bin),--cmds bin/$(1)/$(TARGET)/bin) \
268 $$(if $$(wildcard bin/$(1)/$(TARGET)/lib),--libs bin/$(1)/$(TARGET)/lib) \
271 # Create an IDE source zip, paths have to match --module-source-path
272 $(java_jardir)/$1-sources.zip: bin/status/$1.classes
275 $$(patsubst src/$1/classes/%,-C src/$1/classes %,$$(filter src/$1/classes/%,$$($1_JAVA))) \
276 $$(patsubst bin/gen/$1/classes/%,-C bin/gen/$1/classes %,$$(filter bin/gen/$1/classes/%,$$($1_JAVA)))
279 bin/modules/$1/%: src/$1/classes/%
283 bin/status/$1.classes:
286 --module-source-path "src/*/classes:bin/gen/*/classes" \
287 $(if $(JAVAMODPATH),--module-path $(subst $(S),:,$(JAVAMODPATH))) \
288 $(JAVACFLAGS) $($1_JAVACFLAGS) \
291 $($1_JAVA) $($1_JAVA_generated)
295 #$(foreach module,$(java_MODULES),$(info $(call java_targets,$(module))))
296 $(foreach module,$(java_MODULES),$(eval $(call java_targets,$(module))))
298 # setup run-* targets
301 LD_LIBRARY_PATH=$(FFMPEG_HOME)/lib \
303 $(if $(JAVAMODPATH) $($1_JAVAMODPATH),--module-path $(subst $(S),:,$(JAVAMODPATH) $($1_JAVAMODPATH))) \
304 $(JMAINFLAGS) $($1_JMAINFLAGS) \
310 #$(foreach module,$(java_MODULES),$(foreach main,$($(module)_JMAIN),$(info $(call run_targets,$(module),$(main)))))
311 $(foreach module,$(java_MODULES),$(foreach main,$($(module)_JMAIN),$(eval $(call run_targets,$(module),$(main)))))
313 # ######################################################################
314 # notzed.nativez jdk.foreign export tool via _API variables
315 # ######################################################################
317 # <module>_API List of api's to include
318 # <module>_APIFLAGS Extra flags to pass to export-api for every api in module
319 # <module>_<api>_APIFLAGS Extra flags to pass to export-api for each api
321 define export_targets=
322 bin/status/$1.classes: bin/status/$2.export
323 bin/status/$2.export:
324 mkdir -p bin/gen/$1/gen bin/status
325 $(NATIVEZ_HOME)/bin/export-api \
326 -w bin/gen/$1/gen -d bin/gen/$1/classes $($1_APIFLAGS) $($1_$2_APIFLAGS) src/$1/gen/$2.api
329 bin/status/$2.export.d:
330 @$(NATIVEZ_HOME)/bin/export-api -M -MT "$$(@:.d=) $$@" -MF $$@ \
331 -w bin/gen/$1/gen -d bin/gen/$1/classes $($1_APIFLAGS) $($1_$2_APIFLAGS) src/$1/gen/$2.api 2>/dev/null
333 $(if $(filter clean dist gen,$(MAKECMDGOALS)),,-include bin/status/$2.export.d)
336 #$(foreach module,$(java_MODULES),$(if $($(module)_API),$(foreach api,$($(module)_API),$(info $(call export_targets,$(module),$(api))))))
337 $(foreach module,$(java_MODULES),$(foreach api,$($(module)_API),$(eval $(call export_targets,$(module),$(api)))))
339 # ######################################################################
340 # C and c++ native library support
341 # ######################################################################
347 # functions to find cross-module stuff $(call library-path,modname,libname)
348 library-path=$($(1)_libdir)/$(LIB)$(2)$(SO)
349 library-dir=$($(1)_libdir)/
351 define native_library=
352 # Rule for library $(2) in module $(1)
353 $(2)_OBJS = $(patsubst %.c, $($(1)_objdir)/%.o, $($(2)_SOURCES)) \
354 $(patsubst %.cc, $($(1)_objdir)/%.o, $($(2)_CXXSOURCES))
355 $(2)_SRCS = $(addprefix src/$(1)/native/,$($(2)_SOURCES))
356 $(2)_SO = $($(1)_libdir)/$(LIB)$(2)$(SO)
358 $($(1)_libdir)/$(LIB)$(2)$(SO): $$($(2)_OBJS) $($(2)_LIBADD) $($(2)_DEPENDENCIES)
360 $($(TARGET)_CC) -o $$@ -shared \
361 $($(TARGET)_LDFLAGS) $($(2)_LDFLAGS) $$($(2)_OBJS) $($(2)_LIBADD) $($(TARGET)_LDLIBS) $($(2)_LDLIBS)
363 $(java_libdir)/%: $($(1)_libdir)/%
365 $(java_bindir)/%: $($(1)_bindir)/%
367 $(java_incdir)/%: $($(1)_incdir)/%
370 $($(1)_objdir)/%.o: src/$(1)/native/%.c
372 $($(TARGET)_CC) -Isrc/$(1)/native -Ibin/include/$(1) \
373 $($(TARGET)_CPPFLAGS) $($(2)_CPPFLAGS) \
374 $($(TARGET)_CFLAGS) $($(2)_CFLAGS) -c -o $$@ $$<
376 $($(1)_objdir)/%.o: src/$(1)/native/%.cc
378 $($(TARGET)_CXX) -Isrc/$(1)/native -Ibin/include/$(1) \
379 $($(TARGET)_CPPFLAGS) $($(2)_CPPFLAGS) \
380 $($(TARGET)_CXXFLAGS) $($(2)_CXXFLAGS) -c -o $$@ $$<
382 $($(1)_incdir)/%: src/$(1)/native/%
384 $($(1)_libdir)/%: src/$(1)/native/%
387 # auto-dependencies for c files
388 $($(1)_objdir)/%.d: src/$(1)/native/%.c
391 @$($(TARGET)_CC) -MM -MT "$$(@:.d=.o) $$@" -Isrc/$(1)/jni -Ibin/include/$(1) \
392 $($(TARGET)_CPPFLAGS) $($(2)_CPPFLAGS) $$< -o $$@ 2>/dev/null
394 # auto-dependencies for c++ files
395 $($(1)_objdir)/%.d: src/$(1)/native/%.cc
398 @$($(TARGET)_CXX) -MM -MT "$$(@:.d=.o) $$@" -Isrc/$(1)/jni -Ibin/include/$(1) \
399 $($(TARGET)_CPPFLAGS) $($(2)_CPPFLAGS) $$< -o $$@ 2>/dev/null
401 bin native $(1) $(java_jmoddir)/$(1).jmod: \
402 $($(1)_libdir)/$(LIB)$(2)$(SO) \
403 $(java_libdir)/$(LIB)$(2)$(SO) \
404 $(addprefix $($(1)_incdir)/,$($(2)_HEADERS)) \
405 $(addprefix $(java_incdir)/,$($(2)_HEADERS)) \
406 $(addprefix $($(1)_bindir)/,$($(2)_COMMANDS)) \
407 $(addprefix $(java_bindir)/,$($(2)_COMMANDS)) \
408 $(addprefix $($(1)_libdir)/,$($(2)_LIBRARIES))
410 $(if $(filter clean dist gen,$(MAKECMDGOALS)),,-include $$($(2)_OBJS:.o=.d))
413 #$(foreach module,$(java_MODULES) $(native_MODULES),$(foreach library,$($(module)_NATIVE_LIBRARIES),$(info $(call native_library,$(module),$(library)))))
414 $(foreach module,$(java_MODULES) $(native_MODULES),$(foreach library,$($(module)_NATIVE_LIBRARIES),$(eval $(call native_library,$(module),$(library)))))
416 # ######################################################################
420 tar cfz bin/$(dist_NAME)-$(dist_VERSION).tar.gz \
421 --transform=s,^,$(dist_NAME)-$(dist_VERSION)/, \
422 config.make.in java.make Makefile src \