1 # A module for loading and managing debhelper build system classes.
2 # This module is intended to be used by all dh_auto_* programs.
4 # Copyright: © 2009 Modestas Vainius
7 package Debian::Debhelper::Dh_Buildsystems;
11 use Debian::Debhelper::Dh_Lib;
15 our @EXPORT=qw(&buildsystems_init &buildsystems_do &load_buildsystem &load_all_buildsystems);
17 use constant BUILD_STEPS => qw(configure build test install clean);
19 # Historical order must be kept for backwards compatibility. New
20 # build systems MUST be added to the END of the list.
23 (! compat(7) ? "perl_build" : ()),
27 (compat(7) ? "perl_build" : ()),
39 sub create_buildsystem_instance {
42 my $module = "Debian::Debhelper::Buildsystem::$system";
46 error("unable to load build system class '$system': $@");
49 if (!exists $bsopts{builddir} && defined $opt_builddir) {
50 $bsopts{builddir} = ($opt_builddir eq "") ? undef : $opt_builddir;
52 if (!exists $bsopts{sourcedir} && defined $opt_sourcedir) {
53 $bsopts{sourcedir} = ($opt_sourcedir eq "") ? undef : $opt_sourcedir;
55 if (!exists $bsopts{parallel}) {
56 $bsopts{parallel} = $opt_parallel;
58 return $module->new(%bsopts);
61 # Autoselect a build system from the list of instances
62 sub autoselect_buildsystem {
65 my $selected_level = 0;
67 foreach my $inst (@_) {
68 # Only derived (i.e. more specific) build system can be
69 # considered beyond the currently selected one.
70 next if defined $selected && !$inst->isa(ref $selected);
72 # If the build system says it is auto-buildable at the current
73 # step and it can provide more specific information about its
74 # status than its parent (if any), auto-select it.
75 my $level = $inst->check_auto_buildable($step);
76 if ($level > $selected_level) {
78 $selected_level = $level;
84 # Similar to create_build system_instance(), but it attempts to autoselect
85 # a build system if none was specified. In case autoselection fails, undef
87 sub load_buildsystem {
90 if (defined $system) {
91 my $inst = create_buildsystem_instance($system, @_);
95 # Try to determine build system automatically
97 foreach $system (@BUILDSYSTEMS) {
98 push @buildsystems, create_buildsystem_instance($system, @_);
100 return autoselect_buildsystem($step, @buildsystems);
104 sub load_all_buildsystems {
105 my $incs=shift || \@INC;
106 my (%buildsystems, @buildsystems);
108 foreach my $inc (@$incs) {
109 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Buildsystem");
111 foreach my $module_path (glob "$path/*.pm") {
112 my $name = basename($module_path);
114 next if exists $buildsystems{$name};
115 $buildsystems{$name} = create_buildsystem_instance($name, @_);
120 # Standard debhelper build systems first
121 foreach my $name (@BUILDSYSTEMS) {
122 error("standard debhelper build system '$name' could not be found/loaded")
123 if not exists $buildsystems{$name};
124 push @buildsystems, $buildsystems{$name};
125 delete $buildsystems{$name};
128 # The rest are 3rd party build systems
129 foreach my $name (keys %buildsystems) {
130 my $inst = $buildsystems{$name};
131 $inst->{thirdparty} = 1;
132 push @buildsystems, $inst;
135 return @buildsystems;
138 sub buildsystems_init {
143 # Available command line options
145 "D=s" => \$opt_sourcedir,
146 "sourcedirectory=s" => \$opt_sourcedir,
148 "B:s" => \$opt_builddir,
149 "builddirectory:s" => \$opt_builddir,
151 "S=s" => \$opt_buildsys,
152 "buildsystem=s" => \$opt_buildsys,
155 "list" => \$opt_list,
157 "parallel" => sub { $max_parallel = -1 },
158 "max-parallel=i" => \$max_parallel,
160 $args{options}{$_} = $options{$_} foreach keys(%options);
161 Debian::Debhelper::Dh_Lib::init(%args);
162 set_parallel($max_parallel);
170 if (exists $ENV{DEB_BUILD_OPTIONS}) {
171 # Get number of processes from parallel=n tag limiting it
172 # with $max if needed
173 foreach my $opt (split(/\s+/, $ENV{DEB_BUILD_OPTIONS})) {
174 if ($opt =~ /^parallel=(-?\d+)$/) {
176 if ($max > 0 && $opt_parallel > $max) {
177 $opt_parallel = $max;
184 sub buildsystems_list {
187 my @buildsystems = load_all_buildsystems();
188 my $auto = autoselect_buildsystem($step, grep { ! $_->{thirdparty} } @buildsystems);
191 # List build systems (including auto and specified status)
192 foreach my $inst (@buildsystems) {
193 if (! defined $specified && defined $opt_buildsys && $opt_buildsys eq $inst->NAME()) {
196 printf("%-20s %s", $inst->NAME(), $inst->DESCRIPTION());
197 print " [3rd party]" if $inst->{thirdparty};
201 print "Auto-selected: ", $auto->NAME(), "\n" if defined $auto;
202 print "Specified: ", $specified->NAME(), "\n" if defined $specified;
203 print "No system auto-selected or specified\n"
204 if ! defined $auto && ! defined $specified;
207 sub buildsystems_do {
210 if (!defined $step) {
211 $step = basename($0);
212 $step =~ s/^dh_auto_//;
215 if (grep(/^\Q$step\E$/, BUILD_STEPS) == 0) {
216 error("unrecognized build step: " . $step);
220 buildsystems_list($step);
224 my $buildsystem = load_buildsystem($opt_buildsys, $step);
225 if (defined $buildsystem) {
226 $buildsystem->pre_building_step($step);
227 $buildsystem->$step(@_, @{$dh{U_PARAMS}});
228 $buildsystem->post_building_step($step);