Extending ExtUtils::MakeMaker

Maddingue on 2007-10-15T18:06:07

Just sharing some code to extend ExtUtils::MakeMaker.

I wanted two things: 1. apply the PM_FILTER on the EXE_FILES as well, 2. install a file in /etc.

After reading the documentation, looking at EU::MM source and numerous tries-and-errors, I ended up with the following sub which is, I think, generic enough to be reused by others:

sub MY::postamble {
    my ($mm, %args) = @_;
    my $postamble = "";

    # apply the filter to the EXE_FILES as well
    for my $exe (@{ $mm->{EXE_FILES} }) {
        my $old_rule = '$(CP) '.$exe.' $(INST_SCRIPT)/'.$exe;
        my $new_rule = '$(PM_FILTER) <'.$exe.' >$(INST_SCRIPT)/'.$exe;

        for my $makeline (@{ $mm->{RESULT} }) {
            $makeline =~ s/\Q$old_rule/$new_rule/;
        }
    }

    # add testcover target if available
    $postamble .= eval {
        require ExtUtils::MakeMaker::Coverage;
        ExtUtils::MakeMaker::Coverage::testcover();
    } || "";


    # install data files (in /etc, /usr/share, ...)
    # first, we must add a target in install::
    for my $makeline (@{ $mm->{RESULT} }) {
        $makeline =~ s/(install *::.+)\n/$1 priv_data_files\n/;
    }

    # then, declare the target with the files
    $postamble .= "\nINSTALL = install -D -p\n\npriv_data_files:\n";

    for my $file (@{ $args{files} }) {
        $postamble .= "\t\$(INSTALL) $file /$file\n";
    }

    return $postamble
}

The first code chunk is the part that applies the filter on the executables. I was afraid that replacing CP with the filter could have wrong consequences, so I have to read through the already rendered Makefile, stored in RESULT, in order to replace the good rules.

The second chunk manually add the testcover provided by Steve Peters' ExtUtils::MakeMaker::Coverage, as I'm redefining my own postamble().

The last chunk is the one that install generic data files. It expect files to have been passed to WriteMakefile() this way:

WriteMakefile(
    ...
    postamble => { files => [ ... ] },
    ...
);

Unless I've missed something, such features are not directly available in EU::MM or in a CPAN module. Do you people think these could be useful as modules?


Fear.

schwern on 2007-10-15T19:39:50

This exchange from Soldier pretty much sums up my feelings when I see MakeMaker extensions.


Sandra: You must *feel* something?
Todd: [silence]
Todd: Fear
Sandra: Fear?
Todd: Fear and discipline.
Sandra: Now?
Todd: Always.