=head1 NAME HACKERS - Devel::PPPort internals for hackers =head1 SYNOPSIS So you probably want to hack C? Well, here's some information to get you started with what's lying around in this distribution. =head1 DESCRIPTION =head2 How to backport something First, make sure that what you want to backport is documented. If it's worth backporting, it surely is worth documenting. Send a documentation patch to L if necessary. Also, C cannot automatically generate proper information about the item without at least knowing its API prototype. It can get this from F if the item is a function, but if it is a macro, there needs to be at least a S> line for C to be able to figure things out on its own. Next, figure out where to place your implementation. Look at all the files in F for one that fits what you're planning. If there isn't one, just start a new one and remember to include it from within F. If you do create a new file, it's usually the best approach to just copy an existing file and use it as a template. Each file holds all relevant data for implementing a certain part of the API: =over 2 =item * A list of the provided API in the C<=provides> section. =item * The optional C<=dontwarn> section is used to suppress warnings about particular API elements. Don't use this unless you get such a warning, and be sure to think about using other other alternatives before resorting to adding something in this section. =item * The implementation to add to F in the C<=implementation> section. See L. =item * The code required to add to PPPort.xs for testing the implementation. This code goes into the C<=xshead>, C<=xsinit>, C<=xsmisc>, C<=xsboot> and C<=xsubs> section. Have a look at the template at the bottom of F to see where the code ends up. =item * The tests in the C<=tests> section. Remember not to use any fancy modules or syntax elements, as the test code needs to be able to run with Perl 5.003. (This is because Devel::PPPort itself will run all test files in the process of generating the information about when a feature came into existence.) This means, for example =over =item C isn't supported in C-loops for my $x (1, 2, 3) { } # won't work with 5.003 Instead declare C<$x> just before the statement =item The postfix C statement modifier isn't supported foo for 1..2 won't compile. Instead enclose C in a loop. =item You can't use plain C Instead, wrap it in a string eval C, and be sure it's skipped at execution time on perls earlier than 5.005 =back As of version 3.56 of Devel::PPPort, the old Test style tests have been replaced with the more modern Test::More style, with some limitations. This means, for example, that C is finally available, as well as C. You can pass the number of tests to C, instead of having to have your own C loop. There is no C nor C (as those require C which didn't exist in the earliest perls that Devel::PPPort runs on). C doesn't do a S>. (Perhaps it could, but that would mean converting all the skips in the existing tests.) The existing tests have been changed only as much as necessary so as to get things to work. But feel free to use the full functionality for any new tests you write. Here's a list of the supported functions: cmp_ok curr_test diag display done_testing eq_array eq_hash fail is isnt next_test note ok pass plan skip skip_all within These are copied from F in the perl distribution. Not all of them have been tested back as far as Devel::PPPort supports. Bug reports welcome. It's fine to backport an element only as far as convenient and necessary. But remember that your test file will end up being called on all perl versions available, likely including ones earlier than your backport. That may mean that elements in the C<=xs> sections will have to be C<#idef>'d out so that the object will not get missing symbols when loaded. It also means you have to check for and skip tests that aren't relevant to this version. The recommended way to do this is like: if (ivers($]) < ivers(5.6.2)) { skip "reason", $count; } elsif (if (ivers($]) > ivers(5.5) { skip "other reason", $count; } C is a function automatically made available to all F<.t> files. It converts any reasonble expression of a version number into an integer, which can reliably be compared using numeric comparison operators, with the output of a second C call on a different version number, like in the result above. =back In all sections, lines that begin with C<##> are completely ignored. =head2 Implementation Section Details You can implement API elements via C functions or macros, or simple variables. For a function, just place its body in this C<=implementation> section. But it is preferable to use a macro if feasible. Otherwise, the user must explicitly request that it get loaded, by defining a C> (or I) as described in F. =over =item __UNDEFINED__ If you add the line C<__UNDEFINED__> to the C<=provides> section, you can use lines like this in the C<=implementation> section: __UNDEFINED__ macro some definition to both define C and indicate that it is provided by F. This replaces these C<=implementation> section lines: #ifndef macro # define macro some definition #endif besides automagically making it be considered to be provided. C can have optional arguments and the definition can even span multiple lines, like in __UNDEFINED__ SvMAGIC_set(sv, val) \ STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \ (((XPVMG*) SvANY(sv))->xmg_magic = (val)); } \ STMT_END This usually makes the code more compact and readable. But you should only use this on things that you plan to publicly provide. If something, such as a mnemonic for a constant needs to be defined but isn't really needed for the public at large to know about, you should use __UNDEF_NOT_PROVIDED__ macro some definition instead. To avoid name space conflicts, follow what's in L, below. =item Helper macros If you need to define a helper macro which is not part of C API and its usage is only for the definition of other C macros, then use the C prefix for this macro name (e.g. C). This suppresses any warnings when a macro is defined which is not part of the Perl public API. =item Version numbers Version checking used to be tricky to get correct (besides being buggy in some perl versions). C is used in the C<=tests> section to overcome this. and constructs like the following in the C language sections. #if PERL_VERSION_EQ(5,9,3) #if PERL_VERSION_NE(5,9,3) #if PERL_VERSION_LT(5,9,3) #if PERL_VERSION_GT(5,9,3) #if PERL_VERSION_LE(5,9,3) #if PERL_VERSION_GE(5,9,3) An alternative way of saying things like these is #if { VERSION < 5.9.3 } In this form, the version number can be either of the new form C<5.x.x> or the older form C<5.00x_yy>. Both are translated into the correct preprocessor statements. It is also possible to combine this with other statements: #if { VERSION >= 5.004 } && !defined(sv_vcatpvf) /* a */ #elif { VERSION < 5.004_63 } && { VERSION != 5.004_05 } /* b */ #endif This not only works in the C<=implementation> section, but also in the C<=xsubs>, C<=xsinit>, C<=xsmisc>, C<=xshead> and C<=xsboot> sections. =item Hints If you add a comment like so: /* Hint: PL_expect, PL_copline, PL_rsfp paragraphs of stuff about foo you want to have shown when ppport.h outputs something about PL_expect or PL_copline or PL_rsfp */ Earlier versions of F required an asterisk at the beginning of every continuation line, or else the content would be silently dropped. =item Warnings A more serious caution about C can be displayed by instead saying /* Warning: PL_expect, PL_copline, PL_rsfp paragraphs of stuff about foo you want to have shown when ppport.h outputs something about PL_expect or PL_copline or PL_rsfp */ Earlier versions of F required an asterisk at the beginning of every continuation line, or else the content would be silently dropped. =item Replace When F is run on a file(s), you can cause it to automatically flag occurrences of the constructs you specify, encouraging the author to replace them with different (presumably better) ones. These also are used in any suggested edits and generated patches. There are three ways to do this =over 4 =item in-line comment You can add a trailing comment like so: #define bar foo /* Replace */ __UNDEFINED__ bar foo /* Replace */ These say that C should be replaced by C. NOT the other way around. =item separate comment For situations not amenable to the above, you can say /* Replace foo with bar */ =item define a replacement region It you have several replacements, you can group them together like so: /* Replace: 1 */ #define foo bar #define bat baz /* Replace: 0 */ These replace C with C; C with C. NOT the other way around. =back =item Dependencies F automatically gathers information as to what functions are dependent on what other things from inspecting the source, but if this is insufficient for you, you can add lines like the following: /* foo, bar depends on baz, bat */ Each of C, C depends on each of C, C. =back =head2 Testing After you have furnished your implementation, you need to test it. =head2 Special Makefile targets You can use make regen to regenerate all of the autogenerated files. To get rid of all generated files (except for F and F), use make purge_all That's it. To automatically test C with lots of different Perl versions, you can use the F script. Just pass it a list of all Perl binaries you want to test. =head2 Regenerating F and F C keeps two directories of generated files, in F and F. The files in each are named after Perl version numbers. When a function or macro came into existence is indicated by placing its name in the corresponding file in F. The files in F are the same, except they indicate the earliest release that F supports the element. The delta is effectively what F buys you. The generation process described in this section creates these files. It does so by examining as many perl versions as are available to it. It tries to make sure each element actually compiles, and it runs the test scripts you have furnished on every version. Ideally, this should be done before every release that includes new backporting and/or when blead has added new public API. At a minimum, it should be done as the next major Perl release comes out. The process isn't platform independent. It has currently been tested only under Linux, and it definitely requires at least C and the C utility. The process used to be problematic, with random failures. But it has now been fixed to be reliable. Before starting the regeneration, you need to have gathered certain data. (Options listed below apply to the tools that eventually will use the data, and which are described further below). =over 4 =item * You will first need a whole bunch of different Perls, the more, the better, but only one per version tag (which one is random) will actually get used. dromedary has a sufficient set. They should all have the same Configure options with respect to what functions and macros are enabled. For example, they should all be threaded, or all non-threaded. A mixture will screw up the results. Similarly, they should all or none have quad math (at least as far back as that became available). You can use F to build them. Previous maintainers of this module kept those perls in F, so most of the tools use this as a default, but you'll likely simply use the C<--install=> option to specify where. This should be a path where a S> has been done, so has immediate subdirectories of C and C. C should contain the binaries. It will use all files in this directory whose names begin with C. Actually, not all the binaries need be in this directory. You can specify additional places to look since C<--install=> takes a comma separated list of directories. =item * You also need a freshly built bleadperl. The C<--blead=I> option should be used to specify it. (Some of the tools have a default of C if this option is omitted.) Again, it needs the same Configure options as the earlier versions had. Using C<-DNO_MATHOMS> will change the results, and probably should be avoided. True, these functions are allegedly on their way out, so it could be argued that they shouldn't be encouraged in any way; but some of these have been in limbo for many years, so should be documented. =item * And you will need updated API information. Copy the latest F file from bleadperl to the F directory and run F to collect the remaining information in F. =item * The final step before regenerating everything is to run F to update the F file. =back Having done this, run F which wraps the following steps (which you could instead do by hand, but it's easy to forget things): =over =item * It first does some sanity checking =item * Then it asks you if it's ok to remove all existing todo files in the F and F directories. If you answer no, the process aborts. This is crtical to getting accurate results. =item * It builds the new baseline by running perl devel/mktodo --base in the root directory of the distribution. If there are warnings in blead, it will ask you to examine them, and to ok if it's all right to go ahead. If there are issues with blead, everything following could be wrong. =item * It builds the new todo files by running perl devel/mktodo in the root directory of the distribution. =item * Finally, it adds the remaining information by running perl Makefile.PL && make perl devel/scanprov --mode=write =back =head2 How to build gobs of versions of Perl C supports Perl versions between 5.003 and bleadperl. To guarantee this support, its good to have as many versions as possible to test on. dromedary currently has many such versions. There is a tool to build all the different versions and configurations. You can find it in F. It can currently build the following Perl releases: 5.003 5.004 - 5.004_05 5.005 - 5.005_04 5.6.x 5.7.x 5.8.x 5.9.x 5.1x.x 5.2x.x 5.3x.x =head2 Implementation Knowing which parts of the API are not backwards compatible and probably need C support is another problem that's not easy to deal with manually. If you run perl Makefile.PL --with-apicheck a C file is generated by F that is compiled and linked with C. This C file has the purpose of using each of the public API functions/macros once. The required information is derived from F (just a copy of bleadperl's F), F (which is generated by F and simply collects the rest of the apidoc entries spread over the Perl source code) and F (which lists the API provided purely by Devel::PPPort, along with other elements that are tested only using F). The generated C file (usually, F) won't compile as-is with older perls. And even if it compiles, there's still a good chance of the dynamic linker failing at C time. But that's on purpose! We can use these failures to find changes in the API automatically. The Perl script F calls another script F repeatedly to run C on version after version of perl, in decreasing version order, so we start with blead and work backwards. The latter script generates an F. It starts with the code that successfully worked in the previously tested Perl version, which should be the version one higher than the current one. Call the current one I, and the previous one I. The items that fail to compile in I, but did compile in I must have become available in I. We run the Linux command C to find those undefined symbols in I. We change F to ignore (through C<#ifdef>'s) those and recompile, repeating until F successfully compiles, the dynamic linker is happy, and C runs on this version. Then we repeat the process for I, and so on. (Actually, this process may generate false positives, so by default each failing API call is checked again. If possible, this is done by generating an F for just the one failing API.) Note that the make test is run using F during both passes. Running F currently takes a couple hours on dromedary. If you run it with the C<--nocheck> option, it won't recheck the API calls that failed in the compilation stage and it'll take significantly less time. No one currently associated with maintaining this module understands under what circumstances it is safe to run with C<--nocheck>. By repeating the process over and over, we build up information on when every element first became supported. This information is stored in files in the F directory, one file per version. The file for version I is generated by running version I of perl. We actually want a second piece of information, which is how much F buys you. What happens when regenerating is actually two entire runs through all the perls. The first is accomplished by calling F with the C<--base> option. It automically will call F with each version of perl, NOT using anything in F. When done the results indicate when each API element became available in stock perl, without using F. And then the whole process is repeated, but this time F is included. The files are placed in F. Thus, at the end, we know when each element became available in modified perl, using F. However, only the public API that is implemented as functions (and must appear in F) plus macros whose calling sequence is documented can be checked this way. The final step in the process is calling F. It looks through the header files for when all the symbols provided by C first became defined. It doesn't test the symbols or try to compile them, as it doesn't generally know the API, but it can tell that something exists in release I but not I (by scanning the include files in the F directory of various Perl versions). (It does know if a macro has zero arguments or non-zero arguments, so it does get extra information from the zero argument ones.) =head2 Files Residing in F is the "heart" of C. Each of the files implements a part of the supported API, along with hints, dependency information, XS code and tests. The files are in a POD-like format that is parsed using the functions in F. The scripts F, F and F all use the information in F to generate the main module F, the XS code in F and various test files in F. You can get extra information from F by setting the environment variable C to 1 or 2. All of these files could be generated on the fly while building C, but not having the tests in F will confuse TEST/harness in the core. Not having F will be bad for viewing the docs on C. So unfortunately, it's unavoidable to put some redundancy into the package. =head2 Submitting Patches If you've added some functionality to C, please consider submitting a patch with your work to P5P by sending a pull request to L. When submitting patches, please only add the relevant changes and don't include the differences of the generated files. You can use the C target to delete all autogenerated files. =head2 Integrating into the Perl core When integrating this module into the Perl core, be sure to remove the following files from the distribution. They are either not needed or generated on the fly when building this module in the core: MANIFEST META.yml PPPort.pm =head1 BUGS No known bugs. =head1 COPYRIGHT Version 3.x, Copyright (C) 2004-2019, Marcus Holland-Moritz and Perl 5 porters Version 2.x, Copyright (C) 2001, Paul Marquess. Version 1.x, Copyright (C) 1999, Kenneth Albanowski. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO See F and F. =cut