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