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