ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Makefile.PL
Revision: 1.75
Committed: Sat Aug 22 22:36:23 2009 UTC (14 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-5_22, rel-5_2, rel-5_17
Changes since 1.74: +26 -4 lines
Log Message:
5.17

File Contents

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