1################################################################################
2#
3#  Makefile.PL -- generate Makefile
4#
5################################################################################
6#
7#  Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
8#  Version 2.x, Copyright (C) 2001, Paul Marquess.
9#  Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
10#
11#  This program is free software; you can redistribute it and/or
12#  modify it under the same terms as Perl itself.
13#
14# CAUTION! that this runs on 5.003.  That means there are certain restrictions
15# apply.  The most likely gotchas are in the HACKERS file under "How to
16# backport something".  Under the tests paragraph, there is a list of things
17# to avoid.
18#
19################################################################################
20
21require 5.003;
22
23use strict;
24BEGIN { $^W = 1; }
25
26use ExtUtils::MakeMaker;
27use Config;
28
29use vars '%opt';  # needs to be global, and we can't use 'our'
30
31sub cat_file
32{
33  eval { require File::Spec };
34  return $@ ? join('/', @_) : File::Spec->catfile(@_);
35}
36
37my $t_01_test = cat_file('t', '01_test.t');
38
39unless ($ENV{'PERL_CORE'}) {
40  $ENV{'PERL_CORE'} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
41}
42
43@ARGV = map { /^--with-(apicheck)$/ && ++$opt{$1} ? () : $_ } @ARGV;
44
45my %mf = (
46  NAME           => 'Devel::PPPort',
47  VERSION_FROM   => 'PPPort_pm.PL',
48  PM             => { 'PPPort.pm' => '$(INST_LIBDIR)/PPPort.pm' },
49  H              => [ qw(ppport.h) ],
50  OBJECT         => 'RealPPPort$(OBJ_EXT) $(O_FILES)',
51  XSPROTOARG     => '-noprototypes',
52  CONFIGURE      => \&configure,
53  BUILD_REQUIRES => {
54    "FindBin" => "0",
55  },
56);
57WriteMakefile(%mf);
58
59sub configure
60{
61  my @clean    = qw{ $(H_FILES) RealPPPort.xs RealPPPort.c PPPort.pm t/*.t };
62  my %depend   = (
63    '$(OBJECT)' => '$(H_FILES)',
64    'Makefile' => '$(VERSION_FROM)',
65  );
66  my @C_FILES  = qw{ module2.c module3.c },
67  my %PL_FILES = (
68    'ppport_h.PL'  => 'ppport.h',
69    'PPPort_pm.PL' => 'PPPort.pm',
70    'RealPPPort_xs.PL' => 'RealPPPort.xs',
71    'mktests.PL' => $t_01_test,
72  );
73  my @moreopts;
74
75  if (eval { ExtUtils::MakeMaker->VERSION(6) }) {
76    push @moreopts, AUTHOR => 'Marcus Holland-Moritz <mhx@cpan.org>';
77    push @moreopts, ABSTRACT => 'Perl/Pollution/Portability';
78  }
79
80  if (eval { ExtUtils::MakeMaker->VERSION(6.30_01) }) {
81    print "Setting license tag...\n";
82    push @moreopts, LICENSE => 'perl';
83  }
84
85  if (eval { ExtUtils::MakeMaker->VERSION (6.46) }) {
86    open FH, '<PPPort_pm.PL' or die "cannot open PPPort_pm.PL for reading: $!";
87    my $version;
88    my $line;
89    while ($line = <FH>) {
90      ($version) = $line =~ /^\$VERSION = '([\d.]+(_\d+)?)';$/ and last;
91    };
92    die 'failed to extract $VERSION from PPPort_pm.PL' if not $version;
93    close FH;
94    print "Adding META_MERGE...\n";
95    push @moreopts, META_MERGE => {
96      'meta-spec' => { version => 2 },
97      provides => {
98        'Devel::PPPort' => {
99          file    => 'PPPort.pm',
100          version => $version,
101        },
102      },
103      resources => {
104        bugtracker => {
105          web => 'https://github.com/Dual-Life/Devel-PPPort/issues',
106        },
107        repository => {
108          type => 'git',
109          url  => 'git://github.com/Dual-Life/Devel-PPPort.git',
110          web  => 'https://github.com/Dual-Life/Devel-PPPort',
111        },
112      },
113    };
114  }
115
116  if (not $ENV{'PERL_CORE'}) {
117    # Devel::PPPort is in the core since 5.7.3
118    # 5.11.0+ has site before perl
119    push @moreopts, INSTALLDIRS => (
120      ("$]" >= 5.007003 and "$]" < 5.011)
121        ? 'perl'
122        : 'site'
123    );
124  }
125
126  if ($opt{'apicheck'}) {
127    $PL_FILES{'apicheck_c.PL'} = 'apicheck.c';
128    push @C_FILES, qw{ apicheck.c };
129    push @clean,   qw{ apicheck.c apicheck.i };
130    $depend{'apicheck.i'} = 'ppport.h';
131  }
132
133  if ($Config{gccversion}) {
134    my $define = '-W -Wall';
135    if ($] < 5.035005 && $Config{gccversion} =~ /^(\d+\.\d+)\./ && $1 >= 3.4) {
136      # v5.35.5 enables some C99 features including mixed declarations and code,
137      # and uses them in inline.h, hence we can't enable this warning flag
138      # without generating false positive warnings.
139      # Earlier versions of perl support older compilers that are strict C89,
140      # hence code in ppport.h needs to avoid mixed declarations and code, hence
141      # enable this warning on earlier perls so that we can still spot problems.
142      $define .= ' -Wdeclaration-after-statement';
143    }
144    push @moreopts, DEFINE => $define;
145  }
146
147  return {
148    C        => \@C_FILES,
149    XS       => { 'RealPPPort.xs' => 'RealPPPort.c' },
150    PL_FILES => \%PL_FILES,
151    depend   => \%depend,
152    clean    => { FILES => "@clean" },
153    @moreopts,
154  };
155}
156
157sub MY::postamble
158{
159  package MY;
160  use Config;
161  my $post = shift->SUPER::postamble(@_);
162  # .PHONY is a syntax error in MMK/MMS
163  my $phony = ($Config{make} =~ m/MM(K|S)/) ? 'PHONY' : '.PHONY';
164  $post .= "\n\n${phony}: purge_all regen_pm regen_xs regen_tests regen_h regen_release_date\n\n";
165  $post .= <<'POSTAMBLE';
166
167purge_all: realclean
168	@$(RM_F) PPPort.pm t/*.t
169
170regen_pm:
171	$(PERL) -I$(INST_ARCHLIB) -I$(INST_LIB) -I$(PERL_ARCHLIB) -I$(PERL_LIB) PPPort_pm.PL
172
173regen_xs:
174	$(PERL) -I$(INST_ARCHLIB) -I$(INST_LIB) -I$(PERL_ARCHLIB) -I$(PERL_LIB) RealPPPort_xs.PL
175
176regen_tests:
177	$(PERL) -I$(INST_ARCHLIB) -I$(INST_LIB) -I$(PERL_ARCHLIB) -I$(PERL_LIB) mktests.PL
178
179regen_h:
180	$(PERL) -I$(INST_ARCHLIB) -I$(INST_LIB) -I$(PERL_ARCHLIB) -I$(PERL_LIB) ppport_h.PL
181
182regen_release_date:
183	$(PERL) -I$(INST_ARCHLIB) -I$(INST_LIB) -I$(PERL_ARCHLIB) -I$(PERL_LIB) devel/update_release_date.pl
184
185regen: regen_pm regen_xs regen_tests regen_h regen_release_date
186
187POSTAMBLE
188  return $post;
189}
190
191sub MY::processPL
192{
193  package MY;
194  my $original = shift->SUPER::processPL(@_);
195
196  require "./parts/ppptools.pl";
197  my $includes = join ' ', all_files_in_dir('parts/inc');
198
199  my $updated = '';
200  my @rules = split( m{^\s*$}m, $original );
201  my $rule;
202  foreach $rule ( @rules ) {
203    if ( $rule =~ m{^\s*^PPPort\.pm\s+:}m ) {
204      $rule =~ s{^(\s*^PPPort\.pm\s+:.*)}{$1 $includes}m; # PPPort.pm depends on all files from parts/inc
205      $rule =~ s{pm_to_blib}{}m; # PPPort.pm must not depend on built PPPort.pm in blib/
206    } elsif ( $rule =~ m{^\s*^ppport\.h\s+:}m ) {
207      $rule =~ s{^(\s*^ppport\.h\s+:.*)}{$1 PPPort.pm}m; # ppport.h depends on PPPort.pm
208      $rule =~ s{pm_to_blib}{}m; # ppport.h is used to build RealPPPort.xs so cannot depend on built PPPort in blib/
209    } elsif ( $rule =~ m{^\s*^RealPPPort\.xs\s+:}m ) {
210      $rule =~ s{^(\s*^RealPPPort\.xs\s+:.*)}{$1 ppport.h}m; # RealPPPort.xs depends on ppport.h
211      $rule =~ s{pm_to_blib}{}m; # RealPPPort.xs is used to build PPPort binary, so it cannot depend on it
212    } elsif ( $rule =~ m{^\s*\S+\b01_test\.t\s+:}m ) {
213      $rule =~ s{^(\s*^\S+\b01_test\.t\s+:.*)}{$1 $includes}m; # all tests in t/ depends on all files from parts/inc
214      $rule =~ s{pm_to_blib}{}m; # Generating test files does not depend on built PPPort in blib/
215    }
216    $updated .= $rule;
217  }
218
219  # All test targets depends on pure_all target, so ensure that t/01_test.t generated by mktests.PL is up-to-date
220  $updated .= <<"TESTS_IN_T";
221
222pure_all :: $t_01_test
223	\$(NOECHO) \$(NOOP)
224
225TESTS_IN_T
226
227  return $updated;
228}
229
230sub MY::dist_basics
231{
232  package MY;
233  my $original = shift->SUPER::dist_basics(@_);
234
235  my $updated = '';
236  my @rules = split( m{^\s*$}m, $original );
237  my $rule;
238  foreach $rule ( @rules ) {
239    if ( $rule =~ m{^\s*^manifest\s+:}m ) {
240      $rule =~ s{^(\s*^manifest\s+:.*)}{$1 $t_01_test}m; # make sure we regenerate tests
241    }
242    $updated .= $rule;
243  }
244
245  return $updated;
246}
247
248sub MY::dist_core
249{
250  package MY;
251  my $dist = shift->SUPER::dist_core(@_);
252
253  my $updated = '';
254  my @rules = split( m{^\s*$}m, $dist );
255  my $rule;
256  foreach $rule ( @rules ) {
257    if ( $rule =~ m{^\s*^dist\s+:}m ) {
258        $rule =~ s{:}{: PPPort.pm manifest regen}; # make sure we update PPPort.pm
259        $rule .= qq[\t].q[$(NOECHO) $(ECHO) "Warning: Please check '__MAX_PERL__' value in PPPort_pm.PL"].qq[\n];
260        # checking that the tarball has no Pax Header - avoid false positives by using [P]axHEader
261        $rule .= qq[\t].q[$(NOECHO) zgrep -a -e '[P]axHeader' $(DISTVNAME).tar$(SUFFIX) && ( $(ECHO) "ERROR: Pax Header detected in tarball"; rm -f $(DISTVNAME).tar$(SUFFIX) ) ||:].qq[\n];
262    }
263    $updated .= $rule;
264  }
265
266  return $updated;
267}
268
269
270sub MY::c_o
271{
272  package MY;
273  my $co = shift->SUPER::c_o(@_);
274
275  if ($::opt{'apicheck'} && $co !~ /^\.c\.i:/m) {
276    print "Adding custom rule for preprocessed apicheck file...\n";
277
278    $co .= <<'CO'
279
280.SUFFIXES: .i
281
282.c.i:
283	$(CCCMD) -E -I$(PERL_INC) $(DEFINE) $*.c > $*.i
284CO
285  }
286
287  return $co;
288}
289