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.
37 sub create_buildsystem_instance {
40 my $module = "Debian::Debhelper::Buildsystem::$system";
44 error("unable to load build system class '$system': $@");
47 if (!exists $bsopts{builddir} && defined $opt_builddir) {
48 $bsopts{builddir} = ($opt_builddir eq "") ? undef : $opt_builddir;
50 if (!exists $bsopts{sourcedir} && defined $opt_sourcedir) {
51 $bsopts{sourcedir} = ($opt_sourcedir eq "") ? undef : $opt_sourcedir;
53 if (!exists $bsopts{parallel}) {
54 $bsopts{parallel} = $opt_parallel;
56 return $module->new(%bsopts);
59 # Similar to create_build system_instance(), but it attempts to autoselect
60 # a build system if none was specified. In case autoselection fails, undef
62 sub load_buildsystem {
65 if (defined $system) {
66 my $inst = create_buildsystem_instance($system, @_);
70 # Try to determine build system automatically
72 my $selected_level = 0;
73 for $system (@BUILDSYSTEMS) {
74 my $inst = create_buildsystem_instance($system, @_);
76 # Only derived (i.e. more specific) build system can be
77 # considered beyond the currently selected one.
78 next if defined $selected && !$inst->isa(ref $selected);
80 # If the build system says it is auto-buildable at the current
81 # step and it can provide more specific information about its
82 # status than its parent (if any), auto-select it.
83 my $level = $inst->check_auto_buildable($step);
84 if ($level > $selected_level) {
86 $selected_level = $level;
93 sub load_all_buildsystems {
94 my $incs=shift || \@INC;
95 my (%buildsystems, @buildsystems);
97 for my $inc (@$incs) {
98 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Buildsystem");
100 for my $module_path (glob "$path/*.pm") {
101 my $name = basename($module_path);
103 next if exists $buildsystems{$name};
104 $buildsystems{$name} = create_buildsystem_instance($name, @_);
109 # Standard debhelper build systems first
110 for my $name (@BUILDSYSTEMS) {
111 error("standard debhelper build system '$name' could not be found/loaded")
112 if not exists $buildsystems{$name};
113 push @buildsystems, $buildsystems{$name};
114 delete $buildsystems{$name};
117 # The rest are 3rd party build systems
118 for my $name (keys %buildsystems) {
119 my $inst = $buildsystems{$name};
120 $inst->{thirdparty} = 1;
121 push @buildsystems, $inst;
124 return @buildsystems;
127 sub buildsystems_init {
130 my $max_parallel=-1; # unlimited
132 # Available command line options
134 "D=s" => \$opt_sourcedir,
135 "sourcedirectory=s" => \$opt_sourcedir,
137 "B:s" => \$opt_builddir,
138 "builddirectory:s" => \$opt_builddir,
140 "S=s" => \$opt_buildsys,
141 "buildsystem=s" => \$opt_buildsys,
144 "list" => \$opt_list,
146 "max-parallel=i" => \$max_parallel,
148 $args{options}{$_} = $options{$_} foreach keys(%options);
149 Debian::Debhelper::Dh_Lib::init(%args);
150 set_parallel($max_parallel);
158 if (exists $ENV{DEB_BUILD_OPTIONS}) {
159 # Parse parallel=n tag
160 foreach my $opt (split(/\s+/, $ENV{DEB_BUILD_OPTIONS})) {
161 if ($opt =~ /^parallel=([-\d]+)$/) {
163 if ($n > 0 && ($max == -1 || $n < $max)) {
167 $opt_parallel = $max;
174 sub buildsystems_list {
177 # List build systems (including auto and specified status)
178 my ($auto, $specified);
179 for my $inst (load_all_buildsystems()) {
180 my $is_specified = defined $opt_buildsys && $opt_buildsys eq $inst->NAME();
181 if (! defined $specified && defined $opt_buildsys && $opt_buildsys eq $inst->NAME()) {
182 $specified = $inst->NAME();
184 elsif (! defined $auto && ! $inst->{thirdparty} && $inst->check_auto_buildable($step)) {
185 $auto = $inst->NAME();
187 printf("%-20s %s", $inst->NAME(), $inst->DESCRIPTION());
188 print " [3rd party]" if $inst->{thirdparty};
192 print "Auto-selected: $auto\n" if defined $auto;
193 print "Specified: $specified\n" if defined $specified;
194 print "No system auto-selected or specified\n"
195 if ! defined $auto && ! defined $specified;
198 sub buildsystems_do {
201 if (!defined $step) {
202 $step = basename($0);
203 $step =~ s/^dh_auto_//;
206 if (grep(/^\Q$step\E$/, BUILD_STEPS) == 0) {
207 error("unrecognized build step: " . $step);
211 buildsystems_list($step);
215 my $buildsystem = load_buildsystem($opt_buildsys, $step);
216 if (defined $buildsystem) {
217 $buildsystem->pre_building_step($step);
218 $buildsystem->$step(@_, @{$dh{U_PARAMS}});
219 $buildsystem->post_building_step($step);