1

我有一个顶级的defines.mk 文件,其中列出了某些目录和C 库,具体取决于这样的项目。

KERNEL_LIB = -lkdev  
DRIVER_LIB = -ldriver -lutil -linit $(KERNEL_LIB)
DRIVER_INCLUDE = -I../../include

我使用 XS 来允许 perl 脚本访问这些库,并使用 MakeMaker 生成将链接这些库的 Makefile。我想让它在生成 Makefile 时引入这些定义。

给定这样的 WriteMakefile

WriteMakefile(  
    NAME              => 'generic_scripts',
    VERSION_FROM      => 'generic_scripts.pm', 
    LIBS              => ['-L/usr/local/app/lib -lkdev -lpthread -lrt -ldriver -lutil -linit'],
    DEFINE            => '', 
    INC               => '-I../../include', 
    clean             => {FILES=>"*.o"},
);

我想实现这个

WriteMakefile(  
    NAME              => 'generic_scripts',
    VERSION_FROM      => 'generic_scripts.pm', 
    LIBS              => ['-L/usr/local/dx/lib $(KERNEL_LIB) -lpthread -lrt $(DRIVER_LIB)'],
    DEFINE            => '', 
    INC               => '$(DRIVER_INCLUDE)', 
    clean             => {FILES=>"*.o"},
);

从@mobrule 我现在有了这个 Makefile.PL

use 5.008008;
use ExtUtils::MakeMaker;
use ExtUtils::MM_Unix;
use ExtUtils::MM;

sub MY::post_initialize {
    open my $defs, '<', 'defines.mk';
    my $extra_defines = join '', <$defs>;
    close $defs;
    return $extra_defines;
}

sub MM::init_others {
    my $self = shift;
    $self->ExtUtils::MM_Unix::init_others(@_);

    $self->{EXTRALIBS} = '-L/usr/local/app/lib $(DRIVER_LIB) -lpthread -lrt';
    $self->{BSLOADLIBS} = $self->{LDLOADLIBS} = $self->{EXTRALIBS};
}

WriteMakefile(
    NAME              => 'generic_scripts',
    VERSION_FROM      => 'generic_scripts.pm',
    DEFINE            => '',
    INC               => '$(DRIVER_INCLUDE)',
    clean             => {FILES=>"*.o"},
);

看起来它做了我想要的。谢谢!

4

1 回答 1

0

覆盖该post_initialize方法以包含您的附加定义:

sub MY::post_initialize {
    open my $defs, '<', 'defines.mk';
    my $extra_defines = join '', <$defs>;
    close $defs;
    return $extra_defines;
}

这些将出现在 Makefile 的顶部,因此以后的定义(例如LIBS,可以使用它们)。

下一个问题是让 MakeMaker 让LIBSINC参数中的“无效”条目传递给 Makefile。

在 Windows 上,我认为你可以只放一个:nosearch,比如

LIBS => ['-lm', ':nosearch $(OTHER_LIBS)']

它会通过(请参阅文档ExtUtils::Liblist)。在不起作用的 Unix-y 系统上,您可能需要做一些更激进的事情,比如覆盖init_others

sub MM::init_others {      # MM package is a subclass of ExtUtils::MM_Unix and will
                           # get called instead of ExtUtils::MM_Unix::init_others
    my $self = shift;
    $self->SUPER::init_others(@_); # invoke ExtUtils::MM_Unix::init_others

    # now repair the attributes that ExtUtils::MM_Any::init_others didn't set
    $self->{EXTRALIBS} = '-lkdev $(KERNEL_LIB) -lrt $(DRIVER_LIB)';
    $self->{BSLOADLIBS} = $self->{LDLOADLIBS} = $self->{EXTRALIBS};
    1; 
}

我没有对此进行测试,这可能不是一个完整的工作解决方案。希望它能让你朝着正确的方向前进(如果你还在读这篇文章的话)。

于 2010-11-19T01:04:42.000 回答