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