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