cc_harness 4.1 KB

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