Fix small bug in Downloader
[nikiroo-utils.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
7 # Required parameters (the commented out ones are supposed to change per project):
8
9 #MAIN = path to main java source to compile
10 #MORE = path to supplementary needed resources not linked from MAIN
11 #NAME = name of project (used for jar output file)
12 #PREFIX = usually /usr/local (where to install the program)
13 #TEST = path to main test source to compile
14 #JAR_FLAGS += a list of things to pack, each usually prefixed with "-C bin/"
15 #SJAR_FLAGS += a list of things to pack, each usually prefixed with "-C src/", for *-sources.jar files
16 #TEST_PARAMS = any parameter to pass to the test runnable when "test-run"
17
18 JAVAC = javac
19 JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/
20 JAVA = java
21 JAVA_FLAGS += -cp ./bin/
22 JAR = jar
23 RJAR = java
24 RJAR_FLAGS += -jar
25
26 all: build jar
27
28 help:
29 @echo "Usual options:"
30 @echo "=============="
31 @echo " make : to build the jar file"
32 @echo " make help : to get this help screen"
33 @echo " make libs : to update the libraries into src/"
34 @echo " make build : to update the binaries (not the jar)"
35 @echo " make test : to update the test binaries"
36 @echo " make build jar : to update the binaries and jar file"
37 @echo " make sjar : to create the sources jar file"
38 @echo " make clean : to clean the directory of intermediate files"
39 @echo " make mrpropre : to clean the directory of all outputs"
40 @echo " make run : to run the program from the binaries"
41 @echo " make run-test : to run the test program from the binaries"
42 @echo " make jrun : to run the program from the jar file"
43 @echo " make install : to install the application into $PREFIX"
44
45 .PHONY: all clean mrproper mrpropre build run jrun jar sjar resources test-resources install libs love
46
47 bin:
48 @mkdir -p bin
49
50 jar: $(NAME).jar
51
52 sjar: $(NAME)-sources.jar
53
54 build: resources
55 @echo Compiling program...
56 @echo " src/$(MAIN)"
57 @$(JAVAC) $(JAVAC_FLAGS) "src/$(MAIN).java"
58 @[ "$(MORE)" = "" ] || for sup in $(MORE); do \
59 echo " src/$$sup" ;\
60 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
61 done
62
63 test: test-resources
64 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
65 @[ -e bin/$(MAIN).class ]
66 @echo Compiling test program...
67 @[ "$(TEST)" != "" ] || echo No test sources defined.
68 @[ "$(TEST)" = "" ] || for sup in $(TEST); do \
69 echo " src/$$sup" ;\
70 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
71 done
72
73 clean:
74 rm -rf bin/
75 @echo Removing sources taken from libs...
76 @for lib in libs/*-sources.jar libs/*-sources.patch.jar; do \
77 if [ "$$lib" != 'libs/*-sources.jar' -a "$$lib" != 'libs/*-sources.patch.jar' ]; then \
78 basename "$$lib"; \
79 jar tf "$$lib" | while read -r ln; do \
80 [ -f "src/$$ln" ] && rm "src/$$ln"; \
81 done; \
82 jar tf "$$lib" | tac | while read -r ln; do \
83 [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \
84 done; \
85 fi \
86 done
87
88 mrproper: mrpropre
89
90 mrpropre: clean
91 rm -f $(NAME).jar
92 rm -f $(NAME)-sources.jar
93 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar"
94 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar"
95
96 love:
97 @echo " ...not war."
98
99 resources: libs
100 @echo Copying resources into bin/...
101 @cd src && find . | grep -v '\.java$$' | grep -v '/test/' | while read -r ln; do \
102 if [ -f "$$ln" ]; then \
103 dir="`dirname "$$ln"`"; \
104 mkdir -p "../bin/$$dir" ; \
105 cp "$$ln" "../bin/$$ln" ; \
106 fi ; \
107 done
108 @cp VERSION bin/
109
110 test-resources: resources
111 @echo Copying test resources into bin/...
112 @cd src && find . | grep -v '\.java$$' | grep '/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
120 libs: bin
121 @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs...
122 @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \
123 if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \
124 basename "$$lib"; \
125 jar xf "$$lib"; \
126 fi \
127 done )
128 @[ ! -d libs ] || touch bin/libs
129
130 $(NAME)-sources.jar: libs
131 @echo Making sources JAR file...
132 @echo > bin/manifest
133 @[ "$(SJAR_FLAGS)" = "" ] || echo Creating $(NAME)-sources.jar...
134 @[ "$(SJAR_FLAGS)" = "" ] || $(JAR) cfm $(NAME)-sources.jar bin/manifest $(SJAR_FLAGS)
135 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-sources.jar"...
136 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || cp $(NAME)-sources.jar "$(NAME)-`cat VERSION`-sources.jar"
137
138 $(NAME).jar: resources
139 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
140 @[ -e bin/$(MAIN).class ]
141 @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest
142 @echo >> bin/manifest
143 $(JAR) cfm $(NAME).jar bin/manifest $(JAR_FLAGS)
144 @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"...
145 @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar"
146
147 run:
148 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
149 @[ -e bin/$(MAIN).class ]
150 @echo Running "$(NAME)"...
151 $(JAVA) $(JAVA_FLAGS) $(MAIN)
152
153 jrun:
154 @[ -e $(NAME).jar ] || echo You need to build the jar
155 @[ -e $(NAME).jar ]
156 @echo Running "$(NAME).jar"...
157 $(RJAR) $(RJAR_FLAGS) $(NAME).jar
158
159 run-test:
160 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] || echo You need to build the test sources
161 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ]
162 @echo Running tests for "$(NAME)"...
163 @[ "$(TEST)" != "" ] || echo No test sources defined.
164 [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) )
165
166 install:
167 @[ -e $(NAME).jar ] || echo You need to build the jar
168 @[ -e $(NAME).jar ]
169 mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin"
170 cp $(NAME).jar "$(PREFIX)/lib/"
171 echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)"
172 echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)"
173 chmod a+rx "$(PREFIX)/bin/$(NAME)"
174