TUNING: document worker_processes tuning
[unicorn.git] / GNUmakefile
blobc0016efc33e5da89bbe509dababc5fcbf9a728ca
1 # use GNU Make to run tests in parallel, and without depending on RubyGems
2 all:: test
4 RLFLAGS = -G2
6 MRI = ruby
7 RUBY = ruby
8 RAKE = rake
9 RAGEL = ragel
10 RSYNC = rsync
12 GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE
13 @./GIT-VERSION-GEN
14 -include GIT-VERSION-FILE
15 -include local.mk
16 ruby_bin := $(shell which $(RUBY))
17 ifeq ($(DLEXT),) # "so" for Linux
18 DLEXT := $(shell $(RUBY) -rrbconfig -e 'puts RbConfig::CONFIG["DLEXT"]')
19 endif
20 ifeq ($(RUBY_VERSION),)
21 RUBY_VERSION := $(shell $(RUBY) -e 'puts RUBY_VERSION')
22 endif
24 RUBY_ENGINE := $(shell $(RUBY) -e 'puts((RUBY_ENGINE rescue "ruby"))')
26 isolate_libs := tmp/isolate/$(RUBY_ENGINE)-$(RUBY_VERSION).mk
27 $(isolate_libs): script/isolate_for_tests
28 @$(RUBY) script/isolate_for_tests
29 -include $(isolate_libs)
30 MYLIBS = $(RUBYLIB):$(ISOLATE_LIBS)
32 # dunno how to implement this as concisely in Ruby, and hell, I love awk
33 awk_slow := awk '/def test_/{print FILENAME"--"$$2".n"}' 2>/dev/null
35 rails_vers := $(subst test/rails/app-,,$(wildcard test/rails/app-*))
36 slow_tests := test/unit/test_server.rb test/exec/test_exec.rb \
37 test/unit/test_signals.rb test/unit/test_upload.rb
38 log_suffix = .$(RUBY_ENGINE).$(RUBY_VERSION).log
39 T_r := $(wildcard test/rails/test*.rb)
40 T := $(filter-out $(slow_tests) $(T_r), $(wildcard test/*/test*.rb))
41 T_n := $(shell $(awk_slow) $(slow_tests))
42 T_log := $(subst .rb,$(log_suffix),$(T))
43 T_n_log := $(subst .n,$(log_suffix),$(T_n))
44 T_r_log := $(subst .r,$(log_suffix),$(T_r))
45 test_prefix = $(CURDIR)/test/$(RUBY_ENGINE)-$(RUBY_VERSION)
47 ext := ext/unicorn_http
48 c_files := $(ext)/unicorn_http.c $(ext)/httpdate.c $(wildcard $(ext)/*.h)
49 rl_files := $(wildcard $(ext)/*.rl)
50 base_bins := unicorn unicorn_rails
51 bins := $(addprefix bin/, $(base_bins))
52 man1_rdoc := $(addsuffix _1, $(base_bins))
53 man1_bins := $(addsuffix .1, $(base_bins))
54 man1_paths := $(addprefix man/man1/, $(man1_bins))
55 rb_files := $(bins) $(shell find lib ext -type f -name '*.rb')
56 inst_deps := $(c_files) $(rb_files) GNUmakefile test/test_helper.rb
58 ragel: $(ext)/unicorn_http.c
59 $(ext)/unicorn_http.c: $(rl_files)
60 cd $(@D) && $(RAGEL) unicorn_http.rl -C $(RLFLAGS) -o $(@F)
61 $(ext)/Makefile: $(ext)/extconf.rb $(c_files)
62 cd $(@D) && $(RUBY) extconf.rb
63 $(ext)/unicorn_http.$(DLEXT): $(ext)/Makefile
64 $(MAKE) -C $(@D)
65 lib/unicorn_http.$(DLEXT): $(ext)/unicorn_http.$(DLEXT)
66 @mkdir -p lib
67 install -m644 $< $@
68 http: lib/unicorn_http.$(DLEXT)
70 test-install: $(test_prefix)/.stamp
71 $(test_prefix)/.stamp: $(inst_deps)
72 mkdir -p $(test_prefix)/.ccache
73 tar cf - $(inst_deps) GIT-VERSION-GEN | \
74 (cd $(test_prefix) && tar xf -)
75 $(MAKE) -C $(test_prefix) clean
76 $(MAKE) -C $(test_prefix) http shebang RUBY="$(RUBY)"
77 > $@
79 # this is only intended to be run within $(test_prefix)
80 shebang: $(bins)
81 $(MRI) -i -p -e '$$_.gsub!(%r{^#!.*$$},"#!$(ruby_bin)")' $^
83 t_log := $(T_log) $(T_n_log)
84 test: $(T) $(T_n)
85 @cat $(t_log) | $(MRI) test/aggregate.rb
86 @$(RM) $(t_log)
88 test-exec: $(wildcard test/exec/test_*.rb)
89 test-unit: $(wildcard test/unit/test_*.rb)
90 $(slow_tests): $(test_prefix)/.stamp
91 @$(MAKE) $(shell $(awk_slow) $@)
93 test-integration: $(test_prefix)/.stamp
94 $(MAKE) -C t
96 test-all: test test-rails test-integration
98 TEST_OPTS = -v
99 check_test = grep '0 failures, 0 errors' $(t) >/dev/null
100 ifndef V
101 quiet_pre = @echo '* $(arg)$(extra)';
102 quiet_post = >$(t) 2>&1 && $(check_test)
103 else
104 # we can't rely on -o pipefail outside of bash 3+,
105 # so we use a stamp file to indicate success and
106 # have rm fail if the stamp didn't get created
107 stamp = $@$(log_suffix).ok
108 quiet_pre = @echo $(RUBY) $(arg) $(TEST_OPTS); ! test -f $(stamp) && (
109 quiet_post = && > $(stamp) )2>&1 | tee $(t); \
110 rm $(stamp) 2>/dev/null && $(check_test)
111 endif
113 # not all systems have setsid(8), we need it because we spam signals
114 # stupidly in some tests...
115 rb_setsid := $(RUBY) -e 'Process.setsid' -e 'exec *ARGV'
117 # TRACER='strace -f -o $(t).strace -s 100000'
118 run_test = $(quiet_pre) \
119 $(rb_setsid) $(TRACER) $(RUBY) -w $(arg) $(TEST_OPTS) $(quiet_post) || \
120 (sed "s,^,$(extra): ," >&2 < $(t); exit 1)
122 %.n: arg = $(subst .n,,$(subst --, -n ,$@))
123 %.n: t = $(subst .n,$(log_suffix),$@)
124 %.n: export PATH := $(test_prefix)/bin:$(PATH)
125 %.n: export RUBYLIB := $(test_prefix):$(test_prefix)/lib:$(MYLIBS)
126 %.n: $(test_prefix)/.stamp
127 $(run_test)
129 $(T): arg = $@
130 $(T): t = $(subst .rb,$(log_suffix),$@)
131 $(T): export PATH := $(test_prefix)/bin:$(PATH)
132 $(T): export RUBYLIB := $(test_prefix):$(test_prefix)/lib:$(MYLIBS)
133 $(T): $(test_prefix)/.stamp
134 $(run_test)
136 install: $(bins) $(ext)/unicorn_http.c
137 $(prep_setup_rb)
138 $(RM) lib/unicorn_http.$(DLEXT)
139 $(RM) -r .install-tmp
140 mkdir .install-tmp
141 cp -p bin/* .install-tmp
142 $(RUBY) setup.rb all
143 $(RM) $^
144 mv .install-tmp/* bin/
145 $(RM) -r .install-tmp
146 $(prep_setup_rb)
148 setup_rb_files := .config InstalledFiles
149 prep_setup_rb := @-$(RM) $(setup_rb_files);$(MAKE) -C $(ext) clean
151 clean:
152 -$(MAKE) -C $(ext) clean
153 -$(MAKE) -C Documentation clean
154 $(RM) $(ext)/Makefile lib/unicorn_http.$(DLEXT)
155 $(RM) $(setup_rb_files) $(t_log)
156 $(RM) -r $(test_prefix) man
158 man html:
159 $(MAKE) -C Documentation install-$@
161 pkg_extra := GIT-VERSION-FILE ChangeLog LATEST NEWS \
162 $(ext)/unicorn_http.c $(man1_paths)
164 ChangeLog: GIT-VERSION-FILE .wrongdoc.yml
165 wrongdoc prepare
167 .manifest: ChangeLog $(ext)/unicorn_http.c
168 (git ls-files && for i in $@ $(pkg_extra); do echo $$i; done) | \
169 LC_ALL=C sort > $@+
170 cmp $@+ $@ || mv $@+ $@
171 $(RM) $@+
173 doc: .document $(ext)/unicorn_http.c man html .wrongdoc.yml
174 for i in $(man1_rdoc); do echo > $$i; done
175 find bin lib -type f -name '*.rbc' -exec rm -f '{}' ';'
176 $(RM) -r doc
177 wrongdoc all
178 install -m644 COPYING doc/COPYING
179 install -m644 $(shell grep '^[A-Z]' .document) doc/
180 install -m644 $(man1_paths) doc/
181 tar cf - $$(git ls-files examples/) | (cd doc && tar xf -)
182 $(RM) $(man1_rdoc)
184 # publishes docs to http://unicorn.bogomips.org
185 publish_doc:
186 -git set-file-times
187 $(MAKE) doc
188 find doc/images -type f | \
189 TZ=UTC xargs touch -d '1970-01-01 00:00:02' doc/rdoc.css
190 $(MAKE) doc_gz
191 chmod 644 $$(find doc -type f)
192 $(RSYNC) -av doc/ unicorn.bogomips.org:/srv/unicorn/
193 git ls-files | xargs touch
195 # Create gzip variants of the same timestamp as the original so nginx
196 # "gzip_static on" can serve the gzipped versions directly.
197 doc_gz: docs = $(shell find doc -type f ! -regex '^.*\.\(gif\|jpg\|png\|gz\)$$')
198 doc_gz:
199 for i in $(docs); do \
200 gzip --rsyncable -9 < $$i > $$i.gz; touch -r $$i $$i.gz; done
202 rails_git_url = git://github.com/rails/rails.git
203 rails_git := vendor/rails.git
204 $(rails_git)/info/cloned-stamp:
205 git clone --mirror -q $(rails_git_url) $(rails_git)
206 > $@
208 $(rails_git)/info/v2.2.3-stamp: $(rails_git)/info/cloned-stamp
209 cd $(rails_git) && git fetch
210 cd $(rails_git) && git rev-parse --verify refs/tags/v2.2.3
211 > $@
213 rails_tests := $(addsuffix .r,$(addprefix $(T_r).,$(rails_vers)))
214 test-rails: $(rails_tests)
215 $(T_r).%.r: t = $(addsuffix $(log_suffix),$@)
216 $(T_r).%.r: rv = $(subst .r,,$(subst $(T_r).,,$@))
217 $(T_r).%.r: extra = ' 'v$(rv)
218 $(T_r).%.r: arg = $(T_r)
219 $(T_r).%.r: export PATH := $(test_prefix)/bin:$(PATH)
220 $(T_r).%.r: export RUBYLIB := $(test_prefix):$(test_prefix)/lib:$(MYLIBS)
221 $(T_r).%.r: export UNICORN_RAILS_TEST_VERSION = $(rv)
222 $(T_r).%.r: export RAILS_GIT_REPO = $(CURDIR)/$(rails_git)
223 $(T_r).%.r: $(test_prefix)/.stamp $(rails_git)/info/v2.2.3-stamp
224 $(run_test)
226 ifneq ($(VERSION),)
227 rfproject := mongrel
228 rfpackage := unicorn
229 pkggem := pkg/$(rfpackage)-$(VERSION).gem
230 pkgtgz := pkg/$(rfpackage)-$(VERSION).tgz
231 release_notes := release_notes-$(VERSION)
232 release_changes := release_changes-$(VERSION)
234 release-notes: $(release_notes)
235 release-changes: $(release_changes)
236 $(release_changes):
237 wrongdoc release_changes > $@+
238 $(VISUAL) $@+ && test -s $@+ && mv $@+ $@
239 $(release_notes):
240 wrongdoc release_notes > $@+
241 $(VISUAL) $@+ && test -s $@+ && mv $@+ $@
243 # ensures we're actually on the tagged $(VERSION), only used for release
244 verify:
245 test x"$(shell umask)" = x0022
246 git rev-parse --verify refs/tags/v$(VERSION)^{}
247 git diff-index --quiet HEAD^0
248 test `git rev-parse --verify HEAD^0` = \
249 `git rev-parse --verify refs/tags/v$(VERSION)^{}`
251 fix-perms:
252 git ls-tree -r HEAD | awk '/^100644 / {print $$NF}' | xargs chmod 644
253 git ls-tree -r HEAD | awk '/^100755 / {print $$NF}' | xargs chmod 755
255 gem: $(pkggem)
257 install-gem: $(pkggem)
258 gem install $(CURDIR)/$<
260 $(pkggem): .manifest fix-perms
261 gem build $(rfpackage).gemspec
262 mkdir -p pkg
263 mv $(@F) $@
265 $(pkgtgz): distdir = $(basename $@)
266 $(pkgtgz): HEAD = v$(VERSION)
267 $(pkgtgz): .manifest fix-perms
268 @test -n "$(distdir)"
269 $(RM) -r $(distdir)
270 mkdir -p $(distdir)
271 tar cf - $$(cat .manifest) | (cd $(distdir) && tar xf -)
272 cd pkg && tar cf - $(basename $(@F)) | gzip -9 > $(@F)+
273 mv $@+ $@
275 package: $(pkgtgz) $(pkggem)
277 release: verify package $(release_notes) $(release_changes)
278 # make tgz release on RubyForge
279 rubyforge add_release -f -n $(release_notes) -a $(release_changes) \
280 $(rfproject) $(rfpackage) $(VERSION) $(pkgtgz)
281 # push gem to Gemcutter
282 gem push $(pkggem)
283 # in case of gem downloads from RubyForge releases page
284 -rubyforge add_file \
285 $(rfproject) $(rfpackage) $(VERSION) $(pkggem)
286 $(RAKE) raa_update VERSION=$(VERSION)
287 $(RAKE) fm_update VERSION=$(VERSION)
288 else
289 gem install-gem: GIT-VERSION-FILE
290 $(MAKE) $@ VERSION=$(GIT_VERSION)
291 endif
293 .PHONY: .FORCE-GIT-VERSION-FILE doc $(T) $(slow_tests) man
294 .PHONY: test-install