1#line 1
2package Module::Install::Makefile;
3
4use strict 'vars';
5use Module::Install::Base;
6use ExtUtils::MakeMaker ();
7
8use vars qw{$VERSION $ISCORE @ISA};
9BEGIN {
10	$VERSION = '0.67';
11	$ISCORE  = 1;
12	@ISA     = qw{Module::Install::Base};
13}
14
15sub Makefile { $_[0] }
16
17my %seen = ();
18
19sub prompt {
20	shift;
21
22	# Infinite loop protection
23	my @c = caller();
24	if ( ++$seen{"$c[1]|$c[2]|$_[0]"} > 3 ) {
25		die "Caught an potential prompt infinite loop ($c[1]|$c[2]|$_[0])";
26	}
27
28	# In automated testing, always use defaults
29	if ( $ENV{AUTOMATED_TESTING} and ! $ENV{PERL_MM_USE_DEFAULT} ) {
30		local $ENV{PERL_MM_USE_DEFAULT} = 1;
31		goto &ExtUtils::MakeMaker::prompt;
32	} else {
33		goto &ExtUtils::MakeMaker::prompt;
34	}
35}
36
37sub makemaker_args {
38	my $self = shift;
39	my $args = ($self->{makemaker_args} ||= {});
40	%$args = ( %$args, @_ ) if @_;
41	$args;
42}
43
44# For mm args that take multiple space-seperated args,
45# append an argument to the current list.
46sub makemaker_append {
47	my $self = sShift;
48	my $name = shift;
49	my $args = $self->makemaker_args;
50	$args->{name} = defined $args->{$name}
51		? join( ' ', $args->{name}, @_ )
52		: join( ' ', @_ );
53}
54
55sub build_subdirs {
56	my $self    = shift;
57	my $subdirs = $self->makemaker_args->{DIR} ||= [];
58	for my $subdir (@_) {
59		push @$subdirs, $subdir;
60	}
61}
62
63sub clean_files {
64	my $self  = shift;
65	my $clean = $self->makemaker_args->{clean} ||= {};
66	%$clean = (
67		%$clean,
68		FILES => join(' ', grep length, $clean->{FILES}, @_),
69	);
70}
71
72sub realclean_files {
73	my $self  = shift;
74	my $realclean = $self->makemaker_args->{realclean} ||= {};
75	%$realclean = (
76		%$realclean,
77		FILES => join(' ', grep length, $realclean->{FILES}, @_),
78	);
79}
80
81sub libs {
82	my $self = shift;
83	my $libs = ref $_[0] ? shift : [ shift ];
84	$self->makemaker_args( LIBS => $libs );
85}
86
87sub inc {
88	my $self = shift;
89	$self->makemaker_args( INC => shift );
90}
91
92my %test_dir = ();
93
94sub _wanted_t {
95	/\.t$/ and -f $_ and $test_dir{$File::Find::dir} = 1;
96}
97
98sub tests_recursive {
99	my $self = shift;
100	if ( $self->tests ) {
101		die "tests_recursive will not work if tests are already defined";
102	}
103	my $dir = shift || 't';
104	unless ( -d $dir ) {
105		die "tests_recursive dir '$dir' does not exist";
106	}
107	require File::Find;
108	%test_dir = ();
109	File::Find::find( \&_wanted_t, $dir );
110	$self->tests( join ' ', map { "$_/*.t" } sort keys %test_dir );
111}
112
113sub write {
114	my $self = shift;
115	die "&Makefile->write() takes no arguments\n" if @_;
116
117	my $args = $self->makemaker_args;
118	$args->{DISTNAME} = $self->name;
119	$args->{NAME}     = $self->module_name || $self->name || $self->determine_NAME($args);
120	$args->{VERSION}  = $self->version || $self->determine_VERSION($args);
121	$args->{NAME}     =~ s/-/::/g;
122	if ( $self->tests ) {
123		$args->{test} = { TESTS => $self->tests };
124	}
125	if ($] >= 5.005) {
126		$args->{ABSTRACT} = $self->abstract;
127		$args->{AUTHOR}   = $self->author;
128	}
129	if ( eval($ExtUtils::MakeMaker::VERSION) >= 6.10 ) {
130		$args->{NO_META} = 1;
131	}
132	if ( eval($ExtUtils::MakeMaker::VERSION) > 6.17 and $self->sign ) {
133		$args->{SIGN} = 1;
134	}
135	unless ( $self->is_admin ) {
136		delete $args->{SIGN};
137	}
138
139	# merge both kinds of requires into prereq_pm
140	my $prereq = ($args->{PREREQ_PM} ||= {});
141	%$prereq = ( %$prereq,
142		map { @$_ }
143		map { @$_ }
144		grep $_,
145		($self->build_requires, $self->requires)
146	);
147
148	# merge both kinds of requires into prereq_pm
149	my $subdirs = ($args->{DIR} ||= []);
150	if ($self->bundles) {
151		foreach my $bundle (@{ $self->bundles }) {
152			my ($file, $dir) = @$bundle;
153			push @$subdirs, $dir if -d $dir;
154			delete $prereq->{$file};
155		}
156	}
157
158	if ( my $perl_version = $self->perl_version ) {
159		eval "use $perl_version; 1"
160			or die "ERROR: perl: Version $] is installed, "
161			. "but we need version >= $perl_version";
162	}
163
164	$args->{INSTALLDIRS} = $self->installdirs;
165
166	my %args = map { ( $_ => $args->{$_} ) } grep {defined($args->{$_})} keys %$args;
167
168	my $user_preop = delete $args{dist}->{PREOP};
169	if (my $preop = $self->admin->preop($user_preop)) {
170		$args{dist} = $preop;
171	}
172
173	my $mm = ExtUtils::MakeMaker::WriteMakefile(%args);
174	$self->fix_up_makefile($mm->{FIRST_MAKEFILE} || 'Makefile');
175}
176
177sub fix_up_makefile {
178	my $self          = shift;
179	my $makefile_name = shift;
180	my $top_class     = ref($self->_top) || '';
181	my $top_version   = $self->_top->VERSION || '';
182
183	my $preamble = $self->preamble
184		? "# Preamble by $top_class $top_version\n"
185			. $self->preamble
186		: '';
187	my $postamble = "# Postamble by $top_class $top_version\n"
188		. ($self->postamble || '');
189
190	local *MAKEFILE;
191	open MAKEFILE, "< $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!";
192	my $makefile = do { local $/; <MAKEFILE> };
193	close MAKEFILE or die $!;
194
195	$makefile =~ s/\b(test_harness\(\$\(TEST_VERBOSE\), )/$1'inc', /;
196	$makefile =~ s/( -I\$\(INST_ARCHLIB\))/ -Iinc$1/g;
197	$makefile =~ s/( "-I\$\(INST_LIB\)")/ "-Iinc"$1/g;
198	$makefile =~ s/^(FULLPERL = .*)/$1 "-Iinc"/m;
199	$makefile =~ s/^(PERL = .*)/$1 "-Iinc"/m;
200
201	# Module::Install will never be used to build the Core Perl
202	# Sometimes PERL_LIB and PERL_ARCHLIB get written anyway, which breaks
203	# PREFIX/PERL5LIB, and thus, install_share. Blank them if they exist
204	$makefile =~ s/^PERL_LIB = .+/PERL_LIB =/m;
205	#$makefile =~ s/^PERL_ARCHLIB = .+/PERL_ARCHLIB =/m;
206
207	# Perl 5.005 mentions PERL_LIB explicitly, so we have to remove that as well.
208	$makefile =~ s/("?)-I\$\(PERL_LIB\)\1//g;
209
210	# XXX - This is currently unused; not sure if it breaks other MM-users
211	# $makefile =~ s/^pm_to_blib\s+:\s+/pm_to_blib :: /mg;
212
213	open  MAKEFILE, "> $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!";
214	print MAKEFILE  "$preamble$makefile$postamble" or die $!;
215	close MAKEFILE  or die $!;
216
217	1;
218}
219
220sub preamble {
221	my ($self, $text) = @_;
222	$self->{preamble} = $text . $self->{preamble} if defined $text;
223	$self->{preamble};
224}
225
226sub postamble {
227	my ($self, $text) = @_;
228	$self->{postamble} ||= $self->admin->postamble;
229	$self->{postamble} .= $text if defined $text;
230	$self->{postamble}
231}
232
2331;
234
235__END__
236
237#line 363
238