Makefile.PL 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. use ExtUtils::MakeMaker;
  2. use Config;
  3. use File::Spec;
  4. use 5.006;
  5. use Carp;
  6. my $core = grep { $_ eq 'PERL_CORE=1' } @ARGV;
  7. mkdir "lib/B/C" unless -d "lib/B/C";
  8. unless (-e 'lib/B/C/Flags.pm') {
  9. open PH, ">", "lib/B/C/Flags.pm";
  10. print PH "package B::C::Flags;\n\n";
  11. print PH "\n1;\n";
  12. close PH;
  13. chmod 0644, "lib/B/C/Flags.pm";
  14. }
  15. # generate lib/B/Asmdata.pm beforehand
  16. # my $X = $^X =~ / / ? qq("$^X") : $^X;
  17. if ($core) {
  18. system($^X,"-I../../lib/auto","-I../../lib","bytecode.pl");
  19. } else {
  20. system($^X, "bytecode.pl");
  21. }
  22. my ($use_declare_independent_comalloc, $extra_cflags, $extra_libs) = (0, "", "");
  23. my $have_independent_comalloc = check_independent_comalloc();
  24. if (grep { $_ eq 'INSTALL_PTMALLOC3' } @ARGV) {
  25. install_independent_comalloc() ;
  26. $have_independent_comalloc = check_independent_comalloc();
  27. }
  28. {
  29. my ($e_c) = grep { /-extra-cflags[= ](.+)/ } @ARGV;
  30. my ($e_l) = grep { /-extra-libs[= ](.+)/ } @ARGV;
  31. $extra_cflags .= " $e_c" if $e_c;
  32. $extra_libs .= " $e_l" if $e_l;
  33. }
  34. # cygwin still has the old gdb-6 debugger which does not understand dwarf4 features from gcc-4.5
  35. #if ($Config{gccversion} =~ /^4\.[56]\./) {
  36. # my $gdb_ver = `gdb --version`;
  37. # if ($gdb_ver =~ /gdb 6\./) {
  38. # print "Adding extra_cflags=-gstrict-dwarf for gcc-4.5 for a gdb-6 debugger which does not understand dwarf4 features\n";
  39. # $extra_cflags .= " -gstrict-dwarf";
  40. # }
  41. #}
  42. sub write_b_c_flags {
  43. my $version = shift;
  44. mkdir "lib/B/C" unless -d "lib/B/C";
  45. open PH, ">", "lib/B/C/Flags.pm";
  46. print PH "# written by B::C Makefile.PL. \$extra_{cflags,libs} need a leading space if used.\n";
  47. print PH "package B::C::Flags;\n\n";
  48. my $devnull = $^O eq 'MSWin32' ? 'NUL' : '/dev/null';
  49. print PH "\$VERSION = '$version';\n";
  50. my $REV = '';
  51. if (my $sha1 = `git rev-list HEAD -1 --abbrev=7 --abbrev-commit 2>$devnull`) {
  52. chomp $sha1;
  53. # POSIX systems only with wc
  54. if (my $num = `git rev-list --abbrev-commit HEAD | wc -l 2>$devnull`) {
  55. chomp $num;
  56. $REV .= "-$num";
  57. }
  58. $REV .= '-g'.$sha1;
  59. print PH "\$B::C::REVISION = '$REV';\n\n";
  60. }
  61. print PH "# -fav-init optimization\n";
  62. print PH "\$have_independent_comalloc = $have_independent_comalloc;\n";
  63. print PH "\$use_declare_independent_comalloc = $use_declare_independent_comalloc;\n\n";
  64. print PH "# use extra compiler flags, after ccopts, resp. ldopts\n";
  65. print PH "\$extra_cflags = \"$extra_cflags\";\n";
  66. print PH "\$extra_libs = \"$extra_libs\";\n";
  67. print PH "\n1;\n";
  68. close PH;
  69. chmod 0644, "lib/B/C/Flags.pm";
  70. }
  71. # XXX Check for 5.16.0 B-1.34 and offer to patch it? rather use `perlall --patches=Compiler`
  72. if ($] > 5.015005 and $] < 5.019004) {
  73. # We do not want to support binary stashes. safesyscalls with 5.019004 should be enabled
  74. warn "Warning: Bad perl version $].\n".
  75. " Binary package and symbol names are considered a security risc and not fully supported.\n".
  76. " Check your code to strip \\0 from package names.\n";
  77. } elsif ($] >= 5.019004) {
  78. warn "Warning:\n".
  79. " Binary package and symbol names are considered a security risc and only partially supported.\n".
  80. " Check your code to strip \\0 from package names and enable use warnings 'syscalls'.\n";
  81. }
  82. WriteMakefile(
  83. NAME => "B::C",
  84. VERSION_FROM => "lib/B/C.pm",
  85. PL_FILES => { 'script/perlcc.PL' => 'script/perlcc' },
  86. EXE_FILES => [qw(script/perlcc script/cc_harness script/assemble script/disassemble)],
  87. PREREQ_PM => {'Opcodes' => '0', # optional
  88. 'IPC::Run' => '0', # optional
  89. 'B::Flags' => '0.04', # optional
  90. 'Time::HiRes' => '0', # optional
  91. 'ExtUtils::Embed' => '1.25' # mandatory (missing on redhat)
  92. #'B' => '1.0901' # required but in CORE
  93. },
  94. 'AUTHOR' => 'Malcolm Beattie (retired), '
  95. . 'Reini Urban <perl-compiler@googlegroups.com>',
  96. 'ABSTRACT' => 'Perl compiler',
  97. 'LICENSE' => 'perl',
  98. (($ExtUtils::MakeMaker::VERSION gt '6.31' and $ExtUtils::MakeMaker::VERSION lt '6.46') ?
  99. ('EXTRA_META' => "recommends:\n" .
  100. " B::Flags: 0.04\n".
  101. " B::Debug: 1.16\n".
  102. " Opcodes: 0.10\n".
  103. " IPC::Run: 0\n",
  104. ) : ()),
  105. ($ExtUtils::MakeMaker::VERSION gt '6.46' ?
  106. ('META_MERGE' => {"recommends" =>
  107. {
  108. 'B::Flags' => '0.04',
  109. "B::Debug" => '1.16',
  110. "Opcodes" => '0.10',
  111. "IPC::Run" => 0,
  112. },
  113. resources =>
  114. {
  115. license => 'http://dev.perl.org/licenses/',
  116. homepage => 'http://www.perl-compiler.org',
  117. bugtracker => 'http://code.google.com/p/perl-compiler/issues',
  118. repository => 'http://perl-compiler.googlecode.com/',
  119. MailingList => 'http://groups.google.com/group/perl-compiler',
  120. },
  121. }
  122. ) : ()),
  123. SIGN => 1,
  124. clean => { FILES =>
  125. "bytecode[0-9]* ".
  126. "lib/B/Asmdata.pm script/perlcc ccode* cccode* Ccode* ".
  127. "*.core *.stackdump a.out a.exe *.cee *.c *.asm *.dbg *.plc *.obj ".
  128. "*.concise *~ dll.base dll.exp mod.pl pcc* *.bak *.a"
  129. },
  130. );
  131. sub headerpath {
  132. if ($core) {
  133. return File::Spec->catdir(File::Spec->updir,
  134. File::Spec->updir);
  135. } else {
  136. return File::Spec->catdir($Config::Config{archlibexp}, "CORE");
  137. }
  138. }
  139. # Check for Doug Lea's dlmalloc version, or ptmalloc2 included in glibc
  140. # or the best: ptmalloc3 with independent_comalloc().
  141. # http://www.malloc.de/malloc/ptmalloc3-current.tar.gz
  142. # This improves -fav-init startup speed dramatically (18% tested).
  143. # ptmalloc3 needs #include <malloc-2.8.3.h>, but we don't want to clash
  144. # with an existing malloc.h from perl.h, so we declare it by ourselves.
  145. sub try_compile {
  146. my $testc = shift;
  147. my $libs = shift;
  148. # For consistency (without considering LD_PRELOAD) require perl to
  149. # be compiled with the same malloc library.
  150. return 0 unless $Config{libs} =~ /\b\Q$libs\E\b/;
  151. unless (open PROG, ">", "test.c") {
  152. print ("Can't write test.c\n");
  153. return 0;
  154. }
  155. print PROG $testc;
  156. close PROG;
  157. @candidate = ();
  158. $devnull = $^O eq 'MSWin32' ? "> NUL" : ">/dev/null 2>&1";
  159. my $cmd = "$Config{cc} $Config{ccflags} test.c";
  160. push @candidate, "$cmd -o test$Config{EXE_EXT} $libs $devnull";
  161. push @candidate, "$cmd -otest$Config{EXE_EXT} $libs $devnull";
  162. while (my $cmd1 = shift (@candidate)) {
  163. system ($cmd1);
  164. unlink "test.c", "test$Config{EXE_EXT}";
  165. $? == 0 && return 1;
  166. }
  167. return 0;
  168. }
  169. sub check_independent_comalloc {
  170. my $testori = "
  171. #include <stdlib.h>
  172. #include <malloc.h>
  173. int main() {
  174. void* chunks[3];
  175. size_t sizes[3] = {3,25,4};
  176. if (independent_comalloc( 3, sizes, chunks ) == 0) { exit(1); };
  177. return 0;
  178. }
  179. ";
  180. my $testc = $testori;
  181. if (try_compile($testc)) {
  182. warn "-fav-init2 available: independent_comalloc() as-is activated\n";
  183. return 1;
  184. }
  185. my @extra_libs = ("-lptmalloc3", "-lptmalloc", "-ldlmalloc", "-lnedmalloc");
  186. for my $lib (@extra_libs) {
  187. $lib =~ s/^-l(.+)$/lib$1.lib/ if $^O eq 'MSWin32';
  188. if (try_compile($testc, $lib)) {
  189. $extra_libs = " $lib";
  190. warn "-fav-init2 available: independent_comalloc() with $lib activated\n";
  191. return 1;
  192. }
  193. }
  194. # try without the ptmalloc3 header, just the library
  195. $testc =~ s/#include <malloc>/void** dlindependent_comalloc(size_t, size_t*, void**);/;
  196. for (@extra_libs) {
  197. my $lib = $_;
  198. $lib = 'libptmalloc3.lib' if $^O eq 'MSWin32';
  199. $lib =~ s/^-l(.+)$/lib$1.lib/ if $^O eq 'MSWin32';
  200. if (try_compile($testc, $lib)) {
  201. $extra_libs = " $lib";
  202. $use_declare_independent_comalloc = 1;
  203. warn "-fav-init2 available: dlindependent_comalloc() with $lib activated\n";
  204. return 1;
  205. }
  206. }
  207. # Desperate. External ptmalloc3 header to overcome -I path shadowing
  208. $testc = $testori;
  209. $testc =~ s/#include <malloc.h>/#include "malloc-2.8.3.h"/;
  210. my $lib = "-lptmalloc3";
  211. $lib = 'libptmalloc3.lib' if $^O eq 'MSWin32';
  212. if (try_compile($testc, $lib)) {
  213. $extra_cflags = " -DNEED_MALLOC_283";
  214. $extra_libs = " $lib";
  215. warn "-fav-init2 available: independent_comalloc() with -DNEED_MALLOC_283 $lib activated\n";
  216. return 1;
  217. }
  218. #warn "-fav-init2 not available, independent_comalloc() not detected.\n";
  219. #warn " Install on POSIX systems with:\n";
  220. #warn " $^X Makefile.PL INSTALL_PTMALLOC3\n\n";
  221. return 0;
  222. }
  223. sub _system{
  224. print join(" ",@_),"\n";
  225. local $!;
  226. system @_;
  227. croak $! if $!;
  228. }
  229. sub install_independent_comalloc {
  230. print "INSTALL_PTMALLOC3 from <http://www.malloc.de/en/index.html>\n";
  231. -f 'ptmalloc3-current.tar.gz'
  232. or _system(qw(wget http://www.malloc.de/malloc/ptmalloc3-current.tar.gz));
  233. -d 'ptmalloc3'
  234. or _system('tar xfz ptmalloc3-current.tar.gz');
  235. chdir "ptmalloc3" or die;
  236. if ($Config{useithreads}) {
  237. # linux-pthread is basically the same. linux-shared is also an option.
  238. # just Solaris, SGI and HPUX need different options.
  239. _system(qw(make posix));
  240. } else {
  241. _system(qw(make nothreads));
  242. }
  243. _system('make check');
  244. _system('sudo cp libptmalloc3.a /usr/lib/');
  245. warn "No you must recompile your perl with linking to this library\n";
  246. chdir "..";
  247. }
  248. package MY;
  249. # Ignore certain files
  250. sub libscan {
  251. # Ignore temp testing files
  252. return 0 if $_[1] =~ /^(\.git|\.svn|jit.*|i386|.*\.orig|bytecode.*\.pl|c?ccode.*|regen_lib\.pl)$/;
  253. return 0 if $_[1] =~ /\.svn|~/; # needed for msys perl5.6
  254. # Ignore Bytecode on 5.6 for now. The 5.6 CORE module produces better code (until fixed :)
  255. # Not even the Byteloader works for 5.6 assembled code. The Disassembler does not stop at ret.
  256. return 0 if $] < 5.007 and $_[1] =~ /ByteLoader|Asmdata\.pm|Bytecode\.pm|Assembler\.pm/;
  257. # On windows the C compiler would work if DynaLoader.lib would be provided.
  258. # return 0 if $^O eq 'MSWin32' and !-d ".git" and $_[1] =~ /C\.pm|C\.xs|Stackobj\.pm/;
  259. return $_[1];
  260. }
  261. # Fix ActivePerl for MSVC6
  262. # The linker for cl 12.0.8804 has no -opt:ref,icf, which is MSVC8 linker syntax.
  263. sub const_config {
  264. my $s = shift->SUPER::const_config(@_);
  265. if ($Config::Config{ccversion} eq '12.0.8804' and $Config::Config{cc} eq 'cl') {
  266. $s =~ s/ -opt:ref,icf//gm;
  267. }
  268. $s
  269. }
  270. sub post_constants {
  271. my $mm = shift;
  272. main::write_b_c_flags($mm->{VERSION});
  273. my $libs = "\nLIBS = $Config::Config{libs}";
  274. $libs .= $extra_libs if $extra_libs;
  275. #XXX PERL_MALLOC_DEF = -DPERL_EXTMALLOC_DEF -Dmalloc=Perl_malloc -Dfree=Perl_mfree -Drealloc=Perl_realloc -Dcalloc=Perl_calloc
  276. return "$libs\n";
  277. }
  278. sub ccflags {
  279. my $ccflags = shift->SUPER::ccflags(@_);
  280. $ccflags .= " -DHAVE_INDEPENDENT_COMALLOC" if $have_independent_comalloc;
  281. $ccflags .= $extra_cflags if $extra_cflags;
  282. return $ccflags if !-d ".git" or $ENV{NO_AUTHOR};
  283. # Recommended by http://www.network-theory.co.uk/docs/gccintro/gccintro_32.html
  284. # -ansi -pedantic -Wall -W -Wconversion -Wshadow -Wcast-qual -Wwrite-strings (-W => -WExtra)
  285. $ccflags .= " -ansi -pedantic -Wall -Wextra -Wconversion -Wshadow -Wcast-qual -Wwrite-strings"
  286. if $Config{cc} =~ /gcc/;
  287. }
  288. sub depend {
  289. my $headerpath = main::headerpath();
  290. my @headers = map { File::Spec->catfile($headerpath, $_) } qw(op.h cop.h sv.h);
  291. my $asmdata = File::Spec->catfile('lib', 'B', 'Asmdata.pm');
  292. my $byterun_c = File::Spec->catfile('ByteLoader', 'byterun.c');
  293. my $byterun_h = File::Spec->catfile('ByteLoader', 'byterun.h');
  294. my $cc_runtime_h = File::Spec->catfile($headerpath, 'cc_runtime.h');
  295. my $result = "
  296. $asmdata : Makefile bytecode.pl @headers
  297. \$(PERL) bytecode.pl
  298. $byterun_c : Makefile bytecode.pl @headers
  299. \$(PERL) bytecode.pl
  300. $byterun_h : Makefile bytecode.pl @headers
  301. \$(PERL) bytecode.pl
  302. TAGS : $asmdata
  303. etags --language=perl lib/B/*.pm
  304. ";
  305. if ($] > 5.009) {
  306. $result .= "\ntest :: subdirs-test\n\n";
  307. }
  308. if ($Config{make} eq 'mingw32-make') { # mingw32 make different to msys make
  309. $result .= "\n.PHONY : \$(CONFIGDEP)\n\n";
  310. }
  311. $result;
  312. }
  313. sub test {
  314. shift->SUPER::test(@_) . q(
  315. testmod :: pure_all
  316. PERL_DL_NONLAZY=1 $(FULLPERLRUN) -Iblib/arch -Iblib/lib t/modules.t
  317. testmodall :: pure_all
  318. PERL_DL_NONLAZY=1 $(FULLPERLRUN) -Iblib/arch -Iblib/lib t/modules.t -no-subset -no-date t/top100
  319. testc :: pure_all
  320. PERL_DL_NONLAZY=1 $(FULLPERLRUN) -Iblib/arch -Iblib/lib t/c.t
  321. PERL_DL_NONLAZY=1 $(FULLPERLRUN) -Iblib/arch -Iblib/lib t/c_o3.t
  322. testcc :: pure_all
  323. PERL_DL_NONLAZY=1 $(FULLPERLRUN) -Iblib/arch -Iblib/lib t/cc.t
  324. testbc :: pure_all
  325. PERL_DL_NONLAZY=1 $(FULLPERLRUN) -Iblib/arch -Iblib/lib t/bytecode.t
  326. testfast :: pure_all
  327. NO_AUTHOR=1 $(FULLPERLRUN) -S prove -b -j10
  328. testcritical :: pure_all
  329. t/critical.sh
  330. teststatus :: pure_all
  331. ./status_upd -fqd
  332. testrelease :: pure_all
  333. $(ECHO) run t/release-testing.sh and perlall testvm --all
  334. )
  335. }
  336. sub install {
  337. my $result = shift->SUPER::install(@_);
  338. if ($] > 5.013007 and !-e File::Spec->catfile($headerpath, 'cc_runtime.h')) {
  339. $result =~ s/install :: pure_install doc_install/install :: pure_install doc_install ccinc_install/;
  340. $result .= '
  341. ccinc_install :: $(PERL_INC)/cc_runtime.h
  342. $(PERL_INC)/cc_runtime.h : cc_runtime.h
  343. $(NOECHO) $(MOD_INSTALL) \
  344. read $(PERL_ARCHLIB)/auto/$(FULLEXT)/.packlist \
  345. write $(DESTINSTALLARCHLIB)/auto/$(FULLEXT)/.packlist \
  346. cc_runtime.h $@
  347. ';
  348. }
  349. $result
  350. }
  351. # Local Variables:
  352. # mode: cperl
  353. # cperl-indent-level: 4
  354. # fill-column: 100
  355. # End:
  356. # vim: expandtab shiftwidth=4: