update Makefile.base: copy sources/doc in jar
[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 # - 1.5.0: include sources and readme/changelog in jar
11
12 # Required parameters (the commented out ones are supposed to be per project):
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/"
20 #SJAR_FLAGS += a list of things to pack, each usually prefixed with "-C src/",
21 # for *-sources.jar files
22 #TEST_PARAMS = any parameter to pass to the test runnable when "test-run"
23
24 JAVAC = javac
25 JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/
26 JAVA = java
27 JAVA_FLAGS += -cp ./bin/
28 JAR = jar
29 RJAR = java
30 RJAR_FLAGS += -jar
31
32 all: build jar man
33
34 help:
35 @echo "Usual options:"
36 @echo "=============="
37 @echo " make : to build the jar file and man pages IF possible"
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"
49 @echo " make install : to install the application into $$PREFIX"
50 @echo " make ifman : to make the manual pages (if pandoc is found)"
51 @echo " make man : to make the manual pages (requires pandoc)"
52
53 .PHONY: all clean mrproper mrpropre build run jrun jar sjar resources test-resources install libs ifman man love
54
55 bin:
56 @mkdir -p bin
57
58 jar: $(NAME).jar
59
60 sjar: $(NAME)-sources.jar
61
62 build: 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
71 test: test-resources
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
81 clean:
82 rm -rf bin/
83 @echo Removing sources taken from libs...
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 \
94 done
95
96 mrproper: mrpropre
97
98 mrpropre: clean
99 rm -f $(NAME).jar
100 rm -f $(NAME)-sources.jar
101 rm -f $(NAME).apk
102 rm -f $(NAME)-debug.apk
103 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar"
104 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar"
105
106 love:
107 @echo " ...not war."
108
109 resources: libs
110 @echo Copying resources into bin/...
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
118 @cp VERSION bin/
119
120 test-resources: resources
121 @echo Copying test resources into bin/...
122 @cd src && find . | grep -v '\.java$$' | grep '/test/' | while read -r ln; do \
123 if [ -f "$$ln" ]; then \
124 dir="`dirname "$$ln"`"; \
125 mkdir -p "../bin/$$dir" ; \
126 cp "$$ln" "../bin/$$ln" ; \
127 fi ; \
128 done
129
130 libs: bin
131 @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs...
132 @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \
133 if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \
134 basename "$$lib"; \
135 jar xf "$$lib"; \
136 fi \
137 done )
138 @[ ! -d libs ] || touch bin/libs
139
140 $(NAME)-sources.jar: libs
141 @ls *.md >/dev/null || cp VERSION README.md
142 @echo Making sources JAR file...
143 @echo > bin/manifest
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"
149
150 $(NAME).jar: resources
151 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
152 @[ -e bin/$(MAIN).class ]
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..."
159 @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest
160 @echo >> bin/manifest
161 $(JAR) cfm $(NAME).jar bin/manifest -C ./ *.md $(JAR_FLAGS)
162 @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"...
163 @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar"
164
165 run:
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
171 jrun:
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
177 run-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.
182 [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) )
183
184 install:
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)"
190 echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)"
191 chmod a+rx "$(PREFIX)/bin/$(NAME)"
192 if [ -e "man/man1/$(NAME).1" ]; then \
193 cp -r man/ "$(PREFIX)"/share/; \
194 fi
195
196 ifman:
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
203 man:
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' \
229 | sed 's:--:——:g' \
230 | pandoc -f markdown -t man | sed 's:——:--:g' ; \
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