Initial commit (working)
[fanfix.git] / Makefile.base
CommitLineData
08fe2e33
NR
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
10JAVAC = javac
11JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/ -Xdiags:verbose
12JAVA = java
13JAVA_FLAGS += -cp ./bin/
14JAR = jar
15RJAR = java
16RJAR_FLAGS += -jar
17
18# Usual options:
19# make : to build the jar file
20# make libs : to update the libraries into src/
21# make build : to update the binaries (not the jar)
22# make test : to update the test binaries
23# make build jar : to update the binaries and jar file
24# make clean : to clean the directory of intermediate files
25# make mrpropre : to clean the directory of all outputs
26# make run : to run the program from the binaries
27# make run-test : to run the test program from the binaries
28# make jrun : to run the program from the jar file
29# make install : to install the application into $PREFIX
30
31# Note: build is actually slower than rebuild in most cases except when
32# small changes only are detected ; so we use rebuild by default
33
34all: build jar
35
36.PHONY: all clean mrproper mrpropre build run jrun jar resources install libs love
37
38bin:
39 @mkdir -p bin
40
41jar: $(NAME).jar
42
43build: resources
44 @echo Compiling program...
45 @echo " src/$(MAIN)"
46 @$(JAVAC) $(JAVAC_FLAGS) "src/$(MAIN).java"
47 @[ "$(MORE)" = "" ] || for sup in $(MORE); do \
48 echo " src/$$sup" ;\
49 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
50 done
51
52test:
53 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
54 @[ -e bin/$(MAIN).class ]
55 @echo Compiling test program...
56 @[ "$(TEST)" != "" ] || echo No test sources defined.
57 @[ "$(TEST)" = "" ] || for sup in $(TEST); do \
58 echo " src/$$sup" ;\
59 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
60 done
61
62clean:
63 rm -rf bin/
64 @echo Removing sources taken from libs...
65 @for lib in libs/*.jar; do \
66 basename "$$lib"; \
67 jar tf "$$lib" | while read -r ln; do \
68 [ -f "src/$$ln" ] && rm "src/$$ln"; \
69 done; \
70 jar tf "$$lib" | tac | while read -r ln; do \
71 [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \
72 done; \
73 done
74
75mrproper: mrpropre
76
77mrpropre: clean
78 rm -f $(NAME).jar
79
80love:
81 @echo " ...not war."
82
83resources: libs
84 @echo Copying resources into bin/...
85 @cd src && find . | grep -v '\.java$$' | while read -r ln; do \
86 if [ -f "$$ln" ]; then \
87 dir="`dirname "$$ln"`"; \
88 mkdir -p "../bin/$$dir" ; \
89 cp "$$ln" "../bin/$$ln" ; \
90 fi ; \
91 done
92
93libs: bin
94 @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs...
95 @[ -e bin/libs -o ! -d libs ] || (cd src&& for lib in ../libs/*.jar;do \
96 basename "$$lib"; \
97 jar xf "$$lib"; \
98 done )
99 @[ ! -d libs ] || touch bin/libs
100
101$(NAME).jar: resources
102 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
103 @[ -e bin/$(MAIN).class ]
104 @echo Making JAR file...
105 @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest
106 @echo >> bin/manifest
107 $(JAR) cfm $(NAME).jar bin/manifest $(JAR_FLAGS)
108
109run:
110 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
111 @[ -e bin/$(MAIN).class ]
112 @echo Running "$(NAME)"...
113 $(JAVA) $(JAVA_FLAGS) $(MAIN)
114
115jrun:
116 @[ -e $(NAME).jar ] || echo You need to build the jar
117 @[ -e $(NAME).jar ]
118 @echo Running "$(NAME).jar"...
119 $(RJAR) $(RJAR_FLAGS) $(NAME).jar
120
121run-test:
122 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] || echo You need to build the test sources
123 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ]
124 @echo Running tests for "$(NAME)"...
125 @[ "$(TEST)" != "" ] || echo No test sources defined.
126 [ "$(TEST)" = "" ] || $(JAVA) $(JAVA_FLAGS) $(TEST)
127
128install:
129 @[ -e $(NAME).jar ] || echo You need to build the jar
130 @[ -e $(NAME).jar ]
131 mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin"
132 cp $(NAME).jar "$(PREFIX)/lib/"
133 echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)"
134 echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"$$@\"" >> "$(PREFIX)/bin/$(NAME)"
135 chmod a+rx "$(PREFIX)/bin/$(NAME)"
136