doc/HACKING: "git format-patch" with "-M" is better
[unicorn.git] / GNUmakefile
blobcd879c5c2392476fd86e30fa6d839ee252eab55d
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 cf - $(inst_deps) GIT-VERSION-GEN | \
61 (cd $(test_prefix) && tar xf -)
62 $(MAKE) -C $(test_prefix) clean
63 $(MAKE) -C $(test_prefix) http shebang
64 > $@
66 # this is only intended to be run within $(test_prefix)
67 shebang: $(bins)
68 $(ruby) -i -p -e '$$_.gsub!(%r{^#!.*$$},"#!$(ruby_bin)")' $^
70 t_log := $(T_log) $(T_n_log)
71 test: $(T) $(T_n)
72 @cat $(t_log) | $(ruby) test/aggregate.rb
73 @$(RM) $(t_log)
75 test-exec: $(wildcard test/exec/test_*.rb)
76 test-unit: $(wildcard test/unit/test_*.rb)
77 $(slow_tests): $(test_prefix)/.stamp
78 @$(MAKE) $(shell $(awk_slow) $@)
80 TEST_OPTS = -v
81 check_test = grep '0 failures, 0 errors' $(t) >/dev/null
82 ifndef V
83 quiet_pre = @echo '* $(arg)$(extra)';
84 quiet_post = >$(t) 2>&1 && $(check_test)
85 else
86 # we can't rely on -o pipefail outside of bash 3+,
87 # so we use a stamp file to indicate success and
88 # have rm fail if the stamp didn't get created
89 stamp = $@$(log_suffix).ok
90 quiet_pre = @echo $(ruby) $(arg) $(TEST_OPTS); ! test -f $(stamp) && (
91 quiet_post = && > $(stamp) )2>&1 | tee $(t); \
92 rm $(stamp) 2>/dev/null && $(check_test)
93 endif
95 # not all systems have setsid(8), we need it because we spam signals
96 # stupidly in some tests...
97 rb_setsid := $(ruby) -e 'Process.setsid' -e 'exec *ARGV'
99 # TRACER='strace -f -o $(t).strace -s 100000'
100 run_test = $(quiet_pre) \
101 $(rb_setsid) $(TRACER) $(ruby) -w $(arg) $(TEST_OPTS) $(quiet_post) || \
102 (sed "s,^,$(extra): ," >&2 < $(t); exit 1)
104 %.n: arg = $(subst .n,,$(subst --, -n ,$@))
105 %.n: t = $(subst .n,$(log_suffix),$@)
106 %.n: export PATH := $(test_prefix)/bin:$(PATH)
107 %.n: export RUBYLIB := $(test_prefix):$(test_prefix)/lib:$(RUBYLIB)
108 %.n: $(test_prefix)/.stamp
109 $(run_test)
111 $(T): arg = $@
112 $(T): t = $(subst .rb,$(log_suffix),$@)
113 $(T): export PATH := $(test_prefix)/bin:$(PATH)
114 $(T): export RUBYLIB := $(test_prefix):$(test_prefix)/lib:$(RUBYLIB)
115 $(T): $(test_prefix)/.stamp
116 $(run_test)
118 install: $(bins) $(ext)/unicorn_http.c
119 $(prep_setup_rb)
120 $(RM) lib/unicorn_http.$(DLEXT)
121 $(RM) -r .install-tmp
122 mkdir .install-tmp
123 cp -p bin/* .install-tmp
124 $(ruby) setup.rb all
125 $(RM) $^
126 mv .install-tmp/* bin/
127 $(RM) -r .install-tmp
128 $(prep_setup_rb)
130 setup_rb_files := .config InstalledFiles
131 prep_setup_rb := @-$(RM) $(setup_rb_files);$(MAKE) -C $(ext) clean
133 clean:
134 -$(MAKE) -C $(ext) clean
135 -$(MAKE) -C Documentation clean
136 $(RM) $(ext)/Makefile lib/unicorn_http.$(DLEXT)
137 $(RM) $(setup_rb_files) $(t_log)
138 $(RM) -r $(test_prefix) man
140 man:
141 $(MAKE) -C Documentation install-man
143 pkg_extra := GIT-VERSION-FILE NEWS ChangeLog $(ext)/unicorn_http.c
144 manifest: $(pkg_extra) man
145 $(RM) .manifest
146 $(MAKE) .manifest
148 .manifest:
149 (git ls-files && \
150 for i in $@ $(pkg_extra) $(man1_paths); \
151 do echo $$i; done) | LC_ALL=C sort > $@+
152 cmp $@+ $@ || mv $@+ $@
153 $(RM) $@+
155 NEWS: GIT-VERSION-FILE
156 $(rake) -s news_rdoc > $@+
157 mv $@+ $@
159 SINCE = 0.94.0
160 ChangeLog: LOG_VERSION = $(GIT_VERSION)
161 ChangeLog: log_range = v$(SINCE)..$(LOG_VERSION)
162 ChangeLog: GIT-VERSION-FILE
163 @echo "ChangeLog from $(GIT_URL) ($(log_range))" > $@+
164 @echo >> $@+
165 git log $(log_range) | sed -e 's/^/ /' >> $@+
166 mv $@+ $@
168 news_atom := http://unicorn.bogomips.org/NEWS.atom.xml
169 cgit_atom := http://git.bogomips.org/cgit/unicorn.git/atom/?h=master
170 atom = <link rel="alternate" title="Atom feed" href="$(1)" \
171 type="application/atom+xml"/>
173 # using rdoc 2.4.1+
174 doc: .document $(ext)/unicorn_http.c NEWS ChangeLog
175 for i in $(man1_bins); do > $$i; done
176 rdoc -Na -t "$(shell sed -ne '1s/^= //p' README)"
177 install -m644 COPYING doc/COPYING
178 install -m644 $(shell grep '^[A-Z]' .document) doc/
179 $(MAKE) -C Documentation install-html install-man
180 install -m644 $(man1_paths) doc/
181 cd doc && for i in $(base_bins); do \
182 sed -e '/"documentation">/r man1/'$$i'.1.html' \
183 < $${i}_1.html > tmp && mv tmp $${i}_1.html; done
184 $(ruby) -i -p -e \
185 '$$_.gsub!("</title>",%q{\&$(call atom,$(cgit_atom))})' \
186 doc/ChangeLog.html
187 $(ruby) -i -p -e \
188 '$$_.gsub!("</title>",%q{\&$(call atom,$(news_atom))})' \
189 doc/NEWS.html doc/README.html
190 $(rake) -s news_atom > doc/NEWS.atom.xml
191 cd doc && ln README.html tmp && mv tmp index.html
192 $(RM) $(man1_bins)
194 rails_git_url = git://github.com/rails/rails.git
195 rails_git := vendor/rails.git
196 $(rails_git)/info/cloned-stamp:
197 git clone --mirror -q $(rails_git_url) $(rails_git)
198 > $@
200 rails_tests := $(addsuffix .r,$(addprefix $(T_r).,$(rails_vers)))
201 test-rails: $(rails_tests)
202 $(T_r).%.r: t = $(addsuffix $(log_suffix),$@)
203 $(T_r).%.r: rv = $(subst .r,,$(subst $(T_r).,,$@))
204 $(T_r).%.r: extra = ' 'v$(rv)
205 $(T_r).%.r: arg = $(T_r)
206 $(T_r).%.r: export PATH := $(test_prefix)/bin:$(PATH)
207 $(T_r).%.r: export RUBYLIB := $(test_prefix):$(test_prefix)/lib:$(RUBYLIB)
208 $(T_r).%.r: export UNICORN_RAILS_TEST_VERSION = $(rv)
209 $(T_r).%.r: export RAILS_GIT_REPO = $(CURDIR)/$(rails_git)
210 $(T_r).%.r: $(test_prefix)/.stamp $(rails_git)/info/cloned-stamp
211 $(run_test)
213 ifneq ($(VERSION),)
214 rfproject := mongrel
215 rfpackage := unicorn
216 pkggem := pkg/$(rfpackage)-$(VERSION).gem
217 pkgtgz := pkg/$(rfpackage)-$(VERSION).tgz
218 release_notes := release_notes-$(VERSION)
219 release_changes := release_changes-$(VERSION)
221 release-notes: $(release_notes)
222 release-changes: $(release_changes)
223 $(release_changes):
224 $(rake) -s release_changes > $@+
225 $(VISUAL) $@+ && test -s $@+ && mv $@+ $@
226 $(release_notes):
227 GIT_URL=$(GIT_URL) $(rake) -s release_notes > $@+
228 $(VISUAL) $@+ && test -s $@+ && mv $@+ $@
230 # ensures we're actually on the tagged $(VERSION), only used for release
231 verify:
232 test x"$(shell umask)" = x0022
233 git rev-parse --verify refs/tags/v$(VERSION)^{}
234 git diff-index --quiet HEAD^0
235 test `git rev-parse --verify HEAD^0` = \
236 `git rev-parse --verify refs/tags/v$(VERSION)^{}`
238 fix-perms:
239 git ls-tree -r HEAD | awk '/^100644 / {print $$NF}' | xargs chmod 644
240 git ls-tree -r HEAD | awk '/^100755 / {print $$NF}' | xargs chmod 755
242 gem: $(pkggem)
244 install-gem: $(pkggem)
245 gem install $(CURDIR)/$<
247 $(pkggem): manifest fix-perms
248 gem build $(rfpackage).gemspec
249 mkdir -p pkg
250 mv $(@F) $@
252 $(pkgtgz): distdir = $(basename $@)
253 $(pkgtgz): HEAD = v$(VERSION)
254 $(pkgtgz): manifest fix-perms
255 @test -n "$(distdir)"
256 $(RM) -r $(distdir)
257 mkdir -p $(distdir)
258 tar cf - `cat .manifest` | (cd $(distdir) && tar xf -)
259 cd pkg && tar cf - $(basename $(@F)) | gzip -9 > $(@F)+
260 mv $@+ $@
262 package: $(pkgtgz) $(pkggem)
264 release: verify package $(release_notes) $(release_changes)
265 # make tgz release on RubyForge
266 rubyforge add_release -f -n $(release_notes) -a $(release_changes) \
267 $(rfproject) $(rfpackage) $(VERSION) $(pkgtgz)
268 # push gem to Gemcutter
269 gem push $(pkggem)
270 # in case of gem downloads from RubyForge releases page
271 -rubyforge add_file \
272 $(rfproject) $(rfpackage) $(VERSION) $(pkggem)
273 else
274 gem install-gem: GIT-VERSION-FILE
275 $(MAKE) $@ VERSION=$(GIT_VERSION)
276 endif
278 .PHONY: .FORCE-GIT-VERSION-FILE doc $(T) $(slow_tests) manifest man