| 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 | # - 1.3.0: add 'man' for man(ual) pages |
| 9 | |
| 10 | # Required parameters (the commented out ones are supposed to be per project): |
| 11 | |
| 12 | #MAIN = path to main java source to compile |
| 13 | #MORE = path to supplementary needed resources not linked from MAIN |
| 14 | #NAME = name of project (used for jar output file) |
| 15 | #PREFIX = usually /usr/local (where to install the program) |
| 16 | #TEST = path to main test source to compile |
| 17 | #JAR_FLAGS += a list of things to pack, each usually prefixed with "-C bin/" |
| 18 | #SJAR_FLAGS += a list of things to pack, each usually prefixed with "-C src/", |
| 19 | # for *-sources.jar files |
| 20 | #TEST_PARAMS = any parameter to pass to the test runnable when "test-run" |
| 21 | #ID_FOR_ANDROID = id of activity to launch for Android |
| 22 | #RM_FOR_ANDROID = packages (if it ends with /) or classes to ignore for APK |
| 23 | # generation |
| 24 | |
| 25 | JAVAC = javac |
| 26 | JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/ |
| 27 | JAVA = java |
| 28 | JAVA_FLAGS += -cp ./bin/ |
| 29 | JAR = jar |
| 30 | RJAR = java |
| 31 | RJAR_FLAGS += -jar |
| 32 | |
| 33 | all: build jar man |
| 34 | |
| 35 | help: |
| 36 | @echo "Usual options:" |
| 37 | @echo "==============" |
| 38 | @echo " make : to build the jar file and man pages IF possible" |
| 39 | @echo " make help : to get this help screen" |
| 40 | @echo " make libs : to update the libraries into src/" |
| 41 | @echo " make build : to update the binaries (not the jar)" |
| 42 | @echo " make test : to update the test binaries" |
| 43 | @echo " make build jar : to update the binaries and jar file" |
| 44 | @echo " make sjar : to create the sources jar file" |
| 45 | @echo " make clean : to clean the directory of intermediate files" |
| 46 | @echo " make mrpropre : to clean the directory of all outputs" |
| 47 | @echo " make run : to run the program from the binaries" |
| 48 | @echo " make run-test : to run the test program from the binaries" |
| 49 | @echo " make jrun : to run the program from the jar file" |
| 50 | @echo " make install : to install the application into $$PREFIX" |
| 51 | @echo " make android : to prepare the sources in android/ for Studio" |
| 52 | @echo " make apk : to compile the APK file" |
| 53 | @echo " make ifman : to make the manual pages (if pandoc is found)" |
| 54 | @echo " make man : to make the manual pages (requires pandoc)" |
| 55 | |
| 56 | .PHONY: all clean mrproper mrpropre build run jrun jar sjar resources test-resources install libs love apk android ifman man |
| 57 | |
| 58 | bin: |
| 59 | @mkdir -p bin |
| 60 | |
| 61 | jar: $(NAME).jar |
| 62 | |
| 63 | sjar: $(NAME)-sources.jar |
| 64 | |
| 65 | build: resources |
| 66 | @echo Compiling program... |
| 67 | @echo " src/$(MAIN)" |
| 68 | @$(JAVAC) $(JAVAC_FLAGS) "src/$(MAIN).java" |
| 69 | @[ "$(MORE)" = "" ] || for sup in $(MORE); do \ |
| 70 | echo " src/$$sup" ;\ |
| 71 | $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \ |
| 72 | done |
| 73 | |
| 74 | test: test-resources |
| 75 | @[ -e bin/$(MAIN).class ] || echo You need to build the sources |
| 76 | @[ -e bin/$(MAIN).class ] |
| 77 | @echo Compiling test program... |
| 78 | @[ "$(TEST)" != "" ] || echo No test sources defined. |
| 79 | @[ "$(TEST)" = "" ] || for sup in $(TEST); do \ |
| 80 | echo " src/$$sup" ;\ |
| 81 | $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \ |
| 82 | done |
| 83 | |
| 84 | clean: |
| 85 | rm -rf bin/ |
| 86 | rm -rf android/.gradle android/build android/app/build android/app/build.gradle |
| 87 | [ ! -L android/app/src/main/java ] || rm -rf android/app/src |
| 88 | @echo Removing sources taken from libs... |
| 89 | @for lib in libs/*-sources.jar libs/*-sources.patch.jar; do \ |
| 90 | if [ "$$lib" != 'libs/*-sources.jar' -a "$$lib" != 'libs/*-sources.patch.jar' ]; then \ |
| 91 | basename "$$lib"; \ |
| 92 | jar tf "$$lib" | while read -r ln; do \ |
| 93 | [ -f "src/$$ln" ] && rm "src/$$ln"; \ |
| 94 | done; \ |
| 95 | jar tf "$$lib" | tac | while read -r ln; do \ |
| 96 | [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \ |
| 97 | done; \ |
| 98 | fi \ |
| 99 | done |
| 100 | |
| 101 | mrproper: mrpropre |
| 102 | |
| 103 | mrpropre: clean |
| 104 | rm -f $(NAME).jar |
| 105 | rm -f $(NAME)-sources.jar |
| 106 | rm -f $(NAME).apk |
| 107 | rm -f $(NAME)-debug.apk |
| 108 | [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar" |
| 109 | [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar" |
| 110 | [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.apk" |
| 111 | [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-debug.apk" |
| 112 | |
| 113 | love: |
| 114 | @echo " ...not war." |
| 115 | |
| 116 | resources: libs |
| 117 | @echo Copying resources into bin/... |
| 118 | @cd src && find . | grep -v '\.java$$' | grep -v '/test/' | while read -r ln; do \ |
| 119 | if [ -f "$$ln" ]; then \ |
| 120 | dir="`dirname "$$ln"`"; \ |
| 121 | mkdir -p "../bin/$$dir" ; \ |
| 122 | cp "$$ln" "../bin/$$ln" ; \ |
| 123 | fi ; \ |
| 124 | done |
| 125 | @cp VERSION bin/ |
| 126 | |
| 127 | test-resources: resources |
| 128 | @echo Copying test resources into bin/... |
| 129 | @cd src && find . | grep -v '\.java$$' | grep '/test/' | while read -r ln; do \ |
| 130 | if [ -f "$$ln" ]; then \ |
| 131 | dir="`dirname "$$ln"`"; \ |
| 132 | mkdir -p "../bin/$$dir" ; \ |
| 133 | cp "$$ln" "../bin/$$ln" ; \ |
| 134 | fi ; \ |
| 135 | done |
| 136 | |
| 137 | libs: bin |
| 138 | @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs... |
| 139 | @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \ |
| 140 | if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \ |
| 141 | basename "$$lib"; \ |
| 142 | jar xf "$$lib"; \ |
| 143 | fi \ |
| 144 | done ) |
| 145 | @[ ! -d libs ] || touch bin/libs |
| 146 | |
| 147 | $(NAME)-sources.jar: libs |
| 148 | @echo Making sources JAR file... |
| 149 | @echo > bin/manifest |
| 150 | @[ "$(SJAR_FLAGS)" = "" ] || echo Creating $(NAME)-sources.jar... |
| 151 | @[ "$(SJAR_FLAGS)" = "" ] || $(JAR) cfm $(NAME)-sources.jar bin/manifest $(SJAR_FLAGS) |
| 152 | @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-sources.jar"... |
| 153 | @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || cp $(NAME)-sources.jar "$(NAME)-`cat VERSION`-sources.jar" |
| 154 | |
| 155 | $(NAME).jar: resources |
| 156 | @[ -e bin/$(MAIN).class ] || echo You need to build the sources |
| 157 | @[ -e bin/$(MAIN).class ] |
| 158 | @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest |
| 159 | @echo >> bin/manifest |
| 160 | $(JAR) cfm $(NAME).jar bin/manifest $(JAR_FLAGS) |
| 161 | @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"... |
| 162 | @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar" |
| 163 | |
| 164 | run: |
| 165 | @[ -e bin/$(MAIN).class ] || echo You need to build the sources |
| 166 | @[ -e bin/$(MAIN).class ] |
| 167 | @echo Running "$(NAME)"... |
| 168 | $(JAVA) $(JAVA_FLAGS) $(MAIN) |
| 169 | |
| 170 | jrun: |
| 171 | @[ -e $(NAME).jar ] || echo You need to build the jar |
| 172 | @[ -e $(NAME).jar ] |
| 173 | @echo Running "$(NAME).jar"... |
| 174 | $(RJAR) $(RJAR_FLAGS) $(NAME).jar |
| 175 | |
| 176 | run-test: |
| 177 | @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] || echo You need to build the test sources |
| 178 | @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] |
| 179 | @echo Running tests for "$(NAME)"... |
| 180 | @[ "$(TEST)" != "" ] || echo No test sources defined. |
| 181 | [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) ) |
| 182 | |
| 183 | install: |
| 184 | @[ -e $(NAME).jar ] || echo You need to build the jar |
| 185 | @[ -e $(NAME).jar ] |
| 186 | mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin" |
| 187 | cp $(NAME).jar "$(PREFIX)/lib/" |
| 188 | echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)" |
| 189 | echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)" |
| 190 | chmod a+rx "$(PREFIX)/bin/$(NAME)" |
| 191 | if [ -e "man/man1/$(NAME).1" ]; then \ |
| 192 | cp -r man/ "$(PREFIX)"/share/; \ |
| 193 | fi |
| 194 | |
| 195 | android: android/app/src |
| 196 | |
| 197 | android/app/src: |
| 198 | @[ -d android ] || echo No android/ directory found |
| 199 | @[ -d android ] |
| 200 | @[ -e android/local.properties ] || echo 'You need to create android/local.properties and add "sdk.dir=PATH_TO_SDK"' |
| 201 | @[ -e android/local.properties ] |
| 202 | @mkdir -p android/app/src/main |
| 203 | @echo Linking sources... |
| 204 | @( \ |
| 205 | cd android/app/src/main; \ |
| 206 | ln -s ../../../../src/AndroidManifest.xml .; \ |
| 207 | ln -s ../../../../res .; \ |
| 208 | ln -s ../../../../src ./java; \ |
| 209 | ) |
| 210 | @echo Fixing configuration... |
| 211 | @( \ |
| 212 | cd android/app/src/main/java; \ |
| 213 | excl="\\n";\ |
| 214 | if [ "${RM_FOR_ANDROID}" != "" ]; then \ |
| 215 | echo Ignoring uneeded sources...; \ |
| 216 | for file in ${RM_FOR_ANDROID}; do \ |
| 217 | excl="$${excl}exclude '**/$${file}'\\n";\ |
| 218 | done; \ |
| 219 | fi; \ |
| 220 | cd ../../../ ; \ |
| 221 | cat build.gradle.base \ |
| 222 | | sed 's:\(applicationId "\)":\1${ID_FOR_ANDROID}":' \ |
| 223 | | sed "s:\s*exclude '':$$excl:g" \ |
| 224 | > build.gradle; \ |
| 225 | ) |
| 226 | |
| 227 | apk: libs ${NAME}.apk |
| 228 | @echo Building APK files... |
| 229 | |
| 230 | ${NAME}.apk: ${NAME}-debug.apk |
| 231 | |
| 232 | ${NAME}-debug.apk: android |
| 233 | @echo Starting gradlew assemble... |
| 234 | @( \ |
| 235 | cd android/; \ |
| 236 | bash gradlew assemble && ( \ |
| 237 | cd ..; \ |
| 238 | cp android/app/build/outputs/apk/release/app-release-unsigned.apk ${NAME}.apk; \ |
| 239 | cp android/app/build/outputs/apk/debug/app-debug.apk ${NAME}-debug.apk; \ |
| 240 | [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.apk"...; \ |
| 241 | [ ! -e VERSION ] || cp $(NAME).apk "$(NAME)-`cat VERSION`.apk"; \ |
| 242 | [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-debug.apk"...; \ |
| 243 | [ ! -e VERSION ] || cp $(NAME).apk "$(NAME)-`cat VERSION`-debug.apk"; \ |
| 244 | ); \ |
| 245 | ) |
| 246 | |
| 247 | ifman: |
| 248 | @if pandoc -v >/dev/null 2>&1; then \ |
| 249 | make man; \ |
| 250 | else \ |
| 251 | echo "man pages not generated: "'`'"pandoc' required"; \ |
| 252 | fi |
| 253 | |
| 254 | man: |
| 255 | @echo Checking for possible manual pages... |
| 256 | @if [ -e README.md ]; then \ |
| 257 | echo Sources found for man pages; \ |
| 258 | if pandoc -v >/dev/null 2>&1; then \ |
| 259 | ls README*.md 2>/dev/null \ |
| 260 | | grep 'README\(-..\|\)\.md' \ |
| 261 | | while read man; do \ |
| 262 | echo " Processing page $$lang..."; \ |
| 263 | lang="`echo "$$man" \ |
| 264 | | sed 's:README\.md:en:' \ |
| 265 | | sed 's:README-\(.*\)\.md:\1:'`"; \ |
| 266 | mkdir -p man/"$$lang"/man1; \ |
| 267 | ( \ |
| 268 | echo ".TH \"${NAME}\" 1 `\ |
| 269 | date +%Y-%m-%d\ |
| 270 | ` \"version `cat VERSION`\""; \ |
| 271 | echo; \ |
| 272 | UNAME="`echo "${NAME}" \ |
| 273 | | sed 's:\(.*\):\U\1:g'`"; \ |
| 274 | ( \ |
| 275 | cat "$$man" | head -n1 \ |
| 276 | | sed 's:.*(README\(-fr\|\)\.md).*::g'; \ |
| 277 | cat "$$man" | tail -n+2; \ |
| 278 | ) | sed 's:^#\(#.*\):\1:g' \ |
| 279 | | sed 's:^\(#.*\):\U\1:g;s:# *'"$$UNAME"':# NAME\n'"${NAME}"' \\- :g' \ |
| 280 | | sed 's:--:——:g' \ |
| 281 | | pandoc -f markdown -t man | sed 's:——:--:g' ; \ |
| 282 | ) > man/"$$lang"/man1/"${NAME}.1"; \ |
| 283 | done; \ |
| 284 | mkdir -p "man/man1"; \ |
| 285 | cp man/en/man1/"${NAME}".1 man/man1/; \ |
| 286 | else \ |
| 287 | echo "man pages generation: pandoc required" >&2; \ |
| 288 | false; \ |
| 289 | fi; \ |
| 290 | fi; |
| 291 | |