summaryrefslogtreecommitdiff
path: root/t/buildsystems/03-bs-auto-buildable.t
blob: b2c82e261e8f8470cabbd954ab688ce9cb1d53fa (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
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
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/perl

use strict;
use warnings;
use Test::More tests => 182;

use File::Temp qw(tempdir);
use File::Basename qw(dirname);
use lib dirname(dirname(__FILE__));
use Test::DH;
use File::Path qw(remove_tree make_path);
use Debian::Debhelper::Dh_Lib qw(!dirname);
use Debian::Debhelper::Dh_Buildsystems;

my @STEPS = qw(configure build test install clean);

### Test check_auto_buildable() of each buildsystem
sub test_check_auto_buildable {
	my ($bs, $config, $expected) = @_;

	if (! ref $expected) {
		my %all_steps;
		$all_steps{$_} = $expected foreach (@STEPS);
		$expected = \%all_steps;
	}
	for my $step (@STEPS) {
		my $e = 0;
		if (exists $expected->{$step}) {
			$e = $expected->{$step};
		} elsif (exists $expected->{default}) {
			$e = $expected->{default};
		}
		is( $bs->check_auto_buildable($step), $e,
			$bs->NAME() . "($config): check_auto_buildable($step) == $e" );
	}
}

sub test_autoselection {
	my ($testname, $expected, %args) = @_;
	for my $step (@STEPS) {
		my $bs = load_buildsystem({'enable-thirdparty' => 0}, $step, @_);
		my $e = $expected;
		$e = $expected->{$step} if ref $expected;
		if (defined $bs) {
			is( $bs->NAME(), $e, "autoselection($testname): $step=".((defined $e)?$e:'undef') );
		}
		else {
			is ( undef, $e, "autoselection($testname): $step=".((defined $e)?$e:'undef') );
		}
		&{$args{"code_$step"}}() if exists $args{"code_$step"};
	}
}

my $TEMP_DIR = tempdir('tmp.XXXXXXX', CLEANUP => 1);
my $sourcedir = "${TEMP_DIR}/source";
my $builddir = "${TEMP_DIR}/build";
my %options = (
	'builddir'  => $builddir,
	'sourcedir' => $sourcedir,
);
make_path($sourcedir, $builddir);
use Config;
my $libpath = $ENV{AUTOPKGTEST_TMP} ? $Config{vendorlib} : $Test::DH::ROOT_DIR;
my @bs = load_all_buildsystems([ $libpath ], %options);
my %bs;
my @names = map { $_->NAME() } @bs;

ok(@Debian::Debhelper::Dh_Buildsystems::BUILDSYSTEMS >= 1, "some build systems are built in" );
is_deeply( \@names, \@Debian::Debhelper::Dh_Buildsystems::BUILDSYSTEMS, "load_all_buildsystems() loads all built-in buildsystems" );

# check_auto_buildable() fails with numeric 0
for my $bs (@bs) {
    test_check_auto_buildable($bs, "fails with numeric 0", 0);
	$bs{$bs->NAME()} = $bs;
}

run_auto_buildable_tests();

remove_tree($sourcedir, $builddir);
make_path($sourcedir, $builddir);

run_autoselection_tests();


#### Bulk of test code ####

sub run_auto_buildable_tests {
	create_empty_file("${sourcedir}/configure", 0755);
	test_check_auto_buildable($bs{autoconf}, "configure", { configure => 1, clean => 1 });
	rm_files("${sourcedir}/configure");

	create_empty_file("${sourcedir}/CMakeLists.txt");
	test_check_auto_buildable($bs{cmake}, "CMakeLists.txt", { configure => 1, clean => 1 });
	rm_files("${sourcedir}/CMakeLists.txt");

	create_empty_file("${sourcedir}/Makefile.PL");
	test_check_auto_buildable($bs{perl_makemaker}, "Makefile.PL", { configure => 1 });
	rm_files("${sourcedir}/Makefile.PL");

	create_empty_file("${sourcedir}/meson.build");
	test_check_auto_buildable($bs{meson}, "meson.build", { configure => 1, clean => 1 });
	# Leave meson.build

	create_empty_file("${builddir}/build.ninja");
	test_check_auto_buildable($bs{ninja}, "build.ninja", { configure => 1, build => 1, clean => 1, install => 1, test => 1 });
	# Leave ninja.build

	# Meson + ninja
	test_check_auto_buildable($bs{meson}, "meson.build+build.ninja", { configure => 1, build => 1, clean => 1, install => 1, test => 1 });
	rm_files("${sourcedir}/meson.build", "${builddir}/build.ninja");

	# With Makefile
	create_empty_file("$builddir/Makefile");
	test_check_auto_buildable($bs{makefile}, "Makefile", 1);

	# ... +autoconf
	create_empty_file("${sourcedir}/configure", 0755);
	test_check_auto_buildable($bs{autoconf}, "configure+Makefile", { configure => 1, test => 1, build => 1, install => 1, clean => 1 });
	rm_files("${sourcedir}/configure");

	# ... +cmake
	create_empty_file("${sourcedir}/CMakeLists.txt");
	test_check_auto_buildable($bs{cmake}, "CMakeLists.txt+Makefile", 1);
	create_empty_file("$builddir/CMakeCache.txt"); # strong evidence that cmake was run
	test_check_auto_buildable($bs{cmake}, "CMakeCache.txt+Makefile", 2);
	rm_files("${builddir}/Makefile", "${sourcedir}/CMakeLists.txt");

	# Makefile.PL forces in-source
	#(see note in check_auto_buildable() why always 1 here)
	create_empty_file("${sourcedir}/Makefile.PL");
	create_empty_file("${sourcedir}/Makefile");
	test_check_auto_buildable($bs{perl_makemaker}, "Makefile.PL+Makefile", 1);
	rm_files("${sourcedir}/Makefile.PL", "${sourcedir}/Makefile");

	# Perl Build.PL - handles always
	test_check_auto_buildable($bs{perl_build}, "no Build.PL", 0);
	create_empty_file("${sourcedir}/Build.PL");
	test_check_auto_buildable($bs{perl_build}, "Build.PL", { configure => 1 });
	create_empty_file("${sourcedir}/Build"); # forced in source
	test_check_auto_buildable($bs{perl_build}, "Build.PL+Build", 1);
	rm_files("${sourcedir}/Build.PL", "${sourcedir}/Build");

	# Python Distutils
	test_check_auto_buildable($bs{python_distutils}, "no setup.py", 0);
	create_empty_file("${sourcedir}/setup.py");
	test_check_auto_buildable($bs{python_distutils}, "setup.py", 1);
	rm_files("${sourcedir}/setup.py");
}

sub run_autoselection_tests {
	# Auto-select nothing when no supported build system can be found
	# (see #557006).
	test_autoselection("auto-selects nothing", undef, %options);

	# Autoconf
	create_empty_file("${sourcedir}/configure", 0755);
	create_empty_file("${builddir}/Makefile");
	test_autoselection("autoconf",
					   { configure => "autoconf", build => "autoconf",
						 test => "autoconf", install => "autoconf",
						 clean => "autoconf"
					   }, %options);
	rm_files("${sourcedir}/configure", "${builddir}/Makefile");


	# Perl Makemaker (build, test, clean fail with builddir set [not supported])
	create_empty_file("${sourcedir}/Makefile.PL");
	create_empty_file("${sourcedir}/Makefile");
	test_autoselection("perl_makemaker", "perl_makemaker", %options);
	rm_files("${sourcedir}/Makefile.PL", "${sourcedir}/Makefile");


	# Makefile
	create_empty_file("$builddir/Makefile");
	test_autoselection("makefile", "makefile", %options);
	rm_files("$builddir/Makefile");

	# Python Distutils
	create_empty_file("${sourcedir}/setup.py");
	test_autoselection("python_distutils", "python_distutils", %options);
	rm_files("${sourcedir}/setup.py");

	# Perl Build
	create_empty_file("${sourcedir}/Build.PL");
	create_empty_file("${sourcedir}/Build");
	test_autoselection("perl_build", "perl_build", %options);
	rm_files("${sourcedir}/Build.PL", "${sourcedir}/Build");

	# CMake
	create_empty_file("${sourcedir}/CMakeLists.txt");
	test_autoselection("cmake without CMakeCache.txt",
					   { configure => "cmake", build => "makefile",
						 test => "makefile", install => "makefile",
						 clean => "makefile"
					   },
					   %options,
					   code_configure =>  sub {
						   create_empty_file("$builddir/Makefile");
					   });
	rm_files("${sourcedir}/CMakeLists.txt", "$builddir/Makefile");

	create_empty_file("${sourcedir}/CMakeLists.txt");
	test_autoselection("cmake with CMakeCache.txt",
					   "cmake",
					   %options,
					   code_configure => sub {
						   create_empty_file("$builddir/Makefile");
						   create_empty_file("$builddir/CMakeCache.txt");
					   });
	rm_files("${sourcedir}/CMakeLists.txt", "$builddir/Makefile", "$builddir/CMakeCache.txt");

	create_empty_file("${sourcedir}/CMakeLists.txt");
	create_empty_file("$builddir/Makefile");
	test_autoselection("cmake and existing Makefile", "makefile", %options);
	rm_files("${sourcedir}/CMakeLists.txt", "$builddir/Makefile");

};