GNUmakefile: allow "make V=1 ..." for verbosity
[unicorn.git] / GNUmakefile
blob5ef370d763a29e727cfd0b663fdae33bb34f1de2
1 # use GNU Make to run tests in parallel, and without depending on Rubygems
2 all:: test
3 ruby = ruby
4 -include local.mk
5 ruby_bin := $(shell which $(ruby))
6 ifeq ($(DLEXT),) # "so" for Linux
7 DLEXT := $(shell $(ruby) -rrbconfig -e 'puts Config::CONFIG["DLEXT"]')
8 endif
9 ifeq ($(RUBY_VERSION),)
10 RUBY_VERSION := $(shell $(ruby) -e 'puts RUBY_VERSION')
11 endif
13 # dunno how to implement this as concisely in Ruby, and hell, I love awk
14 awk_slow := awk '/def test_/{print FILENAME"--"$$2".n"}' 2>/dev/null
16 rails_vers := $(subst test/rails/app-,,$(wildcard test/rails/app-*))
17 slow_tests := test/unit/test_server.rb test/exec/test_exec.rb
18 log_suffix = .$(RUBY_VERSION).log
19 T_r := $(wildcard test/rails/test*.rb)
20 T := $(filter-out $(slow_tests) $(T_r), $(wildcard test/*/test*.rb))
21 T_n := $(shell $(awk_slow) $(slow_tests))
22 T_log := $(subst .rb,$(log_suffix),$(T))
23 T_n_log := $(subst .n,$(log_suffix),$(T_n))
24 T_r_log := $(subst .r,$(log_suffix),$(T_r))
25 test_prefix = $(CURDIR)/test/install-$(RUBY_VERSION)
27 http11_deps := $(addprefix ext/unicorn/http11/, \
28 ext_help.h http11.c http11_parser.c http11_parser.h \
29 http11_parser.rl http11_parser_common.rl)
30 inst_deps := $(wildcard bin/*) $(wildcard lib/*.rb) \
31 $(wildcard lib/*/*.rb) $(http11_deps)
33 ext/unicorn/http11/http11_parser.c: ext/unicorn/http11/http11_parser.rl
34 cd $(@D) && ragel $(<F) -C -G2 -o $(@F)
35 ext/unicorn/http11/Makefile: ext/unicorn/http11/extconf.rb
36 cd $(@D) && $(ruby) $(<F)
37 ext/unicorn/http11/http11.$(DLEXT): $(http11_deps) ext/unicorn/http11/Makefile
38 $(MAKE) -C $(@D)
39 lib/unicorn/http11.$(DLEXT): ext/unicorn/http11/http11.$(DLEXT)
40 @mkdir -p lib
41 install -m644 $< $@
42 http11: lib/unicorn/http11.$(DLEXT)
44 $(test_prefix)/.stamp: $(inst_deps)
45 $(MAKE) clean-http11
46 $(MAKE) install-test
47 > $@
49 install-test:
50 mkdir -p $(test_prefix)/.ccache
51 tar c bin ext lib GNUmakefile | (cd $(test_prefix) && tar x)
52 $(MAKE) -C $(test_prefix) http11 shebang
54 # this is only intended to be run within $(test_prefix)
55 shebang: bin/unicorn bin/unicorn_rails
56 $(ruby) -i -p -e '$$_.gsub!(%r{^#!.*$$},"#!$(ruby_bin)")' $^
58 t_log := $(T_log) $(T_n_log)
59 test: $(T) $(T_n)
60 @cat $(t_log) | $(ruby) test/aggregate.rb
61 @$(RM) $(t_log)
63 test-exec: $(wildcard test/exec/test_*.rb)
64 test-unit: $(wildcard test/unit/test_*.rb)
65 $(slow_tests):
66 @$(MAKE) $(shell $(awk_slow) $@)
68 TEST_OPTS = -v
69 TEST_OPTS = -v
70 ifndef V
71 quiet_pre = @echo '* $(arg)$(extra)';
72 quiet_post = >$(t) 2>&1
73 else
74 # we can't rely on -o pipefail outside of bash 3+,
75 # so we use a stamp file to indicate success and
76 # have rm fail if the stamp didn't get created
77 stamp = $@$(log_suffix).ok
78 quiet_pre = @echo $(ruby) $(arg) $(TEST_OPTS); ! test -f $(stamp) && (
79 quiet_post = && > $(stamp) )>&2 | tee $(t); rm $(stamp) 2>/dev/null
80 endif
81 run_test = $(quiet_pre) setsid $(ruby) $(arg) $(TEST_OPTS) $(quiet_post) || \
82 (sed "s,^,$(extra): ," >&2 < $(t); exit 1)
84 %.n: arg = $(subst .n,,$(subst --, -n ,$@))
85 %.n: t = $(subst .n,$(log_suffix),$@)
86 %.n: export PATH := $(test_prefix)/bin:$(PATH)
87 %.n: export RUBYLIB := $(test_prefix)/lib:$(RUBYLIB)
88 %.n: $(test_prefix)/.stamp
89 $(run_test)
91 $(T): arg = $@
92 $(T): t = $(subst .rb,$(log_suffix),$@)
93 $(T): export PATH := $(test_prefix)/bin:$(PATH)
94 $(T): export RUBYLIB := $(test_prefix)/lib:$(RUBYLIB)
95 $(T): $(test_prefix)/.stamp
96 $(run_test)
98 install: bin/unicorn bin/unicorn_rails
99 $(prep_setup_rb)
100 $(RM) -r .install-tmp
101 mkdir .install-tmp
102 cp -p $^ .install-tmp
103 $(ruby) setup.rb all
104 $(RM) $^
105 mv $(addprefix .install-tmp/,$(^F)) bin/
106 $(RM) -r .install-tmp
107 $(prep_setup_rb)
109 clean-http11:
110 -$(MAKE) -C ext/unicorn/http11 clean
111 $(RM) ext/unicorn/http11/Makefile lib/unicorn/http11.$(DLEXT)
113 setup_rb_files := .config InstalledFiles
114 prep_setup_rb := @-$(RM) $(setup_rb_files);$(MAKE) -C ext/unicorn/http11 clean
116 clean: clean-http11
117 $(RM) $(setup_rb_files)
118 $(RM) $(t_log)
119 $(RM) -r $(test_prefix)
121 Manifest:
122 git ls-files > $@+
123 cmp $@+ $@ || mv $@+ $@
124 $(RM) -f $@+
126 # using rdoc 2.4.1
127 doc: .document
128 rdoc -Na -m README -t "$(shell sed -ne '1s/^= //p' README)"
130 rails_git_url = git://github.com/rails/rails.git
131 rails_git := vendor/rails.git
132 $(rails_git)/info/cloned-stamp:
133 git clone --mirror -q $(rails_git_url) $(rails_git)
134 > $@
136 rails_tests := $(addsuffix .r,$(addprefix $(T_r).,$(rails_vers)))
137 test-rails: $(rails_tests)
138 $(T_r).%.r: t = $(addsuffix $(log_suffix),$@)
139 $(T_r).%.r: rv = $(subst .r,,$(subst $(T_r).,,$@))
140 $(T_r).%.r: extra = ' 'v$(rv)
141 $(T_r).%.r: arg = $(T_r)
142 $(T_r).%.r: export PATH := $(test_prefix)/bin:$(PATH)
143 $(T_r).%.r: export RUBYLIB := $(test_prefix)/lib:$(RUBYLIB)
144 $(T_r).%.r: export UNICORN_RAILS_TEST_VERSION = $(rv)
145 $(T_r).%.r: export RAILS_GIT_REPO = $(CURDIR)/$(rails_git)
146 $(T_r).%.r: $(test_prefix)/.stamp $(rails_git)/info/cloned-stamp
147 $(run_test)
149 .PHONY: doc $(T) $(slow_tests) Manifest