| 1 | # Required parameters (the commented out ones are supposed to change per project): |
| 2 | |
| 3 | #MAIN = path to main java source to compile |
| 4 | #MORE = path to supplementary needed resources not linked from MAIN |
| 5 | #NAME = name of project (used for jar output file) |
| 6 | #PREFIX = usually /usr/local (where to install the program) |
| 7 | #TEST = path to main test source to compile |
| 8 | #JAR_FLAGS += a list of things to pack, each usually prefixed with "-C bin/" |
| 9 | #SJAR_FLAGS += a list of things to pack, each usually prefixed with "-C src/", for *-sources.jar files |
| 10 | #TEST_PARAMS = any parameter to pass to the test runnable when "test-run" |
| 11 | |
| 12 | JAVAC = javac |
| 13 | JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/ |
| 14 | JAVA = java |
| 15 | JAVA_FLAGS += -cp ./bin/ |
| 16 | JAR = jar |
| 17 | RJAR = java |
| 18 | RJAR_FLAGS += -jar |
| 19 | |
| 20 | # Usual options: |
| 21 | # make : to build the jar file |
| 22 | # make libs : to update the libraries into src/ |
| 23 | # make build : to update the binaries (not the jar) |
| 24 | # make test : to update the test binaries |
| 25 | # make build jar : to update the binaries and jar file |
| 26 | # make clean : to clean the directory of intermediate files |
| 27 | # make mrpropre : to clean the directory of all outputs |
| 28 | # make run : to run the program from the binaries |
| 29 | # make run-test : to run the test program from the binaries |
| 30 | # make jrun : to run the program from the jar file |
| 31 | # make install : to install the application into $PREFIX |
| 32 | |
| 33 | # Note: build is actually slower than rebuild in most cases except when |
| 34 | # small changes only are detected ; so we use rebuild by default |
| 35 | |
| 36 | all: build jar |
| 37 | |
| 38 | .PHONY: all clean mrproper mrpropre build run jrun jar resources test-resources install libs love |
| 39 | |
| 40 | bin: |
| 41 | @mkdir -p bin |
| 42 | |
| 43 | jar: $(NAME).jar |
| 44 | |
| 45 | build: resources |
| 46 | @echo Compiling program... |
| 47 | @echo " src/$(MAIN)" |
| 48 | @$(JAVAC) $(JAVAC_FLAGS) "src/$(MAIN).java" |
| 49 | @[ "$(MORE)" = "" ] || for sup in $(MORE); do \ |
| 50 | echo " src/$$sup" ;\ |
| 51 | $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \ |
| 52 | done |
| 53 | |
| 54 | test: test-resources |
| 55 | @[ -e bin/$(MAIN).class ] || echo You need to build the sources |
| 56 | @[ -e bin/$(MAIN).class ] |
| 57 | @echo Compiling test program... |
| 58 | @[ "$(TEST)" != "" ] || echo No test sources defined. |
| 59 | @[ "$(TEST)" = "" ] || for sup in $(TEST); do \ |
| 60 | echo " src/$$sup" ;\ |
| 61 | $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \ |
| 62 | done |
| 63 | |
| 64 | clean: |
| 65 | rm -rf bin/ |
| 66 | @echo Removing sources taken from libs... |
| 67 | @for lib in libs/*-sources.jar; do \ |
| 68 | basename "$$lib"; \ |
| 69 | jar tf "$$lib" | while read -r ln; do \ |
| 70 | [ -f "src/$$ln" ] && rm "src/$$ln"; \ |
| 71 | done; \ |
| 72 | jar tf "$$lib" | tac | while read -r ln; do \ |
| 73 | [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \ |
| 74 | done; \ |
| 75 | done |
| 76 | |
| 77 | mrproper: mrpropre |
| 78 | |
| 79 | mrpropre: clean |
| 80 | rm -f $(NAME).jar |
| 81 | rm -f $(NAME)-sources.jar |
| 82 | [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar" |
| 83 | [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar" |
| 84 | |
| 85 | love: |
| 86 | @echo " ...not war." |
| 87 | |
| 88 | resources: libs |
| 89 | @echo Copying resources into bin/... |
| 90 | @cd src && find . | grep -v '\.java$$' | grep -v '/test/' | while read -r ln; do \ |
| 91 | if [ -f "$$ln" ]; then \ |
| 92 | dir="`dirname "$$ln"`"; \ |
| 93 | mkdir -p "../bin/$$dir" ; \ |
| 94 | cp "$$ln" "../bin/$$ln" ; \ |
| 95 | fi ; \ |
| 96 | done |
| 97 | |
| 98 | test-resources: resources |
| 99 | @echo Copying test resources into bin/... |
| 100 | @cd src && find . | grep -v '\.java$$' | grep '/test/' | while read -r ln; do \ |
| 101 | if [ -f "$$ln" ]; then \ |
| 102 | dir="`dirname "$$ln"`"; \ |
| 103 | mkdir -p "../bin/$$dir" ; \ |
| 104 | cp "$$ln" "../bin/$$ln" ; \ |
| 105 | fi ; \ |
| 106 | done |
| 107 | |
| 108 | libs: bin |
| 109 | @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs... |
| 110 | @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar; do \ |
| 111 | basename "$$lib"; \ |
| 112 | jar xf "$$lib"; \ |
| 113 | done ) |
| 114 | @[ ! -d libs ] || touch bin/libs |
| 115 | |
| 116 | $(NAME).jar: resources |
| 117 | @[ -e bin/$(MAIN).class ] || echo You need to build the sources |
| 118 | @[ -e bin/$(MAIN).class ] |
| 119 | @echo Making JAR file... |
| 120 | @echo > bin/manifest |
| 121 | @[ "$(SJAR_FLAGS)" = "" ] || echo Creating $(NAME)-sources.jar... |
| 122 | @[ "$(SJAR_FLAGS)" = "" ] || $(JAR) cfm $(NAME)-sources.jar bin/manifest $(SJAR_FLAGS) |
| 123 | @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-sources.jar"... |
| 124 | @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || cp $(NAME)-sources.jar "$(NAME)-`cat VERSION`-sources.jar" |
| 125 | @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest |
| 126 | @echo >> bin/manifest |
| 127 | $(JAR) cfm $(NAME).jar bin/manifest $(JAR_FLAGS) |
| 128 | @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"... |
| 129 | @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar" |
| 130 | |
| 131 | run: |
| 132 | @[ -e bin/$(MAIN).class ] || echo You need to build the sources |
| 133 | @[ -e bin/$(MAIN).class ] |
| 134 | @echo Running "$(NAME)"... |
| 135 | $(JAVA) $(JAVA_FLAGS) $(MAIN) |
| 136 | |
| 137 | jrun: |
| 138 | @[ -e $(NAME).jar ] || echo You need to build the jar |
| 139 | @[ -e $(NAME).jar ] |
| 140 | @echo Running "$(NAME).jar"... |
| 141 | $(RJAR) $(RJAR_FLAGS) $(NAME).jar |
| 142 | |
| 143 | run-test: |
| 144 | @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] || echo You need to build the test sources |
| 145 | @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] |
| 146 | @echo Running tests for "$(NAME)"... |
| 147 | @[ "$(TEST)" != "" ] || echo No test sources defined. |
| 148 | [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) ) |
| 149 | |
| 150 | install: |
| 151 | @[ -e $(NAME).jar ] || echo You need to build the jar |
| 152 | @[ -e $(NAME).jar ] |
| 153 | mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin" |
| 154 | cp $(NAME).jar "$(PREFIX)/lib/" |
| 155 | echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)" |
| 156 | echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)" |
| 157 | chmod a+rx "$(PREFIX)/bin/$(NAME)" |
| 158 | |