cleanup: avoid redundant error checks for fstat
[unicorn.git] / GNUmakefile
blob963809c9a9b8e08ee3941819ced035e51aadd895
1 # use GNU Make to run tests in parallel, and without depending on Rubygems
2 all:: test
3 ruby = ruby
4 rake = rake
5 ragel = ragel
6 GIT_URL = git://git.bogomips.org/unicorn.git
7 RLFLAGS = -G2
9 GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE
10 @./GIT-VERSION-GEN
11 -include GIT-VERSION-FILE
12 -include local.mk
13 ruby_bin := $(shell which $(ruby))
14 ifeq ($(DLEXT),) # "so" for Linux
15 DLEXT := $(shell $(ruby) -rrbconfig -e 'puts Config::CONFIG["DLEXT"]')
16 endif
17 ifeq ($(RUBY_VERSION),)
18 RUBY_VERSION := $(shell $(ruby) -e 'puts RUBY_VERSION')
19 endif
21 # dunno how to implement this as concisely in Ruby, and hell, I love awk
22 awk_slow := awk '/def test_/{print FILENAME"--"$$2".n"}' 2>/dev/null
24 rails_vers := $(subst test/rails/app-,,$(wildcard test/rails/app-*))
25 slow_tests := test/unit/test_server.rb test/exec/test_exec.rb \
26 test/unit/test_signals.rb test/unit/test_upload.rb
27 log_suffix = .$(RUBY_VERSION).log
28 T_r := $(wildcard test/rails/test*.rb)
29 T := $(filter-out $(slow_tests) $(T_r), $(wildcard test/*/test*.rb))
30 T_n := $(shell $(awk_slow) $(slow_tests))
31 T_log := $(subst .rb,$(log_suffix),$(T))
32 T_n_log := $(subst .n,$(log_suffix),$(T_n))
33 T_r_log := $(subst .r,$(log_suffix),$(T_r))
34 test_prefix = $(CURDIR)/test/install-$(RUBY_VERSION)
36 ext := ext/unicorn_http
37 c_files := $(ext)/unicorn_http.c $(wildcard $(ext)/*.h)
38 rl_files := $(wildcard $(ext)/*.rl)
39 base_bins := unicorn unicorn_rails
40 bins := $(addprefix bin/, $(base_bins))
41 man1_bins := $(addsuffix .1, $(base_bins))
42 man1_paths := $(addprefix man/man1/, $(man1_bins))
43 rb_files := $(bins) $(shell find lib ext -type f -name '*.rb')
44 inst_deps := $(c_files) $(rb_files) GNUmakefile test/test_helper.rb
46 ragel: $(ext)/unicorn_http.c
47 $(ext)/unicorn_http.c: $(rl_files)
48 cd $(@D) && $(ragel) unicorn_http.rl -C $(RLFLAGS) -o $(@F)
49 $(ext)/Makefile: $(ext)/extconf.rb $(c_files)
50 cd $(@D) && $(ruby) extconf.rb
51 $(ext)/unicorn_http.$(DLEXT): $(ext)/Makefile
52 $(MAKE) -C $(@D)
53 lib/unicorn_http.$(DLEXT): $(ext)/unicorn_http.$(DLEXT)
54 @mkdir -p lib
55 install -m644 $< $@
56 http: lib/unicorn_http.$(DLEXT)
58 $(test_prefix)/.stamp: $(inst_deps)
59 mkdir -p $(test_prefix)/.ccache
60 tar c $(inst_deps) GIT-VERSION-GEN | (cd $(test_prefix) && tar x)
61 $(MAKE) -C $(test_prefix) clean
62 $(MAKE) -C $(test_prefix) http shebang
63 > $@
65 # this is only intended to be run within $(test_prefix)
66 shebang: $(bins)
67 $(ruby) -i -p -e '$$_.gsub!(%r{^#!.*$$},"#!$(ruby_bin)")' $^
69 t_log := $(T_log) $(T_n_log)
70 test: $(T) $(T_n)
71 @cat $(t_log) | $(ruby) test/aggregate.rb
72 @$(RM) $(t_log)
74 test-exec: $(wildcard test/exec/test_*.rb)
75 test-unit: $(wildcard test/unit/test_*.rb)
76 $(slow_tests): $(test_prefix)/.stamp
77 @$(MAKE) $(shell $(awk_slow) $@)
79 TEST_OPTS = -v
80 check_test = grep '0 failures, 0 errors' $(t) >/dev/null
81 ifndef V
82 quiet_pre = @echo '* $(arg)$(extra)';
83 quiet_post = >$(t) 2>&1 && $(check_test)
84 else
85 # we can't rely on -o pipefail outside of bash 3+,
86 # so we use a stamp file to indicate success and
87 # have rm fail if the stamp didn't get created
88 stamp = $@$(log_suffix).ok
89 quiet_pre = @echo $(ruby) $(arg) $(TEST_OPTS); ! test -f $(stamp) && (
90 quiet_post = && > $(stamp) )2>&1 | tee $(t); \
91 rm $(stamp) 2>/dev/null && $(check_test)
92 endif
94 # TRACER='strace -f -o $(t).strace -s 100000'
95 run_test = $(quiet_pre) \
96 setsid $(TRACER) $(ruby) -w $(arg) $(TEST_OPTS) $(quiet_post) || \
97 (sed "s,^,$(extra): ," >&2 < $(t); exit 1)
99 %.n: arg = $(subst .n,,$(subst --, -n ,$@))
100 %.n: t = $(subst .n,$(log_suffix),$@)
101 %.n: export PATH := $(test_prefix)/bin:$(PATH)
102 %.n: export RUBYLIB := $(test_prefix):$(test_prefix)/lib:$(RUBYLIB)
103 %.n: $(test_prefix)/.stamp
104 $(run_test)
106 $(T): arg = $@
107 $(T): t = $(subst .rb,$(log_suffix),$@)
108 $(T): export PATH := $(test_prefix)/bin:$(PATH)
109 $(T): export RUBYLIB := $(test_prefix):$(test_prefix)/lib:$(RUBYLIB)
110 $(T): $(test_prefix)/.stamp
111 $(run_test)
113 install: $(bins) $(ext)/unicorn_http.c
114 $(prep_setup_rb)
115 $(RM) lib/unicorn_http.$(DLEXT)
116 $(RM) -r .install-tmp
117 mkdir .install-tmp
118 cp -p bin/* .install-tmp
119 $(ruby) setup.rb all
120 $(RM) $^
121 mv .install-tmp/* bin/
122 $(RM) -r .install-tmp
123 $(prep_setup_rb)
125 setup_rb_files := .config InstalledFiles
126 prep_setup_rb := @-$(RM) $(setup_rb_files);$(MAKE) -C $(ext) clean
128 clean:
129 -$(MAKE) -C $(ext) clean
130 -$(MAKE) -C Documentation clean
131 $(RM) $(ext)/Makefile lib/unicorn_http.$(DLEXT)
132 $(RM) $(setup_rb_files) $(t_log)
133 $(RM) -r $(test_prefix) man
135 man:
136 $(MAKE) -C Documentation install-man
138 pkg_extra := GIT-VERSION-FILE NEWS ChangeLog $(ext)/unicorn_http.c
139 manifest: $(pkg_extra) man
140 $(RM) .manifest
141 $(MAKE) .manifest
143 .manifest:
144 (git ls-files && \
145 for i in $@ $(pkg_extra) $(man1_paths); \
146 do echo $$i; done) | LC_ALL=C sort > $@+
147 cmp $@+ $@ || mv $@+ $@
148 $(RM) $@+
150 NEWS: GIT-VERSION-FILE
151 $(rake) -s news_rdoc > $@+
152 mv $@+ $@
154 SINCE = 0.92.0
155 ChangeLog: log_range = $(shell test -n "$(SINCE)" && echo v$(SINCE)..)
156 ChangeLog: GIT-VERSION-FILE
157 @echo "ChangeLog from $(GIT_URL) ($(SINCE)..$(GIT_VERSION))" > $@+
158 @echo >> $@+
159 git log $(log_range) | sed -e 's/^/ /' >> $@+
160 mv $@+ $@
162 news_atom := http://unicorn.bogomips.org/NEWS.atom.xml
163 cgit_atom := http://git.bogomips.org/cgit/unicorn.git/atom/?h=master
164 atom = <link rel="alternate" title="Atom feed" href="$(1)" \
165 type="application/atom+xml"/>
167 # using rdoc 2.4.1+
168 doc: .document $(ext)/unicorn_http.c NEWS ChangeLog
169 for i in $(man1_bins); do > $$i; done
170 rdoc -Na -t "$(shell sed -ne '1s/^= //p' README)"
171 install -m644 COPYING doc/COPYING
172 install -m644 $(shell grep '^[A-Z]' .document) doc/
173 $(MAKE) -C Documentation install-html install-man
174 install -m644 $(man1_paths) doc/
175 cd doc && for i in $(base_bins); do \
176 sed -e '/"documentation">/r man1/'$$i'.1.html' \
177 < $${i}_1.html > tmp && mv tmp $${i}_1.html; done
178 $(ruby) -i -p -e \
179 '$$_.gsub!("</title>",%q{\&$(call atom,$(cgit_atom))})' \
180 doc/ChangeLog.html
181 $(ruby) -i -p -e \
182 '$$_.gsub!("</title>",%q{\&$(call atom,$(news_atom))})' \
183 doc/NEWS.html doc/README.html
184 $(rake) -s news_atom > doc/NEWS.atom.xml
185 cd doc && ln README.html tmp && mv tmp index.html
186 $(RM) $(man1_bins)
188 rails_git_url = git://github.com/rails/rails.git
189 rails_git := vendor/rails.git
190 $(rails_git)/info/cloned-stamp:
191 git clone --mirror -q $(rails_git_url) $(rails_git)
192 > $@
194 rails_tests := $(addsuffix .r,$(addprefix $(T_r).,$(rails_vers)))
195 test-rails: $(rails_tests)
196 $(T_r).%.r: t = $(addsuffix $(log_suffix),$@)
197 $(T_r).%.r: rv = $(subst .r,,$(subst $(T_r).,,$@))
198 $(T_r).%.r: extra = ' 'v$(rv)
199 $(T_r).%.r: arg = $(T_r)
200 $(T_r).%.r: export PATH := $(test_prefix)/bin:$(PATH)
201 $(T_r).%.r: export RUBYLIB := $(test_prefix):$(test_prefix)/lib:$(RUBYLIB)
202 $(T_r).%.r: export UNICORN_RAILS_TEST_VERSION = $(rv)
203 $(T_r).%.r: export RAILS_GIT_REPO = $(CURDIR)/$(rails_git)
204 $(T_r).%.r: $(test_prefix)/.stamp $(rails_git)/info/cloned-stamp
205 $(run_test)
207 ifneq ($(VERSION),)
208 rfproject := mongrel
209 rfpackage := unicorn
210 pkggem := pkg/$(rfpackage)-$(VERSION).gem
211 pkgtgz := pkg/$(rfpackage)-$(VERSION).tgz
212 release_notes := release_notes-$(VERSION)
213 release_changes := release_changes-$(VERSION)
215 release-notes: $(release_notes)
216 release-changes: $(release_changes)
217 $(release_changes):
218 $(rake) -s release_changes > $@+
219 $(VISUAL) $@+ && test -s $@+ && mv $@+ $@
220 $(release_notes):
221 GIT_URL=$(GIT_URL) $(rake) -s release_notes > $@+
222 $(VISUAL) $@+ && test -s $@+ && mv $@+ $@
224 # ensures we're actually on the tagged $(VERSION), only used for release
225 verify:
226 test x"$(shell umask)" = x0022
227 git rev-parse --verify refs/tags/v$(VERSION)^{}
228 git diff-index --quiet HEAD^0
229 test `git rev-parse --verify HEAD^0` = \
230 `git rev-parse --verify refs/tags/v$(VERSION)^{}`
232 fix-perms:
233 git ls-tree -r HEAD | awk '/^100644 / {print $$NF}' | xargs chmod 644
234 git ls-tree -r HEAD | awk '/^100755 / {print $$NF}' | xargs chmod 755
236 gem: $(pkggem)
238 install-gem: $(pkggem)
239 gem install $(CURDIR)/$<
241 $(pkggem): manifest fix-perms
242 gem build $(rfpackage).gemspec
243 mkdir -p pkg
244 mv $(@F) $@
246 $(pkgtgz): distdir = $(basename $@)
247 $(pkgtgz): HEAD = v$(VERSION)
248 $(pkgtgz): manifest fix-perms
249 @test -n "$(distdir)"
250 $(RM) -r $(distdir)
251 mkdir -p $(distdir)
252 tar c `cat .manifest` | (cd $(distdir) && tar x)
253 cd pkg && tar c $(basename $(@F)) | gzip -9 > $(@F)+
254 mv $@+ $@
256 package: $(pkgtgz) $(pkggem)
258 release: verify package $(release_notes) $(release_changes)
259 rubyforge add_release -f -n $(release_notes) -a $(release_changes) \
260 $(rfproject) $(rfpackage) $(VERSION) $(pkggem)
261 rubyforge add_file \
262 $(rfproject) $(rfpackage) $(VERSION) $(pkgtgz)
263 else
264 gem install-gem: GIT-VERSION-FILE
265 $(MAKE) $@ VERSION=$(GIT_VERSION)
266 endif
268 .PHONY: .FORCE-GIT-VERSION-FILE doc $(T) $(slow_tests) manifest man