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 # Historical order must be kept for backwards compatibility. New
18 # build systems MUST be added to the END of the list.
35 sub create_buildsystem_instance {
38 my $module = "Debian::Debhelper::Buildsystem::$system";
42 error("unable to load build system class '$system': $@");
45 if (!exists $bsopts{builddir} && defined $opt_builddir) {
46 $bsopts{builddir} = ($opt_builddir eq "") ? undef : $opt_builddir;
48 if (!exists $bsopts{sourcedir} && defined $opt_sourcedir) {
49 $bsopts{sourcedir} = ($opt_sourcedir eq "") ? undef : $opt_sourcedir;
51 if (!exists $bsopts{parallel}) {
52 $bsopts{parallel} = $opt_parallel;
54 return $module->new(%bsopts);
57 # Similar to create_build system_instance(), but it attempts to autoselect
58 # a build system if none was specified. In case autoselection fails, undef
60 sub load_buildsystem {
63 if (defined $system) {
64 my $inst = create_buildsystem_instance($system, @_);
68 # Try to determine build system automatically
69 for $system (@BUILDSYSTEMS) {
70 my $inst = create_buildsystem_instance($system, @_);
71 if ($inst->check_auto_buildable($step)) {
79 sub load_all_buildsystems {
80 my $incs=shift || \@INC;
81 my (%buildsystems, @buildsystems);
83 for my $inc (@$incs) {
84 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Buildsystem");
86 for my $module_path (glob "$path/*.pm") {
87 my $name = basename($module_path);
89 next if exists $buildsystems{$name};
90 $buildsystems{$name} = create_buildsystem_instance($name, @_);
95 # Standard debhelper build systems first
96 for my $name (@BUILDSYSTEMS) {
97 error("standard debhelper build system '$name' could not be found/loaded")
98 if not exists $buildsystems{$name};
99 push @buildsystems, $buildsystems{$name};
100 delete $buildsystems{$name};
103 # The rest are 3rd party build systems
104 for my $name (keys %buildsystems) {
105 my $inst = $buildsystems{$name};
106 $inst->{thirdparty} = 1;
107 push @buildsystems, $inst;
110 return @buildsystems;
113 sub buildsystems_init {
118 # Available command line options
120 "D=s" => \$opt_sourcedir,
121 "sourcedirectory=s" => \$opt_sourcedir,
123 "B:s" => \$opt_builddir,
124 "builddirectory:s" => \$opt_builddir,
126 "S=s" => \$opt_buildsys,
127 "buildsystem=s" => \$opt_buildsys,
130 "list" => \$opt_list,
132 "max-parallel:i" => \$max_parallel,
134 $args{options}{$_} = $options{$_} foreach keys(%options);
135 Debian::Debhelper::Dh_Lib::init(%args);
136 set_parallel($max_parallel);
142 if (exists $ENV{DEB_BUILD_OPTIONS}) {
143 # Parse parallel=n tag
145 foreach my $opt (split(/\s+/, $ENV{DEB_BUILD_OPTIONS})) {
146 $n = $1 if $opt =~ /^parallel=(\d+)$/;
148 if (defined $n && $n > 0) {
149 if (!$max || $n < $max) {
153 $opt_parallel = $max;
157 # Invalid value in the parallel tag. Disable.
166 sub buildsystems_list {
169 # List build systems (including auto and specified status)
170 my ($auto, $specified);
171 for my $inst (load_all_buildsystems()) {
172 my $is_specified = defined $opt_buildsys && $opt_buildsys eq $inst->NAME();
173 if (! defined $specified && defined $opt_buildsys && $opt_buildsys eq $inst->NAME()) {
174 $specified = $inst->NAME();
176 elsif (! defined $auto && ! $inst->{thirdparty} && $inst->check_auto_buildable($step)) {
177 $auto = $inst->NAME();
179 printf("%-20s %s", $inst->NAME(), $inst->DESCRIPTION());
180 print " [3rd party]" if $inst->{thirdparty};
184 print "Auto-selected: $auto\n" if defined $auto;
185 print "Specified: $specified\n" if defined $specified;
186 print "No system auto-selected or specified\n"
187 if ! defined $auto && ! defined $specified;
190 sub buildsystems_do {
193 if (!defined $step) {
194 $step = basename($0);
195 $step =~ s/^dh_auto_//;
198 if (grep(/^\Q$step\E$/, qw{configure build test install clean}) == 0) {
199 error("unrecognized build step: " . $step);
203 buildsystems_list($step);
207 my $buildsystem = load_buildsystem($opt_buildsys, $step);
208 if (defined $buildsystem) {
209 $buildsystem->pre_building_step($step);
210 $buildsystem->$step(@_, @{$dh{U_PARAMS}});
211 $buildsystem->post_building_step($step);