1 ################################################################################
3 # PPPort_pm.PL -- generate PPPort.pm
5 # Set the environment variable DPPP_CHECK_LEVEL to more than zero for some
6 # extra checking. 1 or 2 currently
8 ################################################################################
10 # Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
11 # Copyright (C) 2018, The perl5 porters
12 # Version 2.x, Copyright (C) 2001, Paul Marquess.
13 # Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
15 # This program is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
18 ################################################################################
22 require "./parts/ppptools.pl";
23 require "./parts/inc/inctools";
25 my $INCLUDE = 'parts/inc';
28 # The keys of %embed are the names of the items found in all the .fnc files,
29 # and each value is all the information parse_embed returns for that item.
30 my %embed = map { ( $_->{name} => $_ ) }
31 parse_embed(qw(parts/embed.fnc parts/apidoc.fnc parts/ppport.fnc));
33 my(%provides, %prototypes, %explicit);
35 my $data = do { local $/; <DATA> };
37 # Call include(file, params) for every line that begins with %include
38 # These fill in %provides and %prototypes.
39 # The keys of %provides are the items provided by Devel::PPPort, and each
40 # value is the name of the file (in parts/inc/) that has the code to provide
42 # An entry in %prototypes looks like:
43 # 'grok_bin' => 'UV grok_bin(pTHX_ const char * start, STRLEN * len_p, I32 * flags, NV * result)',
45 $data =~ s{^\%(include)\s+(\w+)((?:[^\S\r\n]+.*?)?)\s*$}
46 {eval "$1('$2', $3)" or die $@}gem;
49 $data = expand($data);
51 # Just the list of provided items.
52 my @provided = sort dictionary_order keys %provides;
54 # which further expands $data.
55 $data =~ s{^(.*)__PROVIDED_API__(\s*?)^}
56 {join '', map "$1$_\n", @provided}gem;
60 for (keys %explicit) {
61 length > $len and $len = length;
63 my $format = sprintf '%%-%ds %%-%ds %%s', $len+2, $len+5;
66 $data =~ s!^(.*)__EXPLICIT_API__(\s*?)^!
67 sprintf("$1$format\n", 'Function / Variable', 'Static Request', 'Global Request') .
68 $1 . '-'x$len . "\n" .
69 join('', map { sprintf "$1$format\n", $explicit{$_} eq 'var' ? $_ : "$_()", "NEED_$_", "NEED_${_}_GLOBAL" }
70 sort dictionary_order keys %explicit)
74 # These hashes look like:
75 # { ... 'gv_check' => '5.003007',
76 # 'gv_const_sv' => '5.009003',
77 # 'gv_dump' => '5.006000',
80 # What's provided when without ppport.h, as far as we've been able to
82 my %raw_base = %{&parse_todo('parts/base')};
84 # What's provided when using ppport.h, as far as we've been able to
86 my %raw_todo = %{&parse_todo('parts/todo')};
88 # Invert so each key is the 7 digit version number, and it's value is an array
89 # of all symbols within it, like:
93 # 'save_generic_svref'
96 for (keys %raw_todo) {
97 push @{$todo{int_parse_version($raw_todo{$_})}}, $_;
101 my @todo_list = reverse sort keys %todo;
103 # Here, @todo_list contains the integer version numbers that have support.
104 # The first and final elements give the extremes of the supported versions.
105 # (Use defaults that were reasonable at the time of this commit if the
106 # directories are empty (which should only happen during regeneration of the
107 # base and todo files).). Actually the final element is for blead (at the
108 # time things were regenerated), which is 1 beyond the max version supported.
109 my $INT_MAX_PERL = (@todo_list) ? $todo_list[0] - 1 : '5030000';
110 my $MAX_PERL = format_version($INT_MAX_PERL);
111 my $INT_MIN_PERL = (@todo_list) ? $todo_list[-1] : 5003007;
112 my $MIN_PERL = format_version($INT_MIN_PERL);
114 # check consistency between our list of everything provided, and our lists of
115 # what got provided when
117 if ( exists $raw_todo{$_}
118 && $raw_todo{$_} > $INT_MIN_PERL # INT_MIN_PERL contents are real
119 # symbols, not something to do
120 && exists $raw_base{$_})
122 if ($raw_base{$_} == $raw_todo{$_}) {
123 warn "$INCLUDE/$provides{$_} provides $_, which is still marked "
124 . "todo for " . format_version($raw_todo{$_}) . "\n";
127 check(2, "$_ was ported back to " . format_version($raw_todo{$_}) .
128 " (baseline revision: " . format_version($raw_base{$_}) . ").");
135 next if /^Perl_(.*)/ && exists $embed{$1};
136 next if exists $embed{$_};
138 check(2, "No API definition for provided element $_ found.");
141 # At this point @perl_api is the list of things we provide that weren't found
143 # Add in the .fnc file definitions.
144 push @perl_api, keys %embed;
145 @perl_api = sort dictionary_order @perl_api;
147 for (@perl_api) { # $_ is the item name
148 if (exists $provides{$_} && !exists $raw_base{$_}) {
149 check(2, "Mmmh, $_ doesn't seem to need backporting.");
152 # Create the lines that ppport.h reads. These look like
153 # CopyD|5.009002|5.003007|p
154 my $line = "$_|" . (exists $provides{$_} && exists $raw_base{$_} ? $raw_base{$_} : '') . '|';
155 $line .= ($raw_todo{$_} || '') . '|';
156 $line .= 'p' if exists $provides{$_};
157 if (exists $embed{$_}) {
159 if (exists $e->{flags}{p}) { # Has 'Perl_' prefix
160 my $args = $e->{args};
161 $line .= 'v' if @$args && $args->[-1][0] eq '...';
163 $line .= 'n' if exists $e->{flags}{T}; # No thread context parameter
164 $line .= 'd' if exists $e->{flags}{D}; # deprecated
165 $line .= 'x' if exists $e->{flags}{x}; # experimental
166 $line .= 'c' if exists $e->{flags}{C} # core-only
167 || ( exists $e->{flags}{X}
168 && (exists $e->{flags}{E} || ! exists $e->{flags}{m}));
169 $line .= 'i' if exists $e->{flags}{A}
170 || exists $e->{flags}{C}
171 || ( exists $e->{flags}{X}
172 && ! exists $e->{flags}{E}
173 && exists $e->{flags}{m});
174 $line .= 'u' unless exists $e->{flags}{d}; # undocumented
179 $data =~ s/^([\t ]*)__PERL_API__(\s*?)$/
180 join "\n", map "$1$_", sort dictionary_order @perl_api
183 my $undocumented = "(undocumented)";
187 my $ver = format_version($_);
188 $ver .= " (at least)" if $_ == $todo_list[-1];
189 my $todo = "=item perl $ver\n\n";
190 for (sort dictionary_order @{$todo{$_}}) {
192 $todo .= " (DEPRECATED)" if $embed{$_}->{flags}{D};
193 $todo .= " (marked experimental)" if $embed{$_}->{flags}{x};
194 $todo .= " $undocumented" unless $embed{$_}->{flags}{d};
200 $data =~ s{^__UNSUPPORTED_API__(\s*?)^}
201 {join "\n", @todo}gem;
203 $data =~ s{__MIN_PERL__}{$MIN_PERL}g;
204 $data =~ s{__MAX_PERL__}{$MAX_PERL}g;
206 open FH, ">PPPort.pm" or die "PPPort.pm: $!\n";
214 my($file, $opt) = @_;
216 print "including $file\n";
218 my $data = parse_partspec("$INCLUDE/$file");
220 for (@{$data->{provides}}) {
221 if (exists $provides{$_}) {
222 if ($provides{$_} ne $file) {
223 warn "$file: $_ already provided by $provides{$_}\n";
227 $provides{$_} = $file;
231 for (keys %{$data->{prototypes}}) {
232 $prototypes{$_} = $data->{prototypes}{$_};
233 $prototypes{$_} = normalize_prototype($data->{prototypes}{$_});
234 $data->{implementation} =~ s/^$_(?=\s*\()/$DPPP(my_$_)/mg;
237 my $out = $data->{implementation};
239 if (exists $opt->{indent}) {
240 $out =~ s/^/$opt->{indent}/gm;
249 $code =~ s{^(\s*#\s*(?:el)?if\s+)(.*)$}{$1.expand_pp_expressions($2)}gem;
259 (?:[^\r\n\\]|\\[^\r\n])*
263 (?:[^\r\n\\]|\\[^\r\n])*
267 {expand_undefined($2, $1, $3)}gemx;
268 $code =~ s{^([^\S\r\n]*)__NEED_VAR__\s+(.*?)\s+(\w+)(?:\s*=\s*([^;]+?))?\s*;\s*$}
269 {expand_need_var($1, $3, $2, $4)}gem;
270 $code =~ s{^([^\S\r\n]*)__NEED_DUMMY_VAR__\s+(.*?)\s+(\w+)(?:\s*=\s*([^;]+?))?\s*;\s*$}
271 {expand_need_dummy_var($1, $3, $2, $4)}gem;
277 my($indent, $var, $type, $init) = @_;
279 $explicit{$var} = 'var';
281 my $myvar = "$DPPP(my_$var)";
282 $init = defined $init ? " = $init" : "";
284 my $code = <<ENDCODE;
285 #if defined(NEED_$var)
286 static $type $myvar$init;
287 #elif defined(NEED_${var}_GLOBAL)
295 $code =~ s/^/$indent/mg;
300 sub expand_need_dummy_var
302 my($indent, $var, $type, $init) = @_;
304 $explicit{$var} = 'var';
306 my $myvar = "$DPPP(dummy_$var)";
307 $init = defined $init ? " = $init" : "";
309 my $code = <<ENDCODE;
310 #if defined(NEED_$var)
311 static $type $myvar$init;
312 #elif defined(NEED_${var}_GLOBAL)
319 $code =~ s/^/$indent/mg;
326 my($macro, $withargs, $def) = @_;
327 my $rv = "#ifndef $macro\n# define ";
329 if (defined $def && $def =~ /\S/) {
330 $rv .= sprintf "%-30s %s", $withargs, $def;
341 sub expand_pp_expressions
344 $pp =~ s/\{([^\}]+)\}/expand_pp_expr($1)/ge;
352 if ($expr =~ /^\s*need\s+(\w+)\s*$/i) {
354 my $e = $embed{$func} or die "unknown API function '$func' in NEED\n";
355 my $proto = make_prototype($e);
356 if (exists $prototypes{$func}) {
357 if (compare_prototypes($proto, $prototypes{$func})) {
358 my $proto_no_pTHX = $proto;
359 $proto_no_pTHX =~ s/pTHX_\s*//;
360 if (compare_prototypes($proto_no_pTHX, $prototypes{$func})) {
361 check(1, "differing prototypes for $func:\n API: $proto\n PPP: $prototypes{$func}");
364 check(1, "prototypes differ in pTHX_ for $func:\n API: $proto\n PPP: $prototypes{$func}");
366 $proto = $prototypes{$func};
370 warn "found no prototype for $func\n";;
373 $explicit{$func} = 'func';
375 $proto =~ s/\b$func(?=\s*\()/$DPPP(my_$func)/;
376 my $embed = make_embed($e);
378 return "defined(NEED_$func)\n"
385 . "#if defined(NEED_$func) || defined(NEED_${func}_GLOBAL)\n"
390 die "cannot expand preprocessor expression '$expr'\n";
397 my $a = do { my $x = 'a'; join ',', map { $x++ } 1 .. @{$f->{args}} };
398 my $lastarg = ${$f->{args}}[-1];
400 if ($f->{flags}{T}) {
401 if ($f->{flags}{p}) {
402 return "#define $n $DPPP(my_$n)\n" .
403 "#define Perl_$n $DPPP(my_$n)";
406 return "#define $n $DPPP(my_$n)";
415 if ($f->{flags}{p}) {
416 if ($f->{flags}{f}) {
417 return "#define Perl_$n $DPPP(my_$n)";
419 elsif (@$lastarg && $lastarg->[0] =~ /\.\.\./) {
420 return $undef . "#define $n $DPPP(my_$n)\n" .
421 "#define Perl_$n $DPPP(my_$n)";
424 return $undef . "#define $n($a) $DPPP(my_$n)(aTHX_ $a)\n" .
425 "#define Perl_$n $DPPP(my_$n)";
429 return $undef . "#define $n($a) $DPPP(my_$n)(aTHX_ $a)";
438 if (exists $ENV{DPPP_CHECK_LEVEL} and $ENV{DPPP_CHECK_LEVEL} >= $level) {
439 print STDERR @_, "\n";
444 ################################################################################
446 # !!!!! Do NOT edit this file directly! -- Edit PPPort_pm.PL instead. !!!!!
448 # This file was automatically generated from the definition files in the
449 # parts/inc/ subdirectory by PPPort_pm.PL. To learn more about how all this
450 # works, please read the F<HACKERS> file that came with this distribution.
452 ################################################################################
454 # Perl/Pollution/Portability
456 ################################################################################
458 # Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
459 # Copyright (C) 2018, The perl5 porters
460 # Version 2.x, Copyright (C) 2001, Paul Marquess.
461 # Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
463 # This program is free software; you can redistribute it and/or
464 # modify it under the same terms as Perl itself.
466 ################################################################################
470 Devel::PPPort - Perl/Pollution/Portability
474 Devel::PPPort::WriteFile(); # defaults to ./ppport.h
475 Devel::PPPort::WriteFile('someheader.h');
477 # Same as above but retrieve contents rather than write file
478 my $contents = Devel::PPPort::GetFileContents();
479 my $contents = Devel::PPPort::GetFileContents('someheader.h');
481 =head1 Start using Devel::PPPort for XS projects
484 $ perl -MDevel::PPPort -e'Devel::PPPort::WriteFile'
485 $ perl ppport.h --compat-version=5.6.1 --patch=diff.patch *.xs
486 $ patch -p0 < diff.patch
487 $ echo ppport.h >>MANIFEST
491 Perl's API has changed over time, gaining new features, new functions,
492 increasing its flexibility, and reducing the impact on the C namespace
493 environment (reduced pollution). The header file written by this module,
494 typically F<ppport.h>, attempts to bring some of the newer Perl API
495 features to older versions of Perl, so that you can worry less about
496 keeping track of old releases, but users can still reap the benefit.
498 C<Devel::PPPort> contains two functions, C<WriteFile> and C<GetFileContents>.
499 C<WriteFile>'s only purpose is to write the F<ppport.h> C header file.
500 This file contains a series of macros and, if explicitly requested, functions
501 that allow XS modules to be built using older versions of Perl. Currently,
502 Perl versions from __MIN_PERL__ to __MAX_PERL__ are supported.
504 C<GetFileContents> can be used to retrieve the file contents rather than
507 This module is used by C<h2xs> to write the file F<ppport.h>.
509 =head2 Why use ppport.h?
511 You should use F<ppport.h> in modern code so that your code will work
512 with the widest range of Perl interpreters possible, without significant
515 You should attempt to get older code to fully use F<ppport.h>, because the
516 reduced pollution of newer Perl versions is an important thing. It's so
517 important that the old polluting ways of original Perl modules will not be
518 supported very far into the future, and your module will almost certainly
519 break! By adapting to it now, you'll gain compatibility and a sense of
520 having done the electronic ecology some good.
522 =head2 How to use ppport.h
524 Don't direct the users of your module to download C<Devel::PPPort>.
525 They are most probably not XS writers. Also, don't make F<ppport.h>
526 optional. Rather, just take the most recent copy of F<ppport.h> that
527 you can find (e.g. by generating it with the latest C<Devel::PPPort>
528 release from CPAN), copy it into your project, adjust your project to
529 use it, and distribute the header along with your module.
531 =head2 Running ppport.h
533 But F<ppport.h> is more than just a C header. It's also a Perl script
534 that can check your source code. It will suggest hints and portability
535 notes, and can even make suggestions on how to change your code. You
536 can run it like any other Perl program:
538 perl ppport.h [options] [files]
540 It also has embedded documentation, so you can use
544 to find out more about how to use it.
550 C<WriteFile> takes one optional argument. When called with one
551 argument, it expects to be passed a filename. When called with
552 no arguments, it defaults to the filename F<ppport.h>.
554 The function returns a true value if the file was written successfully.
555 Otherwise it returns a false value.
557 =head2 GetFileContents
559 C<GetFileContents> behaves like C<WriteFile> above, but returns the contents
560 of the would-be file rather than writing it out.
564 F<ppport.h> supports Perl versions from __MIN_PERL__ to __MAX_PERL__
565 in threaded and non-threaded configurations.
567 =head2 Provided Perl compatibility API
569 The header file written by this module, typically F<ppport.h>, provides access
570 to the following elements of the Perl API that are not otherwise available in
571 Perl releases older than when the elements were first introduced. (Note that
572 many of these are not supported all the way back to __MIN_PERL__, but it may
573 be that they are supported back as far as you need; see L</Supported Perl API,
574 sorted by version> for that information.)
578 =head2 Supported Perl API, sorted by version
580 The table in this section lists all the Perl API elements available, sorted by
581 the version in which support starts. This includes all the elements that
582 F<ppport.h> helps out with, as well as those elements that it doesn't.
584 In some cases, it doesn't make practical sense for elements to be supported
585 earlier than they already are. For example, UTF-8 functionality isn't
586 provided prior to the release where it was first introduced.
588 But in other cases, it just is that no one has implemented support yet.
589 Patches welcome! Some elements are ported backward for some releases, but not
590 all the way to __MIN_PERL__.
592 If an element, call it ELEMENT, is not on this list, try using this command to
595 perl ppport.h --api-info=ELEMENT
597 A few of the entries in the list below are marked as DEPRECATED. You should
598 not use these for new code, and should be converting existing uses to use
601 Some of the entries in the list are marked as "experimental". This means
602 these should not generally be used. They may be removed or changed without
603 notice. You can ask why they are experimental by sending email to
604 L<mailto:perl5-porters@perl.org>.
606 And some of the entries are marked as "undocumented". This means that they
607 aren't necessarily considered stable, and could be changed or removed in some
608 future release without warning. It is therefore a bad idea to use them
609 without further checking. It could be that these are considered to be for
610 perl core use only; or it could be, though, that C<Devel::PPPort> doesn't know
611 where to find their documentation, or that it's just an oversight that they
612 haven't been documented. If you want to use one, and potentially have it
613 backported, first send mail to L<mailto:perl5-porters@perl.org>.
623 If you find any bugs, C<Devel::PPPort> doesn't seem to build on your
624 system, or any of its tests fail, please send a bug report to
625 L<https://github.com/Dual-Life/Devel-PPPort/issues/new>.
633 Version 1.x of Devel::PPPort was written by Kenneth Albanowski.
637 Version 2.x was ported to the Perl core by Paul Marquess.
641 Version 3.x was ported back to CPAN by Marcus Holland-Moritz.
645 Versions >= 3.22 are maintained by perl5 porters
651 Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
653 Copyright (C) 2018, The perl5 porters
655 Version 2.x, Copyright (C) 2001, Paul Marquess.
657 Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
659 This program is free software; you can redistribute it and/or
660 modify it under the same terms as Perl itself.
664 See L<h2xs>, L<ppport.h>.
668 package Devel::PPPort;
671 use vars qw($VERSION $data);
677 $data = do { local $/; <DATA> };
678 my $pkg = 'Devel::PPPort';
679 $data =~ s/__PERL_VERSION__/$]/g;
680 $data =~ s/__VERSION__/$VERSION/g;
681 $data =~ s/__PKG__/$pkg/g;
685 sub GetFileContents {
686 my $file = shift || 'ppport.h';
687 defined $data or _init_data();
689 $copy =~ s/\bppport\.h\b/$file/g;
696 my $file = shift || 'ppport.h';
697 my $data = GetFileContents($file);
698 open F, ">$file" or return undef;
712 ----------------------------------------------------------------------
714 ppport.h -- Perl/Pollution/Portability Version __VERSION__
716 Automatically created by __PKG__ running under perl __PERL_VERSION__.
718 Do NOT edit this file directly! -- Edit PPPort_pm.PL and the
719 includes in parts/inc/ instead.
721 Use 'perldoc ppport.h' to view the documentation below.
723 ----------------------------------------------------------------------
727 %include ppphdoc { indent => '|>' }
736 #ifndef _P_P_PORTABILITY_H_
737 #define _P_P_PORTABILITY_H_
739 #ifndef DPPP_NAMESPACE
740 # define DPPP_NAMESPACE DPPP_
743 #define DPPP_CAT2(x,y) CAT2(x,y)
744 #define DPPP_(name) DPPP_CAT2(DPPP_NAMESPACE, name)
816 #endif /* _P_P_PORTABILITY_H_ */
818 /* End of File ppport.h */