Make TUI optional (and update nikiroo-utils):
[fanfix.git] / Makefile.base
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 @cp VERSION bin/
98
99 test-resources: resources
100 @echo Copying test resources into bin/...
101 @cd src && find . | grep -v '\.java$$' | grep '/test/' | while read -r ln; do \
102 if [ -f "$$ln" ]; then \
103 dir="`dirname "$$ln"`"; \
104 mkdir -p "../bin/$$dir" ; \
105 cp "$$ln" "../bin/$$ln" ; \
106 fi ; \
107 done
108
109 libs: bin
110 @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs...
111 @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \
112 if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \
113 basename "$$lib"; \
114 jar xf "$$lib"; \
115 fi \
116 done )
117 @[ ! -d libs ] || touch bin/libs
118
119 $(NAME).jar: resources
120 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
121 @[ -e bin/$(MAIN).class ]
122 @echo Making JAR file...
123 @echo > bin/manifest
124 @[ "$(SJAR_FLAGS)" = "" ] || echo Creating $(NAME)-sources.jar...
125 @[ "$(SJAR_FLAGS)" = "" ] || $(JAR) cfm $(NAME)-sources.jar bin/manifest $(SJAR_FLAGS)
126 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-sources.jar"...
127 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || cp $(NAME)-sources.jar "$(NAME)-`cat VERSION`-sources.jar"
128 @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest
129 @echo >> bin/manifest
130 $(JAR) cfm $(NAME).jar bin/manifest $(JAR_FLAGS)
131 @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"...
132 @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar"
133
134 run:
135 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
136 @[ -e bin/$(MAIN).class ]
137 @echo Running "$(NAME)"...
138 $(JAVA) $(JAVA_FLAGS) $(MAIN)
139
140 jrun:
141 @[ -e $(NAME).jar ] || echo You need to build the jar
142 @[ -e $(NAME).jar ]
143 @echo Running "$(NAME).jar"...
144 $(RJAR) $(RJAR_FLAGS) $(NAME).jar
145
146 run-test:
147 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] || echo You need to build the test sources
148 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ]
149 @echo Running tests for "$(NAME)"...
150 @[ "$(TEST)" != "" ] || echo No test sources defined.
151 [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) )
152
153 install:
154 @[ -e $(NAME).jar ] || echo You need to build the jar
155 @[ -e $(NAME).jar ]
156 mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin"
157 cp $(NAME).jar "$(PREFIX)/lib/"
158 echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)"
159 echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)"
160 chmod a+rx "$(PREFIX)/bin/$(NAME)"
161