| 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 | |
| 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/" |
| 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 |
| 20 | |
| 21 | JAVAC = javac |
| 22 | JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/ |
| 23 | JAVA = java |
| 24 | JAVA_FLAGS += -cp ./bin/ |
| 25 | JAR = jar |
| 26 | RJAR = java |
| 27 | RJAR_FLAGS += -jar |
| 28 | |
| 29 | all: build jar |
| 30 | |
| 31 | help: |
| 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" |
| 48 | |
| 49 | .PHONY: all clean mrproper mrpropre build run jrun jar sjar resources test-resources install libs love |
| 50 | |
| 51 | bin: |
| 52 | @mkdir -p bin |
| 53 | |
| 54 | jar: $(NAME).jar |
| 55 | |
| 56 | sjar: $(NAME)-sources.jar |
| 57 | |
| 58 | build: 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 | |
| 67 | test: test-resources |
| 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 | |
| 77 | clean: |
| 78 | rm -rf bin/ |
| 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 \ |
| 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 \ |
| 91 | done |
| 92 | |
| 93 | mrproper: mrpropre |
| 94 | |
| 95 | mrpropre: clean |
| 96 | rm -f $(NAME).jar |
| 97 | rm -f $(NAME)-sources.jar |
| 98 | rm -f $(NAME).apk |
| 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" |
| 104 | |
| 105 | love: |
| 106 | @echo " ...not war." |
| 107 | |
| 108 | resources: libs |
| 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" ; \ |
| 115 | fi ; \ |
| 116 | done |
| 117 | @cp VERSION bin/ |
| 118 | |
| 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" ; \ |
| 126 | fi ; \ |
| 127 | done |
| 128 | |
| 129 | libs: bin |
| 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 \ |
| 133 | basename "$$lib"; \ |
| 134 | jar xf "$$lib"; \ |
| 135 | fi \ |
| 136 | done ) |
| 137 | @[ ! -d libs ] || touch bin/libs |
| 138 | |
| 139 | $(NAME)-sources.jar: libs |
| 140 | @echo Making sources JAR file... |
| 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" |
| 146 | |
| 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" |
| 155 | |
| 156 | run: |
| 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 | |
| 162 | jrun: |
| 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 | |
| 168 | run-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. |
| 173 | [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) ) |
| 174 | |
| 175 | install: |
| 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)" |
| 181 | echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)" |
| 182 | chmod a+rx "$(PREFIX)/bin/$(NAME)" |
| 183 | |
| 184 | apk: 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 | |