]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Buildsystems.pm
Improvements in DH_OPTIONS handling and DH_AUTO_OPTIONS envvar support.
[debhelper.git] / Debian / Debhelper / Dh_Buildsystems.pm
1 # A module for loading and managing debhelper buildsystem plugins.
2 # This module is intended to be used by all dh_auto_* helper commands.
3 #
4 # Copyright: © 2009 Modestas Vainius
5 # License: GPL-2+
6
7 package Debian::Debhelper::Dh_Buildsystems;
8
9 use strict;
10 use warnings;
11 use Debian::Debhelper::Dh_Lib;
12
13 use base 'Exporter';
14 our @EXPORT=qw(&buildsystems_init &buildsystems_do &load_buildsystem);
15
16 # Historical order must be kept for backwards compatibility. New
17 # buildsystems MUST be added to the END of the list.
18 our @BUILDSYSTEMS = (
19     "autoconf",
20     "perl_makemaker",
21     "makefile",
22     "python_distutils",
23     "perl_build",
24     "cmake",
25 );
26
27 my $opt_buildsys;
28 my $opt_builddir;
29 my $opt_list;
30
31 sub create_buildsystem_instance {
32         my $system=shift;
33         my %bsopts=@_;
34         my $module = "Debian::Debhelper::Buildsystem::$system";
35
36         eval "use $module";
37         if ($@) {
38                 error("unable to load buildsystem class '$system': $@");
39         }
40
41         if (!exists $bsopts{builddir} && defined $opt_builddir) {
42                 $bsopts{builddir} = ($opt_builddir eq "") ? undef : $opt_builddir;
43         }
44         return $module->new(%bsopts);
45 }
46
47 sub load_buildsystem {
48         my ($step, $system)=@_;
49         if (defined $system) {
50                 my $inst = create_buildsystem_instance($system);
51                 return $inst;
52         }
53         else {
54                 # Try to determine build system automatically
55                 for $system (@BUILDSYSTEMS) {
56                         my $inst = create_buildsystem_instance($system, build_step=>$step);
57                         if ($inst->is_buildable()) {
58                                 return $inst;
59                         }
60                 }
61         }
62         return;
63 }
64
65 sub buildsystems_init {
66         my %args=@_;
67
68         # Available command line options
69         my %options = (
70             "b:s" => \$opt_builddir,
71             "builddirectory:s" => \$opt_builddir,
72
73             "c=s" => \$opt_buildsys,
74             "buildsystem=s" => \$opt_buildsys,
75
76             "l" => \$opt_list,
77             "--list" => \$opt_list,
78         );
79         $args{options}{$_} = $options{$_} foreach keys(%options);
80
81         # Pass options from the DH_AUTO_OPTIONS environment variable
82         if (defined $ENV{DH_AUTO_OPTIONS}) {
83                 $args{extra_args} = $ENV{DH_AUTO_OPTIONS};
84         }
85         Debian::Debhelper::Dh_Lib::init(%args);
86 }
87
88 sub buildsystems_list {
89         my $step=shift;
90
91         # List buildsystems (including auto and specified status)
92         my ($auto, $specified);
93         for my $system (@BUILDSYSTEMS) {
94                 my $inst = create_buildsystem_instance($system, build_step => undef);
95                 my $is_specified = defined $opt_buildsys && $opt_buildsys eq $inst->NAME();
96                 if (! defined $specified && defined $opt_buildsys && $opt_buildsys eq $inst->NAME()) {
97                         $specified = $inst->NAME();
98                 }
99                 elsif (! defined $auto && $inst->check_auto_buildable($step)) {
100                         $auto = $inst->NAME();
101                 }
102                 printf("%s - %s\n", $inst->NAME(), $inst->DESCRIPTION());
103         }
104         # List a specified 3rd party buildsystem too.
105         if (! defined $specified && defined $opt_buildsys) {
106                 my $inst = create_buildsystem_instance($opt_buildsys, build_step => undef);
107                 printf("%s - %s.\n", $inst->NAME(), $inst->DESCRIPTION());
108                 $specified = $inst->NAME();
109         }
110         print "\n";
111         print "Auto-selected: $auto\n" if defined $auto;
112         print "Specified: $specified\n" if defined $specified;
113         print "No system auto-selected or specified\n"
114                 if ! defined $auto && ! defined $specified;
115 }
116
117 sub buildsystems_do {
118         my $step=shift;
119
120         if (!defined $step) {
121                 $step = basename($0);
122                 $step =~ s/^dh_auto_//;
123         }
124
125         if (grep(/^\Q$step\E$/, qw{configure build test install clean}) == 0) {
126                 error("unrecognized build step: " . $step);
127         }
128
129         if ($opt_list) {
130                 buildsystems_list($step);
131                 exit 0;
132         }
133
134         my $buildsystem = load_buildsystem($step, $opt_buildsys);
135         if (defined $buildsystem) {
136                 $buildsystem->pre_step($step);
137                 $buildsystem->$step(@_, @{$dh{U_PARAMS}});
138                 $buildsystem->post_step($step);
139         }
140         return 0;
141 }
142
143 1;