1 |
use 5.005; |
2 |
|
3 |
use strict; |
4 |
use ExtUtils::MakeMaker; |
5 |
use Config; |
6 |
|
7 |
$|=1; |
8 |
|
9 |
my $DEFINE; |
10 |
my @LIBS = []; |
11 |
|
12 |
# check for completely broken platforms (such as netbsd) |
13 |
if ($Config{usethreads} && $^O eq "netbsd") { |
14 |
print <<EOF; |
15 |
|
16 |
*** |
17 |
*** Your platform is BROKEN - netbsd pthreads are known to be completely broken. |
18 |
*** There is *no* way to implement coroutines on this platform until the bugs |
19 |
*** are fixed (this has been true for at least netbsd version 4.0). |
20 |
*** |
21 |
*** This affects many modules currently, so the recommended build option |
22 |
*** for perl is to DISABLE THREAD support - it serves no practical purpose |
23 |
*** on POSIX systems anyways, except that it slows down your programs a lot |
24 |
*** and uses a lot more memory than fork (perl threads do not give you threads, |
25 |
*** they are only windows-process emulation ported to unix). |
26 |
*** |
27 |
*** If you really want to try building Coro on your broken platform, enter |
28 |
*** "yes" at the prompt - report back if the bugs is fixed please. (Oh, |
29 |
*** and try out BDB or IO::AIO which suffer from other threading bugs on |
30 |
*** netbsd that never seem to get fixed). |
31 |
*** |
32 |
|
33 |
EOF |
34 |
|
35 |
print "prompt> "; <STDIN> =~ /^yes/ or exit 1; |
36 |
} |
37 |
|
38 |
print <<EOF; |
39 |
|
40 |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
41 |
|
42 |
Coro has a number of configuration options. Due to its maturity, the |
43 |
defaults that Coro chooses are usually fine, so you can decide to skip |
44 |
these questions. Only if something went wrong you should select 'n' |
45 |
here and manually configure Coro, and, of course, report this to the |
46 |
maintainer :) |
47 |
|
48 |
EOF |
49 |
|
50 |
if (prompt ("Skip further questions and use defaults (y/n)?", "y") =~ /[yY]/) { |
51 |
$ENV{PERL_MM_USE_DEFAULT} = 1; |
52 |
} |
53 |
|
54 |
|
55 |
$DEFINE .= " -DHAVE_MMAP" if $Config{d_mmap} eq "define" && $Config{d_munmap} eq "define"; |
56 |
|
57 |
my $iface; |
58 |
|
59 |
if (exists $ENV{CORO_INTERFACE}) { |
60 |
$iface = $ENV{CORO_INTERFACE}; |
61 |
|
62 |
} elsif ($^O =~ /win32/i or $^O =~ /cygwin/ or $^O =~ /mswin/) { |
63 |
$iface = 'w'; |
64 |
|
65 |
} elsif ($^O =~ /irix/) { |
66 |
$iface = "i"; |
67 |
|
68 |
} elsif ($^O =~ /linux/) { |
69 |
# default to assembly on x86 and x86_64, and setjmp on others |
70 |
$iface = $Config{archname} =~ /^(i[3456]86|amd64|x86_64)-/ ? "a" : "s"; |
71 |
|
72 |
} elsif ($^O =~ /(free|net|open)bsd/) { |
73 |
# FreeBSD 4.x has ucontext.h but no makecontext et al. (see BUGS section of |
74 |
# man context). Assume the same problem for all other BSDs. |
75 |
|
76 |
# netbsd is totally broken (pthreads are incomaptible with ucontext or other stack switchign mechanisms) |
77 |
|
78 |
# therefore, default to pthread - hey, it might actually work |
79 |
$iface = "p"; |
80 |
|
81 |
} elsif ($^O =~ /solaris/) { |
82 |
$iface = "s"; |
83 |
|
84 |
} elsif ($^O =~ /darwin/) { |
85 |
$iface = "s"; |
86 |
|
87 |
} elsif ($^O =~ /dragonfly/) { |
88 |
# ucontext is totally broken on dragonfly bsd: |
89 |
# Fatal error 'siglongjmp()ing between thread contexts is undefined by POSIX 1003.1 |
90 |
$iface = "s"; |
91 |
|
92 |
} elsif (-e "/usr/include/ucontext.h") { # shame on this heuristic |
93 |
$iface = "u"; |
94 |
|
95 |
} else { |
96 |
$iface = "s"; |
97 |
} |
98 |
|
99 |
print <<EOF; |
100 |
|
101 |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
102 |
|
103 |
Coro can use a number of methods to implement coroutines at the C |
104 |
level. The default chosen is based on your current confguration and is |
105 |
correct in most cases, but you still can chose between these alternatives: |
106 |
|
107 |
u The unix 'ucontext.h' functions are relatively new and not implemented |
108 |
or well-tested in older unices. They allow very fast coroutine creation |
109 |
and reasonably fast switching. They are, however, usually slower than |
110 |
the other alternatives due to an extra syscall done by swapcontext. And |
111 |
while nominally most portable (it's the only POSIX-standardised |
112 |
interface for coroutines), ucontext functions are, as usual, broken on |
113 |
most/all BSDs. |
114 |
|
115 |
s If the ucontext functions are not working or you don't want |
116 |
to use them for other reasons you can try a workaround using |
117 |
setjmp/longjmp/sigaltstack (also standard unix functions). Coroutine |
118 |
creation is rather slow, but switching is very fast (often much faster |
119 |
than with the ucontext functions). Unfortunately, glibc-2.1 and |
120 |
below don't even feature a working sigaltstack. You cannot use this |
121 |
implementation if some other code uses SIGUSR2 or you plan to create |
122 |
coroutines from an alternative signal stack, as both are being used for |
123 |
coroutine creation. |
124 |
|
125 |
a Handcoded assembly. This is the fastest and most compatible method, |
126 |
with the least side effects, if it works, that is. It has been tested |
127 |
on GNU/Linux x86 and x86_64 systems and should work on all x86/x86_64 |
128 |
systems using the SVR ELF ABI (it is also reported to be working on |
129 |
Strawberry Perl for Windows using MinGW). This is the recommended |
130 |
method on supported platforms. When it doesn't work, use another |
131 |
method, such as (s)etjmp/longjmp. |
132 |
|
133 |
l GNU/Linux. Very old GNU/Linux systems (glibc-2.1 and below) need |
134 |
this hack. Since it is very linux-specific it is also quite fast and |
135 |
recommended even for newer versions; when it works, that is (currently |
136 |
x86 and a few others only. If it compiles, it's usually ok). Newer |
137 |
glibc versions (>= 2.5) stop working with this implementation however. |
138 |
|
139 |
i IRIX. For some reason, SGI really does not like to follow POSIX (does |
140 |
that surprise you?), so this workaround might be needed (it's fast), |
141 |
although [s] and [u] should also work now. |
142 |
|
143 |
w Microsoft Windows. Try this on Microsoft Windows when using Cygwin or |
144 |
the MSVC compilers (e.g. ActiveState Perl, but see "a" for Strawberry |
145 |
Perl), although, as there is no standard on how to do this under |
146 |
windows, different environments might work differently. Doh. |
147 |
|
148 |
p Use pthread API. Try to avoid this option, it was only created to make |
149 |
a point about the programming language shootout. It is likely the |
150 |
slowest method of implementing coroutines. It might work fine as a last |
151 |
resort, however, as the pthread API is slightly better tested than |
152 |
ucontext functions for example. Of course, not on BSDs, who usually |
153 |
have very broken pthread implementations. |
154 |
|
155 |
Coro tries hard to come up with a suitable default for most systems, |
156 |
so pressing return at the prompt usually does the right thing. If you |
157 |
experience problems (e.g. make test fails) then you should experiment with |
158 |
this setting. |
159 |
|
160 |
EOF |
161 |
|
162 |
retry: |
163 |
|
164 |
my $r = prompt "Use which implementation,\n" . |
165 |
"<s>et/longjump, <u>context, <a>ssembly, <i>rix, <l>inux or <w>indows?", |
166 |
$iface; |
167 |
$iface = lc $1 if $r =~ /(\S)/; |
168 |
|
169 |
if ($iface eq "u") { |
170 |
$DEFINE .= " -DCORO_UCONTEXT"; |
171 |
print "\nUsing ucontext implementation\n\n"; |
172 |
conftest ("TEST_makecontext"); |
173 |
} elsif ($iface eq "s") { |
174 |
$DEFINE .= " -DCORO_SJLJ"; |
175 |
print "\nUsing setjmp/longjmp/sigaltstack implementation\n\n"; |
176 |
conftest ("TEST_sigaltstack"); |
177 |
} elsif ($iface eq "l") { |
178 |
$DEFINE .= " -DCORO_LINUX"; |
179 |
print "\nUsing linux-specific implementation\n\n"; |
180 |
} elsif ($iface eq "i") { |
181 |
$DEFINE .= " -DCORO_IRIX"; |
182 |
print "\nUsing irix-specific implementation\n\n"; |
183 |
} elsif ($iface eq "w") { |
184 |
$DEFINE .= " -DCORO_LOSER"; |
185 |
print "\nUsing windows-specific implementation\n\n"; |
186 |
} elsif ($iface eq "a") { |
187 |
$DEFINE .= " -DCORO_ASM"; |
188 |
print "\nUsing handcoded assembly implementation\n\n"; |
189 |
} elsif ($iface eq "p") { |
190 |
$DEFINE .= " -DCORO_PTHREAD"; |
191 |
@LIBS = ["-lpthread"]; |
192 |
print "\nUsing pthread implementation\n\n"; |
193 |
} else { |
194 |
print "\nUnknown implementation \"$iface\"\n"; |
195 |
goto retry; |
196 |
} |
197 |
|
198 |
print <<EOF; |
199 |
|
200 |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
201 |
|
202 |
Per-context stack size factor: Depending on your settings, Coro tries to |
203 |
share the C stacks is creates as much as possible, but sometimes it needs |
204 |
to allocate a new one. This setting controls the maximum size that gets |
205 |
allocated, and should not be set too high, as memory and address space |
206 |
still is wasted even if it's not fully used. The value entered will be |
207 |
multiplied by sizeof(long), which is usually 4 on 32-bit systems, and 8 on |
208 |
64-bit systems. |
209 |
|
210 |
A setting of 16384 (the default) therefore corresponds to a 64k..128k |
211 |
stack, which usually is ample space (you might even want to try 8192 or |
212 |
lower if your program creates many coroutines). |
213 |
|
214 |
On systems supporting mmap and dynamic memory management, the actual |
215 |
memory usually gets allocated on demand, but with many large stacks you |
216 |
can still run out of address space on your typical 32 bit platform (not to |
217 |
forget the pagetables). |
218 |
|
219 |
Some perls (mostly threaded ones and perl compiled under linux 2.6) and |
220 |
some programs (inefficient regexes can use a lot of stack space) may |
221 |
need much, much more: If Coro segfaults with weird backtraces (e.g. in a |
222 |
function prologue) or in t/10_bugs.t, you might want to increase this to |
223 |
65536 or more. |
224 |
|
225 |
The default should be fine, and can be changed at runtime with |
226 |
Coro::State::cctx_stacksize. |
227 |
|
228 |
EOF |
229 |
|
230 |
my $stacksize = prompt ("C stack size factor?", "16384"); |
231 |
$DEFINE .= " -DCORO_STACKSIZE=$stacksize"; |
232 |
|
233 |
print "using a stacksize of $stacksize * sizeof(long)\n"; |
234 |
|
235 |
print <<EOF; |
236 |
|
237 |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
238 |
|
239 |
Coro can optionally put a guard area before each stack segment: When the |
240 |
stack is too small and the access is not too far outside the stack (i.e. |
241 |
within the guard area), then the program will safely segfault instead of |
242 |
running into other data. The cost is some additional overhead with is |
243 |
usually negligible, and extra use of address space. |
244 |
|
245 |
The guard area size currently needs to be specified in pages (typical |
246 |
pagesizes are 4k and 8k). The guard area is only enabled on a few |
247 |
hardcoded architectures and is ignored on others. The actual preprocessor |
248 |
expression disables this feature if: |
249 |
|
250 |
!__i386 && !__x86_64 && !__powerpc && !__m68k \ |
251 |
&& !__alpha && !__mips && !__sparc64 |
252 |
|
253 |
The default, as usual, should be just fine. |
254 |
|
255 |
EOF |
256 |
|
257 |
my $stackguard = prompt ("Number of guard pages (0 disables)?", "4"); |
258 |
$DEFINE .= " -DCORO_STACKGUARD=$stackguard"; |
259 |
|
260 |
print <<EOF; |
261 |
|
262 |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
263 |
|
264 |
Coro can tell valgrind about its stacks and so reduce spurious warnings |
265 |
where valgrind would otherwise complain about possible stack switches. |
266 |
|
267 |
Enabling this does not incur noticable runtime or memory overhead, but it |
268 |
requires that you have the <valgrind/valgrind.h> header file available. |
269 |
|
270 |
Valgrind support is completely optional, so disabling it is the safe |
271 |
choice. |
272 |
|
273 |
EOF |
274 |
|
275 |
my $valgrind = prompt ("Enable valgrind support (y/n)?", |
276 |
-r "/usr/include/valgrind/valgrind.h" ? "y" : "n"); |
277 |
$DEFINE .= " -DCORO_USE_VALGRIND=1" if $valgrind =~ /[yY]/; |
278 |
|
279 |
|
280 |
print <<EOF; |
281 |
|
282 |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
283 |
|
284 |
Coro can use (or even trick) some perl functions into doing what it needs |
285 |
instead of relying on (some) of its own functions. This might increase |
286 |
chances that it compiles and works, but it could just as well result in |
287 |
memory leaks, crashes or silent data corruption. It certainly does result |
288 |
in slightly slower speed and higher memory consumption, though, so YOU |
289 |
SHOULD ENABLE THIS OPTION ONLY AS A LAST RESORT. |
290 |
|
291 |
EOF |
292 |
|
293 |
my $use_internals = prompt ("Prefer perl functions over coro functions (y/n)?", "n"); |
294 |
$DEFINE .= " -DCORO_PREFER_PERL_FUNCTIONS=1" if $use_internals =~ /[yY]/; |
295 |
|
296 |
print <<EOF; |
297 |
|
298 |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
299 |
|
300 |
EOF |
301 |
|
302 |
WriteMakefile( |
303 |
NAME => "Coro::State", |
304 |
VERSION_FROM => "State.pm", |
305 |
DEFINE => $DEFINE, |
306 |
LIBS => @LIBS, |
307 |
DIR => [], |
308 |
depend => { |
309 |
"State.c" => "libcoro/coro.h libcoro/coro.c", |
310 |
}, |
311 |
); |
312 |
|
313 |
sub conftest { |
314 |
my $type = shift; |
315 |
|
316 |
print "\nTrying to detect stack growth direction (for $type)\n"; |
317 |
print "You might see some warnings, this should not concern you.\n\n"; |
318 |
system "$Config{cc} $Config{ccflags} -D$type libcoro/conftest.c"; |
319 |
|
320 |
my $res = qx<./a.out>; |
321 |
$res =~ s/\s+$//; |
322 |
my ($sp, $ss) = split /,/, $res; |
323 |
|
324 |
print "\n\n*****************************************************************************\n"; |
325 |
print "If the testsuite fails PLEASE provide the following information\n"; |
326 |
print "to Marc Lehmann <schmorp\@schmorp.de>: operating system name, version,\n"; |
327 |
print "architecture name and this string '$sp|$ss'. Thanks a lot!\n";#d# |
328 |
print "*****************************************************************************\n\n"; |
329 |
|
330 |
unlink "a.out"; |
331 |
unlink "conftestval"; |
332 |
} |
333 |
|