update to latest nikiroo-utils
[jvcard.git] / Makefile.base
1 # Makefile base template
2 #
3 # Version:
4 # - 1.0.0: add a version comment
5
6 # Required parameters (the commented out ones are supposed to change per project):
7
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"
16
17 JAVAC = javac
18 JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/
19 JAVA = java
20 JAVA_FLAGS += -cp ./bin/
21 JAR = jar
22 RJAR = java
23 RJAR_FLAGS += -jar
24
25 # Usual options:
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
37
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
40
41 all: build jar
42
43 .PHONY: all clean mrproper mrpropre build run jrun jar resources test-resources install libs love
44
45 bin:
46 @mkdir -p bin
47
48 jar: $(NAME).jar
49
50 build: resources
51 @echo Compiling program...
52 @echo " src/$(MAIN)"
53 @$(JAVAC) $(JAVAC_FLAGS) "src/$(MAIN).java"
54 @[ "$(MORE)" = "" ] || for sup in $(MORE); do \
55 echo " src/$$sup" ;\
56 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
57 done
58
59 test: test-resources
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 \
65 echo " src/$$sup" ;\
66 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
67 done
68
69 clean:
70 rm -rf bin/
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 \
74 basename "$$lib"; \
75 jar tf "$$lib" | while read -r ln; do \
76 [ -f "src/$$ln" ] && rm "src/$$ln"; \
77 done; \
78 jar tf "$$lib" | tac | while read -r ln; do \
79 [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \
80 done; \
81 fi \
82 done
83
84 mrproper: mrpropre
85
86 mrpropre: clean
87 rm -f $(NAME).jar
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"
91
92 love:
93 @echo " ...not war."
94
95 resources: libs
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" ; \
102 fi ; \
103 done
104 @cp VERSION bin/
105
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" ; \
113 fi ; \
114 done
115
116 libs: bin
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 \
120 basename "$$lib"; \
121 jar xf "$$lib"; \
122 fi \
123 done )
124 @[ ! -d libs ] || touch bin/libs
125
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...
130 @echo > bin/manifest
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"
140
141 run:
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)
146
147 jrun:
148 @[ -e $(NAME).jar ] || echo You need to build the jar
149 @[ -e $(NAME).jar ]
150 @echo Running "$(NAME).jar"...
151 $(RJAR) $(RJAR_FLAGS) $(NAME).jar
152
153 run-test:
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) )
159
160 install:
161 @[ -e $(NAME).jar ] || echo You need to build the jar
162 @[ -e $(NAME).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)"
168