t/lib.perl: fix Perl integration tests w/o installation
[unicorn.git] / GNUmakefile
blob70e7e1087fe986b5cfc9d8e1057da162128018d6
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
11 OLDDOC = olddoc
12 RDOC = rdoc
13 INSTALL = install
14 PROVE = prove
16 GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE
17 @./GIT-VERSION-GEN
18 -include GIT-VERSION-FILE
19 -include local.mk
20 ruby_bin := $(shell which $(RUBY))
21 ifeq ($(DLEXT),) # "so" for Linux
22 DLEXT := $(shell $(RUBY) -rrbconfig -e 'puts RbConfig::CONFIG["DLEXT"]')
23 endif
24 ifeq ($(RUBY_VERSION),)
25 RUBY_VERSION := $(shell $(RUBY) -e 'puts RUBY_VERSION')
26 endif
28 RUBY_ENGINE := $(shell $(RUBY) -e 'puts((RUBY_ENGINE rescue "ruby"))')
30 # we should never package more than one ext to avoid DSO proliferation:
31 # https://udrepper.livejournal.com/8790.html
32 ext := $(firstword $(wildcard ext/*))
34 ragel: $(ext)/unicorn_http.c
36 rl_files := $(wildcard $(ext)/*.rl)
37 ragel: $(ext)/unicorn_http.c
38 $(ext)/unicorn_http.c: $(rl_files)
39 cd $(@D) && $(RAGEL) unicorn_http.rl -C $(RLFLAGS) -o $(@F)
40 ext_pfx := test/$(RUBY_ENGINE)-$(RUBY_VERSION)
41 tmp_bin := $(ext_pfx)/bin
42 ext_h := $(wildcard $(ext)/*/*.h $(ext)/*.h)
43 ext_src := $(sort $(wildcard $(ext)/*.c) $(ext_h) $(ext)/unicorn_http.c)
44 ext_pfx_src := $(addprefix $(ext_pfx)/,$(ext_src))
45 ext_dir := $(ext_pfx)/$(ext)
46 $(ext)/extconf.rb:
47 @>>$@
48 $(ext_dir) $(tmp_bin) man/man1 doc/man1 pkg t/trash:
49 @mkdir -p $@
50 $(ext_pfx)/$(ext)/%: $(ext)/% | $(ext_dir)
51 $(INSTALL) -m 644 $< $@
52 $(ext_pfx)/$(ext)/Makefile: $(ext)/extconf.rb | $(ext_dir)
53 $(RM) -f $(@D)/*.o
54 cd $(@D) && $(RUBY) $(CURDIR)/$(ext)/extconf.rb $(EXTCONF_ARGS)
55 ext_sfx := _ext.$(DLEXT)
56 ext_dl := $(ext_pfx)/$(ext)/$(notdir $(ext)_ext.$(DLEXT))
57 $(ext_dl): $(ext_src) $(ext_pfx_src) $(ext_pfx)/$(ext)/Makefile
58 $(MAKE) -C $(@D)
59 lib := $(CURDIR)/lib:$(CURDIR)/$(ext_pfx)/$(ext)
60 http build: $(ext_dl)
61 $(ext_pfx)/$(ext)/unicorn_http.c: ext/unicorn_http/unicorn_http.c
63 # dunno how to implement this as concisely in Ruby, and hell, I love awk
64 awk_slow := awk '/def test_/{print FILENAME"--"$$2".n"}' 2>/dev/null
66 slow_tests := test/unit/test_server.rb test/exec/test_exec.rb \
67 test/unit/test_signals.rb test/unit/test_upload.rb
68 log_suffix = .$(RUBY_ENGINE).$(RUBY_VERSION).log
69 T := $(filter-out $(slow_tests), $(wildcard test/*/test*.rb))
70 T_n := $(shell $(awk_slow) $(slow_tests))
71 T_log := $(subst .rb,$(log_suffix),$(T))
72 T_n_log := $(subst .n,$(log_suffix),$(T_n))
74 base_bins := unicorn unicorn_rails
75 bins := $(addprefix bin/, $(base_bins))
76 man1_rdoc := $(addsuffix _1, $(base_bins))
77 man1_bins := $(addsuffix .1, $(base_bins))
78 man1_paths := $(addprefix man/man1/, $(man1_bins))
79 tmp_bins = $(addprefix $(tmp_bin)/, unicorn unicorn_rails)
80 pid := $(shell echo $$PPID)
82 $(tmp_bin)/%: bin/% | $(tmp_bin)
83 $(INSTALL) -m 755 $< $@.$(pid)
84 $(MRI) -i -p -e '$$_.gsub!(%r{^#!.*$$},"#!$(ruby_bin)")' $@.$(pid)
85 mv $@.$(pid) $@
87 bins: $(tmp_bins)
89 t_log := $(T_log) $(T_n_log)
90 test: $(T) $(T_n) test-prove
91 @cat $(t_log) | $(MRI) test/aggregate.rb
92 @$(RM) $(t_log)
94 test-exec: $(wildcard test/exec/test_*.rb)
95 test-unit: $(wildcard test/unit/test_*.rb)
96 $(slow_tests): $(ext_dl)
97 @$(MAKE) $(shell $(awk_slow) $@)
99 # ensure we can require just the HTTP parser without the rest of unicorn
100 test-require: $(ext_dl)
101 $(RUBY) --disable-gems -I$(ext_pfx)/$(ext) -runicorn_http -e Unicorn
103 test_prereq := $(tmp_bins) $(ext_dl)
105 SH_TEST_OPTS =
106 ifdef V
107 ifeq ($(V),2)
108 SH_TEST_OPTS += --trace
109 else
110 SH_TEST_OPTS += --verbose
111 endif
112 endif
114 # do we trust Ruby behavior to be stable? some tests are
115 # (mostly) POSIX sh (not bash or ksh93, so no "set -o pipefail"
116 # TRACER = strace -f -o $(t_pfx).strace -s 100000
117 # TRACER = /usr/bin/time -o $(t_pfx).time
118 t_pfx = trash/$@-$(RUBY_ENGINE)-$(RUBY_VERSION)
119 T_sh = $(wildcard t/t[0-9][0-9][0-9][0-9]-*.sh)
120 $(T_sh): export RUBY := $(RUBY)
121 $(T_sh): export PATH := $(CURDIR)/$(tmp_bin):$(PATH)
122 $(T_sh): export RUBYLIB := $(lib):$(RUBYLIB)
123 $(T_sh): dep $(test_prereq) t/random_blob t/trash/.gitignore
124 cd t && $(TRACER) $(SHELL) $(SH_TEST_OPTS) $(@F) $(TEST_OPTS)
126 t/trash/.gitignore : | t/trash
127 echo '*' >$@
129 dependencies := curl
130 deps := $(addprefix t/.dep+,$(dependencies))
131 $(deps): dep_bin = $(lastword $(subst +, ,$@))
132 $(deps):
133 @which $(dep_bin) > $@.$(pid) 2>/dev/null || :
134 @test -s $@.$(pid) || \
135 { echo >&2 "E '$(dep_bin)' not found in PATH=$(PATH)"; exit 1; }
136 @mv $@.$(pid) $@
137 dep: $(deps)
139 t/random_blob:
140 dd if=/dev/urandom bs=1M count=30 of=$@.$(pid)
141 mv $@.$(pid) $@
143 test-integration: $(T_sh)
145 test-prove: t/random_blob
146 $(PROVE) -vw
148 check: test-require test test-integration
149 test-all: check
151 TEST_OPTS = -v
152 check_test = grep '0 failures, 0 errors' $(t) >/dev/null
153 ifndef V
154 quiet_pre = @echo '* $(arg)$(extra)';
155 quiet_post = >$(t) 2>&1 && $(check_test)
156 else
157 # we can't rely on -o pipefail outside of bash 3+,
158 # so we use a stamp file to indicate success and
159 # have rm fail if the stamp didn't get created
160 stamp = $@$(log_suffix).ok
161 quiet_pre = @echo $(RUBY) $(arg) $(TEST_OPTS); ! test -f $(stamp) && (
162 quiet_post = && > $(stamp) )2>&1 | tee $(t); \
163 rm $(stamp) 2>/dev/null && $(check_test)
164 endif
166 # not all systems have setsid(8), we need it because we spam signals
167 # stupidly in some tests...
168 rb_setsid := $(RUBY) -e 'Process.setsid' -e 'exec *ARGV'
170 # TRACER='strace -f -o $(t).strace -s 100000'
171 run_test = $(quiet_pre) \
172 $(rb_setsid) $(TRACER) $(RUBY) -w $(arg) $(TEST_OPTS) $(quiet_post) || \
173 (sed "s,^,$(extra): ," >&2 < $(t); exit 1)
175 %.n: arg = $(subst .n,,$(subst --, -n ,$@))
176 %.n: t = $(subst .n,$(log_suffix),$@)
177 %.n: export PATH := $(CURDIR)/$(tmp_bin):$(PATH)
178 %.n: export RUBYLIB := $(lib):$(RUBYLIB)
179 %.n: $(test_prereq)
180 $(run_test)
182 $(T): arg = $@
183 $(T): t = $(subst .rb,$(log_suffix),$@)
184 $(T): export PATH := $(CURDIR)/$(tmp_bin):$(PATH)
185 $(T): export RUBYLIB := $(lib):$(RUBYLIB)
186 $(T): $(test_prereq)
187 $(run_test)
189 install: $(bins) $(ext)/unicorn_http.c
190 $(prep_setup_rb)
191 $(RM) -r .install-tmp
192 mkdir .install-tmp
193 cp -p bin/* .install-tmp
194 $(RUBY) setup.rb all
195 $(RM) $^
196 mv .install-tmp/* bin/
197 $(RM) -r .install-tmp
198 $(prep_setup_rb)
200 setup_rb_files := .config InstalledFiles
201 prep_setup_rb := @-$(RM) $(setup_rb_files);$(MAKE) -C $(ext) clean
203 clean:
204 -$(MAKE) -C $(ext) clean
205 $(RM) $(ext)/Makefile
206 $(RM) $(setup_rb_files) $(t_log)
207 $(RM) -r $(ext_pfx) man t/trash
208 $(RM) $(html1)
210 man1 := $(addprefix Documentation/, unicorn.1 unicorn_rails.1)
211 html1 := $(addsuffix .html, $(man1))
212 man : $(man1) | man/man1
213 $(INSTALL) -m 644 $(man1) man/man1
215 html : $(html1) | doc/man1
216 $(INSTALL) -m 644 $(html1) doc/man1
218 %.1.html: %.1
219 $(OLDDOC) man2html -o $@ ./$<
221 pkg_extra := GIT-VERSION-FILE lib/unicorn/version.rb LATEST NEWS \
222 $(ext)/unicorn_http.c $(man1_paths)
224 NEWS:
225 $(OLDDOC) prepare
227 .manifest: $(ext)/unicorn_http.c man NEWS
228 (git ls-files && for i in $@ $(pkg_extra); do echo $$i; done) | \
229 LC_ALL=C sort > $@+
230 cmp $@+ $@ || mv $@+ $@
231 $(RM) $@+
233 PLACEHOLDERS = $(man1_rdoc)
234 doc: .document $(ext)/unicorn_http.c man html .olddoc.yml $(PLACEHOLDERS)
235 find bin lib -type f -name '*.rbc' -exec rm -f '{}' ';'
236 $(RM) -r doc
237 $(OLDDOC) prepare
238 $(RDOC) -f dark216
239 $(OLDDOC) merge
240 $(INSTALL) -m 644 COPYING doc/COPYING
241 $(INSTALL) -m 644 NEWS.atom.xml doc/NEWS.atom.xml
242 $(INSTALL) -m 644 $(shell LC_ALL=C grep '^[A-Z]' .document) doc/
243 $(INSTALL) -m 644 $(man1_paths) doc/
244 tar cf - $$(git ls-files examples/) | (cd doc && tar xf -)
246 # publishes docs to https://yhbt.net/unicorn/
247 publish_doc:
248 -git set-file-times
249 $(MAKE) doc
250 $(MAKE) doc_gz
251 chmod 644 $$(find doc -type f)
252 $(RSYNC) -av doc/ yhbt.net:/srv/yhbt/unicorn/ \
253 --exclude index.html* --exclude created.rid*
254 git ls-files | xargs touch
256 # Create gzip variants of the same timestamp as the original so nginx
257 # "gzip_static on" can serve the gzipped versions directly.
258 doc_gz: docs = $(shell find doc -type f ! -regex '^.*\.gz$$')
259 doc_gz:
260 for i in $(docs); do \
261 gzip --rsyncable -9 < $$i > $$i.gz; touch -r $$i $$i.gz; done
263 ifneq ($(VERSION),)
264 rfpackage := unicorn
265 pkggem := pkg/$(rfpackage)-$(VERSION).gem
266 pkgtgz := pkg/$(rfpackage)-$(VERSION).tgz
268 # ensures we're actually on the tagged $(VERSION), only used for release
269 verify:
270 test x"$(shell umask)" = x0022
271 git rev-parse --verify refs/tags/v$(VERSION)^{}
272 git diff-index --quiet HEAD^0
273 test `git rev-parse --verify HEAD^0` = \
274 `git rev-parse --verify refs/tags/v$(VERSION)^{}`
276 fix-perms:
277 git ls-tree -r HEAD | awk '/^100644 / {print $$NF}' | xargs chmod 644
278 git ls-tree -r HEAD | awk '/^100755 / {print $$NF}' | xargs chmod 755
280 gem: $(pkggem)
282 install-gem: $(pkggem)
283 gem install --local $(CURDIR)/$<
285 $(pkggem): .manifest fix-perms | pkg
286 gem build $(rfpackage).gemspec
287 mv $(@F) $@
289 $(pkgtgz): distdir = $(basename $@)
290 $(pkgtgz): HEAD = v$(VERSION)
291 $(pkgtgz): .manifest fix-perms
292 @test -n "$(distdir)"
293 $(RM) -r $(distdir)
294 mkdir -p $(distdir)
295 tar cf - $$(cat .manifest) | (cd $(distdir) && tar xf -)
296 cd pkg && tar cf - $(basename $(@F)) | gzip -9 > $(@F)+
297 mv $@+ $@
299 package: $(pkgtgz) $(pkggem)
301 release: verify package
302 # push gem to Gemcutter
303 gem push $(pkggem)
304 else
305 gem install-gem: GIT-VERSION-FILE
306 $(MAKE) $@ VERSION=$(GIT_VERSION)
307 endif
309 $(PLACEHOLDERS):
310 echo olddoc_placeholder > $@
312 check-warnings:
313 @(for i in $$(git ls-files '*.rb' bin | grep -v '^setup\.rb$$'); \
314 do $(RUBY) --disable-gems -d -W2 -c \
315 $$i; done) | grep -v '^Syntax OK$$' || :
317 .PHONY: .FORCE-GIT-VERSION-FILE doc $(T) $(slow_tests) man $(T_sh) clean