How can I tell if a C struct has a member in Perl XS?
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...
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...
I wrote a wrapper around ExtUtils::CBuilder
for doing "does this C code compile?" type tests in Build.PL
or Makefile.PL
scripts, called ExtUtils::CChecker.
For example, you can easily test the above by:
use Module::Build;
use ExtUtils::CChecker;
my $cc = ExtUtils::CChecker->new;
$cc->try_compile_run(
define => "TRY_ACCRIGHTS_NOT_CMSG",
source => <<'EOF' );
#include <sys/types.h>
#include <sys/socket.h>
int main(void) {
struct msghdr cmsg;
cmsg.msg_accrights = 0;
return 0;
}
EOF
$cc->new_module_build(
configure_requires => { 'ExtUtils::CChecker' => 0 },
...
)->create_build_script;
精彩评论