| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # This script builds .c files with constants, to be included in the XS | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use blib; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | sub dump_table($$$); | ||||||||||||||||||||||||||||
| 14 | sub from_listing($$); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Maybe someone uses cross-compilation | ||||||||||||||||||||||||||||
| 17 | 1 | ▶ | my $system = lc ($ENV{OSNAME} || $^O); | ||||||||||||||||||||||||||
|
Condition33%1/3
$ENV{'OSNAME'} || $^O
MC/DC0%0/2
$ENV{'OSNAME'} || $^O
$ENV{'OSNAME'}
$^O
Truth table33%1/3
A || B
A= $ENV{'OSNAME'}B= $^O
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | my %float_as_string = map +($_ => 1), qw/FLT_MAX HUGE_VAL/; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | my ( %sc, %cs, %posix, %math, %pc, %ul, %rl, %events, %sig | |||||||||||||||||||||||||||
| 22 | , %fdio, %fsys, %errno, %fcntl, %socket, %locale, %osconsts | ||||||||||||||||||||||||||||
| 23 | , %proc, %time, %user); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | from_listing \%cs, 'confstr.txt'; | |||||||||||||||||||||||||||
| 26 | 1 | from_listing \%errno, 'errno.txt'; | |||||||||||||||||||||||||||
| 27 | 1 | from_listing \%fcntl, 'fcntl.txt'; | |||||||||||||||||||||||||||
| 28 | 1 | from_listing \%fdio, 'fdio.txt'; | |||||||||||||||||||||||||||
| 29 | 1 | from_listing \%fsys, 'fsys.txt'; | |||||||||||||||||||||||||||
| 30 | 1 | from_listing \%locale,'locale.txt'; | |||||||||||||||||||||||||||
| 31 | 1 | from_listing \%math, 'math.txt'; | |||||||||||||||||||||||||||
| 32 | 1 | from_listing \%osconsts,'osconsts.txt'; | |||||||||||||||||||||||||||
| 33 | 1 | from_listing \%pc, 'pathconf.txt'; | |||||||||||||||||||||||||||
| 34 | 1 | from_listing \%events,'events.txt'; | |||||||||||||||||||||||||||
| 35 | 1 | from_listing \%posix, 'posix.txt'; | |||||||||||||||||||||||||||
| 36 | 1 | from_listing \%proc, 'proc.txt'; | |||||||||||||||||||||||||||
| 37 | 1 | from_listing \%rl, 'rlimit.txt'; | |||||||||||||||||||||||||||
| 38 | 1 | from_listing \%sc, 'sysconf.txt'; | |||||||||||||||||||||||||||
| 39 | 1 | from_listing \%sig, 'signals.txt'; | |||||||||||||||||||||||||||
| 40 | 1 | from_listing \%time, 'time.txt'; | |||||||||||||||||||||||||||
| 41 | 1 | from_listing \%ul, 'ulimit.txt'; | |||||||||||||||||||||||||||
| 42 | 1 | from_listing \%user, 'user.txt'; | |||||||||||||||||||||||||||
| 43 | 1 | from_listing \%socket,'socket.txt'; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | #use Data::Dumper; | ||||||||||||||||||||||||||||
| 46 | #warn Dumper \%sc, \%cs, \%posix, \%pc; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | dump_table(\%cs, 'cs_table', 'confstr.c' ); | |||||||||||||||||||||||||||
| 49 | 1 | dump_table(\%errno, 'errno_table', 'errno.c' ); | |||||||||||||||||||||||||||
| 50 | 1 | dump_table(\%fcntl, 'fcntl_table', 'fcntl.c' ); | |||||||||||||||||||||||||||
| 51 | 1 | dump_table(\%fdio, 'fdio_table', 'fdio.c' ); | |||||||||||||||||||||||||||
| 52 | 1 | dump_table(\%fsys, 'fsys_table', 'fsys.c' ); | |||||||||||||||||||||||||||
| 53 | 1 | dump_table(\%locale,'locale_table','locale.c' ); | |||||||||||||||||||||||||||
| 54 | 1 | dump_table(\%math, 'math_table', 'math.c' ); | |||||||||||||||||||||||||||
| 55 | 1 | dump_table(\%osconsts,'os_table', 'osconsts.c' ); | |||||||||||||||||||||||||||
| 56 | 1 | dump_table(\%pc, 'pc_table', 'pathconf.c' ); | |||||||||||||||||||||||||||
| 57 | 1 | dump_table(\%events,'events_table','events.c' ); | |||||||||||||||||||||||||||
| 58 | 1 | dump_table(\%posix, 'pr_table', 'properties.c'); | |||||||||||||||||||||||||||
| 59 | 1 | dump_table(\%proc, 'proc_table', 'proc.c' ); | |||||||||||||||||||||||||||
| 60 | 1 | dump_table(\%rl, 'rl_table', 'rlimit.c' ); | |||||||||||||||||||||||||||
| 61 | 1 | dump_table(\%sc, 'sc_table', 'sysconf.c' ); | |||||||||||||||||||||||||||
| 62 | 1 | dump_table(\%sig, 'sig_table', 'signals.c' ); | |||||||||||||||||||||||||||
| 63 | 1 | dump_table(\%time, 'time_table', 'time.c' ); | |||||||||||||||||||||||||||
| 64 | 1 | dump_table(\%ul, 'ul_table', 'ulimit.c' ); | |||||||||||||||||||||||||||
| 65 | 1 | dump_table(\%user, 'user_table', 'user.c' ); | |||||||||||||||||||||||||||
| 66 | 1 | dump_table(\%socket,'socket_table','socket.c' ); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # System specific compilation helpers | ||||||||||||||||||||||||||||
| 69 | 1 | ▶ | open SYSTEM, '>', 'system.c' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | or die "cannot write to system.c: $!"; | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | my $incl = File::Spec->catfile('system', $system.'.c'); | |||||||||||||||||||||||||||
| 73 | 1 | ▶ | if(-f $incl) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | { print SYSTEM qq{#include "$incl"\n}; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | ▶ | close SYSTEM | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | or die "write errors to system.c: $!"; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | exit 0; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub dump_table($$$) | ||||||||||||||||||||||||||||
| 83 | 19 | ▶ | { my ($consts, $table, $filename) = @_; | ||||||||||||||||||||||||||
|
Subroutine
dump_table: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 84 | 19 | local *TABLE; | |||||||||||||||||||||||||||
| 85 | 19 | ▶ | open TABLE, '>', $filename | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | or die "cannot write to $filename: $!\n"; | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 19 | print TABLE "/* Generated */\n\n" | |||||||||||||||||||||||||||
| 89 | , "missing_is_sorted = 0;\n\n"; | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 19 | foreach my $const (sort keys %$consts) | |||||||||||||||||||||||||||
| 92 | 1610 | { my $klen = length $const; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1610 | my $store; | |||||||||||||||||||||||||||
| 95 | 1610 | ▶ | if($const =~ /^(?:DBL_|LDBL_|FLT_)/ || $float_as_string{$const}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$const =~ /^(?:DBL_|LDBL_|FLT_)/ or $float_as_string{$const}
MC/DC100%2/2
$const =~ /^(?:DBL_|LDBL_|FLT_)/ or $float_as_string{$const}
$const =~ /^(?:DBL_|LDBL_|FLT_)/
$float_as_string{$const}
Truth table100%3/3
A or B
A= $const =~ /^(?:DBL_|LDBL_|FLT_)/B= $float_as_string{$const}
| |||||||||||||||||||||||||||||
| 96 | 30 | { $store = <<_FLOAT; | |||||||||||||||||||||||||||
| 97 | sprintf(float_string, "%g", $const); | ||||||||||||||||||||||||||||
| 98 | (void)hv_store($table, "$const", $klen, newSVpv(float_string, 0), 0); | ||||||||||||||||||||||||||||
| 99 | _FLOAT | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | else | ||||||||||||||||||||||||||||
| 102 | 1580 | ▶ | { my $cast = $const =~ m/^SIG_/ ? '(long)' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 1580 | $store = <<_INT; | |||||||||||||||||||||||||||
| 104 | (void)hv_store($table, "$const", $klen, newSViv($cast$const), 0); | ||||||||||||||||||||||||||||
| 105 | _INT | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1610 | print TABLE <<_CONST | |||||||||||||||||||||||||||
| 109 | #ifdef $const | ||||||||||||||||||||||||||||
| 110 | $store#else | ||||||||||||||||||||||||||||
| 111 | missing[nr_missing++] = "$const"; | ||||||||||||||||||||||||||||
| 112 | #endif | ||||||||||||||||||||||||||||
| 113 | _CONST | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 19 | close TABLE; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub from_listing($$) | ||||||||||||||||||||||||||||
| 120 | 19 | ▶ | { my ($table, $filename) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_listing: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 121 | 19 | my $path = File::Spec->catfile('lists', $filename); | |||||||||||||||||||||||||||
| 122 | 19 | local *IN; | |||||||||||||||||||||||||||
| 123 | 19 | ▶ | open IN, '<', $path | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | or die "cannot read from $path: $!"; | ||||||||||||||||||||||||||||
| 125 | 19 | ▶ | while(<IN>) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 1699 | ▶ | { next if m/^#|^\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 1614 | chomp; | |||||||||||||||||||||||||||
| 128 | 1614 | $table->{$_}++; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 19 | ▶ | close IN | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | or die "errors while reading $path: $!"; | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||