2010-11-18 62 views
1

我有一個頂級的defines.mk文件,其中列出了某些目錄和C庫,包括取決於項目所以。ExtUtils :: MakeMaker包括外部* .mk和使用在* .mk文件中定義LIBS和INC參數在WriteMakefile

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

我用XS允許Perl腳本訪問這些庫和MakeMaker的生成Makefile文件將在我想讓它這樣所產生的Makefile文件時,它拉的這些定義鏈接這些庫。

鑑於這樣

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

看起來像它做了WriteMakefile我想要的是。謝謝!

+0

這裏有問題嗎?你有什麼問題?你有什麼嘗試? – cjm 2010-11-18 20:47:54

+0

好吧,我只是好奇,如果有辦法做到這一點?在線查看文檔看起來並不明顯,因爲WriteMakefile的參數都無法實現這一點。我想我可以將define.mk文件放入Makefile.PL並解析它,但有沒有更簡單的方法? – colekas 2010-11-18 22:45:43

回答

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

我沒有測試過這一點,這可能不是一個完整的工作方案。希望它能讓你朝着正確的方向前進(如果你仍然在讀這些)。

+0

對於post_initialize代碼,難道你不想返回$ extra_defines – colekas 2010-11-19 21:10:54

+0

另外子MM :: init_others應該是MM_Unix :: init_others ..對嗎? – colekas 2010-11-19 21:14:54

+0

另外,我可以很明顯地傳遞INC參數,它只是LIBS參數。 – colekas 2010-11-19 21:37:27

相關問題