1 # Makefile base template
4 # - 1.0.0: add a version comment
5 # - 1.1.0: add 'help', 'sjar'
7 # - 1.2.1: improve 'apk' and add 'android'
8 # - 1.3.0: add 'man' for man(ual) pages
10 # Required parameters (the commented out ones are supposed to be per project):
12 #MAIN = path to main java source to compile
13 #MORE = path to supplementary needed resources not linked from MAIN
14 #NAME = name of project (used for jar output file)
15 #PREFIX = usually /usr/local (where to install the program)
16 #TEST = path to main test source to compile
17 #JAR_FLAGS += a list of things to pack, each usually prefixed with "-C bin/"
18 #SJAR_FLAGS += a list of things to pack, each usually prefixed with "-C src/",
19 # for *-sources.jar files
20 #TEST_PARAMS = any parameter to pass to the test runnable when "test-run"
21 #ID_FOR_ANDROID = id of activity to launch for Android
22 #RM_FOR_ANDROID = packages (if it ends with /) or classes to ignore for APK
26 JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/
28 JAVA_FLAGS += -cp ./bin/
36 @echo "Usual options:"
37 @echo "=============="
38 @echo " make : to build the jar file and man pages IF possible"
39 @echo " make help : to get this help screen"
40 @echo " make libs : to update the libraries into src/"
41 @echo " make build : to update the binaries (not the jar)"
42 @echo " make test : to update the test binaries"
43 @echo " make build jar : to update the binaries and jar file"
44 @echo " make sjar : to create the sources jar file"
45 @echo " make clean : to clean the directory of intermediate files"
46 @echo " make mrpropre : to clean the directory of all outputs"
47 @echo " make run : to run the program from the binaries"
48 @echo " make run-test : to run the test program from the binaries"
49 @echo " make jrun : to run the program from the jar file"
50 @echo " make install : to install the application into $$PREFIX"
51 @echo " make android : to prepare the sources in android/ for Studio"
52 @echo " make apk : to compile the APK file"
53 @echo " make ifman : to make the manual pages (if pandoc is found)"
54 @echo " make man : to make the manual pages (requires pandoc)"
56 .PHONY: all clean mrproper mrpropre build run jrun jar sjar resources test-resources install libs love apk android ifman man
63 sjar: $(NAME)-sources.jar
66 @echo Compiling program...
68 @$(JAVAC) $(JAVAC_FLAGS) "src/$(MAIN).java"
69 @[ "$(MORE)" = "" ] || for sup in $(MORE); do \
71 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
75 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
76 @[ -e bin/$(MAIN).class ]
77 @echo Compiling test program...
78 @[ "$(TEST)" != "" ] || echo No test sources defined.
79 @[ "$(TEST)" = "" ] || for sup in $(TEST); do \
81 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
86 rm -rf android/.gradle android/build android/app/build android/app/build.gradle
87 [ ! -L android/app/src/main/java ] || rm -rf android/app/src
88 @echo Removing sources taken from libs...
89 @for lib in libs/*-sources.jar libs/*-sources.patch.jar; do \
90 if [ "$$lib" != 'libs/*-sources.jar' -a "$$lib" != 'libs/*-sources.patch.jar' ]; then \
92 jar tf "$$lib" | while read -r ln; do \
93 [ -f "src/$$ln" ] && rm "src/$$ln"; \
95 jar tf "$$lib" | tac | while read -r ln; do \
96 [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \
105 rm -f $(NAME)-sources.jar
107 rm -f $(NAME)-debug.apk
108 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar"
109 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar"
110 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.apk"
111 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-debug.apk"
117 @echo Copying resources into bin/...
118 @cd src && find . | grep -v '\.java$$' | grep -v '/test/' | while read -r ln; do \
119 if [ -f "$$ln" ]; then \
120 dir="`dirname "$$ln"`"; \
121 mkdir -p "../bin/$$dir" ; \
122 cp "$$ln" "../bin/$$ln" ; \
127 test-resources: resources
128 @echo Copying test resources into bin/...
129 @cd src && find . | grep -v '\.java$$' | grep '/test/' | while read -r ln; do \
130 if [ -f "$$ln" ]; then \
131 dir="`dirname "$$ln"`"; \
132 mkdir -p "../bin/$$dir" ; \
133 cp "$$ln" "../bin/$$ln" ; \
138 @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs...
139 @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \
140 if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \
145 @[ ! -d libs ] || touch bin/libs
147 $(NAME)-sources.jar: libs
148 @echo Making sources JAR file...
150 @[ "$(SJAR_FLAGS)" = "" ] || echo Creating $(NAME)-sources.jar...
151 @[ "$(SJAR_FLAGS)" = "" ] || $(JAR) cfm $(NAME)-sources.jar bin/manifest $(SJAR_FLAGS)
152 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-sources.jar"...
153 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || cp $(NAME)-sources.jar "$(NAME)-`cat VERSION`-sources.jar"
155 $(NAME).jar: resources
156 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
157 @[ -e bin/$(MAIN).class ]
158 @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest
159 @echo >> bin/manifest
160 $(JAR) cfm $(NAME).jar bin/manifest $(JAR_FLAGS)
161 @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"...
162 @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar"
165 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
166 @[ -e bin/$(MAIN).class ]
167 @echo Running "$(NAME)"...
168 $(JAVA) $(JAVA_FLAGS) $(MAIN)
171 @[ -e $(NAME).jar ] || echo You need to build the jar
173 @echo Running "$(NAME).jar"...
174 $(RJAR) $(RJAR_FLAGS) $(NAME).jar
177 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] || echo You need to build the test sources
178 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ]
179 @echo Running tests for "$(NAME)"...
180 @[ "$(TEST)" != "" ] || echo No test sources defined.
181 [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) )
184 @[ -e $(NAME).jar ] || echo You need to build the jar
186 mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin"
187 cp $(NAME).jar "$(PREFIX)/lib/"
188 echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)"
189 echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)"
190 chmod a+rx "$(PREFIX)/bin/$(NAME)"
191 if [ -e "man/man1/$(NAME).1" ]; then \
192 cp -r man/ "$(PREFIX)"/share/; \
195 android: android/app/src
198 @[ -d android ] || echo No android/ directory found
200 @[ -e android/local.properties ] || echo 'You need to create android/local.properties and add "sdk.dir=PATH_TO_SDK"'
201 @[ -e android/local.properties ]
202 @mkdir -p android/app/src/main
203 @echo Linking sources...
205 cd android/app/src/main; \
206 ln -s ../../../../src/AndroidManifest.xml .; \
207 ln -s ../../../../res .; \
208 ln -s ../../../../src ./java; \
210 @echo Fixing configuration...
212 cd android/app/src/main/java; \
214 if [ "${RM_FOR_ANDROID}" != "" ]; then \
215 echo Ignoring uneeded sources...; \
216 for file in ${RM_FOR_ANDROID}; do \
217 excl="$${excl}exclude '**/$${file}'\\n";\
221 cat build.gradle.base \
222 | sed 's:\(applicationId "\)":\1${ID_FOR_ANDROID}":' \
223 | sed "s:\s*exclude '':$$excl:g" \
227 apk: libs ${NAME}.apk
228 @echo Building APK files...
230 ${NAME}.apk: ${NAME}-debug.apk
232 ${NAME}-debug.apk: android
233 @echo Starting gradlew assemble...
236 bash gradlew assemble && ( \
238 cp android/app/build/outputs/apk/release/app-release-unsigned.apk ${NAME}.apk; \
239 cp android/app/build/outputs/apk/debug/app-debug.apk ${NAME}-debug.apk; \
240 [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.apk"...; \
241 [ ! -e VERSION ] || cp $(NAME).apk "$(NAME)-`cat VERSION`.apk"; \
242 [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-debug.apk"...; \
243 [ ! -e VERSION ] || cp $(NAME).apk "$(NAME)-`cat VERSION`-debug.apk"; \
248 @if pandoc -v >/dev/null 2>&1; then \
251 echo "man pages not generated: "'`'"pandoc' required"; \
255 @echo Checking for possible manual pages...
256 @if [ -e README.md ]; then \
257 echo Sources found for man pages; \
258 if pandoc -v >/dev/null 2>&1; then \
259 ls README*.md 2>/dev/null \
260 | grep 'README\(-..\|\)\.md' \
261 | while read man; do \
262 echo " Processing page $$lang..."; \
263 lang="`echo "$$man" \
264 | sed 's:README\.md:en:' \
265 | sed 's:README-\(.*\)\.md:\1:'`"; \
266 mkdir -p man/"$$lang"/man1; \
268 echo ".TH \"${NAME}\" 1 `\
270 ` \"version `cat VERSION`\""; \
272 UNAME="`echo "${NAME}" \
273 | sed 's:\(.*\):\U\1:g'`"; \
275 cat "$$man" | head -n1 \
276 | sed 's:.*(README\(-fr\|\)\.md).*::g'; \
277 cat "$$man" | tail -n+2; \
278 ) | sed 's:^#\(#.*\):\1:g' \
279 | sed 's:^\(#.*\):\U\1:g;s:# *'"$$UNAME"':# NAME\n'"${NAME}"' \\- :g' \
281 | pandoc -f markdown -t man | sed 's:——:--:g' ; \
282 ) > man/"$$lang"/man1/"${NAME}.1"; \
284 mkdir -p "man/man1"; \
285 cp man/en/man1/"${NAME}".1 man/man1/; \
287 echo "man pages generation: pandoc required" >&2; \