1 # Makefile base template
4 # - 1.0.0: add a version comment
6 # Required parameters (the commented out ones are supposed to change per project):
8 #MAIN = path to main java source to compile
9 #MORE = path to supplementary needed resources not linked from MAIN
10 #NAME = name of project (used for jar output file)
11 #PREFIX = usually /usr/local (where to install the program)
12 #TEST = path to main test source to compile
13 #JAR_FLAGS += a list of things to pack, each usually prefixed with "-C bin/"
14 #SJAR_FLAGS += a list of things to pack, each usually prefixed with "-C src/", for *-sources.jar files
15 #TEST_PARAMS = any parameter to pass to the test runnable when "test-run"
18 JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/
20 JAVA_FLAGS += -cp ./bin/
26 # make : to build the jar file
27 # make libs : to update the libraries into src/
28 # make build : to update the binaries (not the jar)
29 # make test : to update the test binaries
30 # make build jar : to update the binaries and jar file
31 # make clean : to clean the directory of intermediate files
32 # make mrpropre : to clean the directory of all outputs
33 # make run : to run the program from the binaries
34 # make run-test : to run the test program from the binaries
35 # make jrun : to run the program from the jar file
36 # make install : to install the application into $PREFIX
38 # Note: build is actually slower than rebuild in most cases except when
39 # small changes only are detected ; so we use rebuild by default
43 .PHONY: all clean mrproper mrpropre build run jrun jar resources test-resources install libs love
51 @echo Compiling program...
53 @$(JAVAC) $(JAVAC_FLAGS) "src/$(MAIN).java"
54 @[ "$(MORE)" = "" ] || for sup in $(MORE); do \
56 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
60 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
61 @[ -e bin/$(MAIN).class ]
62 @echo Compiling test program...
63 @[ "$(TEST)" != "" ] || echo No test sources defined.
64 @[ "$(TEST)" = "" ] || for sup in $(TEST); do \
66 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
71 @echo Removing sources taken from libs...
72 @for lib in libs/*-sources.jar libs/*-sources.patch.jar; do \
73 if [ "$$lib" != 'libs/*-sources.jar' -a "$$lib" != 'libs/*-sources.patch.jar' ]; then \
75 jar tf "$$lib" | while read -r ln; do \
76 [ -f "src/$$ln" ] && rm "src/$$ln"; \
78 jar tf "$$lib" | tac | while read -r ln; do \
79 [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \
88 rm -f $(NAME)-sources.jar
89 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar"
90 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar"
96 @echo Copying resources into bin/...
97 @cd src && find . | grep -v '\.java$$' | grep -v '/test/' | while read -r ln; do \
98 if [ -f "$$ln" ]; then \
99 dir="`dirname "$$ln"`"; \
100 mkdir -p "../bin/$$dir" ; \
101 cp "$$ln" "../bin/$$ln" ; \
106 test-resources: resources
107 @echo Copying test resources into bin/...
108 @cd src && find . | grep -v '\.java$$' | grep '/test/' | while read -r ln; do \
109 if [ -f "$$ln" ]; then \
110 dir="`dirname "$$ln"`"; \
111 mkdir -p "../bin/$$dir" ; \
112 cp "$$ln" "../bin/$$ln" ; \
117 @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs...
118 @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \
119 if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \
124 @[ ! -d libs ] || touch bin/libs
126 $(NAME).jar: resources
127 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
128 @[ -e bin/$(MAIN).class ]
129 @echo Making JAR file...
131 @[ "$(SJAR_FLAGS)" = "" ] || echo Creating $(NAME)-sources.jar...
132 @[ "$(SJAR_FLAGS)" = "" ] || $(JAR) cfm $(NAME)-sources.jar bin/manifest $(SJAR_FLAGS)
133 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-sources.jar"...
134 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || cp $(NAME)-sources.jar "$(NAME)-`cat VERSION`-sources.jar"
135 @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest
136 @echo >> bin/manifest
137 $(JAR) cfm $(NAME).jar bin/manifest $(JAR_FLAGS)
138 @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"...
139 @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar"
142 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
143 @[ -e bin/$(MAIN).class ]
144 @echo Running "$(NAME)"...
145 $(JAVA) $(JAVA_FLAGS) $(MAIN)
148 @[ -e $(NAME).jar ] || echo You need to build the jar
150 @echo Running "$(NAME).jar"...
151 $(RJAR) $(RJAR_FLAGS) $(NAME).jar
154 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] || echo You need to build the test sources
155 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ]
156 @echo Running tests for "$(NAME)"...
157 @[ "$(TEST)" != "" ] || echo No test sources defined.
158 [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) )
161 @[ -e $(NAME).jar ] || echo You need to build the jar
163 mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin"
164 cp $(NAME).jar "$(PREFIX)/lib/"
165 echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)"
166 echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)"
167 chmod a+rx "$(PREFIX)/bin/$(NAME)"