Makefile.PL 16 KB

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