1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
package Local::Module::Build;
use strict;
use base qw(Module::Build);
use ExtUtils::CBuilder;
# A very conservative set of default limits for the system time.h
my %Limit_Defaults = (
gmtime => {
max => 2147483647,
min => 0,
},
localtime => {
max => 2147483647,
min => 0,
},
mktime => {
max => {
tm_gmtoff => 0,
tm_hour => 3,
tm_isdst => 0,
tm_mday => 19,
tm_min => 14,
tm_mon => 0,
tm_sec => 7,
tm_wday => 2,
tm_yday => 18,
tm_year => 138,
tm_zone => "UTC",
},
min => {
tm_gmtoff => 0,
tm_hour => 0,
tm_isdst => 0,
tm_mday => 1,
tm_min => 0,
tm_mon => 0,
tm_sec => 0,
tm_wday => 4,
tm_yday => 0,
tm_year => 70,
tm_zone => "UTC",
},
},
timegm => {
max => {
tm_gmtoff => 0,
tm_hour => 3,
tm_isdst => 0,
tm_mday => 19,
tm_min => 14,
tm_mon => 0,
tm_sec => 7,
tm_wday => 2,
tm_yday => 18,
tm_year => 138,
tm_zone => "UTC",
},
min => {
tm_gmtoff => 0,
tm_hour => 0,
tm_isdst => 0,
tm_mday => 1,
tm_min => 0,
tm_mon => 0,
tm_sec => 0,
tm_wday => 4,
tm_yday => 0,
tm_year => 70,
tm_zone => "UTC",
}
}
);
sub is_osx_106 {
return 0 unless $^O eq 'darwin';
my $version = `sw_vers -productVersion`;
return $version =~ m{^10\.6\.};
}
sub probe_system_time {
my $self = shift;
$self->note_time_capabilities;
$self->munge("y2038/time64_config.h.in" => "y2038/time64_config.h");
$self->note_time_limits;
$self->munge("y2038/time64_limits.h.in" => "y2038/time64_limits.h");
}
sub munge {
my $self = shift;
my($src, $dest) = @_;
return if $self->up_to_date(["munge_config",$src] => [$dest]);
system $^X, "munge_config", $src, $dest;
$self->add_to_cleanup($dest);
}
sub note_time_capabilities {
my $self = shift;
return if $self->up_to_date(["Build", "y2038/time64_limits.h.in"], ["y2038/time64_limits.h"]);
my %tests = (
HAS_TIMEGM => <<'END',
struct tm *date;
time_t zero;
date = localtime(&zero);
zero = timegm(date);
END
HAS_GMTIME_R => <<'END',
struct tm date;
time_t zero = 0;
(void)gmtime_r(&zero, &date);
END
HAS_LOCALTIME_R => <<'END',
struct tm date;
time_t zero = 0;
(void)localtime_r(&zero, &date);
END
HAS_TM_TM_GMTOFF => <<'END',
struct tm *date;
time_t zero;
int offset;
date = gmtime(&zero);
offset = date->tm_gmtoff;
END
HAS_TM_TM_ZONE => <<'END',
struct tm *date;
time_t zero;
char *zone;
date = gmtime(&zero);
zone = date->tm_zone;
END
);
warn "Probing time.h capabilities.\n";
warn "You may see some C errors, that's ok.\n";
my $cb = ExtUtils::CBuilder->new( quiet => 1 );
for my $test (keys %tests) {
my $code = $tests{$test};
$code = <<END;
#include <time.h>
int main() {
$code
return 0;
}
END
open my $fh, ">", "try.c" or die "Can't write try.c: $!";
print $fh $code;
close $fh;
my $exe = eval {
# Compile AND link to force undefined symbols to error
my $obj = $cb->compile(source => "try.c");
my $exe = $cb->link_executable(objects => $obj, exe_file => "try");
unlink $obj;
$exe;
};
$self->notes($test, $exe ? 1 : 0);
unlink $exe if $exe;
unlink "try.c";
}
}
sub note_time_limits {
my $self = shift;
my $source = "check_max.c";
my $exe = $self->notes("check_max") || "check_max";
unless( $self->up_to_date($source => $exe) ) {
warn "Building a program to test the range of your system time functions...\n";
my $cb = $self->cbuilder;
my $obj = $cb->compile(source => "check_max.c", include_dirs => ['y2038']);
$exe = $cb->link_executable(objects => $obj, exe_file => $exe);
$exe = $self->find_real_exe($exe);
$self->notes(check_max => $exe);
$self->add_to_cleanup($obj, $exe);
}
return if $self->up_to_date(["y2038/time64_limits.h.in", "munge_config", $exe]
=> "y2038/time64_limits.h");
warn " and running it...\n";
require JSON;
my $limits;
my $json = run_in_alarm( 2, sub {
return `./$exe`;
}, sub {
warn " time limit check timed out, using defaults";
return;
});
warn " Done.\n";
if( $json ) {
$json =~ s{^\#.*\n}{}gm;
$limits = JSON::from_json($json);
}
else {
$limits = \%Limit_Defaults;
}
my %config;
for my $key (qw(gmtime localtime)) {
for my $limit (qw(min max)) {
$config{$key."_".$limit} = $limits->{$key}{$limit};
}
}
for my $key (qw(mktime timegm)) {
for my $limit (qw(min max)) {
my $struct = $limits->{$key}{$limit};
for my $tm (keys %$struct) {
$config{$key."_".$limit."_".$tm} = $struct->{$tm};
}
}
}
# Windows lies about being able to handle just a little bit of
# negative time.
for my $key (qw(gmtime_min localtime_min)) {
if( -10_000 < $config{$key} && $config{$key} < 0 ) {
$config{$key} = 0;
}
}
# OS X 10.6's gmtime is broken before -70546986201600
# See Apple bug 7654647
if( is_osx_106 ) {
for my $key (qw(gmtime_min localtime_min)) {
if( $config{$key} < -70546986201600 ) {
$config{$key} = -70546986201600;
}
}
}
for my $key (sort { $a cmp $b } keys %config) {
my $val = $config{$key};
warn sprintf "%15s: %s\n", $key, $val;
$self->notes($key, "$val");
}
return;
}
sub run_in_alarm {
my($timeout, $try, $catch) = @_;
my(@ret, $ret);
my $pass = eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm $timeout;
wantarray ? @ret = $try->() : $ret = $try->();
alarm 0;
1;
};
return wantarray ? @ret : $ret if $pass;
return $catch->() if $@ eq "alarm\n";
die $@;
}
# This is necessary because Cygwin nerotically puts a .exe on
# every executable. This appears to be built into gcc.
sub find_real_exe {
my $self = shift;
my $exe = shift;
my $real_exe;
for ($exe, "$exe.exe") {
$real_exe = $_ if -e;
}
warn "Can't find the executable, thought it was $exe"
unless defined $real_exe;
return $real_exe;
}
sub ACTION_code {
my $self = shift;
$self->probe_system_time;
return $self->SUPER::ACTION_code(@_);
}
1;
|