Version 4.4.5
[nikiroo-utils.git] / Makefile.base
CommitLineData
632cc690
NR
1# Makefile base template
2#
3# Version:
4# - 1.0.0: add a version comment
e8aa5bf9 5# - 1.1.0: add help, sjar
b6af2a6f 6# - 1.1.1: fix "jar" instead of ${JAR}
632cc690 7
ec1f3444
NR
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
80383c14 17#TEST_PARAMS = any parameter to pass to the test runnable when "test-run"
ec1f3444
NR
18
19JAVAC = javac
2cce3dcb 20JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/
ec1f3444
NR
21JAVA = java
22JAVA_FLAGS += -cp ./bin/
23JAR = jar
24RJAR = java
25RJAR_FLAGS += -jar
26
ec1f3444
NR
27all: build jar
28
e8aa5bf9
NR
29help:
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
ec1f3444
NR
47
48bin:
49 @mkdir -p bin
50
51jar: $(NAME).jar
52
e8aa5bf9
NR
53sjar: $(NAME)-sources.jar
54
ec1f3444
NR
55build: 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
80383c14 64test: test-resources
ec1f3444
NR
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
74clean:
75 rm -rf bin/
76 @echo Removing sources taken from libs...
37039355
NR
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"; \
b6af2a6f 80 ${JAR} tf "$$lib" | while read -r ln; do \
37039355
NR
81 [ -f "src/$$ln" ] && rm "src/$$ln"; \
82 done; \
b6af2a6f 83 ${JAR} tf "$$lib" | tac | while read -r ln; do \
37039355
NR
84 [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \
85 done; \
86 fi \
ec1f3444
NR
87 done
88
89mrproper: mrpropre
90
91mrpropre: clean
92 rm -f $(NAME).jar
8afbf872
NR
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"
ec1f3444
NR
96
97love:
98 @echo " ...not war."
99
100resources: libs
101 @echo Copying resources into bin/...
80383c14
NR
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
56566c7b 109 @cp VERSION bin/
80383c14
NR
110
111test-resources: resources
112 @echo Copying test resources into bin/...
113 @cd src && find . | grep -v '\.java$$' | grep '/test/' | while read -r ln; do \
ec1f3444
NR
114 if [ -f "$$ln" ]; then \
115 dir="`dirname "$$ln"`"; \
116 mkdir -p "../bin/$$dir" ; \
117 cp "$$ln" "../bin/$$ln" ; \
118 fi ; \
119 done
120
121libs: bin
122 @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs...
8c8da42a 123 @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \
ac50854f
NR
124 if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \
125 basename "$$lib"; \
b6af2a6f 126 ${JAR} xf "$$lib"; \
ac50854f 127 fi \
ec1f3444
NR
128 done )
129 @[ ! -d libs ] || touch bin/libs
130
e8aa5bf9
NR
131$(NAME)-sources.jar: libs
132 @echo Making sources JAR file...
ec1f3444
NR
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"
e8aa5bf9
NR
138
139$(NAME).jar: resources
140 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
141 @[ -e bin/$(MAIN).class ]
ec1f3444
NR
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
148run:
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
154jrun:
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
160run-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.
80383c14 165 [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) )
ec1f3444
NR
166
167install:
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)"
2cce3dcb 173 echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)"
ec1f3444
NR
174 chmod a+rx "$(PREFIX)/bin/$(NAME)"
175