Makefile.pm 5.75 KB
Newer Older
Ruslan Zakirov's avatar
Ruslan Zakirov committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#line 1
package Module::Install::Makefile;

use strict 'vars';
use Module::Install::Base;
use ExtUtils::MakeMaker ();

use vars qw{$VERSION $ISCORE @ISA};
BEGIN {
	$VERSION = '0.62';
	$ISCORE  = 1;
	@ISA     = qw{Module::Install::Base};
}

sub Makefile { $_[0] }

my %seen = ();

sub prompt {
    shift;

    # Infinite loop protection
    my @c = caller();
    if ( ++$seen{"$c[1]|$c[2]|$_[0]"} > 3 ) {
        die "Caught an potential prompt infinite loop ($c[1]|$c[2]|$_[0])";
    }

    # In automated testing, always use defaults
    if ( $ENV{AUTOMATED_TESTING} and ! $ENV{PERL_MM_USE_DEFAULT} ) {
        local $ENV{PERL_MM_USE_DEFAULT} = 1;
        goto &ExtUtils::MakeMaker::prompt;
    } else {
        goto &ExtUtils::MakeMaker::prompt;
    }
}

sub makemaker_args {
    my $self = shift;
    my $args = ($self->{makemaker_args} ||= {});
    %$args = ( %$args, @_ ) if @_;
    $args;
}

# For mm args that take multiple space-seperated args,
# append an argument to the current list.
sub makemaker_append {
    my $self = shift;
    my $name = shift;
    my $args = $self->makemaker_args;
    $args->{name} = defined $args->{$name}
    	? join( ' ', $args->{name}, @_ )
    	: join( ' ', @_ );
}

sub build_subdirs {
    my $self    = shift;
    my $subdirs = $self->makemaker_args->{DIR} ||= [];
    for my $subdir (@_) {
        push @$subdirs, $subdir;
    }
}

sub clean_files {
    my $self  = shift;
    my $clean = $self->makemaker_args->{clean} ||= {};
    %$clean = (
        %$clean, 
        FILES => join(' ', grep length, $clean->{FILES}, @_),
    );
}

sub realclean_files {
    my $self  = shift;
    my $realclean = $self->makemaker_args->{realclean} ||= {};
    %$realclean = (
        %$realclean, 
        FILES => join(' ', grep length, $realclean->{FILES}, @_),
    );
}

sub libs {
    my $self = shift;
    my $libs = ref $_[0] ? shift : [ shift ];
    $self->makemaker_args( LIBS => $libs );
}

sub inc {
    my $self = shift;
    $self->makemaker_args( INC => shift );
}

sub write {
    my $self = shift;
    die "&Makefile->write() takes no arguments\n" if @_;

    my $args = $self->makemaker_args;
    $args->{DISTNAME} = $self->name;
    $args->{NAME}     = $self->module_name || $self->name || $self->determine_NAME($args);
    $args->{VERSION}  = $self->version || $self->determine_VERSION($args);
    $args->{NAME}     =~ s/-/::/g;
    if ( $self->tests ) {
        $args->{test} = { TESTS => $self->tests };
    }
    if ($] >= 5.005) {
        $args->{ABSTRACT} = $self->abstract;
        $args->{AUTHOR}   = $self->author;
    }
    if ( eval($ExtUtils::MakeMaker::VERSION) >= 6.10 ) {
        $args->{NO_META} = 1;
    }
    if ( eval($ExtUtils::MakeMaker::VERSION) > 6.17 and $self->sign ) {
        $args->{SIGN} = 1;
    }
    unless ( $self->is_admin ) {
        delete $args->{SIGN};
    }

    # merge both kinds of requires into prereq_pm
    my $prereq = ($args->{PREREQ_PM} ||= {});
    %$prereq = ( %$prereq, map { @$_ } map { @$_ } grep $_,
                 ($self->build_requires, $self->requires) );

    # merge both kinds of requires into prereq_pm
    my $subdirs = ($args->{DIR} ||= []);
    if ($self->bundles) {
        foreach my $bundle (@{ $self->bundles }) {
            my ($file, $dir) = @$bundle;
            push @$subdirs, $dir if -d $dir;
            delete $prereq->{$file};
        }
    }

    if ( my $perl_version = $self->perl_version ) {
        eval "use $perl_version; 1"
            or die "ERROR: perl: Version $] is installed, "
                . "but we need version >= $perl_version";
    }

    my %args = map { ( $_ => $args->{$_} ) } grep {defined($args->{$_})} keys %$args;
    if ($self->admin->preop) {
        $args{dist} = $self->admin->preop;
    }

    my $mm = ExtUtils::MakeMaker::WriteMakefile(%args);
    $self->fix_up_makefile($mm->{FIRST_MAKEFILE} || 'Makefile');
}

sub fix_up_makefile {
    my $self          = shift;
    my $makefile_name = shift;
    my $top_class     = ref($self->_top) || '';
    my $top_version   = $self->_top->VERSION || '';

    my $preamble = $self->preamble 
        ? "# Preamble by $top_class $top_version\n"
            . $self->preamble
        : '';
    my $postamble = "# Postamble by $top_class $top_version\n"
        . ($self->postamble || '');

    local *MAKEFILE;
    open MAKEFILE, "< $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!";
    my $makefile = do { local $/; <MAKEFILE> };
    close MAKEFILE or die $!;

    $makefile =~ s/\b(test_harness\(\$\(TEST_VERBOSE\), )/$1'inc', /;
    $makefile =~ s/( -I\$\(INST_ARCHLIB\))/ -Iinc$1/g;
    $makefile =~ s/( "-I\$\(INST_LIB\)")/ "-Iinc"$1/g;
    $makefile =~ s/^(FULLPERL = .*)/$1 "-Iinc"/m;
    $makefile =~ s/^(PERL = .*)/$1 "-Iinc"/m;

    # Module::Install will never be used to build the Core Perl
    # Sometimes PERL_LIB and PERL_ARCHLIB get written anyway, which breaks
    # PREFIX/PERL5LIB, and thus, install_share. Blank them if they exist
    $makefile =~ s/^PERL_LIB = .+/PERL_LIB =/m;
    #$makefile =~ s/^PERL_ARCHLIB = .+/PERL_ARCHLIB =/m;

    # Perl 5.005 mentions PERL_LIB explicitly, so we have to remove that as well.
    $makefile =~ s/("?)-I\$\(PERL_LIB\)\1//g;

    # XXX - This is currently unused; not sure if it breaks other MM-users
    # $makefile =~ s/^pm_to_blib\s+:\s+/pm_to_blib :: /mg;

    open  MAKEFILE, "> $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!";
    print MAKEFILE  "$preamble$makefile$postamble" or die $!;
    close MAKEFILE  or die $!;

    1;
}

sub preamble {
    my ($self, $text) = @_;
    $self->{preamble} = $text . $self->{preamble} if defined $text;
    $self->{preamble};
}

sub postamble {
    my ($self, $text) = @_;
    $self->{postamble} ||= $self->admin->postamble;
    $self->{postamble} .= $text if defined $text;
    $self->{postamble}
}

1;

__END__

#line 334