cc_harness 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!perl
  2. use Config;
  3. use ExtUtils::Embed;
  4. my $coredir = $ENV{PERL_SRC} || "$Config{archlib}/CORE"; # XXX was installarchlib
  5. my $libdir = "$Config{prefix}/lib";
  6. my $useshrplib = $Config{useshrplib} eq 'true';
  7. my $libs = $Config{libs};
  8. my $so = $Config{so};
  9. my ($linkargs, $quiet, $debug);
  10. if (grep {$_ eq '-q' or $_ eq '--quiet'} @ARGV) {
  11. $quiet++;
  12. @ARGV = grep {$_ ne '-q' and $_ ne '--quiet'} @ARGV;
  13. }
  14. if (grep {$_ eq '-d' or $_ eq '--debug'} @ARGV) {
  15. $debug++;
  16. @ARGV = grep {$_ ne '-d' and $_ ne '--debug'} @ARGV;
  17. }
  18. eval { require B::C::Flags; };
  19. if (grep(/^-[cES]$/, @ARGV)) { # compile-only with -c -E or -S
  20. ;
  21. } elsif (grep(/^-Bdynamic$/, @ARGV)) { # force dynamic linking with -Bdynamic
  22. @ARGV = grep{ !/^-Bdynamic$/o } @ARGV;
  23. $linkargs = ldopts;
  24. } elsif (grep(/^-Bstatic$/, @ARGV)) { # force static linking with -Bstatic
  25. @ARGV = grep{ !/^-Bstatic$/o } @ARGV;
  26. $linkargs = ldopts("-std");
  27. for my $lib ("$libdir/libperl.a", "$coredir/libperl.a") {
  28. if (-e $lib) {
  29. $linkargs =~ s|-lperl |$lib |;
  30. push @ARGV, ("$coredir/DynaLoader.o") if -e "$coredir/DynaLoader.o";
  31. #$linkargs .= " $coredir/Win32CORE.o" if $^O eq 'cygwin' and -e "$coredir/Win32CORE.o";
  32. last;
  33. }
  34. }
  35. } elsif (-e "$coredir/$Config{libperl}" and $Config{libperl} !~ /\.$so$/) {
  36. $linkargs = ldopts("-std"); # importlib or static
  37. } elsif ( $useshrplib and -e "$libdir/$Config{libperl}") {
  38. # debian: /usr/lib/libperl.so.5.10.1 and broken ExtUtils::Embed::ldopts
  39. $linkargs = ldopts('-std');
  40. $linkargs =~ s|-lperl |$libdir/$Config{libperl} |;
  41. } elsif ( $useshrplib and -e "$coredir/$Config{libperl}") {
  42. # just help cygwin debugging
  43. $linkargs = ldopts('-std');
  44. $linkargs =~ s|-lperl |$coredir/$Config{libperl} |;
  45. } else { # try dynamic lib if no static lib exists
  46. @ARGV = grep{ !/^-Bdynamic$/o } @ARGV;
  47. $linkargs = ldopts('-std');
  48. }
  49. # Note (probably harmless): No library found for -lnsl
  50. $linkargs = $B::C::Flags::extra_libs . " " . $linkargs;
  51. $linkargs .= " $libs" if index($linkargs,$libs) == -1;
  52. sub cc_harness_msvc {
  53. my @ARGV = @_;
  54. my $obj = "${Output}.obj";
  55. my $compile = ccopts." -c -Fo$obj @ARGV ";
  56. my $link = "-out:$Output $obj ";
  57. if ($debug) {
  58. $compile .= "/Wall " if !$quiet;
  59. # remove conflicting flags
  60. $compile .= "-g ";
  61. $compile =~ s/ -O.? / -Od /;
  62. $compile =~ s/ -DNDEBUG / -DDEBUG /;
  63. }
  64. $compile .= "-DHAVE_INDEPENDENT_COMALLOC " if $B::C::Flags::have_independent_comalloc;
  65. $compile .= $B::C::Flags::extra_cflags;
  66. $compile .= " -I".$_ for split /\s+/, opt(I);
  67. $link .= " -libpath:".$_ for split /\s+/, opt(L);
  68. # TODO: -shared,-static,-sharedxs,-staticxs
  69. $link .= (" ".$B::C::Flags::extra_libs);
  70. if ($stash) {
  71. my @mods = split /-?u /, $stash;
  72. $link .= " ".ldopts("-std", \@mods);
  73. } else {
  74. $link .= " ".ldopts("-std");
  75. }
  76. if ($debug) {
  77. $link .= " /DEBUG";
  78. }
  79. $link .= " perl5$Config{PERL_VERSION}.lib kernel32.lib msvcrt.lib";
  80. print "running $Config{cc} $compile" unless $quiet;
  81. system("$Config{cc} $compile");
  82. print "running $Config{ld} $link" unless $quiet;
  83. system("$Config{ld} $link");
  84. }
  85. if ($^O =~ m/^MSWin/ && $Config{cc} =~ m/^cl/i) {
  86. cc_harness_msvc(@ARGV);
  87. exit;
  88. }
  89. # ActivePerl 5.10.0.1004 claims to use MSVC6 but used MSVC8
  90. #if ($Config::Config{ccversion} eq '12.0.8804' and $Config::Config{cc} eq 'cl') {
  91. # $linkargs =~ s/ -opt:ref,icf//;
  92. #}
  93. my $ccflags = $Config{ccflags};
  94. # crashes on cygwin
  95. if ($^O eq 'cygwin' and $ccflags =~ /-fstack-protector\b/ and $linkargs =~ /-fstack-protector\b/) {
  96. $linkargs =~ s/-fstack-protector\b//;
  97. }
  98. #-pedantic -Wextra -Wconversion
  99. if ($debug and $Config{cc} =~ /gcc/) {
  100. $ccflags .= " -ansi -Wall -Wshadow -Wcast-qual -Wwrite-strings"
  101. if !$quiet;
  102. # remove conflicting flags, esp. -s for strip
  103. $ccflags =~ s/ -O.? / /;
  104. $ccflags =~ s/ -s / /;
  105. $linkargs =~ s/-s / /;
  106. }
  107. $ccflags .= " --no-warn -Wl,--warn-once"
  108. if $Config{cc} =~ /gcc/ and $quiet and $^O ne 'darwin';
  109. $ccflags .= " -DHAVE_INDEPENDENT_COMALLOC" if $B::C::Flags::have_independent_comalloc;
  110. $ccflags .= $B::C::Flags::extra_cflags;
  111. my $cccmd = "$Config{cc} $ccflags -I$coredir @ARGV $linkargs";
  112. print "$cccmd\n" unless $quiet;
  113. exec $cccmd;