| 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 libs/*-sources.patch.jar; do \ |
| 68 | if [ "$$lib" != 'libs/*-sources.jar' -a "$$lib" != 'libs/*-sources.patch.jar' ]; then \ |
| 69 | basename "$$lib"; \ |
| 70 | jar tf "$$lib" | while read -r ln; do \ |
| 71 | [ -f "src/$$ln" ] && rm "src/$$ln"; \ |
| 72 | done; \ |
| 73 | jar tf "$$lib" | tac | while read -r ln; do \ |
| 74 | [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \ |
| 75 | done; \ |
| 76 | fi \ |
| 77 | done |
| 78 | |
| 79 | mrproper: mrpropre |
| 80 | |
| 81 | mrpropre: clean |
| 82 | rm -f $(NAME).jar |
| 83 | rm -f $(NAME)-sources.jar |
| 84 | [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar" |
| 85 | [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar" |
| 86 | |
| 87 | love: |
| 88 | @echo " ...not war." |
| 89 | |
| 90 | resources: libs |
| 91 | @echo Copying resources into bin/... |
| 92 | @cd src && find . | grep -v '\.java$$' | grep -v '/test/' | while read -r ln; do \ |
| 93 | if [ -f "$$ln" ]; then \ |
| 94 | dir="`dirname "$$ln"`"; \ |
| 95 | mkdir -p "../bin/$$dir" ; \ |
| 96 | cp "$$ln" "../bin/$$ln" ; \ |
| 97 | fi ; \ |
| 98 | done |
| 99 | @cp VERSION bin/ |
| 100 | |
| 101 | test-resources: resources |
| 102 | @echo Copying test resources into bin/... |
| 103 | @cd src && find . | grep -v '\.java$$' | grep '/test/' | while read -r ln; do \ |
| 104 | if [ -f "$$ln" ]; then \ |
| 105 | dir="`dirname "$$ln"`"; \ |
| 106 | mkdir -p "../bin/$$dir" ; \ |
| 107 | cp "$$ln" "../bin/$$ln" ; \ |
| 108 | fi ; \ |
| 109 | done |
| 110 | |
| 111 | libs: bin |
| 112 | @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs... |
| 113 | @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \ |
| 114 | if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \ |
| 115 | basename "$$lib"; \ |
| 116 | jar xf "$$lib"; \ |
| 117 | fi \ |
| 118 | done ) |
| 119 | @[ ! -d libs ] || touch bin/libs |
| 120 | |
| 121 | $(NAME).jar: resources |
| 122 | @[ -e bin/$(MAIN).class ] || echo You need to build the sources |
| 123 | @[ -e bin/$(MAIN).class ] |
| 124 | @echo Making JAR file... |
| 125 | @echo > bin/manifest |
| 126 | @[ "$(SJAR_FLAGS)" = "" ] || echo Creating $(NAME)-sources.jar... |
| 127 | @[ "$(SJAR_FLAGS)" = "" ] || $(JAR) cfm $(NAME)-sources.jar bin/manifest $(SJAR_FLAGS) |
| 128 | @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-sources.jar"... |
| 129 | @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || cp $(NAME)-sources.jar "$(NAME)-`cat VERSION`-sources.jar" |
| 130 | @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest |
| 131 | @echo >> bin/manifest |
| 132 | $(JAR) cfm $(NAME).jar bin/manifest $(JAR_FLAGS) |
| 133 | @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"... |
| 134 | @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar" |
| 135 | |
| 136 | run: |
| 137 | @[ -e bin/$(MAIN).class ] || echo You need to build the sources |
| 138 | @[ -e bin/$(MAIN).class ] |
| 139 | @echo Running "$(NAME)"... |
| 140 | $(JAVA) $(JAVA_FLAGS) $(MAIN) |
| 141 | |
| 142 | jrun: |
| 143 | @[ -e $(NAME).jar ] || echo You need to build the jar |
| 144 | @[ -e $(NAME).jar ] |
| 145 | @echo Running "$(NAME).jar"... |
| 146 | $(RJAR) $(RJAR_FLAGS) $(NAME).jar |
| 147 | |
| 148 | run-test: |
| 149 | @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] || echo You need to build the test sources |
| 150 | @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] |
| 151 | @echo Running tests for "$(NAME)"... |
| 152 | @[ "$(TEST)" != "" ] || echo No test sources defined. |
| 153 | [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) ) |
| 154 | |
| 155 | install: |
| 156 | @[ -e $(NAME).jar ] || echo You need to build the jar |
| 157 | @[ -e $(NAME).jar ] |
| 158 | mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin" |
| 159 | cp $(NAME).jar "$(PREFIX)/lib/" |
| 160 | echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)" |
| 161 | echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)" |
| 162 | chmod a+rx "$(PREFIX)/bin/$(NAME)" |
| 163 | |