| 1 | # Makefile base template |
| 2 | # |
| 3 | # Version: |
| 4 | # - 1.0.0: add a version comment |
| 5 | # - 1.1.0: add help, sjar |
| 6 | # - 1.1.1: fix "jar" instead of ${JAR} |
| 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 | |
| 19 | JAVAC = javac |
| 20 | JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/ |
| 21 | JAVA = java |
| 22 | JAVA_FLAGS += -cp ./bin/ |
| 23 | JAR = jar |
| 24 | RJAR = java |
| 25 | RJAR_FLAGS += -jar |
| 26 | |
| 27 | all: build jar |
| 28 | |
| 29 | help: |
| 30 | @echo "Usual options:" |
| 31 | @echo "==============" |
| 32 | @echo " make : to build the jar file" |
| 33 | @echo " make help : to get this help screen" |
| 34 | @echo " make libs : to update the libraries into src/" |
| 35 | @echo " make build : to update the binaries (not the jar)" |
| 36 | @echo " make test : to update the test binaries" |
| 37 | @echo " make build jar : to update the binaries and jar file" |
| 38 | @echo " make sjar : to create the sources jar file" |
| 39 | @echo " make clean : to clean the directory of intermediate files" |
| 40 | @echo " make mrpropre : to clean the directory of all outputs" |
| 41 | @echo " make run : to run the program from the binaries" |
| 42 | @echo " make run-test : to run the test program from the binaries" |
| 43 | @echo " make jrun : to run the program from the jar file" |
| 44 | @echo " make install : to install the application into $PREFIX" |
| 45 | |
| 46 | .PHONY: all clean mrproper mrpropre build run jrun jar sjar resources test-resources install libs love |
| 47 | |
| 48 | bin: |
| 49 | @mkdir -p bin |
| 50 | |
| 51 | jar: $(NAME).jar |
| 52 | |
| 53 | sjar: $(NAME)-sources.jar |
| 54 | |
| 55 | build: resources |
| 56 | @echo Compiling program... |
| 57 | @echo " src/$(MAIN)" |
| 58 | @$(JAVAC) $(JAVAC_FLAGS) "src/$(MAIN).java" |
| 59 | @[ "$(MORE)" = "" ] || for sup in $(MORE); do \ |
| 60 | echo " src/$$sup" ;\ |
| 61 | $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \ |
| 62 | done |
| 63 | |
| 64 | test: test-resources |
| 65 | @[ -e bin/$(MAIN).class ] || echo You need to build the sources |
| 66 | @[ -e bin/$(MAIN).class ] |
| 67 | @echo Compiling test program... |
| 68 | @[ "$(TEST)" != "" ] || echo No test sources defined. |
| 69 | @[ "$(TEST)" = "" ] || for sup in $(TEST); do \ |
| 70 | echo " src/$$sup" ;\ |
| 71 | $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \ |
| 72 | done |
| 73 | |
| 74 | clean: |
| 75 | rm -rf bin/ |
| 76 | @echo Removing sources taken from libs... |
| 77 | @for lib in libs/*-sources.jar libs/*-sources.patch.jar; do \ |
| 78 | if [ "$$lib" != 'libs/*-sources.jar' -a "$$lib" != 'libs/*-sources.patch.jar' ]; then \ |
| 79 | basename "$$lib"; \ |
| 80 | ${JAR} tf "$$lib" | while read -r ln; do \ |
| 81 | [ -f "src/$$ln" ] && rm "src/$$ln"; \ |
| 82 | done; \ |
| 83 | ${JAR} tf "$$lib" | tac | while read -r ln; do \ |
| 84 | [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \ |
| 85 | done; \ |
| 86 | fi \ |
| 87 | done |
| 88 | |
| 89 | mrproper: mrpropre |
| 90 | |
| 91 | mrpropre: clean |
| 92 | rm -f $(NAME).jar |
| 93 | rm -f $(NAME)-sources.jar |
| 94 | [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar" |
| 95 | [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar" |
| 96 | |
| 97 | love: |
| 98 | @echo " ...not war." |
| 99 | |
| 100 | resources: libs |
| 101 | @echo Copying resources into bin/... |
| 102 | @cd src && find . | grep -v '\.java$$' | grep -v '/test/' | while read -r ln; do \ |
| 103 | if [ -f "$$ln" ]; then \ |
| 104 | dir="`dirname "$$ln"`"; \ |
| 105 | mkdir -p "../bin/$$dir" ; \ |
| 106 | cp "$$ln" "../bin/$$ln" ; \ |
| 107 | fi ; \ |
| 108 | done |
| 109 | @cp VERSION bin/ |
| 110 | |
| 111 | test-resources: resources |
| 112 | @echo Copying test resources into bin/... |
| 113 | @cd src && find . | grep -v '\.java$$' | grep '/test/' | while read -r ln; do \ |
| 114 | if [ -f "$$ln" ]; then \ |
| 115 | dir="`dirname "$$ln"`"; \ |
| 116 | mkdir -p "../bin/$$dir" ; \ |
| 117 | cp "$$ln" "../bin/$$ln" ; \ |
| 118 | fi ; \ |
| 119 | done |
| 120 | |
| 121 | libs: bin |
| 122 | @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs... |
| 123 | @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \ |
| 124 | if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \ |
| 125 | basename "$$lib"; \ |
| 126 | ${JAR} xf "$$lib"; \ |
| 127 | fi \ |
| 128 | done ) |
| 129 | @[ ! -d libs ] || touch bin/libs |
| 130 | |
| 131 | $(NAME)-sources.jar: libs |
| 132 | @echo Making sources JAR file... |
| 133 | @echo > bin/manifest |
| 134 | @[ "$(SJAR_FLAGS)" = "" ] || echo Creating $(NAME)-sources.jar... |
| 135 | @[ "$(SJAR_FLAGS)" = "" ] || $(JAR) cfm $(NAME)-sources.jar bin/manifest $(SJAR_FLAGS) |
| 136 | @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-sources.jar"... |
| 137 | @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || cp $(NAME)-sources.jar "$(NAME)-`cat VERSION`-sources.jar" |
| 138 | |
| 139 | $(NAME).jar: resources |
| 140 | @[ -e bin/$(MAIN).class ] || echo You need to build the sources |
| 141 | @[ -e bin/$(MAIN).class ] |
| 142 | @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest |
| 143 | @echo >> bin/manifest |
| 144 | $(JAR) cfm $(NAME).jar bin/manifest $(JAR_FLAGS) |
| 145 | @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"... |
| 146 | @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar" |
| 147 | |
| 148 | run: |
| 149 | @[ -e bin/$(MAIN).class ] || echo You need to build the sources |
| 150 | @[ -e bin/$(MAIN).class ] |
| 151 | @echo Running "$(NAME)"... |
| 152 | $(JAVA) $(JAVA_FLAGS) $(MAIN) |
| 153 | |
| 154 | jrun: |
| 155 | @[ -e $(NAME).jar ] || echo You need to build the jar |
| 156 | @[ -e $(NAME).jar ] |
| 157 | @echo Running "$(NAME).jar"... |
| 158 | $(RJAR) $(RJAR_FLAGS) $(NAME).jar |
| 159 | |
| 160 | run-test: |
| 161 | @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] || echo You need to build the test sources |
| 162 | @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] |
| 163 | @echo Running tests for "$(NAME)"... |
| 164 | @[ "$(TEST)" != "" ] || echo No test sources defined. |
| 165 | [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) ) |
| 166 | |
| 167 | install: |
| 168 | @[ -e $(NAME).jar ] || echo You need to build the jar |
| 169 | @[ -e $(NAME).jar ] |
| 170 | mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin" |
| 171 | cp $(NAME).jar "$(PREFIX)/lib/" |
| 172 | echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)" |
| 173 | echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)" |
| 174 | chmod a+rx "$(PREFIX)/bin/$(NAME)" |
| 175 | |