This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
new perldelta
[perl5.git] / dist / Devel-PPPort / HACKERS
1 =head1 NAME
2
3 HACKERS - Devel::PPPort internals for hackers
4
5 =head1 SYNOPSIS
6
7 So you probably want to hack C<Devel::PPPort>?
8
9 Well, here's some information to get you started with what's
10 lying around in this distribution.
11
12 =head1 DESCRIPTION
13
14 =head2 How to backport something
15
16 First, make sure that what you want to backport is documented.  If it's worth
17 backporting, it surely is worth documenting.  Submit a documentation patch to
18 L<https://github.com/Perl/perl5/issues> if necessary.  Also, C<Devel::PPPort>
19 cannot automatically generate proper information about the item without at
20 least knowing its API prototype.  It can get this from F<embed.fnc> if the item
21 is a function, but if it is a macro, there needs to be at least a S<C<=for
22 apidoc>> line for C<Devel::PPPort> to be able to figure things out on its own.
23
24 Next, figure out where to place your implementation.  Look at all the files in
25 F<parts/inc/> for one that fits what you're planning.  If there isn't one,
26 just start a new one and remember to include it from within F<PPPort_pm.PL>.
27 If you do create a new file, it's usually the best approach to just copy an
28 existing file and use it as a template.
29
30 Each file holds all relevant data for implementing a certain part
31 of the API:
32
33 =over 2
34
35 =item *
36
37 A list of the provided API in the C<=provides> section.
38
39 =item *
40
41 The optional C<=dontwarn> section is used to suppress warnings about particular
42 API elements.  Don't use this unless you get such a warning, and be sure to
43 think about using other other alternatives before resorting to adding something
44 in this section.
45
46 =item *
47
48 The implementation to add to F<ppport.h> in the C<=implementation>
49 section.  See L</Implementation Section Details>.
50
51 =item *
52
53 The code required to add to PPPort.xs for testing the implementation.
54 This code goes into the C<=xshead>, C<=xsinit>, C<=xsmisc>, C<=xsboot>
55 and C<=xsubs> section. Have a look at the template at the bottom
56 of F<RealPPPort_xs.PL> to see where the code ends up.
57
58 =item *
59
60 The tests in the C<=tests> section. Remember not to use any fancy
61 modules or syntax elements, as the test code needs to be able to run
62 with Perl 5.003.  (This is because Devel::PPPort itself will run all test files
63 in the process of generating the information about when a feature came into
64 existence.)  This means, for example
65
66 =over
67
68 =item C<my> isn't supported in C<for>-loops
69
70     for my $x (1, 2, 3) { }    # won't work with 5.003
71
72 Instead declare C<$x> just before the statement
73
74 =item The postfix C<for> statement modifier isn't supported
75
76     foo for 1..2
77
78 won't compile.  Instead enclose C<foo> in a loop.
79
80 =item You can't use plain C<qr//>
81
82 Instead, wrap it in a string eval C<eval "qr//">, and be sure it's skipped at
83 execution time on perls earlier than 5.005
84
85 =back
86
87 As of version 3.56 of Devel::PPPort, the old Test style tests have been
88 replaced with the more modern Test::More style, with some limitations.  This
89 means, for example, that C<is> is finally available, as well as
90 C<done_testing>.  You can pass the number of tests to C<skip>, instead of
91 having to have your own C<for> loop.
92
93 There is no C<like> nor C<unlike> (as those require C<qr> which didn't exist in
94 the earliest perls that Devel::PPPort runs on).
95
96 C<skip> doesn't do a S<C<last SKIP>>.  (Perhaps it could, but that would mean
97 converting all the skips in the existing tests.)
98
99 The existing tests have been changed only as much as necessary so as to get
100 things to work.  But feel free to use the full functionality for any new tests
101 you write.
102
103 Here's a list of the supported functions:
104
105  cmp_ok
106  curr_test
107  diag
108  display
109  done_testing
110  eq_array
111  eq_hash
112  fail
113  is
114  isnt
115  next_test
116  note
117  ok
118  pass
119  plan
120  skip
121  skip_all
122  within
123
124 These are copied from F<t/test.pl> in the perl distribution.  Not all of them
125 have been tested back as far as Devel::PPPort supports.  Bug reports welcome.
126
127 It's fine to backport an element only as far as convenient and necessary.  But
128 remember that your test file will end up being called on all perl versions
129 available, likely including ones earlier than your backport.  That may mean
130 that elements in the C<=xs> sections will have to be C<#idef>'d out so that the
131 object will not get missing symbols when loaded.
132
133 It also means you have to check for and skip tests that aren't relevant to this
134 version.  The recommended way to do this is like:
135
136  if (ivers($]) < ivers(5.6)) {           # No quotes needed
137     skip "reason", $count;
138  }
139  elsif (if (ivers($]) > ivers("5.5.4") {
140      # Quotes needed for double-dotted versions prior to 5.6.0
141     skip "other reason", $count;
142  }
143  else {
144     do_the_test
145  }
146
147 C<ivers()> is a function automatically made available to all F<.t> files.  It
148 converts any reasonble expression of a version number into an integer, which
149 can reliably be compared using numeric comparison operators, with the output of
150 a second C<ivers()> call on a different version number, like in the result above.
151
152 It's worth emphasizing that, due to bugs in early perl parsing, if you use a
153 version number containing two dots on a version befor 5.6.0, it has to be
154 quoted.
155
156 =back
157
158 In all sections, lines that begin with C<##> are completely ignored.
159
160 =head2 Implementation Section Details
161
162 You can implement API elements via C functions or macros, or simple variables.
163 It is preferable to use a macro if feasible.  Otherwise, the user must
164 explicitly request that it get loaded, by defining a C<NEED_I<function>> (or
165 I<variable>) as described in F<ppport.h>.  If a function, I<foo> is required,
166 place its body in this C<=implementation> section, like so:
167
168  #if { NEED foo }
169
170  char *
171  foo(pTHX_ const U8 *arg1, const U32 arg2, ...)
172  {
173     ...
174  }
175
176  #endif
177
178 Similarly for a variable.
179
180 It's obviously best to use a macro if at all feasible.  Sometimes what once
181 was implemented with a macro now requires a function; perhaps an edge case was
182 overlooked.  Doing so will cause the new F<ppport.h> to not be drop-in
183 compatible with the older version, and can hence cause breakage.  This
184 incompatiblity (while easily solved) really needs to be stressed in
185 documentation.
186
187 =over
188
189 =item __UNDEFINED__
190
191 If you add the line C<__UNDEFINED__> to the C<=provides> section, you can use
192 lines like this in the C<=implementation> section:
193
194   __UNDEFINED__ macro    some definition
195
196 to both define C<macro> and indicate that it is provided by F<ppport.h>.  This
197 replaces these C<=implementation> section lines:
198
199   #ifndef macro
200   #  define macro    some definition
201   #endif
202
203 besides automagically making it be considered to be provided.  C<macro> can
204 have optional arguments and the definition can even span multiple lines, like
205 in
206
207   __UNDEFINED__ SvMAGIC_set(sv, val) \
208                 STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
209                 (((XPVMG*) SvANY(sv))->xmg_magic = (val)); } \
210                 STMT_END
211
212 This usually makes the code more compact and readable.
213
214 But you should only use this on things that you plan to publicly provide.  If
215 something, such as a mnemonic for a constant needs to be defined but isn't
216 really needed for the public at large to know about, you should use
217
218  __UNDEF_NOT_PROVIDED__ macro   some definition
219
220 instead.  To avoid name space conflicts, follow what's in L</Helper macros>,
221 below.
222
223 =item __REDEFINE__
224
225 If you add the line C<__REDEFINE__> to the C<=provides> section, you can use
226 lines like this in the C<=implementation> section:
227
228   __REDEFINE__ macro    some definition
229
230 to both redefine C<macro> and indicate that it is provided by F<ppport.h>.  This
231 replaces these C<=implementation> section lines:
232
233   #undef macro
234   #ifndef macro
235   #  define macro    some definition
236   #endif
237
238
239 =item Helper macros
240
241 If you need to define a helper macro which is not part of C<Devel::PPPort> API
242 and its usage is only for the definition of other C<Devel::PPPort> macros, then
243 use the C<D_PPP_> prefix for this macro name (e.g. C<D_PPP_SVPV_NOLEN_LP_ARG>).
244 This suppresses any warnings when a macro is defined which is not part of the
245 Perl public API.
246
247 =item Version numbers
248
249 Version checking used to be tricky to get correct (besides being buggy in some
250 perl versions).
251 C<ivers()> is used in the C<=tests> section to overcome this. and constructs
252 like the following in the C language sections.
253
254   #if { VERSION < 5.9.3 }
255
256 You SHOULD be using this construct or the alternatives listed below for ALL
257 version checks, and not come up with something on your own.
258
259 In this form, the version number can be either of the new form C<5.x.y> or the
260 older form C<5.00x_yy>. Both are translated into the correct preprocessor
261 statements. It is also possible to combine this with other statements:
262
263   #if { VERSION >= 5.004 } && !defined(sv_vcatpvf)
264     /* a */
265   #elif { VERSION < 5.004_63 } && { VERSION != 5.004_05 }
266     /* b */
267   #endif
268
269 This not only works in the C<=implementation> section, but also in
270 the C<=xsubs>, C<=xsinit>, C<=xsmisc>, C<=xshead> and C<=xsboot> sections.
271
272 Alternatively, you can use the forms now available in regular Perl:
273
274   #if PERL_VERSION_EQ(5,9,3)
275   #if PERL_VERSION_NE(5,9,3)
276   #if PERL_VERSION_LT(5,9,3)
277   #if PERL_VERSION_GT(5,9,3)
278   #if PERL_VERSION_LE(5,9,3)
279   #if PERL_VERSION_GE(5,9,3)
280
281 These forms have the advantage over the '{ VERSION ... }' form in that you may
282 use the special value '*' for the final number to mean ALL possible values for
283 it.  Thus,
284
285  #if PERL_VERSION_EQ(5,31,'*')
286
287 means all perls in the 5.31 series.  And
288
289  #if PERL_VERSION_NE(5,24,'*')
290
291 means all perls EXCEPT 5.24 ones.  And
292
293  #if PERL_VERSION_LE(5,9,'*')
294
295 is effectively
296
297  #if PERL_VERSION_LT(5,10,0)
298
299 =item Hints
300
301 If you add a comment like so:
302
303  /* Hint: PL_expect, PL_copline, PL_rsfp
304     paragraphs of stuff you want to have shown when ppport.h outputs
305     something about any one of PL_expect, PL_copline, or PL_rsfp
306  */
307
308 Earlier versions of F<ppport.h> required an asterisk at the beginning of every
309 continuation line, or else the content would be silently dropped.
310
311 =item Warnings
312
313 A more serious caution can be displayed by instead saying
314
315  /* Warning: PL_expect, PL_copline, PL_rsfp
316     paragraphs of stuff you want to have shown when ppport.h outputs
317     something about any one of PL_expect, PL_copline, or PL_rsfp
318  */
319
320 Earlier versions of F<ppport.h> required an asterisk at the beginning of every
321 continuation line, or else the content would be silently dropped.
322
323 =item Replace
324
325 When F<ppport.h> is run on a file(s), you can cause it to automatically flag
326 occurrences of the constructs you specify, encouraging the author to replace
327 them with different (presumably better) ones.  These also are used in any
328 suggested edits and generated patches.
329
330 There are three ways to do this
331
332 =over 4
333
334 =item in-line comment
335
336 You can add a trailing comment like so:
337
338  #define bar foo    /* Replace */
339  __UNDEFINED__ bar foo  /* Replace */
340
341 These say that C<foo> should be replaced by C<bar>.  NOT the other way around.
342
343 =item separate comment
344
345 For situations not amenable to the above, you can say
346
347  /* Replace foo with bar */
348
349 =item define a replacement region
350
351 It you have several replacements, you can group them together like so:
352
353  /* Replace: 1 */
354  #define foo bar
355  #define bat baz
356  /* Replace: 0 */
357
358 These replace C<bar> with C<foo>; C<baz> with C<bat>.  NOT the other way
359 around.
360
361 =back
362
363 =item Dependencies
364
365 F<ppport.h> automatically gathers information as to what functions are
366 dependent on what other things from inspecting the source, but if this is
367 insufficient for you, you can add lines like the following:
368
369  /* foo, bar depends on baz, bat */
370
371 Each of C<foo>, C<bar> depends on each of C<baz>, C<bat>.
372
373 =back
374
375 =head2 Testing
376
377 After you have furnished your implementation, you need to test it.
378
379 =head2 Special Makefile targets
380
381 You can use
382
383     make regen
384
385 to regenerate all of the autogenerated files. To get rid of all
386 generated files (except for F<parts/todo/*> and F<parts/base/*>),
387 use
388
389     make purge_all
390
391 That's it.
392
393 To automatically test C<Devel::PPPort> with lots of different Perl
394 versions, you can use the F<soak> script. Just pass it a list of
395 all Perl binaries you want to test.
396
397 =head2 Regenerating F<ppport.h> and F<PPPort.pm>
398
399 C<Devel::PPPort> keeps two directories of generated files, in F<parts/base> and
400 F<parts/todo>.  The files in each are named after Perl version numbers.  When a
401 function or macro came into existence is indicated by placing its name in the
402 corresponding file in F<parts/base>.  The files in F<parts/todo> are the same,
403 except they indicate the earliest release that F<ppport.h> supports the
404 element.  The delta is effectively what F<ppport.h> buys you.
405
406 The generation process described in this section creates these files.  It does
407 so by examining as many perl versions as are available to it.  It tries to make
408 sure each element actually compiles, and it runs the test scripts you have
409 furnished on every version.
410
411 Ideally, this should be done before every release that includes new backporting
412 and/or when blead has added new public API.  At a minimum, it should be done as
413 the next major Perl release comes out.
414
415 The process isn't platform independent. It has currently been tested only under
416 Linux, and it definitely requires at least C<gcc> and the C<nm> utility.
417 The process used to be problematic, with random failures.  But it has now been
418 fixed to be reliable.
419
420 Before starting the regeneration, you need to have gathered certain data.
421 (Options listed below apply to the tools that eventually will use the data, and
422 which are described further below).
423
424 =over 4
425
426 =item *
427
428 You will first need a whole bunch of different Perls, the more, the better, but
429 only one per version tag (which one is random) will actually get used.
430 dromedary has a sufficient set.  They should all have the same Configure
431 options with respect to what functions and macros are enabled.  For example,
432 they should all be threaded, or all non-threaded.  A mixture will screw up the
433 results.  Similarly, they should all or none have quad math (at least as far
434 back as that became available).  You can use F<devel/buildperl.pl> to build
435 them.
436
437 Previous maintainers of this module kept those perls in
438 F</tmp/perl/install/default>, so most of the tools use this as a default, but
439 you'll likely simply use the C<--install=> option to specify where.  This
440 should be a path where a S<C<make install>> has been done, so has immediate
441 subdirectories of C</bin> and C</lib>.  C</bin> should contain the binaries.
442 It will use all files in this directory whose names begin with C<perl5>.
443
444 Actually, not all the binaries need be in this directory.  You can specify
445 additional places to look since C<--install=> takes a comma separated list of
446 directories.
447
448 =item *
449
450 You also need a freshly built bleadperl.  The C<--blead=I<path>> option should
451 be used to specify it.  (Some of the tools have a default of C<bleadperl-debug>
452 if this option is omitted.)  Again, it needs the same Configure options as the
453 earlier versions had.  Using C<-DNO_MATHOMS> will change the results, and
454 probably should be avoided.  True, these functions are allegedly on their way
455 out, so it could be argued that they shouldn't be encouraged in any way; but
456 some of these have been in limbo for many years, so should be documented.
457
458 =item *
459
460 And you will need updated API information. Copy the latest F<embed.fnc> file
461 from bleadperl to the F<parts> directory and run F<devel/mkapidoc.pl> to
462 collect the remaining information in F<parts/apidoc.fnc>.  This needs to be
463 done after the perl has been compiled, as there are generated files that feed
464 it.
465
466 =item *
467
468 The final step before regenerating everything is to run
469 F<devel/mkppport_fnc.pl> to update the F</parts/ppport.fnc> file.
470
471 =back
472
473 Having done this, run F<devel/regenerate> which wraps the following steps
474 (which you could instead do by hand, but it's easy to forget things):
475
476 =over
477
478 =item *
479
480 It first does some sanity checking
481
482 =item *
483
484 Then it asks you if it's ok to remove all existing todo files in the
485 F<parts/base> and F<parts/todo> directories.  If you answer no, the process
486 aborts.
487
488 This is crtical to getting accurate results.
489
490 =item *
491
492 It builds the new baseline by running
493
494     perl devel/mktodo --base
495
496 in the root directory of the distribution.
497
498 If there are warnings in blead, it will ask you to examine them, and to ok if
499 it's all right to go ahead.  If there are issues with blead, everything
500 following could be wrong.
501
502 =item *
503
504 It builds the new todo files by running
505
506     perl devel/mktodo
507
508 in the root directory of the distribution.
509
510 =item *
511
512 Finally, it adds the remaining information by running
513
514     perl Makefile.PL && make
515     perl devel/scanprov --mode=write
516
517 =back
518
519 =head2 How to build gobs of versions of Perl
520
521 C<Devel::PPPort> supports Perl versions between 5.003 and bleadperl.
522 To guarantee this support, its good to have as many versions as possible to
523 test on.  dromedary currently has many such versions.
524
525 There is a tool to build all the different
526 versions and configurations. You can find it in F<devel/buildperl.pl>.
527 It can currently build the following Perl releases:
528
529     5.003
530     5.004 - 5.004_05
531     5.005 - 5.005_04
532     5.6.x
533     5.7.x
534     5.8.x
535     5.9.x
536     5.1x.x
537     5.2x.x
538     5.3x.x
539
540 =head2 Implementation
541
542 Knowing which parts of the API are not backwards compatible and
543 probably need C<Devel::PPPort> support is another problem that's
544 not easy to deal with manually. If you run
545
546     perl Makefile.PL --with-apicheck
547
548 a C file is generated by F<parts/apicheck.pl> that is compiled
549 and linked with C<Devel::PPPort>. This C file has the purpose of
550 using each of the public API functions/macros once.
551
552 The required information is derived from F<parts/embed.fnc> (just
553 a copy of bleadperl's F<embed.fnc>), F<parts/apidoc.fnc> (which
554 is generated by F<devel/mkapidoc.pl> and simply collects the rest
555 of the apidoc entries spread over the Perl source code) and
556 F<parts/ppport.fnc> (which lists the API provided purely by
557 Devel::PPPort, along with other elements that are tested only using
558 F<ppport.h>).
559
560 The generated C file (usually, F<apicheck.c>) won't compile as-is
561 with older perls. And even if it compiles, there's still a good chance of the
562 dynamic linker failing at C<make test> time. But that's on purpose!
563
564 We can use these failures to find changes in the API automatically.
565 The Perl script F<devel/mktodo> calls another script F<devel/mktodo.pl>
566 repeatedly to run C<Devel::PPPort> on version after version of perl, in
567 decreasing version order, so we start with blead and work backwards.  The
568 latter script generates an F<apicheck.c>.  It starts with the code that
569 successfully worked in the previously tested Perl version, which should be the
570 version one higher than the current one.  Call the current one I<n>, and the
571 previous one I<n+1>.  The items that fail to compile in I<n>, but did compile
572 in I<n+1> must have become available in I<n+1>.  We run the Linux command C<nm>
573 to find those undefined symbols in I<n>.  We change F<apicheck.c> to ignore
574 (through C<#ifdef>'s) those and recompile, repeating until F<apicheck.c>
575 successfully compiles, the dynamic linker is happy, and C<make test> runs on
576 this version.  Then we repeat the process for I<n-1>, and so on.  (Actually,
577 this process may generate false positives, so by default each failing API call
578 is checked again.  If possible, this is done by generating an F<apicheck.c> for
579 just the one failing API.)  Note that the make test is run using F<ppport.h>
580 during both passes.
581
582 Running F<devel/mktodo> currently takes a couple hours on dromedary.
583
584 If you run it with the C<--nocheck> option, it won't recheck the API calls
585 that failed in the compilation stage and it'll take significantly less time.
586 No one currently associated with maintaining this module understands under what
587 circumstances it is safe to run with C<--nocheck>.
588
589 By repeating the process over and over, we build up information on when every
590 element first became supported.  This information is stored in files in the
591 F<parts/base> directory, one file per version.  The file for version I<n+1> is
592 generated by running version I<n> of perl.
593
594 We actually want a second piece of information, which is how much F<ppport.h>
595 buys you.  What happens when regenerating is actually two entire runs through
596 all the perls.  The first is accomplished by calling F<devel/mktodo> with the
597 C<--base> option.  It automically will call F<devel/mktodo.pl> with each
598 version of perl, NOT using anything in F<ppport.h>.  When done the results
599 indicate  when each API element became available in stock perl, without using
600 F<ppport.h>.
601
602 And then the whole process is repeated, but this time F<ppport.h> is included.
603 The files are placed in F<parts/todo>.  Thus, at the end, we know when each
604 element became available in modified perl, using F<ppport.h>.
605
606 However, only the public API that is implemented as functions (and must appear
607 in F<embed.fnc>) plus macros whose calling sequence is documented can be
608 checked this way.  The final step in the process is calling F<devel/scanprov>.
609 It looks through the header files for when all the symbols provided by
610 C<Devel::PPPort> first became defined.  It doesn't test the symbols or try to
611 compile them, as it doesn't generally know the API, but it can tell that
612 something exists in release I<n+1> but not I<n> (by scanning the include files
613 in the F<CORE> directory of various Perl versions).  (It does know if a macro
614 has zero arguments or non-zero arguments, so it does get extra information from
615 the zero argument ones.)
616
617 =head2 Files
618
619 Residing in F<parts/inc/> is the "heart" of C<Devel::PPPort>. Each
620 of the files implements a part of the supported API, along with
621 hints, dependency information, XS code and tests.
622 The files are in a POD-like format that is parsed using the
623 functions in F<parts/ppptools.pl>.
624
625 The scripts F<PPPort_pm.PL>, F<RealPPPort_xs.PL> and F<mktests.PL> all
626 use the information in F<parts/inc/> to generate the main module
627 F<PPPort.pm>, the XS code in F<RealPPPort.xs> and various test files
628 in F<t/>.
629
630 You can get extra information from F<PPPort_pm.PL> by setting the environment
631 variable C<DPPP_CHECK_LEVEL> to 1 or 2.
632
633 All of these files could be generated on the fly while building
634 C<Devel::PPPort>, but not having the tests in F<t/> will confuse
635 TEST/harness in the core. Not having F<PPPort.pm> will be bad for
636 viewing the docs on C<search.cpan.org>. So unfortunately, it's
637 unavoidable to put some redundancy into the package.
638
639 =head2 Submitting Patches
640
641 If you've added some functionality to C<Devel::PPPort>, please
642 consider submitting a patch with your work to P5P by sending a pull request to
643
644 L<https://github.com/Dual-Life/Devel-PPPort/pulls>.
645
646 When submitting patches, please only add the relevant changes
647 and don't include the differences of the generated files. You
648 can use the C<purge_all> target to delete all autogenerated
649 files.
650
651 =head2 Integrating into the Perl core
652
653 When integrating this module into the Perl core, be sure to
654 remove the following files from the distribution. They are
655 either not needed or generated on the fly when building this
656 module in the core:
657
658   MANIFEST
659   META.yml
660   PPPort.pm
661
662 =head1 BUGS
663
664 No known bugs.
665
666 =head1 COPYRIGHT
667
668 Version 3.x, Copyright (C) 2004-2020, Marcus Holland-Moritz
669 and Perl 5 porters
670
671 Version 2.x, Copyright (C) 2001, Paul Marquess.
672
673 Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
674
675 This program is free software; you can redistribute it and/or
676 modify it under the same terms as Perl itself.
677
678 =head1 SEE ALSO
679
680 See F<ppport.h> and F<devel/regenerate>.
681
682 =cut