]> git.donarmstrong.com Git - debhelper.git/blob - t/buildsystems/buildsystem_tests
27d7b943c7cb9cc08b4dddaa373d401be736c188
[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()" => "$default_builddir",
130         "get_buildpath()" => "$default_builddir",
131         "get_buildpath(a/b)" =>  "$default_builddir/a/b",
132         "get_source_rel2builddir()" => "../autoconf",
133         "get_source_rel2builddir(a/b)" => "../autoconf/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 # Enforce "hard" 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, "hard out of source enforced, sourcedir=builddir", \%tmp);
144
145 # sourcedir=autoconf (builddir should be dropped)
146 $bs = $BS_CLASS->new(builddir => "autoconf", 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 # Enforce "soft" out of source tree building when
161 # sourcedir=builddir=autoconf hence builddir should be dropped.
162 $bs->enforce_out_of_source_building("autoconf");
163 test_buildsystem_paths_api($bs, "soft out of source enforced, sourcedir=builddir", \%tmp);
164
165 # builddir=bld/dir, sourcedir=autoconf. Should be the same as sourcedir=autoconf.
166 $bs = $BS_CLASS->new(builddir => "bld/dir", sourcedir => "autoconf");
167 $bs->enforce_in_source_building();
168 test_buildsystem_paths_api($bs, "in source enforced, sourcedir=autoconf", \%tmp);
169
170 # builddir=../bld/dir (relative to the curdir)
171 $bs = $BS_CLASS->new(builddir => "bld/dir/", sourcedir => "autoconf");
172 %tmp = (
173         "get_sourcedir()" => "autoconf",
174         "get_sourcepath(a/b)" => "autoconf/a/b",
175         "get_builddir()" => "bld/dir",
176         "get_buildpath()" => "bld/dir",
177         "get_buildpath(a/b)" =>  "bld/dir/a/b",
178         "get_source_rel2builddir()" => "../../autoconf",
179         "get_source_rel2builddir(a/b)" => "../../autoconf/a/b",
180         "get_build_rel2sourcedir()" => "../bld/dir",
181         "get_build_rel2sourcedir(a/b)" => "../bld/dir/a/b",
182 );
183 test_buildsystem_paths_api($bs, "builddir=../bld/dir, sourcedir=autoconf", \%tmp);
184
185 ### Test if all buildsystems can be loaded
186 @bs = load_all_buildsystems([ $INC[0] ]);
187 @tmp = map { $_->NAME() } @bs;
188 is_deeply( \@tmp, \@BUILDSYSTEMS, "load_all_buildsystems() loads all built-in buildsystems" );
189
190 ### Test check_auto_buildable() of each buildsystem
191 sub test_check_auto_buildable {
192         my $bs=shift;
193         my $config=shift;
194         my $expected=shift;
195         my @steps=@_ || @STEPS;
196
197         if (! ref $expected) {
198                 my %all_steps;
199                 $all_steps{$_} = $expected foreach (@steps);
200                 $expected = \%all_steps;
201         }
202         for my $step (@steps) {
203                 my $e = 0;
204                 if (exists $expected->{$step}) {
205                         $e = $expected->{$step};
206                 } elsif (exists $expected->{default}) {
207                         $e = $expected->{default};
208                 }
209                 if ($e) {
210                         ok( $bs->check_auto_buildable($step),
211                             $bs->NAME() . "($config): check_auto_buildable($step)" );
212                 }
213                 else {
214                         ok( ! $bs->check_auto_buildable($step),
215                             $bs->NAME() . "($config): ! check_auto_buildable($step)" );
216                 }
217         }
218 }
219
220 $tmpdir = tempdir("tmp.XXXXXX");
221 $builddir = "$tmpdir/builddir";
222 mkdir $builddir;
223 %tmp = (
224         builddir => "$tmpdir/builddir",
225         sourcedir => $tmpdir
226 );
227
228 $bs{autoconf} = load_buildsystem("autoconf", undef, %tmp);
229 $bs{cmake} = load_buildsystem("cmake", undef, %tmp);
230 $bs{perl_mm} = load_buildsystem("perl_makemaker", undef, %tmp);
231 $bs = load_buildsystem("makefile", undef, %tmp);
232
233 test_check_auto_buildable($bs{autoconf}, "no configure", 0);
234 test_check_auto_buildable($bs{cmake}, "no CMakeLists.txt", 0);
235 test_check_auto_buildable($bs{perl_mm}, "no Makefile.PL", 0);
236 test_check_auto_buildable($bs, "no Makefile", 0);
237
238 touch "$tmpdir/configure", 0755;
239 test_check_auto_buildable($bs{autoconf}, "configure", { configure => 1 });
240
241 touch "$tmpdir/CMakeLists.txt";
242 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt", { configure => 1 });
243
244 touch "$tmpdir/Makefile.PL";
245 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL",
246     { configure => 1, install => 1 });
247
248 # With Makefile
249 touch "$builddir/Makefile";
250 test_check_auto_buildable($bs, "Makefile", { configure => 0, default => 1 });
251 test_check_auto_buildable($bs{autoconf}, "configure+Makefile", { configure => 1 });
252 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt+Makefile", 1);
253
254 # Makefile.PL forces in-source
255 #(see note in check_auto_buildable() why always 1 here)
256 unlink "$builddir/Makefile";
257 touch "$tmpdir/Makefile";
258 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL+Makefile", 1);
259
260 # Perl Build.PL - handles always
261 $bs = load_buildsystem("perl_build", undef, %tmp);
262 test_check_auto_buildable($bs, "no Build.PL", 0);
263 touch "$tmpdir/Build.PL";
264 test_check_auto_buildable($bs, "Build.PL", { configure => 1 });
265 touch "$tmpdir/Build"; # forced in source
266 test_check_auto_buildable($bs, "Build.PL+Build", 1);
267
268 # Python Distutils
269 $bs = load_buildsystem("python_distutils", undef, %tmp);
270 test_check_auto_buildable($bs, "no setup.py", 0);
271 touch "$tmpdir/setup.py";
272 test_check_auto_buildable($bs, "setup.py", 1);
273
274 cleandir($tmpdir);
275
276 ### Now test if it can autoselect a proper buildsystem for a typical package
277 sub test_autoselection {
278         my $system=shift;
279         my $expected=shift;
280         for my $step (@STEPS) {
281                 my $bs = load_buildsystem(undef, $step, @_);
282                 my $e = $expected;
283                 $e = $expected->{$step} if ref $expected;
284                 if (defined $bs) {
285                         is( $bs->NAME(), $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
286                 }
287                 else {
288                         is ( undef, $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
289                 }
290         }
291 }
292
293 # Autoconf
294 touch "$tmpdir/configure", 0755;
295 touch "$builddir/Makefile";
296 test_autoselection("autoconf",
297     { configure => "autoconf", build => "makefile",
298       test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
299 cleandir $tmpdir;
300
301 # Perl Makemaker (build, test, clean fail with builddir set [not supported])
302 touch "$tmpdir/Makefile.PL";
303 touch "$tmpdir/Makefile";
304 test_autoselection("perl_makemaker", "perl_makemaker", %tmp);
305 cleandir $tmpdir;
306
307 # Makefile
308 touch "$builddir/Makefile";
309 test_autoselection("makefile", { build => "makefile", test => "makefile",
310                 install => "makefile", clean => "makefile" }, %tmp);
311 cleandir $tmpdir;
312
313 # Python Distutils
314 touch "$tmpdir/setup.py";
315 test_autoselection("python_distutils", "python_distutils", %tmp);
316 cleandir $tmpdir;
317
318 # Perl Build
319 touch "$tmpdir/Build.PL";
320 touch "$tmpdir/Build";
321 test_autoselection("perl_build", "perl_build", %tmp);
322 cleandir $tmpdir;
323
324 # CMake
325 touch "$tmpdir/CMakeLists.txt";
326 touch "$builddir/Makefile";
327 test_autoselection("cmake",
328     { configure => "cmake", build => "makefile",
329       test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
330 cleandir $tmpdir;
331
332 ### Test buildsystems_init() and commandline/env argument handling
333 sub get_load_bs_source {
334         my ($system, $step)=@_;
335         $step = (defined $step) ? "'$step'" : 'undef';
336         $system = (defined $system) ? "'$system'" : 'undef';
337
338 return <<EOF;
339 use strict;
340 use warnings;
341 use Debian::Debhelper::Dh_Buildsystems;
342
343 buildsystems_init();
344 my \$bs = load_buildsystem($system, $step);
345 if (defined \$bs) {
346         print 'NAME=', \$bs->NAME(), "\\n";
347         print \$_, "=", (defined \$bs->{\$_}) ? \$bs->{\$_} : 'undef', "\\n"
348             foreach (sort keys \%\$bs);
349 }
350 EOF
351 }
352
353 is_deeply( process_stdout("DH_AUTO_OPTIONS='--builddirectory=autoconf/bld\\ dir --sourcedirectory autoconf' $^X -- -",
354                           get_load_bs_source(undef, "configure")),
355     [ 'NAME=autoconf', 'builddir=autoconf/bld dir', 'makecmd=make', 'sourcedir=autoconf' ],
356     "dh_auto_options w/space, autoconf autoselection and sourcedir/builddir" );
357
358 is_deeply( process_stdout("$^X -- - -cautoconf -d autoconf", get_load_bs_source("autoconf", "build")),
359     [ 'NAME=autoconf', 'builddir=undef', 'makecmd=make', 'sourcedir=autoconf' ],
360     "forced autoconf and sourcedir" );
361
362 is_deeply( process_stdout("$^X -- - -b -cautoconf", get_load_bs_source("autoconf", "build")),
363     [ 'NAME=autoconf', "builddir=$default_builddir", 'makecmd=make', 'sourcedir=.' ],
364     "forced autoconf and default build directory" );
365
366 # Build the autoconf test package
367 sub dh_auto_do_autoconf {
368         my $sourcedir=shift;
369         my $builddir=shift;
370         my %args=@_;
371
372         my (@lines, @extra_args);
373         my $buildpath = $sourcedir;
374         my @dh_auto_args = ("-d", $sourcedir);
375         my $dh_auto_str = "-d $sourcedir";
376         if ($builddir) {
377                 push @dh_auto_args, "-b", $builddir;
378                 $dh_auto_str .= " -b $builddir";
379                 $buildpath = $builddir;
380         }
381
382         my $do_dh_auto = sub {
383                 my $step=shift;
384                 my @extra_args;
385                 my $extra_str = "";
386                 if (exists $args{"${step}_args"}) {
387                         push @extra_args, @{$args{"${step}_args"}};
388                         $extra_str .= " $_" foreach (@extra_args);
389                 }
390                 is ( system("$TOPDIR/dh_auto_$step", @dh_auto_args, "--", @extra_args), 0,
391                          "dh_auto_$step $dh_auto_str$extra_str" );
392                 return @extra_args;
393         };
394         
395         @extra_args = &$do_dh_auto('configure');
396         ok ( -f "$buildpath/Makefile", "$buildpath/Makefile exists" );
397         @lines=();
398         if (ok( open(FILE, "$buildpath/stamp_configure"), "$buildpath/stamp_configure exists") ) {
399                 @lines = @{readlines(\*FILE)};
400         }
401         is_deeply( \@lines, \@extra_args, "$buildpath/stamp_configure contains extra args" );
402
403         &$do_dh_auto('build');
404         ok ( -f "$buildpath/stamp_build", "$buildpath/stamp_build exists" );
405         &$do_dh_auto('test');
406         ok ( -f "$buildpath/stamp_test", "$buildpath/stamp_test exists" );
407         &$do_dh_auto('install');
408         @lines=();
409         if ( ok(open(FILE, "$buildpath/stamp_install"), "$buildpath/stamp_install exists") ) {
410                 @lines = @{readlines(\*FILE)};
411         } 
412         is_deeply( \@lines, [ "DESTDIR=".Cwd::getcwd()."/debian/testpackage" ],
413             "$buildpath/stamp_install contains DESTDIR" );
414         &$do_dh_auto('clean');
415         if ($builddir) {
416                 ok ( ! -e "$buildpath", "builddir $buildpath was removed" );
417         }
418         else {
419                 ok ( ! -e "$buildpath/Makefile" && ! -e "$buildpath/stamp_configure", "Makefile and stamps gone" );
420         }
421         ok ( -x "$sourcedir/configure", "configure script renamins after clean" );
422 }
423
424 dh_auto_do_autoconf('autoconf');
425 dh_auto_do_autoconf('autoconf', 'bld/dir', configure_args => [ "--extra-autoconf-configure-arg" ]);
426 ok ( ! -e 'autoconf/bld', "autoconf/bld got deleted too" );
427
428 END {
429         system("rm", "-rf", $tmpdir);
430         system("$TOPDIR/dh_clean");
431 }