651b10c1fbfaea632874bb80fb1801465857e734
[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
9 # Required parameters (the commented out ones are supposed to be per project):
10
11 #MAIN = path to main java source to compile
12 #MORE = path to supplementary needed resources not linked from MAIN
13 #NAME = name of project (used for jar output file)
14 #PREFIX = usually /usr/local (where to install the program)
15 #TEST = path to main test source to compile
16 #JAR_FLAGS += a list of things to pack, each usually prefixed with "-C bin/"
17 #SJAR_FLAGS += a list of things to pack, each usually prefixed with "-C src/",
18 # for *-sources.jar files
19 #TEST_PARAMS = any parameter to pass to the test runnable when "test-run"
20 #ID_FOR_ANDROID = id of activity to launch for Android
21 #RM_FOR_ANDROID = packages (if it ends with /) or classes to ignore for APK
22 # generation
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
33
34 help:
35 @echo "Usual options:"
36 @echo "=============="
37 @echo " make : to build the jar file"
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 android : to prepare the sources in android/ for Studio"
51 @echo " make apk : to compile the APK file"
52
53 .PHONY: all clean mrproper mrpropre build run jrun jar sjar resources test-resources install libs love apk android
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 rm -rf android/.gradle android/build android/app/build android/app/build.gradle
84 [ ! -L android/app/src/main/java ] || rm -rf android/app/src
85 @echo Removing sources taken from libs...
86 @for lib in libs/*-sources.jar libs/*-sources.patch.jar; do \
87 if [ "$$lib" != 'libs/*-sources.jar' -a "$$lib" != 'libs/*-sources.patch.jar' ]; then \
88 basename "$$lib"; \
89 jar tf "$$lib" | while read -r ln; do \
90 [ -f "src/$$ln" ] && rm "src/$$ln"; \
91 done; \
92 jar tf "$$lib" | tac | while read -r ln; do \
93 [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \
94 done; \
95 fi \
96 done
97
98 mrproper: mrpropre
99
100 mrpropre: clean
101 rm -f $(NAME).jar
102 rm -f $(NAME)-sources.jar
103 rm -f $(NAME).apk
104 rm -f $(NAME)-debug.apk
105 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar"
106 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar"
107 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.apk"
108 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-debug.apk"
109
110 love:
111 @echo " ...not war."
112
113 resources: libs
114 @echo Copying resources into bin/...
115 @cd src && find . | grep -v '\.java$$' | grep -v '/test/' | while read -r ln; do \
116 if [ -f "$$ln" ]; then \
117 dir="`dirname "$$ln"`"; \
118 mkdir -p "../bin/$$dir" ; \
119 cp "$$ln" "../bin/$$ln" ; \
120 fi ; \
121 done
122 @cp VERSION bin/
123
124 test-resources: resources
125 @echo Copying test resources into bin/...
126 @cd src && find . | grep -v '\.java$$' | grep '/test/' | while read -r ln; do \
127 if [ -f "$$ln" ]; then \
128 dir="`dirname "$$ln"`"; \
129 mkdir -p "../bin/$$dir" ; \
130 cp "$$ln" "../bin/$$ln" ; \
131 fi ; \
132 done
133
134 libs: bin
135 @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs...
136 @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \
137 if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \
138 basename "$$lib"; \
139 jar xf "$$lib"; \
140 fi \
141 done )
142 @[ ! -d libs ] || touch bin/libs
143
144 $(NAME)-sources.jar: libs
145 @echo Making sources JAR file...
146 @echo > bin/manifest
147 @[ "$(SJAR_FLAGS)" = "" ] || echo Creating $(NAME)-sources.jar...
148 @[ "$(SJAR_FLAGS)" = "" ] || $(JAR) cfm $(NAME)-sources.jar bin/manifest $(SJAR_FLAGS)
149 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-sources.jar"...
150 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || cp $(NAME)-sources.jar "$(NAME)-`cat VERSION`-sources.jar"
151
152 $(NAME).jar: resources
153 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
154 @[ -e bin/$(MAIN).class ]
155 @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest
156 @echo >> bin/manifest
157 $(JAR) cfm $(NAME).jar bin/manifest $(JAR_FLAGS)
158 @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"...
159 @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar"
160
161 run:
162 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
163 @[ -e bin/$(MAIN).class ]
164 @echo Running "$(NAME)"...
165 $(JAVA) $(JAVA_FLAGS) $(MAIN)
166
167 jrun:
168 @[ -e $(NAME).jar ] || echo You need to build the jar
169 @[ -e $(NAME).jar ]
170 @echo Running "$(NAME).jar"...
171 $(RJAR) $(RJAR_FLAGS) $(NAME).jar
172
173 run-test:
174 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] || echo You need to build the test sources
175 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ]
176 @echo Running tests for "$(NAME)"...
177 @[ "$(TEST)" != "" ] || echo No test sources defined.
178 [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) )
179
180 install:
181 @[ -e $(NAME).jar ] || echo You need to build the jar
182 @[ -e $(NAME).jar ]
183 mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin"
184 cp $(NAME).jar "$(PREFIX)/lib/"
185 echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)"
186 echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)"
187 chmod a+rx "$(PREFIX)/bin/$(NAME)"
188
189 android: android/app/src
190
191 android/app/src:
192 @[ -d android ] || echo No android/ directory found
193 @[ -d android ]
194 @[ -e android/local.properties ] || echo 'You need to create android/local.properties and add "sdk.dir=PATH_TO_SDK"'
195 @[ -e android/local.properties ]
196 @mkdir -p android/app/src/main
197 @echo Linking sources...
198 @( \
199 cd android/app/src/main; \
200 ln -s ../../../../src/AndroidManifest.xml .; \
201 ln -s ../../../../res .; \
202 ln -s ../../../../src ./java; \
203 )
204 @echo Fixing configuration...
205 @( \
206 cd android/app/src/main/java; \
207 excl="\\n";\
208 if [ "${RM_FOR_ANDROID}" != "" ]; then \
209 echo Ignoring uneeded sources...; \
210 for file in ${RM_FOR_ANDROID}; do \
211 excl="$${excl}exclude '**/$${file}'\\n";\
212 done; \
213 fi; \
214 cd ../../../ ; \
215 cat build.gradle.base \
216 | sed 's:\(applicationId "\)":\1${ID_FOR_ANDROID}":' \
217 | sed "s:\s*exclude '':$$excl:g" \
218 > build.gradle; \
219 )
220
221 apk: libs ${NAME}.apk
222 @echo Building APK files...
223
224 ${NAME}.apk: ${NAME}-debug.apk
225
226 ${NAME}-debug.apk: android
227 @echo Starting gradlew assemble...
228 @( \
229 cd android/; \
230 bash gradlew assemble && ( \
231 cd ..; \
232 cp android/app/build/outputs/apk/release/app-release-unsigned.apk ${NAME}.apk; \
233 cp android/app/build/outputs/apk/debug/app-debug.apk ${NAME}-debug.apk; \
234 [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.apk"...; \
235 [ ! -e VERSION ] || cp $(NAME).apk "$(NAME)-`cat VERSION`.apk"; \
236 [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-debug.apk"...; \
237 [ ! -e VERSION ] || cp $(NAME).apk "$(NAME)-`cat VERSION`-debug.apk"; \
238 ); \
239 )
240