2
#ifndef _P_P_PORTABILITY_H_
3
#define _P_P_PORTABILITY_H_
5
/* Perl/Pollution/Portability Version 1.0007 */
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 retreive the Devel::PPPort
11
module from CPAN, contact the author at <kjahds@kjahds.com>, or check
12
with the Perl maintainers. */
14
/* If you needed to customize this file for your project, please mention
15
your changes, and visible alter the version number. */
19
In order for a Perl extension module to be as portable as possible
20
across differing versions of Perl itself, certain steps need to be taken.
21
Including this header is the first major one, then using dTHR is all the
22
appropriate places and using a PL_ prefix to refer to global Perl
23
variables is the second.
27
/* If you use one of a few functions that were not present in earlier
28
versions of Perl, please add a define before the inclusion of ppport.h
29
for a static include, or use the GLOBAL request in a single module to
30
produce a global definition that can be referenced from the other
33
Function: Static define: Extern define:
34
newCONSTSUB() NEED_newCONSTSUB NEED_newCONSTSUB_GLOBAL
39
/* To verify whether ppport.h is needed for your module, and whether any
40
special defines should be used, ppport.h can be run through Perl to check
41
your source code. Simply say:
43
perl -x ppport.h *.c *.h *.xs foo/*.c [etc]
45
The result will be a list of patches suggesting changes that should at
46
least be acceptable, if not necessarily the most efficient solution, or a
47
fix for all possible problems. It won't catch where dTHR is needed, and
48
doesn't attempt to account for global macro or function definitions,
49
nested includes, typemaps, etc.
51
In order to test for the need of dTHR, please try your module under a
52
recent version of Perl that has threading compiled-in.
59
@ARGV = ("*.xs") if !@ARGV;
60
%badmacros = %funcs = %macros = (); $replace = 0;
62
$funcs{$1} = 1 if /Provide:\s+(\S+)/;
63
$macros{$1} = 1 if /^#\s*define\s+([a-zA-Z0-9_]+)/;
64
$replace = $1 if /Replace:\s+(\d+)/;
65
$badmacros{$2}=$1 if $replace and /^#\s*define\s+([a-zA-Z0-9_]+).*?\s+([a-zA-Z0-9_]+)/;
66
$badmacros{$1}=$2 if /Replace (\S+) with (\S+)/;
68
foreach $filename (map(glob($_),@ARGV)) {
69
unless (open(IN, "<$filename")) {
70
warn "Unable to read from $file: $!\n";
73
print "Scanning $filename...\n";
74
$c = ""; while (<IN>) { $c .= $_; } close(IN);
75
$need_include = 0; %add_func = (); $changes = 0;
76
$has_include = ($c =~ /#.*include.*ppport/m);
78
foreach $func (keys %funcs) {
79
if ($c =~ /#.*define.*\bNEED_$func(_GLOBAL)?\b/m) {
80
if ($c !~ /\b$func\b/m) {
81
print "If $func isn't needed, you don't need to request it.\n" if
82
$changes += ($c =~ s/^.*#.*define.*\bNEED_$func\b.*\n//m);
88
if ($c =~ /\b$func\b/m) {
96
if (not $need_include) {
97
foreach $macro (keys %macros) {
98
if ($c =~ /\b$macro\b/m) {
99
print "Uses $macro\n";
105
foreach $badmacro (keys %badmacros) {
106
if ($c =~ /\b$badmacro\b/m) {
107
$changes += ($c =~ s/\b$badmacro\b/$badmacros{$badmacro}/gm);
108
print "Uses $badmacros{$badmacro} (instead of $badmacro)\n";
113
if (scalar(keys %add_func) or $need_include != $has_include) {
115
$inc = join('',map("#define NEED_$_\n", sort keys %add_func)).
116
"#include \"ppport.h\"\n";
117
$c = "$inc$c" unless $c =~ s/#.*include.*XSUB.*\n/$&$inc/m;
118
} elsif (keys %add_func) {
119
$inc = join('',map("#define NEED_$_\n", sort keys %add_func));
120
$c = "$inc$c" unless $c =~ s/^.*#.*include.*ppport.*$/$inc$&/m;
122
if (!$need_include) {
123
print "Doesn't seem to need ppport.h.\n";
124
$c =~ s/^.*#.*include.*ppport.*\n//m;
130
open(OUT,">/tmp/ppport.h.$$");
133
open(DIFF, "diff -u $filename /tmp/ppport.h.$$|");
134
while (<DIFF>) { s!/tmp/ppport\.h\.$$!$filename.patched!; print STDOUT; }
136
unlink("/tmp/ppport.h.$$");
144
#ifndef PERL_REVISION
145
#ifndef __PATCHLEVEL_H_INCLUDED__
146
#include "patchlevel.h"
148
#ifndef PERL_REVISION
149
#define PERL_REVISION (5)
151
#define PERL_VERSION PATCHLEVEL
152
#define PERL_SUBVERSION SUBVERSION
153
/* Replace PERL_PATCHLEVEL with PERL_VERSION */
158
#define PERL_BCDVERSION ((PERL_REVISION * 0x1000000L) + (PERL_VERSION * 0x1000L) + PERL_SUBVERSION)
161
#define ERRSV perl_get_sv("@",FALSE)
164
#if (PERL_VERSION < 4) || ((PERL_VERSION == 4) && (PERL_SUBVERSION <= 5))
166
#define PL_sv_undef sv_undef
167
#define PL_sv_yes sv_yes
168
#define PL_sv_no sv_no
170
#define PL_stdingv stdingv
171
#define PL_hints hints
172
#define PL_curcop curcop
173
#define PL_curstash curstash
174
#define PL_copline copline
180
#define dTHR extern int no_such_variable
184
#define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
188
#define gv_stashpvn(str,len,flags) gv_stashpv(str,flags)
192
#define newSVpvn(data,len) ((len) ? newSVpv ((data), (len)) : newSVpv ("", 0))
197
#define newRV_inc(sv) newRV(sv)
203
#define newRV_noinc(sv) \
205
SV *nsv = (SV*)newRV(sv); \
210
#if defined(CRIPPLED_CC) || defined(USE_THREADS)
214
SV *nsv = (SV *) newRV(sv);
221
#define newRV_noinc(sv) \
222
((PL_Sv=(SV*)newRV(sv), SvREFCNT_dec(sv), (SV*)PL_Sv)
227
/* Provide: newCONSTSUB */
229
/* newCONSTSUB from IO.xs is in the core starting with 5.004_63 */
230
#if (PERL_VERSION < 4) || ((PERL_VERSION == 4) && (PERL_SUBVERSION < 63))
232
#if defined(NEED_newCONSTSUB)
235
extern void newCONSTSUB _((HV * stash, char *name, SV * sv));
238
#if defined(NEED_newCONSTSUB) || defined(NEED_newCONSTSUB_GLOBAL)
240
newCONSTSUB(stash, name, sv)
245
U32 oldhints = PL_hints;
246
HV *old_cop_stash = PL_curcop->cop_stash;
247
HV *old_curstash = PL_curstash;
248
line_t oldline = PL_curcop->cop_line;
250
PL_curcop->cop_line = PL_copline;
252
PL_hints &= ~HINT_BLOCK_SCOPE;
254
PL_curstash = PL_curcop->cop_stash = stash;
258
#if (PERL_VERSION < 3) || ((PERL_VERSION == 3) && (PERL_SUBVERSION < 22))
259
/* before 5.003_22 */
262
#if (PERL_VERSION == 3) && (PERL_SUBVERSION == 22)
266
/* 5.003_23 onwards */
267
start_subparse(FALSE, 0),
271
newSVOP(OP_CONST, 0, newSVpv(name, 0)),
272
newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == ""
274
newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
278
PL_curcop->cop_stash = old_cop_stash;
279
PL_curstash = old_curstash;
280
PL_curcop->cop_line = oldline;
283
#endif /* newCONSTSUB */
285
#endif /* _P_P_PORTABILITY_H_ */