remove uneeded iml file
[fanfix.git] / Makefile.base
CommitLineData
ae78e517
NR
1# Makefile base template
2#
3# Version:
4# - 1.0.0: add a version comment
5# - 1.1.0: add help, sjar
9b05b757 6# - 1.2.0: add apk
ae78e517 7
08fe2e33
NR
8# Required parameters (the commented out ones are supposed to change per project):
9
10#MAIN = path to main java source to compile
11#MORE = path to supplementary needed resources not linked from MAIN
12#NAME = name of project (used for jar output file)
13#PREFIX = usually /usr/local (where to install the program)
14#TEST = path to main test source to compile
15#JAR_FLAGS += a list of things to pack, each usually prefixed with "-C bin/"
dd56a893 16#SJAR_FLAGS += a list of things to pack, each usually prefixed with "-C src/", for *-sources.jar files
a6395bef 17#TEST_PARAMS = any parameter to pass to the test runnable when "test-run"
9b05b757
NR
18#ID_FOR_ANDROID = id of activity to launch for Android
19#RM_FOR_ANDROID = paths to remove for APK generation
08fe2e33
NR
20
21JAVAC = javac
614574c4 22JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/
08fe2e33
NR
23JAVA = java
24JAVA_FLAGS += -cp ./bin/
25JAR = jar
26RJAR = java
27RJAR_FLAGS += -jar
28
08fe2e33
NR
29all: build jar
30
ae78e517
NR
31help:
32 @echo "Usual options:"
33 @echo "=============="
34 @echo " make : to build the jar file"
35 @echo " make help : to get this help screen"
36 @echo " make libs : to update the libraries into src/"
37 @echo " make build : to update the binaries (not the jar)"
38 @echo " make test : to update the test binaries"
39 @echo " make build jar : to update the binaries and jar file"
40 @echo " make sjar : to create the sources jar file"
41 @echo " make clean : to clean the directory of intermediate files"
42 @echo " make mrpropre : to clean the directory of all outputs"
43 @echo " make run : to run the program from the binaries"
44 @echo " make run-test : to run the test program from the binaries"
45 @echo " make jrun : to run the program from the jar file"
9b05b757
NR
46 @echo " make install : to install the application into $$PREFIX"
47 @echo " make apk : to compile the APK file"
ae78e517
NR
48
49.PHONY: all clean mrproper mrpropre build run jrun jar sjar resources test-resources install libs love
08fe2e33
NR
50
51bin:
52 @mkdir -p bin
53
54jar: $(NAME).jar
55
ae78e517
NR
56sjar: $(NAME)-sources.jar
57
08fe2e33
NR
58build: resources
59 @echo Compiling program...
60 @echo " src/$(MAIN)"
61 @$(JAVAC) $(JAVAC_FLAGS) "src/$(MAIN).java"
62 @[ "$(MORE)" = "" ] || for sup in $(MORE); do \
63 echo " src/$$sup" ;\
64 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
65 done
66
a6395bef 67test: test-resources
08fe2e33
NR
68 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
69 @[ -e bin/$(MAIN).class ]
70 @echo Compiling test program...
71 @[ "$(TEST)" != "" ] || echo No test sources defined.
72 @[ "$(TEST)" = "" ] || for sup in $(TEST); do \
73 echo " src/$$sup" ;\
74 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
75 done
76
77clean:
78 rm -rf bin/
9b05b757 79 rm -rf android/.gradle android/build android/app/build android/app/src android/app/build.gradle
08fe2e33 80 @echo Removing sources taken from libs...
3871aa4e
NR
81 @for lib in libs/*-sources.jar libs/*-sources.patch.jar; do \
82 if [ "$$lib" != 'libs/*-sources.jar' -a "$$lib" != 'libs/*-sources.patch.jar' ]; then \
83 basename "$$lib"; \
84 jar tf "$$lib" | while read -r ln; do \
85 [ -f "src/$$ln" ] && rm "src/$$ln"; \
86 done; \
87 jar tf "$$lib" | tac | while read -r ln; do \
88 [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \
89 done; \
90 fi \
08fe2e33
NR
91 done
92
93mrproper: mrpropre
94
95mrpropre: clean
96 rm -f $(NAME).jar
dd56a893 97 rm -f $(NAME)-sources.jar
9b05b757
NR
98 rm -f $(NAME).apk
99 rm -f $(NAME)-debug.apk
dd56a893
NR
100 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar"
101 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar"
9b05b757
NR
102 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.apk"
103 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-debug.apk"
08fe2e33
NR
104
105love:
106 @echo " ...not war."
107
108resources: libs
109 @echo Copying resources into bin/...
a6395bef
NR
110 @cd src && find . | grep -v '\.java$$' | grep -v '/test/' | while read -r ln; do \
111 if [ -f "$$ln" ]; then \
112 dir="`dirname "$$ln"`"; \
113 mkdir -p "../bin/$$dir" ; \
114 cp "$$ln" "../bin/$$ln" ; \
115 fi ; \
116 done
39c3c689 117 @cp VERSION bin/
a6395bef
NR
118
119test-resources: resources
120 @echo Copying test resources into bin/...
121 @cd src && find . | grep -v '\.java$$' | grep '/test/' | while read -r ln; do \
08fe2e33
NR
122 if [ -f "$$ln" ]; then \
123 dir="`dirname "$$ln"`"; \
124 mkdir -p "../bin/$$dir" ; \
125 cp "$$ln" "../bin/$$ln" ; \
126 fi ; \
127 done
128
129libs: bin
130 @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs...
c1873e56 131 @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \
9119671d
NR
132 if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \
133 basename "$$lib"; \
134 jar xf "$$lib"; \
135 fi \
08fe2e33
NR
136 done )
137 @[ ! -d libs ] || touch bin/libs
138
ae78e517
NR
139$(NAME)-sources.jar: libs
140 @echo Making sources JAR file...
dd56a893
NR
141 @echo > bin/manifest
142 @[ "$(SJAR_FLAGS)" = "" ] || echo Creating $(NAME)-sources.jar...
143 @[ "$(SJAR_FLAGS)" = "" ] || $(JAR) cfm $(NAME)-sources.jar bin/manifest $(SJAR_FLAGS)
144 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-sources.jar"...
145 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || cp $(NAME)-sources.jar "$(NAME)-`cat VERSION`-sources.jar"
ae78e517
NR
146
147$(NAME).jar: resources
148 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
149 @[ -e bin/$(MAIN).class ]
08fe2e33
NR
150 @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest
151 @echo >> bin/manifest
152 $(JAR) cfm $(NAME).jar bin/manifest $(JAR_FLAGS)
dd56a893 153 @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"...
d3c15421 154 @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar"
08fe2e33
NR
155
156run:
157 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
158 @[ -e bin/$(MAIN).class ]
159 @echo Running "$(NAME)"...
160 $(JAVA) $(JAVA_FLAGS) $(MAIN)
161
162jrun:
163 @[ -e $(NAME).jar ] || echo You need to build the jar
164 @[ -e $(NAME).jar ]
165 @echo Running "$(NAME).jar"...
166 $(RJAR) $(RJAR_FLAGS) $(NAME).jar
167
168run-test:
169 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] || echo You need to build the test sources
170 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ]
171 @echo Running tests for "$(NAME)"...
172 @[ "$(TEST)" != "" ] || echo No test sources defined.
a6395bef 173 [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) )
08fe2e33
NR
174
175install:
176 @[ -e $(NAME).jar ] || echo You need to build the jar
177 @[ -e $(NAME).jar ]
178 mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin"
179 cp $(NAME).jar "$(PREFIX)/lib/"
180 echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)"
fe999aa4 181 echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)"
08fe2e33
NR
182 chmod a+rx "$(PREFIX)/bin/$(NAME)"
183
9b05b757
NR
184apk: libs ${NAME}.apk
185 @echo Building APK files...
186
187${NAME}.apk: ${NAME}-debug.apk
188
189${NAME}-debug.apk:
190 @[ -d android ] || echo No android/ directory found
191 @[ -d android ]
192 @[ -e android/local.properties ] || echo 'You need to create android/local.properties and add "sdk.dir=PATH_TO_SDK"'
193 @[ -e android/local.properties ]
194 @mkdir -p android/app/src/main/java
195 cp src/AndroidManifest.xml android/app/src/main
196 cp -r res android/app/src/main
197 cp -r src/* android/app/src/main/java/
198 @echo Starting gradlew assemble...
199 @( \
200 cd android/app; \
201 cat build.gradle.base | sed 's:applicationId "":applicationId "${ID_FOR_ANDROID}":' > build.gradle; \
202 cd src/main/java; \
203 if [ "${RM_FOR_ANDROID}" != "" ]; then \
204 echo Removing uneeded sources...; \
205 rm -rf ${RM_FOR_ANDROID}; \
206 fi; \
207 cd ../../../../; \
208 bash gradlew assemble && ( \
209 cd ..; \
210 cp android/app/build/outputs/apk/release/app-release-unsigned.apk ${NAME}.apk; \
211 cp android/app/build/outputs/apk/debug/app-debug.apk ${NAME}-debug.apk; \
212 [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.apk"...; \
213 [ ! -e VERSION ] || cp $(NAME).apk "$(NAME)-`cat VERSION`.apk"; \
214 [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-debug.apk"...; \
215 [ ! -e VERSION ] || cp $(NAME).apk "$(NAME)-`cat VERSION`-debug.apk"; \
216 ); \
217 )
218