summaryrefslogtreecommitdiffstats
path: root/preps/perl.d/perl-pkgbuild
blob: 09b86616e93f58ec524955799a2daf8b0bc47add (plain)
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
#!/usr/bin/env perl

use warnings 'FATAL' => 'all';
use strict;

my $PROG = 'perl-pkgbuild';

sub putpkgbuild
{
	my($sect, $subsect, $text) = @_;
	open my $pipe, '|-', 'putpkgtree' => 'PKGBUILD', $sect, $subsect
		or die "open putpkgtree: $!";
	print $pipe $text;
	close $pipe or exit $? >> 8;
}

sub putfuncs
{
	my($funcs) = @_;
	for my $f (keys %$funcs){
		my $sects = $funcs->{$f};
		while(my ($s, $txt) = each %$sects){
			putpkgbuild($f, $s, $txt);
		}
	}
}

sub startfunc
{
	my($name) = @_;
	return <<"ENDTXT";
${name}()
(
  cd "\$srcdir/\$_ddir"
ENDTXT
}

sub functxt
{
	my $fmt = shift;
	$fmt .= "\n" unless($fmt =~ /\n\z/);
	my $txt = sprintf $fmt, @_;
	$txt =~ s/^/  /gm;
	return $txt;
}

sub main
{
	if(@_ == 0 || ($_[0] ne 'MM' && $_[0] ne 'MB')){
		print STDERR qq{usage: $PROG ["MM" or "MB"]\n};
		return 1;
	}

	my $type = shift;
	my($script, $make, $iargs);
	my @badenv = qw/PERL5LIB/;
	my @exports = qw/PERL_MM_USE_DEFAULT=1/;
	if($type eq 'MM'){
		$script = 'Makefile.PL';
		$make = 'make';
		$iargs = q{INSTALLDIRS=vendor DESTDIR="$pkgdir"};
		push @exports, 'PERL_AUTOINSTALL=--skipdeps';
		push @badenv, 'PERL_MM_OPT';
	}else{
		$script = 'Build.PL';
		$make = './Build';
		$iargs = q{--installdirs=vendor --destdir="$pkgdir"};
		push @exports, 'MODULEBUILDRC=/dev/null';
		push @badenv, 'PERL_MB_OPT';
	}

	my %funcs;
	my @fnames = qw/build check package/;
	for my $f (@fnames){
		$funcs{$f}{'beg'} = startfunc($f);
		# Module::Build uses env vars for each stage of Build
		if($type eq 'MB'){
			$funcs{$f}{'beg'} .= functxt(<<"ENDTXT");
export @exports
unset @badenv
ENDTXT
		}
	}

	# ExtUtils::MakeMaker only uses env vars for Makefile.PL
	if($type eq 'MM'){
		$funcs{'build'}{'beg'} .= functxt(<<'ENDTXT', "@exports", "@badenv");
export %s
unset %s
ENDTXT
	}

	$funcs{'build'}{'body'} = functxt(<<'ENDTXT', $script, $make);
/usr/bin/perl %s
%s
ENDTXT

	# Be a little paranoid, but we don't need PERL_AUTOINSTALL here.
	# TODO: make this hack prettier
	if($type eq 'MM'){
		$funcs{'check'}{'beg'} .= functxt(<<'ENDTXT', 'PERL_MM_USE_DEFAULT=1', 'PERL5LIB');
export %s
unset %s
ENDTXT
	}
	$funcs{'check'}{'body'} = functxt("%s test", $make);

	$funcs{'package'}{'body'} = functxt(<<'ENDTXT', $make, $iargs);
%s install %s
ENDTXT

	for my $f (@fnames){
		$funcs{$f}{'end'} = ")\n";
	}
	putfuncs(\%funcs);

	putpkgbuild('suffix', 'body', <<'ENDTXT');
# Local Variables:
# mode: shell-script
# sh-basic-offset: 2
# End:
# vim:set ts=2 sw=2 et:
ENDTXT

	return 0;
}

exit main(@ARGV);