views:

48

answers:

1

Is there an ExtUtils::* or Module::Build (or other) analog to Ruby's mkmf.have_struct_member?

I'd like to do something like (in the manner of a hints/ file):

....
if struct_has_member("msghdr", "msg_accrights") {
    $self->{CCFLAGS} = join(' ', $self->{CCFLAGS}, "-DTRY_ACCRIGHTS_NOT_CMSG");    
}
...

Config.pm doesn't track the specific information I'm looking for, and ExtUtils::FindFunctions didn't seem quite appropriate here...

+2  A: 

I know this is not built into either MakeMaker or Module::Build. There might be a thing on CPAN to do it, but the usual way is to use ExtUtils::CBuilder to compile up a little test program and see if it runs.

use ExtUtils::CBuilder;

open my $fh, ">", "try.c" or die $!;
print $fh <<'END';
#include <time.h>

int main(void) {
    struct tm *test;
    long foo = test->tm_gmtoff;

    return 0;
}
END

close $fh;

$has{"tm.tm_gmtoff"} = 1 if
    eval { ExtUtils::CBuilder->new->compile(source => "try.c"); 1 };

Probably want to do that in a temp file and clean up after it, etc...

Schwern
Don't test the value of `close`?
Kinopiko
@Kinopiko Its not meant as a complete tutorial on safe Perl I/O. I think I can count the number of times I've had a bug because `close` failed on my left hand. These days I use autodie and it takes care of everything.
Schwern