gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:57:33 26 4
gpt4 key购买 nike

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

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

最佳答案

覆盖 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;
}

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

关于c - ExtUtils::MakeMaker 包括外部 *.mk 并使用 *.mk 文件中的定义用于 WriteMakefile 中的 LIBS 和 INC 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4218864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com