Merge branch 'subtree'
[nikiroo-utils.git] / Makefile.base
... / ...
CommitLineData
1# Makefile base template
2#
3# Version:
4# - 1.0.0: add a version comment
5# - 1.1.0: add 'help', 'sjar'
6# - 1.2.0: add 'apk'
7# - 1.2.1: improve 'apk' and add 'android'
8# - 1.3.0: add 'man' for man(ual) pages
9# - 1.4.0: remove android stuff (not working anyway)
10# - 1.5.0: include sources and readme/changelog in jar
11# - 1.5.1: include binaries from libs/bin/ into the jar
12
13# Required parameters (the commented out ones are supposed to be per project):
14
15#MAIN = path to main java source to compile
16#MORE = path to supplementary needed resources not linked from MAIN
17#NAME = name of project (used for jar output file)
18#PREFIX = usually /usr/local (where to install the program)
19#TEST = path to main test source to compile
20#JAR_FLAGS += a list of things to pack, each usually prefixed with "-C bin/"
21#SJAR_FLAGS += a list of things to pack, each usually prefixed with "-C src/",
22# for *-sources.jar files
23#TEST_PARAMS = any parameter to pass to the test runnable when "test-run"
24
25JAVAC = javac
26JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/
27JAVA = java
28JAVA_FLAGS += -cp ./bin/
29JAR = jar
30RJAR = java
31RJAR_FLAGS += -jar
32
33all: build jar man
34
35help:
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 ifman : to make the manual pages (if pandoc is found)"
52 @echo " make man : to make the manual pages (requires pandoc)"
53
54.PHONY: all clean mrproper mrpropre build run jrun jar sjar resources test-resources install libs ifman man love
55
56bin:
57 @mkdir -p bin
58
59jar: $(NAME).jar
60
61sjar: $(NAME)-sources.jar
62
63build: resources
64 @echo Compiling program...
65 @echo " src/$(MAIN)"
66 @$(JAVAC) $(JAVAC_FLAGS) "src/$(MAIN).java"
67 @[ "$(MORE)" = "" ] || for sup in $(MORE); do \
68 echo " src/$$sup" ;\
69 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
70 done
71
72test: test-resources
73 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
74 @[ -e bin/$(MAIN).class ]
75 @echo Compiling test program...
76 @[ "$(TEST)" != "" ] || echo No test sources defined.
77 @[ "$(TEST)" = "" ] || for sup in $(TEST); do \
78 echo " src/$$sup" ;\
79 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
80 done
81
82clean:
83 rm -rf bin/
84 @echo Removing sources taken from libs...
85 @for lib in libs/*-sources.jar libs/*-sources.patch.jar; do \
86 if [ "$$lib" != 'libs/*-sources.jar' -a "$$lib" != 'libs/*-sources.patch.jar' ]; then \
87 basename "$$lib"; \
88 jar tf "$$lib" | while read -r ln; do \
89 [ -f "src/$$ln" ] && rm "src/$$ln"; \
90 done; \
91 jar tf "$$lib" | tac | while read -r ln; do \
92 [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \
93 done; \
94 fi \
95 done
96
97mrproper: mrpropre
98
99mrpropre: clean
100 rm -f $(NAME).jar
101 rm -f $(NAME)-sources.jar
102 rm -f $(NAME).apk
103 rm -f $(NAME)-debug.apk
104 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar"
105 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar"
106
107love:
108 @echo " ...not war."
109
110resources: libs
111 @echo Copying resources into bin/...
112 @cd src && find . | grep -v '\.java$$' | grep -v '/test/' | while read -r ln; do \
113 if [ -f "$$ln" ]; then \
114 dir="`dirname "$$ln"`"; \
115 mkdir -p "../bin/$$dir" ; \
116 cp "$$ln" "../bin/$$ln" ; \
117 fi ; \
118 done
119 @cp VERSION bin/
120
121test-resources: resources
122 @echo Copying test resources into bin/...
123 @cd src && find . | grep -v '\.java$$' | grep '/test/' | while read -r ln; do \
124 if [ -f "$$ln" ]; then \
125 dir="`dirname "$$ln"`"; \
126 mkdir -p "../bin/$$dir" ; \
127 cp "$$ln" "../bin/$$ln" ; \
128 fi ; \
129 done
130
131libs: bin
132 @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs...
133 @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \
134 if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \
135 basename "$$lib"; \
136 jar xf "$$lib"; \
137 fi \
138 done )
139 @[ ! -d libs ] || touch bin/libs
140
141$(NAME)-sources.jar: libs
142 @ls *.md >/dev/null || cp VERSION README.md
143 @echo Making sources JAR file...
144 @echo > bin/manifest
145 @[ "$(SJAR_FLAGS)" != "" ] || echo No sources JAR file defined, skipping
146 @[ "$(SJAR_FLAGS)" = "" ] || echo Creating $(NAME)-sources.jar...
147 @[ "$(SJAR_FLAGS)" = "" ] || $(JAR) cfm $(NAME)-sources.jar bin/manifest -C ./ *.md $(SJAR_FLAGS)
148 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-sources.jar"...
149 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || cp $(NAME)-sources.jar "$(NAME)-`cat VERSION`-sources.jar"
150
151$(NAME).jar: resources
152 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
153 @[ -e bin/$(MAIN).class ]
154 @ls *.md >/dev/null || cp VERSION README.md
155 @echo "Copying documentation into bin/..."
156 @cp -r *.md bin/ || cp VERSION bin/no-documentation.md
157 @[ ! -d libs/bin/ ] || echo "Copying additional binaries from libs/bin/ into bin/..."
158 @[ ! -d libs/bin/ ] || cp -r libs/bin/* bin/
159 @echo "Copying sources into bin/..."
160 @cp -r src/* bin/
161 @echo "Making jar..."
162 @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest
163 @echo >> bin/manifest
164 $(JAR) cfm $(NAME).jar bin/manifest -C ./ *.md $(JAR_FLAGS)
165 @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"...
166 @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar"
167
168run:
169 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
170 @[ -e bin/$(MAIN).class ]
171 @echo Running "$(NAME)"...
172 $(JAVA) $(JAVA_FLAGS) $(MAIN)
173
174jrun:
175 @[ -e $(NAME).jar ] || echo You need to build the jar
176 @[ -e $(NAME).jar ]
177 @echo Running "$(NAME).jar"...
178 $(RJAR) $(RJAR_FLAGS) $(NAME).jar
179
180run-test:
181 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] || echo You need to build the test sources
182 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ]
183 @echo Running tests for "$(NAME)"...
184 @[ "$(TEST)" != "" ] || echo No test sources defined.
185 [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) )
186
187install:
188 @[ -e $(NAME).jar ] || echo You need to build the jar
189 @[ -e $(NAME).jar ]
190 mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin"
191 cp $(NAME).jar "$(PREFIX)/lib/"
192 echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)"
193 echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)"
194 chmod a+rx "$(PREFIX)/bin/$(NAME)"
195 if [ -e "man/man1/$(NAME).1" ]; then \
196 cp -r man/ "$(PREFIX)"/share/; \
197 fi
198
199ifman:
200 @if pandoc -v >/dev/null 2>&1; then \
201 make man; \
202 else \
203 echo "man pages not generated: "'`'"pandoc' required"; \
204 fi
205
206man:
207 @echo Checking for possible manual pages...
208 @if [ -e README.md ]; then \
209 echo Sources found for man pages; \
210 if pandoc -v >/dev/null 2>&1; then \
211 ls README*.md 2>/dev/null \
212 | grep 'README\(-..\|\)\.md' \
213 | while read man; do \
214 echo " Processing page $$lang..."; \
215 lang="`echo "$$man" \
216 | sed 's:README\.md:en:' \
217 | sed 's:README-\(.*\)\.md:\1:'`"; \
218 mkdir -p man/"$$lang"/man1; \
219 ( \
220 echo ".TH \"${NAME}\" 1 `\
221 date +%Y-%m-%d\
222 ` \"version `cat VERSION`\""; \
223 echo; \
224 UNAME="`echo "${NAME}" \
225 | sed 's:\(.*\):\U\1:g'`"; \
226 ( \
227 cat "$$man" | head -n1 \
228 | sed 's:.*(README\(-fr\|\)\.md).*::g'; \
229 cat "$$man" | tail -n+2; \
230 ) | sed 's:^#\(#.*\):\1:g' \
231 | sed 's:^\(#.*\):\U\1:g;s:# *'"$$UNAME"':# NAME\n'"${NAME}"' \\- :g' \
232 | sed 's:--:——:g' \
233 | pandoc -f markdown -t man | sed 's:——:--:g' ; \
234 ) > man/"$$lang"/man1/"${NAME}.1"; \
235 done; \
236 mkdir -p "man/man1"; \
237 cp man/en/man1/"${NAME}".1 man/man1/; \
238 else \
239 echo "man pages generation: pandoc required" >&2; \
240 false; \
241 fi; \
242 fi;
243