TestBC.pm 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. #
  2. # was t/test.pl - from CORE
  3. use File::Spec;
  4. use B::C::Config;
  5. use Test::More;
  6. sub curr_test {
  7. $test = shift if @_;
  8. return $test;
  9. }
  10. sub next_test {
  11. my $retval = $test;
  12. $test = $test + 1; # don't use ++
  13. $retval;
  14. }
  15. my $cp_0037 = # EBCDIC code page 0037
  16. '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x25\x0B\x0C\x0D\x0E\x0F' .
  17. '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' .
  18. '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' .
  19. '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' .
  20. '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' .
  21. '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xBA\xE0\xBB\xB0\x6D' .
  22. '\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' .
  23. '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x4F\xD0\xA1\x07' .
  24. '\x20\x21\x22\x23\x24\x15\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' .
  25. '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\xFF' .
  26. '\x41\xAA\x4A\xB1\x9F\xB2\x6A\xB5\xBD\xB4\x9A\x8A\x5F\xCA\xAF\xBC' .
  27. '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' .
  28. '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' .
  29. '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xFD\xFE\xFB\xFC\xAD\xAE\x59' .
  30. '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' .
  31. '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xDD\xDE\xDB\xDC\x8D\x8E\xDF';
  32. my $cp_1047 = # EBCDIC code page 1047
  33. '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x15\x0B\x0C\x0D\x0E\x0F' .
  34. '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' .
  35. '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' .
  36. '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' .
  37. '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' .
  38. '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xAD\xE0\xBD\x5F\x6D' .
  39. '\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' .
  40. '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x4F\xD0\xA1\x07' .
  41. '\x20\x21\x22\x23\x24\x25\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' .
  42. '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\xFF' .
  43. '\x41\xAA\x4A\xB1\x9F\xB2\x6A\xB5\xBB\xB4\x9A\x8A\xB0\xCA\xAF\xBC' .
  44. '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' .
  45. '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' .
  46. '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xFD\xFE\xFB\xFC\xBA\xAE\x59' .
  47. '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' .
  48. '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xDD\xDE\xDB\xDC\x8D\x8E\xDF';
  49. my $cp_bc = # EBCDIC code page POSiX-BC
  50. '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x15\x0B\x0C\x0D\x0E\x0F' .
  51. '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' .
  52. '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' .
  53. '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' .
  54. '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' .
  55. '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xBB\xBC\xBD\x6A\x6D' .
  56. '\x4A\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' .
  57. '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xFB\x4F\xFD\xFF\x07' .
  58. '\x20\x21\x22\x23\x24\x25\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' .
  59. '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\x5F' .
  60. '\x41\xAA\xB0\xB1\x9F\xB2\xD0\xB5\x79\xB4\x9A\x8A\xBA\xCA\xAF\xA1' .
  61. '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' .
  62. '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' .
  63. '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xE0\xFE\xDD\xFC\xAD\xAE\x59' .
  64. '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' .
  65. '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xC0\xDE\xDB\xDC\x8D\x8E\xDF';
  66. my $straight = # Avoid ranges
  67. '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F' .
  68. '\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F' .
  69. '\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F' .
  70. '\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F' .
  71. '\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F' .
  72. '\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F' .
  73. '\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F' .
  74. '\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F' .
  75. '\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F' .
  76. '\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F' .
  77. '\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF' .
  78. '\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF' .
  79. '\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF' .
  80. '\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF' .
  81. '\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF' .
  82. '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF';
  83. # The following 2 functions allow tests to work on both EBCDIC and
  84. # ASCII-ish platforms. They convert string scalars between the native
  85. # character set and the set of 256 characters which is usually called
  86. # Latin1.
  87. #
  88. # These routines don't work on UTF-EBCDIC and UTF-8.
  89. sub native_to_latin1($) {
  90. my $string = shift;
  91. return $string if ord('^') == 94; # ASCII, Latin1
  92. my $cp;
  93. if (ord('^') == 95) { # EBCDIC 1047
  94. $cp = \$cp_1047;
  95. }
  96. elsif (ord('^') == 106) { # EBCDIC POSIX-BC
  97. $cp = \$cp_bc;
  98. }
  99. elsif (ord('^') == 176) { # EBCDIC 037 */
  100. $cp = \$cp_0037;
  101. }
  102. else {
  103. die "Unknown native character set";
  104. }
  105. eval '$string =~ tr/' . $$cp . '/' . $straight . '/';
  106. return $string;
  107. }
  108. sub latin1_to_native($) {
  109. my $string = shift;
  110. return $string if ord('^') == 94; # ASCII, Latin1
  111. my $cp;
  112. if (ord('^') == 95) { # EBCDIC 1047
  113. $cp = \$cp_1047;
  114. }
  115. elsif (ord('^') == 106) { # EBCDIC POSIX-BC
  116. $cp = \$cp_bc;
  117. }
  118. elsif (ord('^') == 176) { # EBCDIC 037 */
  119. $cp = \$cp_0037;
  120. }
  121. else {
  122. die "Unknown native character set";
  123. }
  124. eval '$string =~ tr/' . $straight . '/' . $$cp . '/';
  125. return $string;
  126. }
  127. sub ord_latin1_to_native {
  128. # given an input code point, return the platform's native
  129. # equivalent value. Anything above latin1 is itself.
  130. my $ord = shift;
  131. return $ord if $ord > 255;
  132. return ord latin1_to_native(chr $ord);
  133. }
  134. sub ord_native_to_latin1 {
  135. # given an input platform code point, return the latin1 equivalent value.
  136. # Anything above latin1 is itself.
  137. my $ord = shift;
  138. return $ord if $ord > 255;
  139. return ord native_to_latin1(chr $ord);
  140. }
  141. sub _where {
  142. my @caller = caller($Level);
  143. return "at $caller[1] line $caller[2]";
  144. }
  145. # runperl - Runs a separate perl interpreter.
  146. # Arguments :
  147. # switches => [ command-line switches ]
  148. # nolib => 1 # don't use -I../lib (included by default)
  149. # prog => one-liner (avoid quotes)
  150. # progs => [ multi-liner (avoid quotes) ]
  151. # progfile => perl script
  152. # stdin => string to feed the stdin
  153. # stderr => redirect stderr to stdout
  154. # args => [ command-line arguments to the perl program ]
  155. # verbose => print the command line
  156. my $is_mswin = $^O eq 'MSWin32';
  157. my $is_msvc = $is_mswin and $Config{cc} eq 'cl' ? 1 : 0;
  158. my $is_netware = $^O eq 'NetWare';
  159. my $is_macos = $^O eq 'MacOS';
  160. my $is_vms = $^O eq 'VMS';
  161. my $is_cygwin = $^O eq 'cygwin';
  162. sub _quote_args {
  163. my ($runperl, $args) = @_;
  164. foreach (@$args) {
  165. # In VMS protect with doublequotes because otherwise
  166. # DCL will lowercase -- unless already doublequoted.
  167. $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0;
  168. $$runperl .= ' ' . $_;
  169. }
  170. }
  171. sub _create_runperl { # Create the string to qx in runperl().
  172. my %args = @_;
  173. my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
  174. #- this allows, for example, to set PERL_RUNPERL_DEBUG=/usr/bin/valgrind
  175. if ($ENV{PERL_RUNPERL_DEBUG}) {
  176. $runperl = "$ENV{PERL_RUNPERL_DEBUG} $runperl";
  177. }
  178. unless ($args{nolib}) {
  179. if ($is_macos) {
  180. $runperl .= ' -I::lib';
  181. # Use UNIX style error messages instead of MPW style.
  182. $runperl .= ' -MMac::err=unix' if $args{stderr};
  183. }
  184. else {
  185. $runperl .= ' "-I../lib"'; # doublequotes because of VMS
  186. }
  187. }
  188. if ($args{switches}) {
  189. local $Level = 2;
  190. die "test.pl:runperl(): 'switches' must be an ARRAYREF " . _where()
  191. unless ref $args{switches} eq "ARRAY";
  192. _quote_args(\$runperl, $args{switches});
  193. }
  194. if (defined $args{prog}) {
  195. die "test.pl:runperl(): both 'prog' and 'progs' cannot be used " . _where()
  196. if defined $args{progs};
  197. $args{progs} = [$args{prog}]
  198. }
  199. if (defined $args{progs}) {
  200. die "test.pl:runperl(): 'progs' must be an ARRAYREF " . _where()
  201. unless ref $args{progs} eq "ARRAY";
  202. foreach my $prog (@{$args{progs}}) {
  203. if ($is_mswin || $is_netware || $is_vms) {
  204. $runperl .= qq ( -e "$prog" );
  205. }
  206. else {
  207. $runperl .= qq ( -e '$prog' );
  208. }
  209. }
  210. } elsif (defined $args{progfile}) {
  211. $runperl .= " ".($args{progfile} =~ m/\s/ ? qq{"$args{progfile}"} : $args{progfile});
  212. } else {
  213. # You probaby didn't want to be sucking in from the upstream stdin
  214. die "test.pl:runperl(): none of prog, progs, progfile, args, "
  215. . " switches or stdin specified"
  216. unless defined $args{args} or defined $args{switches}
  217. or defined $args{stdin};
  218. }
  219. if (defined $args{stdin}) {
  220. # so we don't try to put literal newlines and crs onto the
  221. # command line.
  222. $args{stdin} =~ s/\n/\\n/g;
  223. $args{stdin} =~ s/\r/\\r/g;
  224. if ($is_mswin || $is_netware || $is_vms) {
  225. $runperl = qq{$^X -e "print qq(} .
  226. $args{stdin} . q{)" | } . $runperl;
  227. }
  228. elsif ($is_macos) {
  229. # MacOS can only do two processes under MPW at once;
  230. # the test itself is one; we can't do two more, so
  231. # write to temp file
  232. my $stdin = qq{$^X -e 'print qq(} . $args{stdin} . qq{)' > teststdin; };
  233. if ($args{verbose}) {
  234. my $stdindisplay = $stdin;
  235. $stdindisplay =~ s/\n/\n\#/g;
  236. print STDERR "# $stdindisplay\n";
  237. }
  238. `$stdin`;
  239. $runperl .= q{ < teststdin };
  240. }
  241. else {
  242. $runperl = qq{$^X -e 'print qq(} .
  243. $args{stdin} . q{)' | } . $runperl;
  244. }
  245. }
  246. if (defined $args{args}) {
  247. _quote_args(\$runperl, $args{args});
  248. }
  249. $runperl .= ' 2>&1' if $args{stderr} && !$is_mswin && !$is_macos;
  250. $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
  251. if ($args{verbose}) {
  252. my $runperldisplay = $runperl;
  253. $runperldisplay =~ s/\n/\n\#/g;
  254. print STDERR "# $runperldisplay\n";
  255. }
  256. return $runperl;
  257. }
  258. sub runperl {
  259. die "test.pl:runperl() does not take a hashref"
  260. if ref $_[0] and ref $_[0] eq 'HASH';
  261. my $runperl = &_create_runperl;
  262. # ${^TAINT} is invalid in perl5.00505
  263. my $tainted;
  264. eval '$tainted = ${^TAINT};' if $] >= 5.006;
  265. my %args = @_;
  266. exists $args{switches} && grep m/^-T$/, @{$args{switches}} and $tainted = $tainted + 1;
  267. if ($tainted) {
  268. # We will assume that if you're running under -T, you really mean to
  269. # run a fresh perl, so we'll brute force launder everything for you
  270. my $sep;
  271. eval "require Config; Config->import";
  272. if ($@) {
  273. warn "test.pl had problems loading Config: $@";
  274. $sep = ':';
  275. } else {
  276. $sep = $Config{path_sep};
  277. }
  278. my @keys = grep {exists $ENV{$_}} qw(CDPATH IFS ENV BASH_ENV);
  279. local @ENV{@keys} = ();
  280. # Untaint, plus take out . and empty string:
  281. local $ENV{'DCL$PATH'} = $1 if $is_vms && ($ENV{'DCL$PATH'} =~ /(.*)/s);
  282. $ENV{PATH} =~ /(.*)/s;
  283. local $ENV{PATH} =
  284. join $sep, grep { $_ ne "" and $_ ne "." and -d $_ and
  285. ($is_mswin or $is_vms or !(stat && (stat _)[2]&0022)) }
  286. split quotemeta ($sep), $1;
  287. $ENV{PATH} .= "$sep/bin" if $is_cygwin; # Must have /bin under Cygwin
  288. $runperl =~ /(.*)/s;
  289. $runperl = $1;
  290. my ($err,$result,$stderr) = run_cmd($runperl, $args{timeout});
  291. $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
  292. return $result;
  293. } else {
  294. my ($err,$result,$stderr) = run_cmd($runperl, $args{timeout});
  295. $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
  296. return $result;
  297. }
  298. }
  299. *run_perl = \&runperl; # Nice alias.
  300. sub DIE {
  301. print STDERR "# @_\n";
  302. exit 1;
  303. }
  304. # A somewhat safer version of the sometimes wrong $^X.
  305. my $Perl;
  306. sub which_perl {
  307. unless (defined $Perl) {
  308. $Perl = $^X;
  309. # VMS should have 'perl' aliased properly
  310. return $Perl if $^O eq 'VMS';
  311. my $exe;
  312. eval "require Config; Config->import";
  313. if ($@) {
  314. warn "test.pl had problems loading Config: $@";
  315. $exe = '';
  316. } else {
  317. $exe = $Config{exe_ext};
  318. }
  319. $exe = '' unless defined $exe;
  320. # This doesn't absolutize the path: beware of future chdirs().
  321. # We could do File::Spec->abs2rel() but that does getcwd()s,
  322. # which is a bit heavyweight to do here.
  323. if ($Perl =~ /^perl\Q$exe\E$/i) {
  324. my $perl = "perl$exe";
  325. eval "require File::Spec";
  326. if ($@) {
  327. warn "test.pl had problems loading File::Spec: $@";
  328. $Perl = "./$perl";
  329. } else {
  330. $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
  331. }
  332. }
  333. # Build up the name of the executable file from the name of
  334. # the command.
  335. if ($Perl !~ /\Q$exe\E$/i) {
  336. $Perl .= $exe;
  337. }
  338. warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
  339. # For subcommands to use.
  340. $ENV{PERLEXE} = $Perl;
  341. }
  342. return $Perl;
  343. }
  344. sub unlink_all {
  345. my $count = 0;
  346. foreach my $file (@_) {
  347. 1 while unlink $file;
  348. if( -f $file ){
  349. print STDERR "# Couldn't unlink '$file': $!\n";
  350. }else{
  351. ++$count;
  352. }
  353. }
  354. $count;
  355. }
  356. my %tmpfiles;
  357. END { unlink_all keys %tmpfiles }
  358. # A regexp that matches the tempfile names
  359. $::tempfile_regexp = 'tmp\d+[A-Z][A-Z]?';
  360. # Avoid ++, avoid ranges, avoid split //
  361. my @letters = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);
  362. sub tempfile {
  363. my $count = 0;
  364. do {
  365. my $temp = $count;
  366. my $try = "tmp$$";
  367. do {
  368. $try = $try . $letters[$temp % 26];
  369. $temp = int ($temp / 26);
  370. } while $temp;
  371. # Need to note all the file names we allocated, as a second request may
  372. # come before the first is created.
  373. if (!-e $try && !$tmpfiles{$try}) {
  374. # We have a winner
  375. $tmpfiles{$try} = 1;
  376. return $try;
  377. }
  378. $count = $count + 1;
  379. } while $count < 26 * 26;
  380. die "Can't find temporary file name starting 'tmp$$'";
  381. }
  382. # This is the temporary file for _fresh_perl
  383. my $tmpfile = tempfile();
  384. #
  385. # _fresh_perl
  386. #
  387. # The $resolve must be a subref that tests the first argument
  388. # for success, or returns the definition of success (e.g. the
  389. # expected scalar) if given no arguments.
  390. #
  391. sub _fresh_perl {
  392. my($prog, $resolve, $runperl_args, $name) = @_;
  393. $runperl_args ||= {};
  394. $runperl_args->{progfile} = $tmpfile;
  395. $runperl_args->{stderr} = 1;
  396. open TEST, ">", $tmpfile or die "Cannot open $tmpfile: $!";
  397. # VMS adjustments
  398. if( $^O eq 'VMS' ) {
  399. $prog =~ s#/dev/null#NL:#;
  400. # VMS file locking
  401. $prog =~ s{if \(-e _ and -f _ and -r _\)}
  402. {if (-e _ and -f _)}
  403. }
  404. print TEST $prog;
  405. close TEST or die "Cannot close $tmpfile: $!";
  406. my $results = runperl(%$runperl_args);
  407. my $status = $?;
  408. # Clean up the results into something a bit more predictable.
  409. $results =~ s/\n+$//;
  410. $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
  411. $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
  412. # bison says 'parse error' instead of 'syntax error',
  413. # various yaccs may or may not capitalize 'syntax'.
  414. $results =~ s/^(syntax|parse) error/syntax error/mig;
  415. if ($^O eq 'VMS') {
  416. # some tests will trigger VMS messages that won't be expected
  417. $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
  418. # pipes double these sometimes
  419. $results =~ s/\n\n/\n/g;
  420. }
  421. my $pass = $resolve->($results);
  422. unless ($pass) {
  423. diag "# PROG: \n$prog\n";
  424. diag "# EXPECTED:\n", $resolve->(), "\n";
  425. diag "# GOT:\n$results\n";
  426. diag "# STATUS: $status\n";
  427. }
  428. # Use the first line of the program as a name if none was given
  429. unless( $name ) {
  430. ($first_line, $name) = $prog =~ /^((.{1,50}).*)/;
  431. $name .= '...' if length $first_line > length $name;
  432. }
  433. ok($pass, "fresh_perl - $name");
  434. }
  435. #
  436. # fresh_perl_is
  437. #
  438. # Combination of run_perl() and is().
  439. #
  440. sub fresh_perl_is {
  441. my($prog, $expected, $runperl_args, $name) = @_;
  442. local $Level = 2;
  443. _fresh_perl($prog,
  444. sub { @_ ? $_[0] eq $expected : $expected },
  445. $runperl_args, $name);
  446. }
  447. #
  448. # fresh_perl_like
  449. #
  450. # Combination of run_perl() and like().
  451. #
  452. sub fresh_perl_like {
  453. my($prog, $expected, $runperl_args, $name) = @_;
  454. local $Level = 2;
  455. _fresh_perl($prog,
  456. sub { @_ ?
  457. $_[0] =~ (ref $expected ? $expected : /$expected/) :
  458. $expected },
  459. $runperl_args, $name);
  460. }
  461. # Set a watchdog to timeout the entire test file
  462. # NOTE: If the test file uses 'threads', then call the watchdog() function
  463. # _AFTER_ the 'threads' module is loaded.
  464. sub watchdog ($;$)
  465. {
  466. my $timeout = shift;
  467. my $method = shift || "";
  468. my $timeout_msg = 'Test process timed out - terminating';
  469. # Valgrind slows perl way down so give it more time before dying.
  470. $timeout *= 10 if $ENV{PERL_VALGRIND};
  471. my $pid_to_kill = $$; # PID for this process
  472. if ($method eq "alarm") {
  473. goto WATCHDOG_VIA_ALARM;
  474. }
  475. # shut up use only once warning
  476. my $threads_on = $threads::threads && $threads::threads;
  477. # Don't use a watchdog process if 'threads' is loaded -
  478. # use a watchdog thread instead
  479. if (!$threads_on || $method eq "process") {
  480. # On Windows and VMS, try launching a watchdog process
  481. # using system(1, ...) (see perlport.pod)
  482. if ($is_mswin || $is_vms) {
  483. # On Windows, try to get the 'real' PID
  484. if ($is_mswin) {
  485. eval { require Win32; };
  486. if (defined(&Win32::GetCurrentProcessId)) {
  487. $pid_to_kill = Win32::GetCurrentProcessId();
  488. }
  489. }
  490. # If we still have a fake PID, we can't use this method at all
  491. return if ($pid_to_kill <= 0);
  492. # Launch watchdog process
  493. my $watchdog;
  494. eval {
  495. local $SIG{'__WARN__'} = sub {
  496. diag("Watchdog warning: $_[0]");
  497. };
  498. my $sig = $is_vms ? 'TERM' : 'KILL';
  499. my $prog = "sleep($timeout);" .
  500. "warn qq/# $timeout_msg" . '\n/;' .
  501. "kill(q/$sig/, $pid_to_kill);";
  502. # On Windows use the indirect object plus LIST form to guarantee
  503. # that perl is launched directly rather than via the shell (see
  504. # perlfunc.pod), and ensure that the LIST has multiple elements
  505. # since the indirect object plus COMMANDSTRING form seems to
  506. # hang (see perl #121283). Don't do this on VMS, which doesn't
  507. # support the LIST form at all.
  508. if ($is_mswin) {
  509. my $runperl = which_perl();
  510. if ($runperl =~ m/\s/) {
  511. $runperl = qq{"$runperl"};
  512. }
  513. $watchdog = system({ $runperl } 1, $runperl, '-e', $prog);
  514. }
  515. else {
  516. my $cmd = _create_runperl(prog => $prog);
  517. $watchdog = system(1, $cmd);
  518. }
  519. };
  520. if ($@ || ($watchdog <= 0)) {
  521. diag('Failed to start watchdog');
  522. diag($@) if $@;
  523. undef($watchdog);
  524. return;
  525. }
  526. # Add END block to parent to terminate and
  527. # clean up watchdog process
  528. eval("END { local \$! = 0; local \$? = 0;
  529. wait() if kill('KILL', $watchdog); };");
  530. return;
  531. }
  532. # Try using fork() to generate a watchdog process
  533. my $watchdog;
  534. eval { $watchdog = fork() };
  535. if (defined($watchdog)) {
  536. if ($watchdog) { # Parent process
  537. # Add END block to parent to terminate and
  538. # clean up watchdog process
  539. eval "END { local \$! = 0; local \$? = 0;
  540. wait() if kill('KILL', $watchdog); };";
  541. return;
  542. }
  543. ### Watchdog process code
  544. # Load POSIX if available
  545. eval { require POSIX; };
  546. # Execute the timeout
  547. sleep($timeout - 2) if ($timeout > 2); # Workaround for perlbug #49073
  548. sleep(2);
  549. # Kill test process if still running
  550. if (kill(0, $pid_to_kill)) {
  551. diag($timeout_msg);
  552. kill('KILL', $pid_to_kill);
  553. if ($is_cygwin) {
  554. # sometimes the above isn't enough on cygwin
  555. sleep 1; # wait a little, it might have worked after all
  556. system("/bin/kill -f $pid_to_kill");
  557. }
  558. }
  559. # Don't execute END block (added at beginning of this file)
  560. $NO_ENDING = 1;
  561. # Terminate ourself (i.e., the watchdog)
  562. POSIX::_exit(1) if (defined(&POSIX::_exit));
  563. exit(1);
  564. }
  565. # fork() failed - fall through and try using a thread
  566. }
  567. # Use a watchdog thread because either 'threads' is loaded,
  568. # or fork() failed
  569. if (eval {require threads; 1}) {
  570. 'threads'->create(sub {
  571. # Load POSIX if available
  572. eval { require POSIX; };
  573. # Execute the timeout
  574. my $time_left = $timeout;
  575. do {
  576. $time_left = $time_left - sleep($time_left);
  577. } while ($time_left > 0);
  578. # Kill the parent (and ourself)
  579. select(STDERR); $| = 1;
  580. diag($timeout_msg);
  581. POSIX::_exit(1) if (defined(&POSIX::_exit));
  582. my $sig = $is_vms ? 'TERM' : 'KILL';
  583. kill($sig, $pid_to_kill);
  584. })->detach();
  585. return;
  586. }
  587. # If everything above fails, then just use an alarm timeout
  588. WATCHDOG_VIA_ALARM:
  589. if (eval { alarm($timeout); 1; }) {
  590. # Load POSIX if available
  591. eval { require POSIX; };
  592. # Alarm handler will do the actual 'killing'
  593. $SIG{'ALRM'} = sub {
  594. select(STDERR); $| = 1;
  595. diag($timeout_msg);
  596. POSIX::_exit(1) if (defined(&POSIX::_exit));
  597. my $sig = $is_vms ? 'TERM' : 'KILL';
  598. kill($sig, $pid_to_kill);
  599. };
  600. }
  601. }
  602. # now my new B::C functions
  603. sub run_cmd {
  604. my ($cmd, $timeout) = @_;
  605. my ($result, $out, $err) = (0, '', '');
  606. if ( ! defined $IPC::Run::VERSION ) {
  607. local $@;
  608. if (ref($cmd) eq 'ARRAY') {
  609. $cmd = join " ", @$cmd;
  610. }
  611. # watchdog(10*$timeout) if $timeout and $ENV{PERL_CORE};
  612. # No real way to trap STDERR?
  613. $cmd .= " 2>&1" if $^O !~ /^MSWin32|VMS/;
  614. warn $cmd."\n" if $ENV{TEST_VERBOSE};
  615. $out = `$cmd`;
  616. warn "# $out\n" if $ENV{TEST_VERBOSE};
  617. $result = $?;
  618. }
  619. else {
  620. my $in;
  621. # XXX TODO this fails with spaces in path. pass and check ARRAYREF then
  622. my @cmd = ref($cmd) eq 'ARRAY' ? @$cmd : split /\s+/, $cmd;
  623. warn join(" ", @cmd)."\n" if $ENV{TEST_VERBOSE};
  624. eval {
  625. # XXX TODO hanging or stacktrace'd children are not killed on cygwin
  626. my $h = IPC::Run::start(\@cmd, \$in, \$out, \$err);
  627. if ($timeout) {
  628. my $secs10 = $timeout / 10;
  629. for (1..$secs10) {
  630. if(!$h->pumpable) {
  631. last;
  632. }
  633. else {
  634. $h->pump_nb;
  635. diag sprintf("waiting %d[s]",$_*10) if $_ > 30;
  636. sleep 10;
  637. }
  638. }
  639. if($h->pumpable) {
  640. $h->kill_kill;
  641. $err .= "Timed out waiting for process exit";
  642. }
  643. }
  644. $h->finish or die "cmd returned $?";
  645. $result = $h->result(0);
  646. };
  647. warn $out."\n" if $out and $ENV{TEST_VERBOSE};
  648. $err .= " \$\@ = $@" if $@;
  649. warn $err."\n" if $err and $ENV{TEST_VERBOSE};
  650. }
  651. return ($result, $out, $err);
  652. }
  653. sub Mblib {
  654. if ($ENV{PERL_CORE}) {
  655. $is_mswin ? '-I..\..\lib' : '-I../../lib';
  656. } else {
  657. $is_mswin ? '-Iblib\arch -Iblib\lib' : '-Iblib/arch -Iblib/lib';
  658. }
  659. }
  660. sub perlcc {
  661. if ($ENV{PERL_CORE}) {
  662. $is_mswin ? 'script\perlcc' : 'script/perlcc'
  663. } else {
  664. $is_mswin ? 'blib\script\perlcc' : 'blib/script/perlcc';
  665. }
  666. }
  667. sub cc_harness {
  668. if ($ENV{PERL_CORE} ) {
  669. $is_mswin ? 'script\cc_harness' : 'script/cc_harness';
  670. } else {
  671. $is_mswin ? 'blib\script\cc_harness' : 'blib/script/cc_harness';
  672. }
  673. }
  674. sub tests {
  675. my $in = shift @ARGV || "t/TESTS";
  676. $in = "TESTS" unless -f $in;
  677. undef $/;
  678. open TEST, "< $in" or die "Cannot open $in";
  679. my @tests = split /\n####+.*##\n/, <TEST>;
  680. close TEST;
  681. delete $tests[$#tests] unless $tests[$#tests];
  682. @tests;
  683. }
  684. sub run_cc_test {
  685. my ($cnt, $backend, $script, $expect, $keep_c, $keep_c_fail, $todo) = @_;
  686. my ($opt, $got);
  687. local($\, $,); # guard against -l and other things that screw with
  688. # print
  689. $expect =~ s/\n$//;
  690. my ($out,$result,$stderr) = ('');
  691. my $fnbackend = lc($backend); #C,-O2
  692. ($fnbackend,$opt) = $fnbackend =~ /^(cc?)(,-o.)?/;
  693. $opt =~ s/,-/_/ if $opt;
  694. $opt = '' unless $opt;
  695. #if ($] > 5.023007 and $fnbackend eq 'cc' and !$Config{usecperl}) {
  696. #print "ok $cnt # skip CC for 5.24\n";
  697. #return 1;
  698. #}
  699. use Config;
  700. require B::C::Config;
  701. if ($] >= 5.025004 and !$Config{usecperl} and is_CI()) {
  702. require Carp;
  703. if ($Carp::VERSION ge '1.42') {
  704. ok(1, "skip Unsupported Carp version $Carp::VERSION >= 1.42");
  705. return 0;
  706. }
  707. }
  708. # note that the smokers run the c.t and c_o3.t tests in parallel, with possible
  709. # interleaving file writes even for the .pl.
  710. my $test = $fnbackend."code".$cnt.$opt.".pl";
  711. my $cfile = $fnbackend."code".$cnt.$opt.".c";
  712. my @obj;
  713. @obj = ($fnbackend."code".$cnt.$opt.".obj",
  714. $fnbackend."code".$cnt.$opt.".ilk",
  715. $fnbackend."code".$cnt.$opt.".pdb")
  716. if $Config{cc} =~ /^cl/i; # MSVC uses a lot of intermediate files
  717. my $exe = $fnbackend."code".$cnt.$opt.$Config{exe_ext};
  718. unlink ($test, $cfile, $exe, @obj);
  719. open T, ">", $test; print T $script; close T;
  720. # Being able to test also the CORE B in older perls
  721. my $Mblib = $] >= 5.009005 ? Mblib() : "";
  722. my $useshrplib = $Config{useshrplib} =~ /^(true|yes)$/;
  723. unless ($Mblib) { # check for -Mblib from the testsuite
  724. if (grep { m{blib(/|\\)arch$} } @INC) {
  725. $Mblib = Mblib(); # forced -Mblib via cmdline without
  726. # printing to stderr
  727. $backend = "-qq,$backend,-q" if !$ENV{TEST_VERBOSE} and $] > 5.007;
  728. }
  729. } else {
  730. $backend = "-qq,$backend,-q" if !$ENV{TEST_VERBOSE} and $] > 5.007;
  731. }
  732. $backend .= ",-fno-warnings" if $] >= 5.013005;
  733. $backend .= ",-fno-fold" if $] >= 5.013009;
  734. $got = run_perl(switches => [ "$Mblib -MO=$backend,-o${cfile}" ],
  735. verbose => $ENV{TEST_VERBOSE}, # for debugging
  736. nolib => $ENV{PERL_CORE} ? 0 : 1, # include ../lib only in CORE
  737. stderr => 1, # to capture the "ccode.pl syntax ok"
  738. timeout => 120,
  739. progfile => $test);
  740. if (! $? and -s $cfile) {
  741. use ExtUtils::Embed ();
  742. $ExtUtils::Embed::Verbose = 1 if $ENV{TEST_VERBOSE} and $ENV{TEST_VERBOSE} >= 2;
  743. my $coredir = $ENV{PERL_CORE}
  744. ? File::Spec->catdir('..', '..')
  745. : File::Spec->catdir($Config{installarchlib}, "CORE");
  746. my $command;
  747. if ($ENV{PERL_CORE}) { # ignore ccopts
  748. if ($is_mswin) {
  749. $command = $Config{optimize}." ".$Config{ccflags}.' -I"..\..\lib\CORE"';
  750. } else {
  751. $command = $Config{optimize}." ".$Config{ccflags}." -I".$coredir;
  752. if ($Config{ccflags} =~ /-flto/ and -s $cfile > 50000) { # too big
  753. diag ("$cfile too big, size ", -s $cfile, " use -O1")
  754. if $ENV{TEST_VERBOSE} > 1;
  755. $command =~ s/-O[23] /-O1 /;
  756. }
  757. }
  758. } else {
  759. $command = ExtUtils::Embed::ccopts();
  760. }
  761. $command .= " -DHAVE_INDEPENDENT_COMALLOC "
  762. if $B::C::Config::have_independent_comalloc;
  763. $command .= " -o $exe $cfile ".$B::C::Config::extra_cflags . " ";
  764. if ($is_msvc) {
  765. if ($Config{ccversion} eq '12.0.8804') {
  766. $command =~ s/ -opt:ref,icf//;
  767. }
  768. $command .= " -Od"; # not only appveyor.
  769. $command =~ s{ [/-]O[123]}{ };
  770. my $obj = $obj[0];
  771. $command =~ s/ \Q-o $exe\E / -c -Fo$obj /;
  772. my $cmdline = "$Config{cc} $command >NUL"; # need to silence it
  773. diag ($cmdline) if $ENV{TEST_VERBOSE} > 1;
  774. run_cmd($cmdline, 20);
  775. $command = '';
  776. }
  777. my $libdir = File::Spec->catdir($Config{prefix}, "lib");
  778. my $so = $Config{so};
  779. my $libperl = $Config{libperl};
  780. my $pkg = ($Config{usecperl} and $libperl =~ /libcperl/) ? "cperl" : "perl";
  781. my $linkargs = $ENV{PERL_CORE}
  782. ? ExtUtils::Embed::_ccdlflags." ".ExtUtils::Embed::_ldflags()
  783. ." -L../.. -l$pkg ".$Config{libs}
  784. : ExtUtils::Embed::ldopts('-std');
  785. # At least cygwin gcc-4.3 crashes with 2x -fstack-protector
  786. $linkargs =~ s/-fstack-protector //
  787. if $command =~ /-fstack-protector /
  788. and $linkargs =~ /-fstack-protector /;
  789. if ($^O =~ /^(cygwin|MSWin32|msys)/) {
  790. if (index($command, "Win32CORE") < 0) {
  791. my $archdir = $ENV{PERL_CORE} ? "../.." : $Config{archlib};
  792. my $win32core = "-L$archdir/lib/auto/Win32CORE -lWin32CORE";
  793. if (-e "$archdir/lib/auto/Win32CORE/Win32CORE.a") {
  794. $win32core = "$archdir/lib/auto/Win32CORE/Win32CORE.a";
  795. }
  796. if ($linkargs =~ / (-lc?perl)/) {
  797. $linkargs =~ s{ (-lc?perl)}{ $win32core $1};
  798. } else {
  799. $linkargs .= " $win32core";
  800. }
  801. }
  802. $linkargs .= " -Od" if $ENV{APPVEYOR} and $^O eq 'MSWin32';
  803. }
  804. if ( -e "$coredir/$libperl" and $libperl !~ /\.$so$/) {
  805. $command .= $linkargs;
  806. } elsif ( $useshrplib and (-e "$libdir/$libperl" or -e "/usr/lib/$libperl")) {
  807. # debian: /usr/lib/libperl.so.5.10.1 and broken ExtUtils::Embed::ldopts
  808. if ($libperl =~ /\.$so$/) {
  809. my $libperlpath = File::Spec->catfile($coredir, $libperl);
  810. $linkargs =~ s|-lperl |$libperlpath |; # link directly
  811. }
  812. $command .= $linkargs;
  813. } else {
  814. $command .= $linkargs;
  815. if ($command !~ /(-lc?perl|CORE\/libperl5)/ and !$is_mswin) {
  816. if ($Config{usecperl} and $useshrplib) {
  817. $command .= " -lcperl";
  818. } else {
  819. $command .= " -lperl";
  820. }
  821. }
  822. }
  823. $command .= $B::C::Config::extra_libs;
  824. my $NULL = $is_mswin ? '' : '2>/dev/null';
  825. my $cmdline = "$Config{cc} $command $NULL";
  826. if ($is_msvc) {
  827. $cmdline = "$Config{ld} $Config{optimize} $linkargs -out:$exe $obj[0] $command";
  828. }
  829. diag ($cmdline) if $ENV{TEST_VERBOSE} and $ENV{TEST_VERBOSE} == 2;
  830. run_cmd($cmdline, 30);
  831. unless (-e $exe) {
  832. if ($ENV{PERL_CORE}) {
  833. if ($^O =~ /^(MSWin32|hpux)/) {
  834. # mingw with gcc and cygwin should work, but not tested.
  835. ok(1, "skip $^O not yet ready");
  836. return 1;
  837. }
  838. }
  839. if ($todo and $todo =~ /TODO /) {
  840. $todo =~ s/TODO //g;
  841. TODO:
  842. {
  843. local $TODO = $todo;
  844. ok(0, "$todo failed $cmdline");
  845. }
  846. } else {
  847. ok(0, "failed $cmdline");
  848. }
  849. print STDERR "# ",system("$Config{cc} $command"), "\n";
  850. #unlink ($test, $cfile, $exe, @obj) unless $keep_c_fail;
  851. return 0;
  852. }
  853. $exe = "./".$exe unless $is_mswin;
  854. # system("/bin/bash -c ulimit -d 1000000") if -e "/bin/bash";
  855. ($result,$out,$stderr) = run_cmd($exe, 5);
  856. if (defined($out) and !$result) {
  857. if ($out =~ /^$expect$/) {
  858. if ($todo eq '#') {
  859. ok(1);
  860. } else {
  861. ok(1, $todo);
  862. }
  863. unlink ($test, $cfile, $exe, @obj) unless $keep_c;
  864. return 1;
  865. } else {
  866. # cc test failed, double check uncompiled
  867. $got = run_perl
  868. (verbose => $ENV{TEST_VERBOSE}, # for debugging
  869. nolib => $ENV{PERL_CORE} ? 0 : 1, # include ../lib only in CORE
  870. stderr => 1, # to capture the "ccode.pl syntax ok"
  871. timeout => 10,
  872. progfile => $test);
  873. if (! $? and $got =~ /^$expect$/) {
  874. $expect =~ s/\n//msg;
  875. $out =~ s/\n//msg;
  876. ok(1, "$todo wanted: \"$expect\", got: \"$out\"");
  877. } else {
  878. ok(1, "skip also fails uncompiled");
  879. return 1;
  880. }
  881. unlink ($test, $cfile, $exe, @obj) unless $keep_c_fail;
  882. return 0;
  883. }
  884. } else {
  885. $out = '';
  886. }
  887. }
  888. if ($todo and $todo =~ /TODO/) {
  889. $todo =~ s/#TODO//g;
  890. TODO:
  891. {
  892. local $TODO = $todo ? $todo : $];
  893. $expect =~ s/\n//msg;
  894. $out =~ s/\n//msg;
  895. ok(0, "wanted: \"$expect\", \$\? = $?, got: \"$out\"");
  896. }
  897. } else {
  898. $expect =~ s/\n//msg;
  899. $out =~ s/\n//msg;
  900. ok(0, "wanted: \"$expect\", \$\? = $?, got: \"$out\"");
  901. }
  902. if ($stderr) {
  903. $stderr =~ s/\n./\n# /xmsg;
  904. print "# $stderr\n";
  905. }
  906. unlink ($test, $cfile, $exe, @obj) unless $keep_c_fail;
  907. return 0;
  908. }
  909. my $is_CI;
  910. sub is_CI {
  911. return $is_CI if defined $is_CI;
  912. $is_CI = ($ENV{TRAVIS}
  913. or $ENV{APPVEYOR}
  914. or $ENV{CI} # circle ci, drone (tea-ci), gitlab
  915. # https://gitlab.com/help/ci/variables/README#variables
  916. # TODO: Azure
  917. )
  918. ? 1 : 0;
  919. return $is_CI;
  920. }
  921. sub prepare_c_tests {
  922. use Config;
  923. if ($^O eq 'VMS') {
  924. print "1..0 # skip - B::C doesn't work on VMS\n";
  925. exit 0;
  926. }
  927. if (($Config{'extensions'} !~ /\bB\b/) ) {
  928. print "1..0 # Skip -- Perl configured without B module\n";
  929. exit 0;
  930. }
  931. if ($is_mswin and $ENV{PERL_CORE}) {
  932. print "1..0 # Skip -- MSWin32 tests not yet ready\n";
  933. exit 0;
  934. }
  935. # with 5.10 and 5.8.9 PERL_COPY_ON_WRITE was renamed to PERL_OLD_COPY_ON_WRITE
  936. if ($Config{ccflags} =~ /-DPERL_OLD_COPY_ON_WRITE/) {
  937. print "1..0 # Skip -- no OLD COW for now\n";
  938. exit 0;
  939. }
  940. if ($ENV{PERL_CORE}
  941. and -f File::Spec->catfile($Config{'sitearch'}, "Opcodes.pm"))
  942. {
  943. print "1..0 # Skip -- <sitearch>/Opcodes.pm installed. Possible XS conflict\n";
  944. exit 0;
  945. }
  946. }
  947. sub run_c_tests {
  948. my $backend = $_[0];
  949. my @todo = @{$_[1]};
  950. my @skip = @{$_[2]};
  951. use Config;
  952. my $AUTHOR = (-d ".git" and !$ENV{NO_AUTHOR}) ? 1 : 0;
  953. my $keep_c = 0; # set it to keep the pl, c and exe files
  954. my $keep_c_fail = 1; # keep on failures
  955. my %todo = map { $_ => 1 } @todo;
  956. my %skip = map { $_ => 1 } @skip;
  957. my @tests = tests();
  958. # add some CC specific tests after 100
  959. # perl -lne "/^\s*sub pp_(\w+)/ && print \$1" lib/B/CC.pm > ccpp
  960. # for p in `cat ccpp`; do echo -n "$p "; grep -m1 " $p[(\[ ]" *.concise; done
  961. #
  962. # grep -A1 "coverage: ny" lib/B/CC.pm|grep sub
  963. # pp_stub pp_cond_expr pp_dbstate pp_reset pp_stringify pp_ncmp pp_preinc
  964. # pp_formline pp_enterwrite pp_leavewrite pp_entergiven pp_leavegiven
  965. # pp_dofile pp_grepstart pp_mapstart pp_grepwhile pp_mapwhile
  966. if ($backend =~ /^CC/) {
  967. local $/;
  968. my $cctests = <<'CCTESTS';
  969. my ($r_i,$i_i,$d_d)=(0,2,3.0); $r_i=$i_i*$i_i; $r_i*=$d_d; print $r_i;
  970. >>>>
  971. 12
  972. ######### 101 - CC types and arith ###############
  973. if ($x eq "2"){}else{print "ok"}
  974. >>>>
  975. ok
  976. ######### 102 - CC cond_expr,stub,scope ############
  977. require B; my $x=1e1; my $s="$x"; print ref B::svref_2object(\$s)
  978. >>>>
  979. B::PV
  980. ######### 103 - CC stringify srefgen ############
  981. @a=(1..4);while($a=shift@a){print $a;}continue{$a=~/2/ and reset q(a);}
  982. >>>>
  983. 12
  984. ######### 104 CC reset ###############################
  985. %int::; %double::; my int $r;my $i:int=2;our double $d=3.0; $r=$i*$i; $r*=$d; print $r;
  986. >>>>
  987. 12
  988. ######### 105 CC attrs ###############################
  989. my $s=q{ok};END{print $s}END{$x = 0}
  990. >>>>
  991. ok
  992. ######### 106 CC 296/297 ###############################
  993. hi(); sub hi{ print q{ok}; }
  994. >>>>
  995. ok
  996. ######### 107 CC sub 491 ###############################
  997. CCTESTS
  998. my $i = 100;
  999. for (split /\n####+.*##\n/, $cctests) {
  1000. next unless $_;
  1001. if ($ENV{PERL_CORE}) {
  1002. s/use blib;//; # fixup blib
  1003. }
  1004. $tests[$i] = $_;
  1005. $i++;
  1006. }
  1007. }
  1008. if (is_CI()
  1009. and ($Config{ccflags} =~ /-flto/ or $ENV{SKIP_SLOW_TESTS})
  1010. and $ENV{PERL_CORE}) {
  1011. diag "skipping slow tests, ".$#tests," => 10";
  1012. @tests = @tests[0..9];
  1013. }
  1014. plan tests => scalar @tests;
  1015. #print "1..".(scalar @tests)."\n";
  1016. my $cnt = 1;
  1017. for (@tests) {
  1018. my $todo = $todo{$cnt} ? "#TODO" : "#";
  1019. # skip empty CC holes to have the same test indices in STATUS and t/testcc.sh
  1020. unless ($_) {
  1021. ok(1, "skip hole for CC");
  1022. $cnt++;
  1023. next;
  1024. }
  1025. # to bypass CI timeouts (no output has been received in the last 10m0s)
  1026. warn "# $cnt...\n" if is_CI() and ($cnt % 10 == 0);
  1027. # only once. skip subsequent tests 29 on MSVC. 7:30min!
  1028. if ($cnt == 29 and !$AUTHOR) {
  1029. $todo{$cnt} = $skip{$cnt} = 1;
  1030. }
  1031. if ($todo{$cnt} and $skip{$cnt} and
  1032. # those are currently blocking the system
  1033. # do not even run them at home if TODO+SKIP
  1034. (!$AUTHOR
  1035. or ($cnt==15 and $backend eq 'C,-O1') # hanging
  1036. or ($cnt==103 and $backend eq 'CC,-O2') # hanging
  1037. ))
  1038. {
  1039. ok(1, "skip $cnt");
  1040. } else {
  1041. my ($script, $expect) = split />>>+\n/;
  1042. die "Invalid empty t/TESTS" if !$script or $expect eq '';
  1043. if ($cnt == 4 and $] >= 5.017005) {
  1044. $expect = 'zzz2y2y2';
  1045. }
  1046. run_cc_test($cnt, $backend.($cnt == 46 ? ',-fstash' : ''),
  1047. $script, $expect, $keep_c, $keep_c_fail, $todo);
  1048. }
  1049. $cnt++;
  1050. }
  1051. }
  1052. sub plctestok {
  1053. my ($num, $base, $script, $todo) = @_;
  1054. plctest($num,'^ok', $base, $script, $todo);
  1055. }
  1056. sub plctest {
  1057. my ($num, $expected, $base, $script, $todo) = @_;
  1058. if ($] > 5.021006 and !$B::C::Config::have_byteloader) {
  1059. ok(1, "SKIP perl5.22 broke ByteLoader");
  1060. return 1;
  1061. }
  1062. if ($is_mswin and $ENV{PERL_CORE}) {
  1063. ok(1, "SKIP MSWin32 tests not yet ready");
  1064. return 1;
  1065. }
  1066. my $name = $base."_$num";
  1067. unlink($name, "$name.plc", "$name.pl", "$name.exe");
  1068. open F, ">", "$base.pl";
  1069. print F $script;
  1070. print F "\n";
  1071. close F;
  1072. my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
  1073. # we don't want to change STDOUT/STDERR on STDOUT/STDERR tests, so no -qq
  1074. my $nostdoutclobber = $base !~ /^ccode93i/;
  1075. my $b = ($] > 5.008 and $nostdoutclobber) ? "-qq,Bytecode" : "Bytecode";
  1076. my $Mblib = Mblib;
  1077. my $cmd = "$runperl $Mblib -MO=$b,-o$name.plc $base.pl";
  1078. diag($cmd) if $ENV{TEST_VERBOSE} and $ENV{TEST_VERBOSE} > 1;
  1079. system $cmd;
  1080. # $out =~ s/^$base.pl syntax OK\n//m;
  1081. unless (-e "$name.plc") {
  1082. ok(0, '#B::Bytecode failed');
  1083. return 1;
  1084. }
  1085. $cmd = "$runperl $Mblib -MByteLoader $name.plc";
  1086. diag($cmd) if $ENV{TEST_VERBOSE} and $ENV{TEST_VERBOSE} > 1;
  1087. my $out = qx($cmd);
  1088. chomp $out;
  1089. my $ok = $out =~ /$expected/;
  1090. if ($todo and $todo =~ /TODO/) {
  1091. $todo =~ s/TODO //g;
  1092. TODO: {
  1093. local $TODO = $todo;
  1094. ok($ok);
  1095. }
  1096. } else {
  1097. ok($ok, $todo ? "$todo" : '');
  1098. }
  1099. if ($ok) {
  1100. unlink("$name.plc", "$base.pl");
  1101. }
  1102. }
  1103. sub ctestok {
  1104. my ($num, $backend, $base, $script, $todo) = @_;
  1105. my $qr = '^ok'; # how lame
  1106. ctest($num, $qr, $backend, $base, $script, $todo);
  1107. }
  1108. sub ctest {
  1109. my ($num, $expected, $backend, $base, $script, $todo) = @_;
  1110. my $name = $base."_$num";
  1111. my $b = $backend; # protect against parallel test name clashes
  1112. my $CPERL = $Config{usecperl};
  1113. #if ($] > 5.021006 and $backend =~ /^CC/i) { ok(1, "skip CC for 5.22 WIP"); return 1; }
  1114. #if ($] >= 5.025 and !$CPERL and $todo !~ /TODO /) {
  1115. # $todo .= 'TODO - no 5.26 yet';
  1116. #}
  1117. $b =~ s/-(D.*|f.*|v),//g;
  1118. $b =~ s/-/_/g;
  1119. $b =~ s/[, ]//g;
  1120. $b = lc($b);
  1121. $name .= $b;
  1122. unlink($name, "$name.c", "$name.pl", "$name.exe");
  1123. open F, ">", "$name.pl";
  1124. print F $script;
  1125. close F;
  1126. my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
  1127. # we don't want to change STDOUT/STDERR on STDOUT/STDERR tests, so no -qq
  1128. my $nostdoutclobber = $base !~ /^ccode93i/;
  1129. my $post = '';
  1130. my $Mblib = Mblib();
  1131. $b = ($] > 5.008 and $nostdoutclobber) ? "-qq,$backend" : "$backend";
  1132. ($b, $post) = split(" ", $b);
  1133. $post = '' unless $post;
  1134. $b .= q(,-fno-fold,-fno-warnings) if $] >= 5.013005 and $b !~ /-(O3|ffold|fwarnings)/;
  1135. diag("$runperl $Mblib -MO=$b,-o$name.c $post $name.pl")
  1136. if $ENV{TEST_VERBOSE} and $ENV{TEST_VERBOSE} > 1;
  1137. system "$runperl $Mblib -MO=$b,-o$name.c $post $name.pl";
  1138. unless (-e "$name.c") {
  1139. ok (undef, "$todo B::$backend failed to compile");
  1140. return 1;
  1141. }
  1142. my $cc_harness = cc_harness();
  1143. my $cmd = "$runperl $Mblib $cc_harness -q ".($is_msvc ? "" : "-o $name ")."$name.c";
  1144. if ($ENV{TEST_VERBOSE} and $ENV{TEST_VERBOSE} > 1) {
  1145. $cmd =~ s/ -q / /;
  1146. diag("$cmd");
  1147. }
  1148. system "$cmd";
  1149. my $exe = $name.$Config{exe_ext};
  1150. unless (-e $exe) {
  1151. if ($ENV{PERL_CORE} and $is_msvc) {
  1152. ok(1, "skip MSVC"); return 1;
  1153. }
  1154. if ($todo and $todo =~ /TODO/) {
  1155. $todo =~ s/TODO //g;
  1156. TODO: {
  1157. local $TODO = $todo;
  1158. ok(undef, "failed to compile");
  1159. }
  1160. } else {
  1161. ok(undef, "failed to compile $todo");
  1162. }
  1163. return;
  1164. }
  1165. $exe = "./".$exe unless $is_mswin;
  1166. ($result,$out,$stderr) = run_cmd($exe, 5);
  1167. my $ok;
  1168. if (defined($out) and !$result) {
  1169. chomp $out;
  1170. $ok = $out =~ /$expected/;
  1171. diag($out) if $ENV{TEST_VERBOSE};
  1172. unless ($ok) { #crosscheck uncompiled
  1173. my $out1 = `$runperl $name.pl`;
  1174. unless ($out1 =~ /$expected/) {
  1175. ok(1, "skip also fails uncompiled $todo");
  1176. return 1;
  1177. }
  1178. }
  1179. if ($todo and $todo =~ /TODO/) {
  1180. $todo =~ s/TODO //g;
  1181. TODO: {
  1182. local $TODO = $todo;
  1183. ok ($out =~ /$expected/);
  1184. diag($out) if $ENV{TEST_VERBOSE};
  1185. }
  1186. } else {
  1187. ok ($out =~ /$expected/, $todo);
  1188. }
  1189. } else {
  1190. if ($todo and $todo =~ /TODO/) {
  1191. $todo =~ s/TODO //g;
  1192. TODO: {
  1193. local $TODO = $todo;
  1194. ok (undef);
  1195. }
  1196. } else {
  1197. #crosscheck uncompiled
  1198. my $out1 = `$runperl $name.pl`;
  1199. unless ($out1 =~ /$expected/) {
  1200. ok(1, "skip also fails uncompiled");
  1201. return $ok;
  1202. }
  1203. ok (undef, $todo);
  1204. }
  1205. }
  1206. unlink("$name.pl");
  1207. if ($ok) {
  1208. unlink($name, "$name.c", "$name.exe");
  1209. }
  1210. $ok
  1211. }
  1212. sub ccompileok {
  1213. my ($num, $backend, $base, $script, $todo) = @_;
  1214. my $name = $base."_$num";
  1215. unlink($name, "$name.c", "$name.pl", "$name.exe");
  1216. open F, ">", "$name.pl";
  1217. print F $script;
  1218. close F;
  1219. my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
  1220. my $b = $] > 5.008 ? "-qq,$backend" : "$backend";
  1221. my $Mblib = Mblib();
  1222. system "$runperl $Mblib -MO=$b,-o$name.c $name.pl";
  1223. unless (-e "$name.c") {
  1224. ok (undef, "#B::$backend failed");
  1225. return 1;
  1226. }
  1227. my $cc_harness = cc_harness();
  1228. system "$runperl $Mblib $cc_harness -q -o $name $name.c";
  1229. my $ok = -e $name or -e "$name.exe";
  1230. if ($todo and $todo =~ /TODO/) {
  1231. TODO: {
  1232. $todo =~ s/TODO //g;
  1233. local $TODO = $todo;
  1234. ok($ok);
  1235. }
  1236. } else {
  1237. ok($ok, $todo);
  1238. }
  1239. unlink("$name.pl");
  1240. if ($ok) {
  1241. unlink($name, "$name.c", "$name.exe");
  1242. }
  1243. }
  1244. sub todo_tests_default {
  1245. my $what = shift;
  1246. my $DEBUGGING = ($Config{ccflags} =~ m/-DDEBUGGING/);
  1247. my $ITHREADS = ($Config{useithreads});
  1248. my $CPERL = ($Config{usecperl});
  1249. my @todo = ();
  1250. # no IO::Scalar
  1251. push @todo, (15) if $] < 5.007;
  1252. # broken by fbb32b8bebe8ad C: revert *-,*+,*! fetch magic, assign all core GVs to their global symbols
  1253. push @todo, (42..43) if $] < 5.012 or $^O eq 'cygwin';
  1254. push @todo, 28 if $] > 5.023 and
  1255. ($Config{cc} =~ / -m32/ or $Config{ccflags} =~ / -m32/);
  1256. push @todo, (21, 38) if $^O eq 'cygwin'; #hangs
  1257. push @todo, (15,27,41..45) if $] >= 5.025 and !$CPERL;
  1258. if ($what =~ /^c(|_o[1-4])$/) {
  1259. # a regression
  1260. push @todo, (41) if $] < 5.007; #regressions
  1261. push @todo, (12) if $what eq 'c_o3' and !$ITHREADS and $] >= 5.008009 and $] < 5.010;
  1262. #push @todo, (48) if $] >= 5.018; # opfree
  1263. push @todo, (48) if $what eq 'c_o4' and $] < 5.021 and $ITHREADS;
  1264. push @todo, (8,18,19,25,26,28) if $what eq 'c_o4' and !$ITHREADS;
  1265. #push @todo, (10) if $what eq 'c_o4' and $] > 5.023;
  1266. push @todo, (29) if $] >= 5.021006 and $ITHREADS;
  1267. push @todo, (10,15,27,41,42,43,44,45,49,50)
  1268. if $] >= 5.021006 and $what eq 'c_o4';
  1269. push @todo, (13,18,29,34)
  1270. if $] >= 5.021006 and $what eq 'c_o4' and $ITHREADS;
  1271. push @todo, (12,14,38)
  1272. if $] >= 5.021006 and $what eq 'c_o4' and !$ITHREADS;
  1273. } elsif ($what =~ /^cc/) {
  1274. push @todo, (21,30,105,106);
  1275. push @todo, (22,41,45,103) if $] < 5.007; #regressions
  1276. push @todo, (104,105) if $] < 5.007; # leaveloop, no cxstack
  1277. push @todo, (42,43) if $] > 5.008 and $] <= 5.008005 and !$ITHREADS;
  1278. #push @todo, (33,45) if $] >= 5.010 and $] < 5.012;
  1279. push @todo, (10,16,50) if $what eq 'cc_o2';
  1280. push @todo, (29) if $] < 5.008008;
  1281. push @todo, (22) if $] < 5.010 and !$ITHREADS;
  1282. push @todo, (46); # HvKEYS(%Exporter::) is 0 unless Heavy is included also
  1283. # solaris also. I suspected nvx<=>cop_seq_*
  1284. push @todo, (12) if $is_mswin and $Config{cc} =~ /^cl/i;
  1285. push @todo, (26) if $what =~ /^cc_o[12]/;
  1286. push @todo, (27) if $] > 5.008008 and $] < 5.009;
  1287. #push @todo, (27) if $] > 5.008008 and $] < 5.009 and $what eq 'cc_o2';
  1288. push @todo, (103) if ($] >= 5.012 and $] < 5.014 and !$ITHREADS);
  1289. push @todo, (12,19) if $] >= 5.019; # XXX had 25 also
  1290. push @todo, (25) if $] >= 5.021006 and !$CPERL;
  1291. push @todo, (29) if $] >= 5.021006 and $what eq 'cc_o1';
  1292. push @todo, (24,29) if $] >= 5.021006 and $what eq 'cc_o2';
  1293. push @todo, (103) if $CPERL and $ITHREADS;
  1294. push @todo, (103) if $^O eq 'cygwin';
  1295. push @todo, (9,10,15,24,26,27,41..45,103) if $] > 5.023007 and !$CPERL;
  1296. }
  1297. push @todo, (48) if $] > 5.007 and $] < 5.009 and $^O =~ /MSWin32|cygwin/i;
  1298. return @todo;
  1299. }
  1300. 1;
  1301. # Local Variables:
  1302. # mode: cperl
  1303. # cperl-indent-level: 4
  1304. # fill-column: 78
  1305. # End:
  1306. # vim: expandtab shiftwidth=4: