forked from openresty/openresty-devel-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx-build
executable file
·229 lines (174 loc) · 6.01 KB
/
ngx-build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env perl
use strict;
use warnings;
use File::Spec;
use Getopt::Std;
my %opts;
getopts("fp:", \%opts) or usage();
if (@ARGV == 0) {
usage();
}
my $force = $opts{f};
if ($force) {
shift;
}
my $patch = $opts{p};
if ($patch) {
$patch = File::Spec->rel2abs($patch);
}
my $version = shift or
usage();
if ($version !~ /^(\d+)\.(\d+)\.(\d+)$/) {
warn "bad nginx version number: $version\n";
usage();
}
my @ver = ($2, $2, $3);
my $ver = sprintf("%03d%03d%03d", $1, $2, $3);
my @opts;
if (($1 == 1 && $2 == 1 && $3 >= 4)
|| ($1 == 1 && $2 > 1)
|| ($1 > 1))
{
for my $arg (@ARGV) {
if ($arg =~ /^--add-module=.*?ngx_http_upstream_keepalive/) {
next;
}
push @opts, $arg;
}
} else {
@opts = @ARGV;
}
my $cc = $ENV{NGX_BUILD_CC};
my $optimize = $ENV{NGX_BUILD_OPT};
my $jobs = $ENV{NGX_BUILD_JOBS} || 1;
push @opts, '--with-poll_module';
my $found_cc_opt;
my @new_opts;
for my $opt (@opts) {
if (defined $cc) {
if ($opt =~ /^--with-cc=/) {
next;
}
}
if (defined $optimize) {
if ($opt =~ /^--with-cc-opt=/) {
if ($opt =~ s/-O\d?\b/$optimize/g) {
$found_cc_opt = 1;
} else {
$opt .= " $optimize";
}
$found_cc_opt = 1;
}
}
push @new_opts, $opt;
}
@opts = @new_opts;
if (defined $cc) {
push @opts, "--with-cc=$cc";
}
if (defined $optimize && !$found_cc_opt) {
push @opts, "--with-cc-opt=$optimize";
}
my $root = `pwd`;
chomp $root;
my $target = "$root/work/nginx";
my $home = `cd ~ && pwd`;
chomp $home;
my $build_root = "buildroot";
if (! -d $build_root) {
mkdir $build_root or
die "failed to create directory $build_root.\n";
}
chdir $build_root or
die "failed to switch to directory $build_root\n";
my $tarball = "nginx-$version.tar.gz";
if ($force || ! -f $tarball || ! -d "nginx-$version") {
if (!-d "$home/work") {
mkdir "$home/work" or
die "failed to create directory ~/work\n";
}
if (-f "$home/work/$tarball") {
shell("cp $home/work/$tarball ./");
} else {
shell("axel -n2 -o $home/work/$tarball http://nginx.org/download/$tarball");
shell("cp $home/work/$tarball ./");
}
shell("tar -xzvf $tarball");
apply_patches();
}
chdir "nginx-$version" or die "cannot switch to nginx-$version\n";
if ($force || ! -f "Makefile" || -M "$root/config" < -M "Makefile" ||
-M "$root/util/build.sh" < -M "Makefile")
{
configure_nginx();
}
if (-f "$target/logs/nginx.pid") {
system("kill -QUIT `cat $target/logs/nginx.pid") == 0 or
warn "failed to quit existing nginx.\n";
}
shell("make -j$jobs");
shell("make install");
sub configure_nginx {
system("./configure", "--prefix=$target",
@opts) == 0 or
die "failed to configure nginx.\n";
}
sub apply_patches {
if ($patch) {
shell("patch -p0 < $patch");
}
if ($ver lt '001000014' || ($ver ge '001001000' && $ver lt '001001017')) {
chdir "nginx-$version" or die "cannot switch to nginx-$version\n";
warn "applying the null char fixes security patch";
shell("patch -p0 -l < $root/../ngx_openresty/patches/nginx-$version-null_character_fixes.patch");
chdir "..";
}
unless ($ver ge '001001015') {
chdir "nginx-$version" or die "cannot switch to nginx-$version\n";
warn "applying subrequest cycle protection patch";
shell("patch -p1 -l < $root/../ngx_openresty/patches/nginx-$version-subrequest_loop.patch");
chdir "..";
}
warn "applying the upsteram_pipelining patch...\n";
shell("patch -p0 < $root/../ngx_openresty/patches/nginx-$version-upstream_pipelining.patch");
warn "applying no-pool patch...\n";
shell("patch -p0 < $root/../no-pool-nginx/nginx-$version-no_pool.patch");
warn "applying no-error-page patch...\n";
shell("patch -p0 < $root/../ngx_openresty/patches/nginx-$version-no_error_pages.patch");
warn "applying the max_subrequests patch...\n";
shell("patch -p0 < $root/../ngx_openresty/patches/nginx-$version-max_subrequests.patch");
unless ($ver ge '001001011' or ($ver ge '001000012' and $ver le '001001000')) {
shell("patch -p0 < $root/../ngx_openresty/patches/nginx-$version-variable_header_ignore_no_hash.patch");
}
shell("patch -p0 < $root/../ngx_openresty/patches/nginx-$version-reset_wev_handler_in_named_locations.patch");
shell("patch -p0 < $root/../ngx_openresty/patches/nginx-$version-filter_finalize_hang.patch");
shell("patch -p0 < $root/../ngx_openresty/patches/nginx-$version-add_core_vars_polluting_globals.patch");
unless ($ver ge '001001006') {
shell("patch -p0 < $root/../ngx_openresty/patches/nginx-$version-named_location_clear_mods_ctx.patch");
}
shell("patch -p0 < $root/../ngx_openresty/patches/nginx-$version-allow_request_body_updating.patch");
unless ($ver ge '001001009' or ($ver ge '001000012' and $ver le '001001000')) {
shell("patch -p0 < $root/../ngx_openresty/patches/nginx-$version-epoll_check_stale_wev.patch");
}
#shell("patch -p0 < $root/../ngx_openresty/patches/nginx-$version-gzip_ok_invalid_read_fix.patch");
unless ($ver ge '001000009') {
shell("patch -p0 < $root/../ngx_openresty/patches/nginx-$version-log_escape_non_ascii.patch");
}
unless ($ver ge '001000008') {
warn "applying request-body-preread patch...\n";
shell("patch -p0 < $root/../ngx_openresty/patches/nginx-$version-request_body_preread_fix.patch");
chdir "nginx-$version" or die "cannot switch to nginx-$version\n";
warn "applying request-body-in-single-buf patch...\n";
shell("patch -p1 -l < $root/../ngx_openresty/patches/nginx-$version-request_body_in_single_buf.patch");
chdir ".." or die "cannot switch to ..\n";
}
}
sub usage {
die "Usage: $0 <nginx-version> <options>\n";
}
sub shell {
my $cmd = shift;
warn "$cmd\n";
system($cmd) == 0 or
die "failed to run command \"$cmd\"\n";
}