cc_harness 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!perl
  2. use Config;
  3. use ExtUtils::Embed;
  4. my ($coredir, $libdir);
  5. if ($ENV{PERL_CORE}) {
  6. $libdir = $coredir = $^O eq 'MSWin32'? '../../lib/CORE' : '../..';
  7. } else {
  8. $libdir = $Config{prefix} . "/lib";
  9. $coredir = $ENV{PERL_SRC} || $Config{archlib}."/CORE";
  10. }
  11. my $useshrplib = $Config{useshrplib} =~ /^(true|yes)$/;
  12. my $libs = $Config{libs};
  13. my $so = $Config{so};
  14. my $libperl = $Config{libperl};
  15. my ($linkargs, $quiet, $debug);
  16. my $pkg = ($Config{usecperl} and $libperl =~ /libcperl/) ? "cperl" : "perl";
  17. if (grep {$_ eq '-q' or $_ eq '--quiet'} @ARGV) {
  18. $quiet++;
  19. @ARGV = grep {$_ ne '-q' and $_ ne '--quiet'} @ARGV;
  20. }
  21. if (grep {$_ eq '-d' or $_ eq '--debug'} @ARGV) {
  22. $debug++;
  23. @ARGV = grep {$_ ne '-d' and $_ ne '--debug'} @ARGV;
  24. }
  25. if (grep {$_ eq '-A'} @ARGV) {
  26. @ARGV = grep {$_ ne '-A'} @ARGV;
  27. push @ARGV, '-DALLOW_PERL_OPTIONS';
  28. }
  29. eval { require B::C::Config; };
  30. if (grep(/^-[cES]$/, @ARGV)) { # compile-only with -c -E or -S
  31. ;
  32. } elsif (grep(/^-Bdynamic$/, @ARGV)) { # force dynamic linking with -Bdynamic
  33. @ARGV = grep{ !/^-Bdynamic$/o } @ARGV;
  34. $linkargs = ldopts;
  35. } elsif (grep(/^-Bstatic$/, @ARGV)) { # force static linking with -Bstatic
  36. @ARGV = grep{ !/^-Bstatic$/o } @ARGV;
  37. $linkargs = ldopts("-std");
  38. for my $lib ("$libdir/lib$pkg.a", "$coredir/lib$pkg.a") {
  39. if (-e $lib) {
  40. $linkargs =~ s|-l$pkg |$lib |;
  41. push @ARGV, ("$coredir/DynaLoader.o") if -e "$coredir/DynaLoader.o";
  42. #$linkargs .= " $coredir/Win32CORE.o" if $^O eq 'cygwin' and -e "$coredir/Win32CORE.o";
  43. last;
  44. }
  45. }
  46. } elsif (-e "$coredir/$libperl" and $libperl !~ /\.$so$/) {
  47. $linkargs = ldopts("-std"); # importlib or static
  48. } elsif ( $useshrplib and -e "$libdir/$libperl") {
  49. # debian: /usr/lib/libperl.so.5.10.1 and broken ExtUtils::Embed::ldopts
  50. $linkargs = ldopts('-std');
  51. $linkargs =~ s|-lc?perl |$libdir/$libperl |;
  52. } elsif ( $useshrplib and -e "$coredir/$libperl") {
  53. # just help cygwin debugging
  54. $linkargs = ldopts('-std');
  55. $linkargs =~ s|-lc?perl |$coredir/$libperl |;
  56. } else { # try dynamic lib if no static lib exists
  57. @ARGV = grep{ !/^-Bdynamic$/o } @ARGV;
  58. $linkargs = ldopts('-std');
  59. }
  60. # Note (probably harmless): No library found for -lnsl
  61. # another ldopts bug: ensure Win32CORE gets added.
  62. if ($^O =~ /^(cygwin|MSWin32|msys)/) {
  63. if (index($linkargs, "Win32CORE") < 0) {
  64. my $archdir = $ENV{PERL_CORE} ? "../.." : $Config{archlib};
  65. my $win32core = "-L$archdir/lib/auto/Win32CORE -lWin32CORE";
  66. if (-e "$archdir/lib/auto/Win32CORE/Win32CORE.a") {
  67. $win32core = "$archdir/lib/auto/Win32CORE/Win32CORE.a";
  68. }
  69. if ($linkargs =~ m{ (-lc?perl|\S*/$libperl)}) {
  70. $linkargs =~ s{ (-lc?perl|\S*/$libperl)}{ $win32core $1};
  71. } else {
  72. $linkargs .= " $win32core";
  73. }
  74. }
  75. }
  76. $linkargs .= " -L".$coredir if $ENV{PERL_CORE};
  77. $linkargs .= " -l$pkg" unless $linkargs =~ /( -lc?perl|CORE\/libc?perl)/;
  78. $linkargs = $B::C::Config::extra_libs . " " . $linkargs;
  79. $linkargs .= " $libs" if index($linkargs,$libs) == -1;
  80. sub cc_harness_msvc {
  81. my @ARGV = @_;
  82. my $cfile = 'a.c';
  83. my $Output;
  84. for my $i (0 .. @ARGV) {
  85. if ($ARGV[$i] eq '-o') {
  86. $Output = $ARGV[$i+1];
  87. }
  88. }
  89. my @cfile = grep {!/^-/} @ARGV;
  90. if (@cfile == 1) {
  91. $cfile = $cfile[0];
  92. } elsif (@cfile > 1) {
  93. die "too many arguments @cfile";
  94. }
  95. my $obj = $cfile;
  96. $obj =~ s/\.c$/.obj/;
  97. unless ($Output) {
  98. $Output = $cfile;
  99. $Output =~ s/\.c$/.exe/;
  100. }
  101. my $compile = ccopts()." -c @ARGV ";
  102. if ($debug) {
  103. $compile .= "/Wall " if !$quiet;
  104. # remove conflicting flags
  105. $compile .= "-g ";
  106. $compile =~ s/ -O.? / -Od /;
  107. $compile =~ s/ -DNDEBUG / -DDEBUG /;
  108. } else {
  109. # avoid expensive optimisations with low memory (1-2 hours!)
  110. $compile .= " -Od" if $ENV{APPVEYOR} and $^O eq 'MSWin32';
  111. if ($ENV{PERL_CORE} and $Config{ccflags} =~ /-flto/) { # too big
  112. $compile =~ s/-O\d / -O1 /;
  113. }
  114. }
  115. $compile .= "-DHAVE_INDEPENDENT_COMALLOC " if $B::C::Config::have_independent_comalloc;
  116. $compile .= $B::C::Config::extra_cflags;
  117. $compile = ' -I"..\..\lib\CORE"'.$compile if $ENV{PERL_CORE};
  118. print "running $Config{cc} $compile" unless $quiet;
  119. system("$Config{cc} $compile");
  120. my $link = "-out:$Output $obj ";
  121. $link .= " -libpath:".$coredir if $ENV{PERL_CORE};
  122. # TODO: -shared,-static,-sharedxs,-staticxs
  123. $link .= (" ".$B::C::Config::extra_libs);
  124. if ($stash) {
  125. my @mods = split /-?u /, $stash;
  126. $link .= " ".ldopts("-std", \@mods);
  127. } else {
  128. $link .= " ".ldopts("-std");
  129. }
  130. $link .= " ".$Config{optimize};
  131. if ($debug) {
  132. $link .= " /DEBUG";
  133. } else {
  134. # avoid expensive folding with low memory
  135. $link .= " /OPT:NOREF" if $ENV{APPVEYOR} and $^O eq 'MSWin32';
  136. }
  137. if (index($link, "Win32CORE") < 0) {
  138. my $archdir = $ENV{PERL_CORE} ? "../.." : $Config{archlib};
  139. my $win32core = "-L$archdir/lib/auto/Win32CORE -lWin32CORE";
  140. if (-e "$archdir/lib/auto/Win32CORE/Win32CORE.lib") {
  141. $win32core = "$archdir/lib/auto/Win32CORE/Win32CORE.lib";
  142. }
  143. $link .= " $win32core";
  144. }
  145. $link .= " ".($Config{usecperl}?"c":"")."perl5$Config{PERL_VERSION}.lib";
  146. $link .= " kernel32.lib msvcrt.lib";
  147. print "running $Config{ld} $link" unless $quiet;
  148. system("$Config{ld} $link");
  149. }
  150. if ($^O eq 'MSWin32' && $Config{cc} =~ 'cl') {
  151. cc_harness_msvc(@ARGV);
  152. exit;
  153. }
  154. # ActivePerl 5.10.0.1004 claims to use MSVC6 but used MSVC8
  155. #if ($Config::Config{ccversion} eq '12.0.8804' and $Config::Config{cc} eq 'cl') {
  156. # $linkargs =~ s/ -opt:ref,icf//;
  157. #}
  158. my $ccflags = $Config{optimize}." ".$Config{ccflags};
  159. # crashes on cygwin
  160. if ($^O eq 'cygwin' and $ccflags =~ /-fstack-protector /
  161. and $linkargs =~ /-fstack-protector /)
  162. {
  163. $linkargs =~ s/-fstack-protector //;
  164. }
  165. #-pedantic -Wextra -Wconversion
  166. if ($debug and $Config{cc} =~ /\bgcc/ and $Config{gccversion}) {
  167. $ccflags .= " -ansi -Wall -Wshadow -Wcast-qual -Wwrite-strings"
  168. if !$quiet;
  169. # remove conflicting flags, esp. -s for strip
  170. $ccflags =~ s/ -O.? / /;
  171. $ccflags =~ s/ -s / /;
  172. $linkargs =~ s/-s / /;
  173. }
  174. # -Wl,--warn-once is not gold compatible
  175. $ccflags .= " --no-warn"
  176. if $Config{cc} =~ /\bgcc/ and $Config{gccversion} and $quiet and $^O ne 'darwin';
  177. $ccflags .= " -DHAVE_INDEPENDENT_COMALLOC" if $B::C::Config::have_independent_comalloc;
  178. $ccflags .= $B::C::Config::extra_cflags;
  179. my $cccmd = "$Config{cc} $ccflags -I$coredir @ARGV $linkargs";
  180. print "$cccmd\n" unless $quiet;
  181. exec $cccmd;