makefile
[nikiroo-utils.git] / Makefile
1 #
2 # Java Makefile:
3 # > NAME: name of project (used for jar output file)
4 # > MAIN: path to main java source to compile
5 # > MORE: path to supplementary needed resources not linked from MAIN
6 # > TEST: path to main test source to compile
7 # > JAR_FLAGS: a list of things to pack, each usually prefixed with "-C bin/"
8 # > SJAR_FLAGS: like JAR_FLAGS, but for *-sources.jar
9 # > PREFIX: usually /usr/local (where to install the program)
10 #
11 NAME = nikiroo-utils
12 MAIN = be/nikiroo/tests/utils/Test
13 TEST = be/nikiroo/tests/utils/Test
14
15 # Option for this program: UI=android (or UI=awt by default)
16 ifeq ($(UI),android)
17 MORE += be/nikiroo/utils/android/test/TestAndroid
18 TEST += be/nikiroo/utils/android/ImageUtilsAndroid
19 else
20 MORE += be/nikiroo/utils/ui/ImageUtilsAwt
21 MORE += be/nikiroo/utils/ui/ImageTextAwt
22 TEST += be/nikiroo/utils/ui/test/TestUI
23 endif
24
25
26 JAR_MISC = -C ./ LICENSE -C ./ VERSION -C libs/ licenses
27 JAR_FLAGS += -C bin/ be -C bin/ org $(JAR_MISC)
28 SJAR_FLAGS += -C src/ org -C src/ be $(JAR_MISC)
29
30 PREFIX = /usr/local
31
32 # Makefile base template
33 #
34 # Version:
35 # - 1.0.0: add a version comment
36 # - 1.1.0: add 'help', 'sjar'
37 # - 1.2.0: add 'apk'
38 # - 1.2.1: improve 'apk' and add 'android'
39 # - 1.3.0: add 'man' for man(ual) pages
40 # - 1.4.0: remove android stuff (not working anyway)
41 # - 1.5.0: include sources and readme/changelog in jar
42 # - 1.5.1: include binaries from libs/bin/ into the jar
43 # - 1.6.0: rework the system without need of a ./configure.sh, add uninstall
44
45
46 JAVAC = javac
47 JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/
48 JAVA = java
49 JAVA_FLAGS += -cp ./bin/
50 JAR = jar
51 RJAR = java
52 RJAR_FLAGS += -jar
53
54 all: build jar man
55
56 help:
57 @echo "Usual options:"
58 @echo "=============="
59 @echo " make : to build the jar file and man pages IF possible"
60 @echo " make help : to get this help screen"
61 @echo " make libs : to update the libraries into src/"
62 @echo " make build : to update the binaries (not the jar)"
63 @echo " make test : to update the test binaries"
64 @echo " make build jar : to update the binaries and jar file"
65 @echo " make sjar : to create the sources jar file"
66 @echo " make clean : to clean the directory of intermediate files"
67 @echo " make mrpropre : to clean the directory of all outputs"
68 @echo " make run : to run the program from the binaries"
69 @echo " make run-test : to run the test program from the binaries"
70 @echo " make jrun : to run the program from the jar file"
71 @echo " make install : to install the application into $$PREFIX"
72 @echo " make uninstall : to uninstall the application from $$PREFIX"
73 @echo " make man : to make the manual pages (requires pandoc)"
74
75 .PHONY: all clean mrproper mrpropre build run jrun jar sjar resources \
76 test-resources install libs man love
77
78 bin:
79 @mkdir -p bin
80
81 jar: $(NAME).jar
82
83 sjar: $(NAME)-sources.jar
84
85 build: resources
86 @echo Compiling program...
87 @echo " src/$(MAIN)"
88 @$(JAVAC) $(JAVAC_FLAGS) "src/$(MAIN).java"
89 @[ "$(MORE)" = "" ] || for sup in $(MORE); do \
90 echo " src/$$sup" ;\
91 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
92 done
93
94 test: test-resources
95 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
96 @[ -e bin/$(MAIN).class ]
97 @echo Compiling test program...
98 @[ "$(TEST)" != "" ] || echo No test sources defined.
99 @[ "$(TEST)" = "" ] || for sup in $(TEST); do \
100 echo " src/$$sup" ;\
101 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
102 done
103
104 clean:
105 rm -rf bin/
106 @echo Removing sources taken from libs...
107 @for lib in libs/*-sources.jar libs/*-sources.patch.jar; do \
108 if [ "$$lib" != 'libs/*-sources.jar' \
109 -a "$$lib" != 'libs/*-sources.patch.jar' ]; \
110 then \
111 basename "$$lib"; \
112 jar tf "$$lib" | while read -r ln; do \
113 [ -f "src/$$ln" ] && rm "src/$$ln"; \
114 done; \
115 jar tf "$$lib" | tac | while read -r ln; do \
116 if [ -d "src/$$ln" ]; then \
117 rmdir "src/$$ln" 2>/dev/null || true; \
118 fi; \
119 done; \
120 fi \
121 done
122
123 mrproper: mrpropre
124 mrpropre: clean man
125 rm -f $(NAME).jar
126 rm -f $(NAME)-sources.jar
127 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar"
128 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar"
129
130 love:
131 @echo " ...not war."
132
133 resources: libs
134 @echo Copying resources and documentation into bin/...
135 @if ! cp *.md bin/ 2>/dev/null; then \
136 if [ -e VERSION ]; then \
137 cp VERSION bin/no-documentation.md; \
138 else \
139 echo > bin/no-documentation.md; \
140 fi; \
141 fi
142 @cd src && find . | grep -v '\.java$$' \
143 | grep -v '/test/' | while read -r ln; do \
144 if [ -f "$$ln" ]; then \
145 dir="`dirname "$$ln"`"; \
146 mkdir -p "../bin/$$dir" ; \
147 cp "$$ln" "../bin/$$ln" ; \
148 fi ; \
149 done
150 @[ ! -e VERSION ] || cp VERSION bin/
151
152 test-resources: resources
153 @echo Copying test resources into bin/...
154 @cd src && find . | grep -v '\.java$$' \
155 | grep '/test/' | while read -r ln; do \
156 if [ -f "$$ln" ]; then \
157 dir="`dirname "$$ln"`"; \
158 mkdir -p "../bin/$$dir" ; \
159 cp "$$ln" "../bin/$$ln" ; \
160 fi ; \
161 done
162
163 libs: bin
164 @if [ ! -e bin/libs -a -d libs ]; then \
165 echo Extracting sources from libs...; \
166 cd src; \
167 for lib in ../libs/*-sources.jar \
168 ../libs/*-sources.patch.jar; do \
169 if [ "$$lib" != '../libs/*-sources.jar' \
170 -a "$$lib" != '../libs/*-sources.patch.jar' ]; \
171 then \
172 basename "$$lib"; \
173 jar xf "$$lib"; \
174 fi; \
175 done; \
176 fi;
177 @[ ! -d libs ] || touch bin/libs
178
179 $(NAME)-sources.jar: libs
180 @echo Making sources JAR file...
181 @echo > bin/manifest
182 @if [ "$(SJAR_FLAGS)" = "" ]; then \
183 echo No sources JAR file defined, skipping; \
184 else \
185 echo Creating $(NAME)-sources.jar...; \
186 $(JAR) cfm $(NAME)-sources.jar \
187 bin/manifest -C ./ *.md $(SJAR_FLAGS); \
188 if [ -e VERSION ]; then \
189 echo Copying to "$(NAME)-`cat VERSION`-sources.jar"...;\
190 cp $(NAME)-sources.jar \
191 "$(NAME)-`cat VERSION`-sources.jar"; \
192 fi; \
193 fi;
194
195 $(NAME).jar: resources
196 @if [ -d libs/bin/ ]; then \
197 echo "Copying additional binaries from libs/bin/ into bin/...";\
198 cp -r libs/bin/* bin/; \
199 fi;
200 @echo "Copying sources into bin/..."
201 @cp -r src/* bin/
202 @echo "Making jar..."
203 @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest
204 @echo >> bin/manifest
205 $(JAR) cfm $(NAME).jar bin/manifest -C ./ *.md $(JAR_FLAGS)
206 @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"...
207 @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar"
208
209 run:
210 @echo Running "$(NAME)"...
211 $(JAVA) $(JAVA_FLAGS) $(MAIN)
212
213 jrun:
214 @echo Running "$(NAME).jar"...
215 $(RJAR) $(RJAR_FLAGS) $(NAME).jar
216
217 run-test:
218 @echo Running tests for "$(NAME)"...
219 @[ "$(TEST)" != "" ] || echo No test sources defined.
220 @if [ "`whereis tput`" = "tput:" ]; then \
221 ok='"[ ok ]"'; \
222 ko='"[ !! ]"'; \
223 cols=80; \
224 else \
225 ok="`tput bold`[`tput setf 2` OK `tput init``tput bold`]`tput init`"; \
226 ko="`tput bold`[`tput setf 4` !! `tput init``tput bold`]`tput init`"; \
227 cols="`tput cols`"; \
228 fi; \
229 [ "$(TEST)" = "" ] || ( \
230 clear; \
231 for test in $(TEST); do \
232 $(JAVA) $(JAVA_FLAGS) "$$test" "$$cols" "$$ok" "$$ko"; \
233 done; \
234 );
235
236
237 install: man
238 @[ -e $(NAME).jar ] || echo You need to build the jar
239 @[ -e $(NAME).jar ]
240 mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin"
241 cp $(NAME).jar "$(PREFIX)/lib/"
242 ( \
243 echo "#!/bin/sh"; \
244 echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" \
245 ) > "$(PREFIX)/bin/$(NAME)"
246 chmod a+rx "$(PREFIX)/bin/$(NAME)"
247
248 uninstall: man
249 rm "$(PREFIX)/bin/$(NAME)"
250 rm "$(PREFIX)/lib/$(NAME).jar"
251 rmdir "$(PREFIX)/bin" 2>/dev/null
252
253 man:
254 @$(MAKE) -f man.d $(MAKECMDGOALS) NAME=$(NAME)
255