Attempt to extract the refcounted_he definition
[p5sagit/Devel-Size.git] / Makefile.PL
1 #!/usr/bin/perl -w
2 use 5.005;
3 use ExtUtils::MakeMaker;
4 use File::Spec::Functions 'catfile';
5 use strict;
6
7 use Config;
8 (unpack "B*", pack "N", $Config{ptrsize}) =~ /^0+1(0+)$/
9     or die "Your pointer size of $Config{ptrsize} is very confusing";
10 my $ptr_bits = length $1;
11
12 write_header(
13     'refcounted_he.h' =>
14     extract_refcounted_he(catfile($Config{archlib}, 'CORE', 'hv.h'))
15 );
16
17 WriteMakefile(
18     OPTIMIZE => "-g",
19     NAME => 'Devel::Memory',
20     VERSION_FROM => 'lib/Devel/Memory.pm',
21     DEFINE => "-DALIGN_BITS=$ptr_bits",
22     PREREQ_PM => {
23         'Test::More' => 0,
24         'JSON::XS' => 0,
25         'HTML::Entities' => 0,
26         'Mojolicious::Lite' => 0,
27         'Devel::Dwarn' => 0,
28         XSLoader => 0,
29         ORLite => 0,
30     },
31     EXE_FILES => [ 'bin/dmemtree.pl' ],
32     clean => {
33         FILES => 'refcounted_he.h',
34     },
35     (eval $ExtUtils::MakeMaker::VERSION >= 6.47 ? (MIN_PERL_VERSION => '5.005') : ()),
36     (eval $ExtUtils::MakeMaker::VERSION >= 6.31 ? (LICENSE => 'perl') : ()),
37 );
38
39 sub extract_refcounted_he {
40     my ($header) = @_;
41     open my $fh, '<', $header or die $!;
42
43     my $def;
44     while (<$fh>) {
45         next unless /struct refcounted_he \{/ .. /\};/;
46         $def .= $_;
47     }
48     close $fh or die $!;
49
50     return $def;
51 }
52
53 sub write_header {
54     my ($filename, $contents) = @_;
55     my $guard_name = uc $filename;
56     $guard_name =~ tr/./_/;
57
58     open my $fh, '>', $filename or die $!;
59     print { $fh } "#ifndef ${guard_name}\n";
60     print { $fh } "#define ${guard_name}\n";
61     print { $fh } $contents or die $!;
62     print { $fh } "#endif /* ${guard_name} */\n";
63     close $fh or die $!;
64 }