ExtUtils :: MakeMaker包括外部* .mk,并在* .mk文件中使用定义,用于WriteMakefile中的LIBS和INC参数

时间:2010-11-18 19:46:17

标签: c perl makefile makemaker

我有一个顶级的define.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"},
);

看起来它就像我想要的那样。谢谢!

1 个答案:

答案 0 :(得分: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; 
}

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