Commit | Line | Data |
---|---|---|
ae78e517 NR |
1 | # Makefile base template |
2 | # | |
3 | # Version: | |
4 | # - 1.0.0: add a version comment | |
53282dec NR |
5 | # - 1.1.0: add 'help', 'sjar' |
6 | # - 1.2.0: add 'apk' | |
7 | # - 1.2.1: improve 'apk' and add 'android' | |
ae78e517 | 8 | |
53282dec | 9 | # Required parameters (the commented out ones are supposed to be per project): |
08fe2e33 NR |
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/" | |
53282dec NR |
17 | #SJAR_FLAGS += a list of things to pack, each usually prefixed with "-C src/", |
18 | # for *-sources.jar files | |
a6395bef | 19 | #TEST_PARAMS = any parameter to pass to the test runnable when "test-run" |
9b05b757 | 20 | #ID_FOR_ANDROID = id of activity to launch for Android |
53282dec NR |
21 | #RM_FOR_ANDROID = packages (if it ends with /) or classes to ignore for APK |
22 | # generation | |
08fe2e33 NR |
23 | |
24 | JAVAC = javac | |
614574c4 | 25 | JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/ |
08fe2e33 NR |
26 | JAVA = java |
27 | JAVA_FLAGS += -cp ./bin/ | |
28 | JAR = jar | |
29 | RJAR = java | |
30 | RJAR_FLAGS += -jar | |
31 | ||
08fe2e33 NR |
32 | all: build jar |
33 | ||
ae78e517 NR |
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" | |
9b05b757 | 49 | @echo " make install : to install the application into $$PREFIX" |
53282dec | 50 | @echo " make android : to prepare the sources in android/ for Studio" |
9b05b757 | 51 | @echo " make apk : to compile the APK file" |
ae78e517 | 52 | |
53282dec | 53 | .PHONY: all clean mrproper mrpropre build run jrun jar sjar resources test-resources install libs love apk android |
08fe2e33 NR |
54 | |
55 | bin: | |
56 | @mkdir -p bin | |
57 | ||
58 | jar: $(NAME).jar | |
59 | ||
ae78e517 NR |
60 | sjar: $(NAME)-sources.jar |
61 | ||
08fe2e33 NR |
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 | ||
a6395bef | 71 | test: test-resources |
08fe2e33 NR |
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/ | |
53282dec NR |
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 | |
08fe2e33 | 85 | @echo Removing sources taken from libs... |
3871aa4e NR |
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 \ | |
08fe2e33 NR |
96 | done |
97 | ||
98 | mrproper: mrpropre | |
99 | ||
100 | mrpropre: clean | |
101 | rm -f $(NAME).jar | |
dd56a893 | 102 | rm -f $(NAME)-sources.jar |
9b05b757 NR |
103 | rm -f $(NAME).apk |
104 | rm -f $(NAME)-debug.apk | |
dd56a893 NR |
105 | [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar" |
106 | [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar" | |
9b05b757 NR |
107 | [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.apk" |
108 | [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-debug.apk" | |
08fe2e33 NR |
109 | |
110 | love: | |
111 | @echo " ...not war." | |
112 | ||
113 | resources: libs | |
114 | @echo Copying resources into bin/... | |
a6395bef NR |
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 | |
39c3c689 | 122 | @cp VERSION bin/ |
a6395bef NR |
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 \ | |
08fe2e33 NR |
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... | |
c1873e56 | 136 | @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \ |
9119671d NR |
137 | if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \ |
138 | basename "$$lib"; \ | |
139 | jar xf "$$lib"; \ | |
140 | fi \ | |
08fe2e33 NR |
141 | done ) |
142 | @[ ! -d libs ] || touch bin/libs | |
143 | ||
ae78e517 NR |
144 | $(NAME)-sources.jar: libs |
145 | @echo Making sources JAR file... | |
dd56a893 NR |
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" | |
ae78e517 NR |
151 | |
152 | $(NAME).jar: resources | |
153 | @[ -e bin/$(MAIN).class ] || echo You need to build the sources | |
154 | @[ -e bin/$(MAIN).class ] | |
08fe2e33 NR |
155 | @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest |
156 | @echo >> bin/manifest | |
157 | $(JAR) cfm $(NAME).jar bin/manifest $(JAR_FLAGS) | |
dd56a893 | 158 | @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"... |
d3c15421 | 159 | @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar" |
08fe2e33 NR |
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. | |
a6395bef | 178 | [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) ) |
08fe2e33 NR |
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)" | |
fe999aa4 | 186 | echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)" |
08fe2e33 NR |
187 | chmod a+rx "$(PREFIX)/bin/$(NAME)" |
188 | ||
53282dec | 189 | android: android/app/src |
9b05b757 | 190 | |
53282dec | 191 | android/app/src: |
9b05b757 NR |
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 ] | |
53282dec NR |
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 | ||
205 | apk: libs ${NAME}.apk | |
206 | @echo Building APK files... | |
207 | ||
208 | ${NAME}.apk: ${NAME}-debug.apk | |
209 | ||
210 | ${NAME}-debug.apk: android | |
9b05b757 NR |
211 | @echo Starting gradlew assemble... |
212 | @( \ | |
53282dec NR |
213 | cd android/app/src/main/java; \ |
214 | excl="\\n";\ | |
9b05b757 | 215 | if [ "${RM_FOR_ANDROID}" != "" ]; then \ |
53282dec NR |
216 | echo Ignoring uneeded sources...; \ |
217 | for file in ${RM_FOR_ANDROID}; do \ | |
218 | excl="$${excl}exclude '**/$${file}'\\n";\ | |
219 | done; \ | |
9b05b757 | 220 | fi; \ |
53282dec NR |
221 | cd ../../../ ; \ |
222 | cat build.gradle.base \ | |
223 | | sed 's:\(applicationId "\)":\1${ID_FOR_ANDROID}":' \ | |
224 | | sed "s:\s*exclude '':$$excl:g" \ | |
225 | > build.gradle; \ | |
226 | cd ../ ; \ | |
9b05b757 NR |
227 | bash gradlew assemble && ( \ |
228 | cd ..; \ | |
229 | cp android/app/build/outputs/apk/release/app-release-unsigned.apk ${NAME}.apk; \ | |
230 | cp android/app/build/outputs/apk/debug/app-debug.apk ${NAME}-debug.apk; \ | |
231 | [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.apk"...; \ | |
232 | [ ! -e VERSION ] || cp $(NAME).apk "$(NAME)-`cat VERSION`.apk"; \ | |
233 | [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-debug.apk"...; \ | |
234 | [ ! -e VERSION ] || cp $(NAME).apk "$(NAME)-`cat VERSION`-debug.apk"; \ | |
235 | ); \ | |
236 | ) | |
237 |