1 # Makefile base template
4 # - 1.0.0: add a version comment
5 # - 1.1.0: add help, sjar
8 # Required parameters (the commented out ones are supposed to change per project):
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/"
16 #SJAR_FLAGS += a list of things to pack, each usually prefixed with "-C src/", for *-sources.jar files
17 #TEST_PARAMS = any parameter to pass to the test runnable when "test-run"
18 #ID_FOR_ANDROID = id of activity to launch for Android
19 #RM_FOR_ANDROID = paths to remove for APK generation
22 JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/
24 JAVA_FLAGS += -cp ./bin/
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"
46 @echo " make install : to install the application into $$PREFIX"
47 @echo " make apk : to compile the APK file"
49 .PHONY: all clean mrproper mrpropre build run jrun jar sjar resources test-resources install libs love
56 sjar: $(NAME)-sources.jar
59 @echo Compiling program...
61 @$(JAVAC) $(JAVAC_FLAGS) "src/$(MAIN).java"
62 @[ "$(MORE)" = "" ] || for sup in $(MORE); do \
64 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
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 \
74 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
79 rm -rf android/.gradle android/build android/app/build android/app/src android/app/build.gradle
80 @echo Removing sources taken from libs...
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 \
84 jar tf "$$lib" | while read -r ln; do \
85 [ -f "src/$$ln" ] && rm "src/$$ln"; \
87 jar tf "$$lib" | tac | while read -r ln; do \
88 [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \
97 rm -f $(NAME)-sources.jar
99 rm -f $(NAME)-debug.apk
100 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar"
101 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar"
102 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.apk"
103 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-debug.apk"
109 @echo Copying resources into bin/...
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" ; \
119 test-resources: resources
120 @echo Copying test resources into bin/...
121 @cd src && find . | grep -v '\.java$$' | grep '/test/' | while read -r ln; do \
122 if [ -f "$$ln" ]; then \
123 dir="`dirname "$$ln"`"; \
124 mkdir -p "../bin/$$dir" ; \
125 cp "$$ln" "../bin/$$ln" ; \
130 @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs...
131 @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \
132 if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \
137 @[ ! -d libs ] || touch bin/libs
139 $(NAME)-sources.jar: libs
140 @echo Making sources JAR file...
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"
147 $(NAME).jar: resources
148 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
149 @[ -e bin/$(MAIN).class ]
150 @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest
151 @echo >> bin/manifest
152 $(JAR) cfm $(NAME).jar bin/manifest $(JAR_FLAGS)
153 @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"...
154 @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar"
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)
163 @[ -e $(NAME).jar ] || echo You need to build the jar
165 @echo Running "$(NAME).jar"...
166 $(RJAR) $(RJAR_FLAGS) $(NAME).jar
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.
173 [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) )
176 @[ -e $(NAME).jar ] || echo You need to build the jar
178 mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin"
179 cp $(NAME).jar "$(PREFIX)/lib/"
180 echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)"
181 echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)"
182 chmod a+rx "$(PREFIX)/bin/$(NAME)"
184 apk: libs ${NAME}.apk
185 @echo Building APK files...
187 ${NAME}.apk: ${NAME}-debug.apk
190 @[ -d android ] || echo No android/ directory found
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...
201 cat build.gradle.base | sed 's:applicationId "":applicationId "${ID_FOR_ANDROID}":' > build.gradle; \
203 if [ "${RM_FOR_ANDROID}" != "" ]; then \
204 echo Removing uneeded sources...; \
205 rm -rf ${RM_FOR_ANDROID}; \
208 bash gradlew assemble && ( \
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"; \