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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
# run with -l switch
#
# check a bunch of stuff.
# write #define/#undef to file given in ARGV[0].
# now also writes #include's too.
#
use strict;
use warnings;
use Config;
use List::Util;
use Devel::CheckLib;
my ($out, $flagfile) = @ARGV;
my $conf = [];
my $flags = [];
my $WIN32 = 0; # native
generate_conf($conf, $flags);
write_conf ($conf, $out);
exit 0;
sub generate_conf {
my ($defines, $flags) = @_;
my $longest = 0;
# if lencheck is 1, \$longest is longest so far. sub is expected to
# update it if it has one longer and skip actual test.
#
# if lencheck is 0, \$longest contains global longest. sub is expected
# to use that when printing and to run the test.
for my $check ( 1, 0 ) {
# win32 native if on windows but not under some
# sort of unix environ. i.e. native.
# sets $WIN32. do this first.
try_win32_native ($check, \$longest, $defines);
try_headers($check, \$longest, $defines, qw(
-HAVE_WINDOWS_H Windows.h
HAVE_WINSOCK2_H winsock2.h
HAVE_IPHLPAPI_H iphlpapi.h
HAVE_CTYPE_H ctype.h
HAVE_ERR_H err.h
HAVE_ERRNO_H errno.h
-HAVE_FCNTL_H fcntl.h
HAVE_INTTYPES_H inttypes.h
HAVE_IO_H io.h
HAVE_NETINET_IN_H netinet/in.h
HAVE_NET_IF_DL_H net/if_dl.h
HAVE_NET_IF_H net/if.h
HAVE_PROCESS_H process.h
HAVE_STDINT_H stdint.h
HAVE_STDIO_H stdio.h
HAVE_STDLIB_H stdlib.h
HAVE_STRING_H string.h
HAVE_SYS_FILE_H sys/file.h
HAVE_SYS_IOCTL_H sys/ioctl.h
-HAVE_SYS_RANDOM_H sys/random.h
HAVE_SYS_RESOURCE_H sys/resource.h
HAVE_SYS_SOCKET_H sys/socket.h
HAVE_SYS_SOCKIO_H sys/sockio.h
HAVE_SYS_STAT_H sys/stat.h
-HAVE_SYS_TIME_H sys/time.h
HAVE_SYS_TYPES_H sys/types.h
HAVE_SYS_WAIT_H sys/wait.h
-HAVE_TIME_H time.h
HAVE_UNISTD_H unistd.h
));
#try_getentropy ($check, \$longest, $defines);
#try_getrandom ($check, \$longest, $defines);
try_lstat ($check, \$longest, $defines);
try_sa_len ($check, \$longest, $defines);
try_symlink ($check, \$longest, $defines);
#try_threads ($check, \$longest, $defines);
}
}
sub try_headers {
my ($check, $long, $defs, @try) = @_;
for ( List::Util::pairs(@try) ) {
my ($key, $val) = @$_;
next if $key =~ /^-/;
check_header($check, $long, $defs, $key, $val);
}
}
sub check_header {
my ($check, $long, $defs, $def, $hdr) = @_;
my $msg = join ' ', 'Checking', $hdr;
my $len = length $msg;
if ($check) {
$$long = $len if $len > $$long;
return;
}
my $more = $$long - $len;
my ($conf, $fh) = gen_confh_fh($defs);
print $fh join("\n", map {s/^\s+//; $_} split(/\n/, qq{
#include <${hdr}>
}));
close $fh;
my $win32permit = 1;
$win32permit = 0 if !$WIN32 && $def =~ /WINSOCK/;
$win32permit = 0 if !$WIN32 && $def =~ /IPHLPAPI/;
if ( $win32permit && check_lib(
header => $conf,
#lib => 'c', # because not linking here.. duh!
ccflags => '-I.',
debug => 0,
)) {
push @$defs, "#define $def 1";
push @$defs, "#include <${hdr}>";
print $msg, ' ..', '.'x$more, ' found';
}
else {
push @$defs, "#undef $def";
print $msg, ' ..', '.'x$more, ' missing';
}
unlink $conf;
}
sub gen_confh {
my ($defs) = @_;
my ($fh, $fname) = File::Temp::tempfile(
'assertconfXXXXXXXX', SUFFIX => '.h', UNLINK => 0
);
print $fh $_ for @$defs;
close $fh;
return $fname;
}
sub gen_confh_fh {
my ($defs) = @_;
my ($fh, $fname) = File::Temp::tempfile(
'assertconfXXXXXXXX', SUFFIX => '.h', UNLINK => 0
);
print $fh $_ for @$defs;
return $fname, $fh;
}
sub try_getentropy {
my ($check, $long, $defs) = @_;
my $msg = 'Checking getentropy()';
my $len = length $msg;
if ($check) { $$long = $len if $len > $$long; return; }
my $more = $$long - $len;
my $conf = gen_confh($defs);
if ( check_lib(
header => $conf,
lib => 'c', # always do at least this
ccflags => '-I.',
debug => 0,
function => q{
unsigned char p[4];
int r, c = 4;
r = getentropy(&p, c);
return 0;
(void)r;
},
)) {
push @$defs, '#define HAVE_GETENTROPY 1';
print $msg, ' ..', '.'x$more, ' found';
}
else {
push @$defs, '#undef HAVE_GETENTROPY';
print $msg, ' ..', '.'x$more, ' missing';
}
unlink $conf;
}
sub try_getrandom {
my ($check, $long, $defs) = @_;
my $msg = 'Checking getrandom()';
my $len = length $msg;
if ($check) { $$long = $len if $len > $$long; return; }
my $more = $$long - $len;
my $conf = gen_confh($defs);
if ( check_lib(
header => $conf,
lib => 'c', # always do at least this
ccflags => '-I.',
debug => 0,
function => '
unsigned char p[4];
int r, c = 4;
r = getrandom(c, &p, 0);
return 0;
(void)r;
',
)) {
push @$defs, '#define HAVE_GETRANDOM 1';
print $msg, ' ..', '.'x$more, ' found';
}
else {
push @$defs, '#undef HAVE_GETRANDOM';
print $msg, ' ..', '.'x$more, ' missing';
}
unlink $conf;
}
sub try_lstat {
my ($check, $long, $defs) = @_;
my $msg = 'Checking lstat()';
my $len = length $msg;
if ($check) { $$long = $len if $len > $$long; return; }
my $more = $$long - $len;
my $conf = gen_confh($defs);
my $Werror = $WIN32 ? '-WX' : '-Werror';
my $lib = $WIN32 ? 'ucrt' : 'c';
if ( check_lib(
header => $conf,
lib => $lib, # always do at least this
ccflags => "-I. $Werror",
debug => 0,
function => q{
struct stat buf;
int rc, islink;
rc = lstat("foo", &buf);
islink = ((buf.st_mode & S_IFMT) == S_IFLNK);
return 0;
(void)rc;
(void)islink;
},
)) {
push @$defs, '#define HAVE_LSTAT 1';
print $msg, ' ..', '.'x$more, ' found';
}
else {
push @$defs, '#undef HAVE_LSTAT';
print $msg, ' ..', '.'x$more, ' missing';
}
unlink $conf;
}
sub try_symlink {
my ($check, $long, $defs) = @_;
my $msg = 'Checking symlink()';
my $len = length $msg;
if ($check) { $$long = $len if $len > $$long; return; }
my $more = $$long - $len;
my $conf = gen_confh($defs);
my $has_symlink = eval {
symlink $conf, 'assertlibSYMLINK.h';
return 1 if -l 'assertlibSYMLINK.h';
return 0;
};
my $Werror = $WIN32 ? '-WX' : '-Werror';
my $lib = $WIN32 ? 'ucrt' : 'c';
if ( check_lib(
header => $conf,
lib => $lib, # always do at least this
ccflags => "-I. $Werror",
debug => 0,
function => q{
struct stat buf;
int rc;
rc = lstat("assertlibSYMLINK.h", &buf);
if ((buf.st_mode & S_IFMT) == S_IFLNK)
return 0;
return 1;
(void)rc;
},
)) {
push @$defs, '#define HAVE_SYMLINK 1';
print $msg, ' ..', '.'x$more, ' found';
}
else {
push @$defs, '#undef HAVE_SYMLINK';
print $msg, ' ..', '.'x$more, ' missing';
}
unlink $conf, 'assertlibSYMLINK.h';
}
sub try_sa_len {
my ($check, $long, $defs) = @_;
my $msg = 'Checking sa_len member';
my $len = length $msg;
if ($check) { $$long = $len if $len > $$long; return; }
my $more = $$long - $len;
my $conf = gen_confh($defs);
if ( check_lib(
header => $conf,
lib => 'c', # always do at least this
ccflags => '-I.',
debug => 0,
function => q{
struct ifreq ifr;
unsigned char *r;
r = (unsigned char*)&ifr.ifr_addr.sa_len;
return 0;
(void)r;
},
)) {
push @$defs, '#define HAVE_SA_LEN 1';
print $msg, ' ..', '.'x$more, ' found';
}
else {
push @$defs, '#undef HAVE_SA_LEN';
print $msg, ' ..', '.'x$more, ' missing';
}
unlink $conf;
}
sub try_win32_native {
my ($check, $long, $defs) = @_;
my $msg = 'Checking win32 native';
my $len = length $msg;
if ($check) { $$long = $len if $len > $$long; return; }
my $more = $$long - $len;
my $iswin32 = $^O eq 'MSWin32'; # need Cygwin too?
my $isnmake = $Config{make} eq 'nmake';
if ( $iswin32 and $isnmake ) {
push @$defs, '#define USE_WIN32_NATIVE 1';
print $msg, ' ..', '.'x$more, ' found';
$WIN32 = 1;
}
else {
push @$defs, '#undef USE_WIN32_NATIVE';
print $msg, ' ..', '.'x$more, ' missing';
$WIN32 = 0;
}
}
sub try_threads {
my ($check, $long, $defs) = @_;
my $msg = 'Checking thread storage';
my $len = length $msg;
if ($check) { $$long = $len if $len > $$long; return; }
my $more = $$long - $len;
# can we skip this if not -Dusethreads ?
my ($conf1, $fh1) = gen_confh_fh($defs);
print $fh1 q{
int dec(void) {
static __thread int foo = 3;
return --foo;
}
};
close $fh1;
my ($conf2, $fh2) = gen_confh_fh($defs);
print $fh2 q{
int dec(void) {
static __declspec(thread) int foo = 3;
return --foo;
}
};
close $fh2;
my $Werror = $WIN32 ? '-WX' : '-Werror';
my $lib = $WIN32 ? 'ucrt' : 'c';
if ( check_lib(
header => $conf1,
lib => $lib, # always do at least this
ccflags => "-I. $Werror",
debug => 0,
function => q{
int rv;
rv = dec();
rv = dec();
rv = dec();
return rv;
},
)) {
push @$defs, '#define TLS __thread';
print $msg, ' ..', '.'x$more, ' __thread';
}
elsif ( check_lib(
header => $conf2,
lib => $lib, # always do at least this
ccflags => "-I. $Werror",
debug => 0,
function => q{
int rv;
rv = dec();
rv = dec();
rv = dec();
return rv;
},
)) {
push @$defs, '#define TLS __declspec(thread)';
print $msg, ' ..', '.'x$more, ' __declspec(thread)';
}
else {
push @$defs, '#undef TLS';
print $msg, ' ..', '.'x$more, ' none';
}
unlink $conf1, $conf2;
}
sub write_conf {
my ($defines, $outfile) = @_;
open my $fh, '>', $outfile
or die 'open: ', $outfile, ": $!";
print $fh '#ifndef UU_EUMM_H';
print $fh '#define UU_EUMM_H';
print $fh $_
for @$defines;
print $fh '#endif';
}
|