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