5 /* Perl/Pollution/Portability Version 1.0003 */
7 /* Copyright (C) 1999, Kenneth Albanowski. This code may be used and
8 distributed under the same license as any version of Perl. */
10 /* For the latest version of this code, please contact the author at
11 <kjahds@kjahds.com>, or check with the Perl maintainers. */
13 /* If you needed to customize this file for your project, please mention
17 Modified for Perl 5.6.0 by Russ Allbery (use PERL_VERSION instead of
23 In order for a Perl extension module to be as portable as possible
24 across differing versions of Perl itself, certain steps need to be taken.
25 Including this header is the first major one, then using dTHR is all the
26 appropriate places and using a PL_ prefix to refer to global Perl
27 variables is the second.
31 /* If you use one of a few functions that were not present in earlier
32 versions of Perl, please add a define before the inclusion of ppport.h
33 for a static include, or use the GLOBAL request in a single module to
34 produce a global definition that can be referenced from the other
37 Function: Static define: Extern define:
38 newCONSTSUB() NEED_newCONSTSUB NEED_newCONSTSUB_GLOBAL
43 /* To verify whether ppport.h is needed for your module, and whether any
44 special defines should be used, ppport.h can be run through Perl to check
45 your source code. Simply say:
47 perl -x ppport.h *.c *.h *.xs foo/perl.c [etc]
49 The result will be a list of patches suggesting changes that should at
50 least be acceptable, if not necessarily the most efficient solution, or a
51 fix for all possible problems. It won't catch where dTHR is needed, and
52 doesn't attempt to account for global macro or function definitions,
53 nested includes, typemaps, etc.
55 In order to test for the need of dTHR, please try your module under a
56 recent version of Perl that has threading compiled-in.
63 @ARGV = ("*.xs") if !@ARGV;
64 %badmacros = %funcs = %macros = ();
66 $funcs{$1} = 1 if /Provide:\s+(\S+)/;
67 $macros{$1} = 1 if /^#\s*define\s+([a-zA-Z0-9_]+)/;
68 $badmacros{$2}=$1 if /^#\s*define\s+(PL_\S+)\s+(\S+)/;
70 foreach $filename (map(glob($_),@ARGV)) {
71 unless (open(IN, "<$filename")) {
72 warn "Unable to read from $file: $!\n";
75 print "Scanning $filename...\n";
76 $c = ""; while (<IN>) { $c .= $_; } close(IN);
77 $need_include = 0; %add_func = (); $changes = 0;
78 $has_include = ($c =~ /#.*include.*ppport/m);
80 foreach $func (keys %funcs) {
81 if ($c =~ /#.*define.*\bNEED_$func(_GLOBAL)?\b/m) {
82 if ($c !~ /\b$func\b/m) {
83 print "If $func isn't needed, you don't need to request it.\n" if
84 $changes += ($c =~ s/^.*#.*define.*\bNEED_$func\b.*\n//m);
90 if ($c =~ /\b$func\b/m) {
98 if (not $need_include) {
99 foreach $macro (keys %macros) {
100 if ($c =~ /\b$macro\b/m) {
101 print "Uses $macro\n";
107 foreach $badmacro (keys %badmacros) {
108 if ($c =~ /\b$badmacro\b/m) {
109 $changes += ($c =~ s/\b$badmacro\b/$badmacros{$badmacro}/gm);
110 print "Uses $badmacros{$badmacro} (instead of $badmacro)\n";
115 if (scalar(keys %add_func) or $need_include != $has_include) {
117 $inc = join('',map("#define NEED_$_\n", sort keys %add_func)).
118 "#include \"ppport.h\"\n";
119 $c = "$inc$c" unless $c =~ s/#.*include.*XSUB.*\n/$&$inc/m;
120 } elsif (keys %add_func) {
121 $inc = join('',map("#define NEED_$_\n", sort keys %add_func));
122 $c = "$inc$c" unless $c =~ s/^.*#.*include.*ppport.*$/$inc$&/m;
124 if (!$need_include) {
125 print "Doesn't seem to need ppport.h.\n";
126 $c =~ s/^.*#.*include.*ppport.*\n//m;
132 open(OUT,">/tmp/ppport.h.$$");
135 open(DIFF, "diff -u $filename /tmp/ppport.h.$$|");
136 while (<DIFF>) { s!/tmp/ppport\.h\.$$!$filename.patched!; print STDOUT; }
138 unlink("/tmp/ppport.h.$$");
147 #if !defined(PERL_VERSION) && !defined(PERL_PATCHLEVEL)
148 # ifndef __PATCHLEVEL_H_INCLUDED__
149 # include <patchlevel.h>
153 # define PERL_REVISION (5)
154 # ifdef PERL_PATCHLEVEL
155 # define PERL_VERSION PERL_PATCHLEVEL
157 # define PERL_VERSION PATCHLEVEL
158 # define PERL_SUBVERSION SUBVERSION
163 # define ERRSV perl_get_sv("@",false)
166 #if (PERL_REVISION == 5) && ((PERL_VERSION < 4) || ((PERL_VERSION == 4) && (PERL_SUBVERSION <= 4)))
167 # define PL_sv_undef sv_undef
168 # define PL_sv_yes sv_yes
169 # define PL_sv_no sv_no
171 # define PL_stdingv stdingv
172 # define PL_hints hints
173 # define PL_curcop curcop
174 # define PL_curstash curstash
175 # define PL_copline copline
178 #if (PERL_REVISION == 5) && (PERL_VERSION < 5)
181 # define dTHR extern int Perl___notused
183 # define dTHR extern int errno
188 # define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
191 /* Perl tries to export a bunch of its own functions. Mutter. */
196 #endif /* !PPPORT_H */