]> git.donarmstrong.com Git - debhelper.git/blob - t/buildsystems/buildsystem_tests
Add a test suite for build systems.
[debhelper.git] / t / buildsystems / buildsystem_tests
1 #!/usr/bin/perl
2
3 use Test::More tests => 224;
4
5 use strict;
6 use warnings;
7 use IPC::Open2;
8 use Cwd ();
9 use File::Temp qw(tempfile tempdir);
10 use File::Basename ();
11
12 # Let the tests to be run from anywhere but currect directory
13 # is expected to be the one where this test lives in.
14 chdir File::Basename::dirname($0) or die "Unable to chdir to ".File::Basename::dirname($0);
15
16 use_ok( 'Debian::Debhelper::Dh_Lib' );
17 use_ok( 'Debian::Debhelper::Buildsystem' );
18 use_ok( 'Debian::Debhelper::Dh_Buildsystems' );
19
20 my $TOPDIR = "../..";
21 my @STEPS = qw(configure build test install clean);
22 my @BUILDSYSTEMS = qw(autoconf perl_makemaker makefile python_distutils perl_build cmake);
23 my $BS_CLASS = 'Debian::Debhelper::Buildsystem';
24
25 my ($bs, @bs, %bs);
26 my ($tmp, @tmp, %tmp);
27 my ($tmpdir, $builddir, $default_builddir);
28
29 ### Common subs ####
30 sub touch {
31         my $file=shift;
32         my $chmod=shift;
33         open FILE, ">", $file and close FILE or die "Unable to touch $file";
34         chmod $chmod, $file if defined $chmod;
35 }
36
37 sub cleandir {
38         my $dir=shift;
39         system ("find", $dir, "-type", "f", "-delete");
40 }
41 sub readlines {
42         my $h=shift;
43         my @lines = <$h>;
44         close $h;
45         chop @lines;
46         return \@lines;
47 }
48
49 sub process_stdout {
50         my ($cmdline, $stdin) = @_;
51         my ($reader, $writer);
52
53         open2($reader, $writer, $cmdline) or die "Unable to exec $cmdline";
54         print $writer $stdin if $stdin;
55         close $writer;
56         return readlines($reader);
57 }
58
59 ### Test Buildsystem class API methods
60 is( $BS_CLASS->_canonpath("path/to/the/./nowhere/../../somewhere"),
61     "path/to/somewhere", "_canonpath no1" );
62 is( $BS_CLASS->_canonpath("path/to/../forward/../../somewhere"),
63     "somewhere","_canonpath no2" );
64 is( $BS_CLASS->_canonpath("path/to/../../../somewhere"),
65     "../somewhere","_canonpath no3" );
66 is( $BS_CLASS->_rel2rel("path/my/file", "path/my"),
67     "file", "_rel2rel no1" );
68 is( $BS_CLASS->_rel2rel("path/dir/file", "path/my"),
69     "../dir/file", "_rel2rel no2" );
70 is( $BS_CLASS->_rel2rel("file", "/root/path/my", "/root"),
71     "../../file", "_rel2rel no3" );
72
73 ### Test Buildsystem class path API methods under different configurations
74 sub test_buildsystem_paths_api {
75         my ($bs, $config, $expected)=@_;
76
77         my $api_is = sub {
78                 my ($got, $name)=@_;
79                 is( $got, $expected->{$name}, "paths API ($config): $name")
80         };
81
82         &$api_is( $bs->get_sourcedir(), 'get_sourcedir()' );
83         &$api_is( $bs->get_sourcepath("a/b"), 'get_sourcepath(a/b)' );
84         &$api_is( $bs->get_builddir(), 'get_builddir()' );
85         &$api_is( $bs->get_buildpath(), 'get_buildpath()' );
86         &$api_is( $bs->get_buildpath("a/b"), 'get_buildpath(a/b)' );
87         &$api_is( $bs->get_source_rel2builddir(), 'get_source_rel2builddir()' );
88         &$api_is( $bs->get_source_rel2builddir("a/b"), 'get_source_rel2builddir(a/b)' );
89         &$api_is( $bs->get_build_rel2sourcedir(), 'get_build_rel2sourcedir()' );
90         &$api_is( $bs->get_build_rel2sourcedir("a/b"), 'get_build_rel2sourcedir(a/b)' );
91 }
92
93 # Defaults
94 $bs = $BS_CLASS->new();
95 $default_builddir = $bs->DEFAULT_BUILD_DIRECTORY();
96 %tmp = (
97         "get_sourcedir()" => ".",
98         "get_sourcepath(a/b)" => "./a/b",
99         "get_builddir()" => undef,
100         "get_buildpath()" => ".",
101         "get_buildpath(a/b)" =>  "./a/b",
102         "get_source_rel2builddir()" => ".",
103         "get_source_rel2builddir(a/b)" => "./a/b",
104         "get_build_rel2sourcedir()" => ".",
105         "get_build_rel2sourcedir(a/b)" => "./a/b",
106 );
107 test_buildsystem_paths_api($bs, "no builddir, no sourcedir", \%tmp);
108
109 # builddir=bld/dir
110 $bs = $BS_CLASS->new(builddir => "bld/dir");
111 %tmp = (
112         "get_sourcedir()" => ".",
113         "get_sourcepath(a/b)" => "./a/b",
114         "get_builddir()" => "bld/dir",
115         "get_buildpath()" => "bld/dir",
116         "get_buildpath(a/b)" =>  "bld/dir/a/b",
117         "get_source_rel2builddir()" => "../..",
118         "get_source_rel2builddir(a/b)" => "../../a/b",
119         "get_build_rel2sourcedir()" => "bld/dir",
120         "get_build_rel2sourcedir(a/b)" => "bld/dir/a/b",
121 );
122 test_buildsystem_paths_api($bs, "builddir=bld/dir, no sourcedir", \%tmp);
123
124 # Default builddir, sourcedir=autoconf
125 $bs = $BS_CLASS->new(builddir => undef, sourcedir => "autoconf");
126 %tmp = (
127         "get_sourcedir()" => "autoconf",
128         "get_sourcepath(a/b)" => "autoconf/a/b",
129         "get_builddir()" => "autoconf/$default_builddir",
130         "get_buildpath()" => "autoconf/$default_builddir",
131         "get_buildpath(a/b)" =>  "autoconf/$default_builddir/a/b",
132         "get_source_rel2builddir()" => "..",
133         "get_source_rel2builddir(a/b)" => "../a/b",
134         "get_build_rel2sourcedir()" => "$default_builddir",
135         "get_build_rel2sourcedir(a/b)" => "$default_builddir/a/b",
136 );
137 test_buildsystem_paths_api($bs, "default builddir, sourcedir=autoconf", \%tmp);
138
139 # Enforced out of source tree building
140 # sourcedir=builddir=autoconf hence default builddir is implied
141 $bs = $BS_CLASS->new(builddir => "./autoconf", sourcedir => "autoconf/");
142 $bs->enforce_out_of_source_building();
143 test_buildsystem_paths_api($bs, "out of source enforced, sourcedir=autoconf/", \%tmp);
144
145 # sourcedir=autoconf (builddir should be dropped)
146 $bs = $BS_CLASS->new(builddir => ".", sourcedir => "autoconf");
147 %tmp = (
148         "get_sourcedir()" => "autoconf",
149         "get_sourcepath(a/b)" => "autoconf/a/b",
150         "get_builddir()" => undef,
151         "get_buildpath()" => "autoconf",
152         "get_buildpath(a/b)" =>  "autoconf/a/b",
153         "get_source_rel2builddir()" => ".",
154         "get_source_rel2builddir(a/b)" => "./a/b",
155         "get_build_rel2sourcedir()" => ".",
156         "get_build_rel2sourcedir(a/b)" => "./a/b",
157 );
158 test_buildsystem_paths_api($bs, "no builddir, sourcedir=autoconf", \%tmp);
159
160 # builddir=bld/dir, sourcedir=autoconf. Should be the same as sourcedir=autoconf.
161 $bs = $BS_CLASS->new(builddir => "bld/dir", sourcedir => "autoconf");
162 $bs->enforce_in_source_building();
163 test_buildsystem_paths_api($bs, "in source enforced, sourcedir=autoconf", \%tmp);
164
165 # builddir=../bld/dir (relative to the sourcedir)
166 $bs = $BS_CLASS->new(builddir => "../bld/dir/", sourcedir => "autoconf");
167 %tmp = (
168         "get_sourcedir()" => "autoconf",
169         "get_sourcepath(a/b)" => "autoconf/a/b",
170         "get_builddir()" => "bld/dir",
171         "get_buildpath()" => "bld/dir",
172         "get_buildpath(a/b)" =>  "bld/dir/a/b",
173         "get_source_rel2builddir()" => "../../autoconf",
174         "get_source_rel2builddir(a/b)" => "../../autoconf/a/b",
175         "get_build_rel2sourcedir()" => "../bld/dir",
176         "get_build_rel2sourcedir(a/b)" => "../bld/dir/a/b",
177 );
178 test_buildsystem_paths_api($bs, "builddir=../bld/dir, sourcedir=autoconf", \%tmp);
179
180 # Builddir relative to the pwd (same path as above).
181 $bs = $BS_CLASS->new(builddir => "./bld/dir", sourcedir => "autoconf");
182 test_buildsystem_paths_api($bs, "builddir=./bld/dir, sourcedir=autoconf", \%tmp);
183
184 ### Test if all buildsystems can be loaded
185 @bs = load_all_buildsystems([ $INC[0] ]);
186 @tmp = map { $_->NAME() } @bs;
187 is_deeply( \@tmp, \@BUILDSYSTEMS, "load_all_buildsystems() loads all built-in buildsystems" );
188
189 ### Test check_auto_buildable() of each buildsystem
190 sub test_check_auto_buildable {
191         my $bs=shift;
192         my $config=shift;
193         my $expected=shift;
194         my @steps=@_ || @STEPS;
195
196         if (! ref $expected) {
197                 my %all_steps;
198                 $all_steps{$_} = $expected foreach (@steps);
199                 $expected = \%all_steps;
200         }
201         for my $step (@steps) {
202                 my $e = 0;
203                 if (exists $expected->{$step}) {
204                         $e = $expected->{$step};
205                 } elsif (exists $expected->{default}) {
206                         $e = $expected->{default};
207                 }
208                 if ($e) {
209                         ok( $bs->check_auto_buildable($step),
210                             $bs->NAME() . "($config): check_auto_buildable($step)" );
211                 }
212                 else {
213                         ok( ! $bs->check_auto_buildable($step),
214                             $bs->NAME() . "($config): ! check_auto_buildable($step)" );
215                 }
216         }
217 }
218
219 $tmpdir = tempdir("tmp.XXXXXX");
220 $builddir = "$tmpdir/builddir";
221 mkdir $builddir;
222 %tmp = (
223         builddir => 'builddir',
224         sourcedir => $tmpdir
225 );
226
227 $bs{autoconf} = load_buildsystem("autoconf", undef, %tmp);
228 $bs{cmake} = load_buildsystem("cmake", undef, %tmp);
229 $bs{perl_mm} = load_buildsystem("perl_makemaker", undef, %tmp);
230 $bs = load_buildsystem("makefile", undef, %tmp);
231
232 test_check_auto_buildable($bs{autoconf}, "no configure", 0);
233 test_check_auto_buildable($bs{cmake}, "no CMakeLists.txt", 0);
234 test_check_auto_buildable($bs{perl_mm}, "no Makefile.PL", 0);
235 test_check_auto_buildable($bs, "no Makefile", 0);
236
237 touch "$tmpdir/configure", 0755;
238 test_check_auto_buildable($bs{autoconf}, "configure", { configure => 1 });
239
240 touch "$tmpdir/CMakeLists.txt";
241 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt", { configure => 1 });
242
243 touch "$tmpdir/Makefile.PL";
244 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL",
245     { configure => 1, install => 1 });
246
247 # With Makefile
248 touch "$builddir/Makefile";
249 test_check_auto_buildable($bs, "Makefile", { configure => 0, default => 1 });
250 test_check_auto_buildable($bs{autoconf}, "configure+Makefile", { configure => 1 });
251 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt+Makefile", 1);
252
253 # Makefile.PL forces in-source
254 #(see note in check_auto_buildable() why always 1 here)
255 unlink "$builddir/Makefile";
256 touch "$tmpdir/Makefile";
257 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL+Makefile", 1);
258
259 # Perl Build.PL - handles always
260 $bs = load_buildsystem("perl_build", undef, %tmp);
261 test_check_auto_buildable($bs, "no Build.PL", 0);
262 touch "$tmpdir/Build.PL";
263 test_check_auto_buildable($bs, "Build.PL", { configure => 1 });
264 touch "$tmpdir/Build"; # forced in source
265 test_check_auto_buildable($bs, "Build.PL+Build", 1);
266
267 # Python Distutils
268 $bs = load_buildsystem("python_distutils", undef, %tmp);
269 test_check_auto_buildable($bs, "no setup.py", 0);
270 touch "$tmpdir/setup.py";
271 test_check_auto_buildable($bs, "setup.py", 1);
272
273 cleandir($tmpdir);
274
275 ### Now test if it can autoselect a proper buildsystem for a typical package
276 sub test_autoselection {
277         my $system=shift;
278         my $expected=shift;
279         for my $step (@STEPS) {
280                 my $bs = load_buildsystem(undef, $step, @_);
281                 my $e = $expected;
282                 $e = $expected->{$step} if ref $expected;
283                 if (defined $bs) {
284                         is( $bs->NAME(), $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
285                 }
286                 else {
287                         is ( undef, $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
288                 }
289         }
290 }
291
292 # Autoconf
293 touch "$tmpdir/configure", 0755;
294 touch "$builddir/Makefile";
295 test_autoselection("autoconf",
296     { configure => "autoconf", build => "makefile",
297       test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
298 cleandir $tmpdir;
299
300 # Perl Makemaker (build, test, clean fail with builddir set [not supported])
301 touch "$tmpdir/Makefile.PL";
302 touch "$tmpdir/Makefile";
303 test_autoselection("perl_makemaker", "perl_makemaker", %tmp);
304 cleandir $tmpdir;
305
306 # Makefile
307 touch "$builddir/Makefile";
308 test_autoselection("makefile", { build => "makefile", test => "makefile",
309                 install => "makefile", clean => "makefile" }, %tmp);
310 cleandir $tmpdir;
311
312 # Python Distutils
313 touch "$tmpdir/setup.py";
314 test_autoselection("python_distutils", "python_distutils", %tmp);
315 cleandir $tmpdir;
316
317 # Perl Build
318 touch "$tmpdir/Build.PL";
319 touch "$tmpdir/Build";
320 test_autoselection("perl_build", "perl_build", %tmp);
321 cleandir $tmpdir;
322
323 # CMake
324 touch "$tmpdir/CMakeLists.txt";
325 touch "$builddir/Makefile";
326 test_autoselection("cmake",
327     { configure => "cmake", build => "makefile",
328       test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
329 cleandir $tmpdir;
330
331 ### Test buildsystems_init() and commandline/env argument handling
332 sub get_load_bs_source {
333         my ($system, $step)=@_;
334         $step = (defined $step) ? "'$step'" : 'undef';
335         $system = (defined $system) ? "'$system'" : 'undef';
336
337 return <<EOF;
338 use strict;
339 use warnings;
340 use Debian::Debhelper::Dh_Buildsystems;
341
342 buildsystems_init();
343 my \$bs = load_buildsystem($system, $step);
344 if (defined \$bs) {
345         print 'NAME=', \$bs->NAME(), "\\n";
346         print \$_, "=", (defined \$bs->{\$_}) ? \$bs->{\$_} : 'undef', "\\n"
347             foreach (sort keys \%\$bs);
348 }
349 EOF
350 }
351
352 is_deeply( process_stdout("DH_AUTO_OPTIONS='--builddirectory=bld\\ dir --sourcedirectory autoconf' $^X -- -",
353                           get_load_bs_source(undef, "configure")),
354     [ 'NAME=autoconf', 'builddir=autoconf/bld dir', 'makecmd=make', 'sourcedir=autoconf' ],
355     "dh_auto_options w/space, autoconf autoselection and sourcedir/builddir" );
356
357 is_deeply( process_stdout("$^X -- - -cautoconf -d autoconf", get_load_bs_source("autoconf", "build")),
358     [ 'NAME=autoconf', 'builddir=undef', 'makecmd=make', 'sourcedir=autoconf' ],
359     "forced autoconf and sourcedir" );
360
361 is_deeply( process_stdout("$^X -- - -b -cautoconf", get_load_bs_source("autoconf", "build")),
362     [ 'NAME=autoconf', "builddir=$default_builddir", 'makecmd=make', 'sourcedir=.' ],
363     "forced autoconf and default build directory" );
364
365 # Build the autoconf test package
366 sub dh_auto_do_autoconf {
367         my $sourcedir=shift;
368         my $builddir=shift;
369         my %args=@_;
370
371         my (@lines, @extra_args);
372         my $buildpath = $sourcedir;
373         my @dh_auto_args = ("-d", $sourcedir);
374         my $dh_auto_str = "-d $sourcedir";
375         if ($builddir) {
376                 push @dh_auto_args, "-b", $builddir;
377                 $dh_auto_str .= " -b $builddir";
378                 $buildpath .= "/$builddir";
379         }
380
381         my $do_dh_auto = sub {
382                 my $step=shift;
383                 my @extra_args;
384                 my $extra_str = "";
385                 if (exists $args{"${step}_args"}) {
386                         push @extra_args, @{$args{"${step}_args"}};
387                         $extra_str .= " $_" foreach (@extra_args);
388                 }
389                 is ( system("$TOPDIR/dh_auto_$step", @dh_auto_args, "--", @extra_args), 0,
390                          "dh_auto_$step $dh_auto_str$extra_str" );
391                 return @extra_args;
392         };
393         
394         @extra_args = &$do_dh_auto('configure');
395         ok ( -f "$buildpath/Makefile", "$buildpath/Makefile exists" );
396         @lines=();
397         if (ok( open(FILE, "$buildpath/stamp_configure"), "$buildpath/stamp_configure exists") ) {
398                 @lines = @{readlines(\*FILE)};
399         }
400         is_deeply( \@lines, \@extra_args, "$buildpath/stamp_configure contains extra args" );
401
402         &$do_dh_auto('build');
403         ok ( -f "$buildpath/stamp_build", "$buildpath/stamp_build exists" );
404         &$do_dh_auto('test');
405         ok ( -f "$buildpath/stamp_test", "$buildpath/stamp_test exists" );
406         &$do_dh_auto('install');
407         @lines=();
408         if ( ok(open(FILE, "$buildpath/stamp_install"), "$buildpath/stamp_install exists") ) {
409                 @lines = @{readlines(\*FILE)};
410         } 
411         is_deeply( \@lines, [ "DESTDIR=".Cwd::getcwd()."/debian/testpackage" ],
412             "$buildpath/stamp_install contains DESTDIR" );
413         &$do_dh_auto('clean');
414         if ($builddir) {
415                 ok ( ! -e "$buildpath", "builddir $buildpath was removed" );
416         }
417         else {
418                 ok ( ! -e "$buildpath/Makefile" && ! -e "$buildpath/stamp_configure", "Makefile and stamps gone" );
419         }
420         ok ( -x "$sourcedir/configure", "configure script renamins after clean" );
421 }
422
423 dh_auto_do_autoconf('autoconf');
424 dh_auto_do_autoconf('autoconf', 'bld/dir', configure_args => [ "--extra-autoconf-configure-arg" ]);
425 ok ( ! -e 'autoconf/bld', "autoconf/bld got deleted too" );
426
427 END {
428         system("rm", "-rf", $tmpdir);
429         system("$TOPDIR/dh_clean");
430 }