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

发布于 2024-10-03 01:52:00 字数 1909 浏览 2 评论 0原文

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

看起来它可以满足我的要求。谢谢!

I have a top level defines.mk file which lists certain directories and C libraries to include depending on the project like so.

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

I use XS to allow perl scripts to access these libraries and MakeMaker to generate the Makefile which will link these libraries in. I want to make it such that when the Makefile is generated, it pulls in these defines.

Given a WriteMakefile like this

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"},
);

I want to achieve this

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"},
);

From @mobrule I now have this 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"},
);

Which looks like it does what I want. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤单情人 2024-10-10 01:52:00

覆盖 post_initialize 方法以包含其他定义:

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

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

下一个问题是让 MakeMaker 让 LIBS 和 INC 参数中的“无效”条目传递到 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; 
}

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

Override the post_initialize method to include your additional definitions:

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

These will appear at the top of the Makefile, so later definitions (e.g. LIBS, can use them).

The next problem is getting MakeMaker to let the "invalid" entries in the LIBS and INC parameters passed through to the Makefile.

On Windows I think you can just put a :nosearch, like

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

and it will pass through (consult the docs to ExtUtils::Liblist). On Unix-y systems that doesn't work, and you may need to do something more radical like overriding 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; 
}

I haven't tested this and this might not be a complete working solution. Hopefully it will get you going in the right direction (if you are still reading this far).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文