1 # A module for loading and managing debhelper buildsystem plugins.
2 # This module is intended to be used by all dh_auto_* helper commands.
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 # buildsystems MUST be added to the END of the list.
33 sub create_buildsystem_instance {
36 my $module = "Debian::Debhelper::Buildsystem::$system";
40 error("unable to load buildsystem class '$system': $@");
43 if (!exists $bsopts{builddir} && defined $opt_builddir) {
44 $bsopts{builddir} = ($opt_builddir eq "") ? undef : $opt_builddir;
46 if (!exists $bsopts{sourcedir} && defined $opt_sourcedir) {
47 $bsopts{sourcedir} = ($opt_sourcedir eq "") ? undef : $opt_sourcedir;
49 return $module->new(%bsopts);
52 # Similar to create_buildsystem_instance(), but it attempts to autoselect
53 # a buildsystem if none was specified. In case autoselection fails, undef
55 sub load_buildsystem {
58 if (defined $system) {
59 my $inst = create_buildsystem_instance($system, @_);
63 # Try to determine build system automatically
64 for $system (@BUILDSYSTEMS) {
65 my $inst = create_buildsystem_instance($system, @_);
66 if ($inst->check_auto_buildable($step)) {
74 sub load_all_buildsystems {
75 my $incs=shift || \@INC;
76 my (%buildsystems, @buildsystems);
78 for my $inc (@$incs) {
79 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Buildsystem");
81 for my $module_path (glob "$path/*.pm") {
82 my $name = basename($module_path);
84 next if exists $buildsystems{$name};
85 $buildsystems{$name} = create_buildsystem_instance($name, @_);
90 # Push debhelper built-in buildsystems first
91 for my $name (@BUILDSYSTEMS) {
92 error("Debhelper built-in buildsystem '$name' could not be found/loaded")
93 if not exists $buildsystems{$name};
94 push @buildsystems, $buildsystems{$name};
95 delete $buildsystems{$name};
98 # The rest are 3rd party buildsystems
99 for my $name (keys %buildsystems) {
100 my $inst = $buildsystems{$name};
101 $inst->{thirdparty} = 1;
102 push @buildsystems, $inst;
105 return @buildsystems;
108 sub buildsystems_init {
111 # Available command line options
113 "d" => undef, # cancel default D_FLAG option spec
114 "d=s" => \$opt_sourcedir,
115 "sourcedirectory=s" => \$opt_sourcedir,
117 "b:s" => \$opt_builddir,
118 "builddirectory:s" => \$opt_builddir,
120 "c=s" => \$opt_buildsys,
121 "buildsystem=s" => \$opt_buildsys,
124 "--list" => \$opt_list,
126 $args{options}{$_} = $options{$_} foreach keys(%options);
128 # Pass options from the DH_AUTO_OPTIONS environment variable
129 if (defined $ENV{DH_AUTO_OPTIONS}) {
130 $args{extra_args} = $ENV{DH_AUTO_OPTIONS};
132 Debian::Debhelper::Dh_Lib::init(%args);
135 sub buildsystems_list {
138 # List buildsystems (including auto and specified status)
139 my ($auto, $specified);
140 my @buildsystems = load_all_buildsystems();
141 for my $inst (@buildsystems) {
142 my $is_specified = defined $opt_buildsys && $opt_buildsys eq $inst->NAME();
143 if (! defined $specified && defined $opt_buildsys && $opt_buildsys eq $inst->NAME()) {
144 $specified = $inst->NAME();
146 elsif (! defined $auto && ! $inst->{thirdparty} && $inst->check_auto_buildable($step)) {
147 $auto = $inst->NAME();
149 printf("%s - %s", $inst->NAME(), $inst->DESCRIPTION());
150 print " [3rd party]" if $inst->{thirdparty};
154 print "Auto-selected: $auto\n" if defined $auto;
155 print "Specified: $specified\n" if defined $specified;
156 print "No system auto-selected or specified\n"
157 if ! defined $auto && ! defined $specified;
160 sub buildsystems_do {
163 if (!defined $step) {
164 $step = basename($0);
165 $step =~ s/^dh_auto_//;
168 if (grep(/^\Q$step\E$/, qw{configure build test install clean}) == 0) {
169 error("unrecognized build step: " . $step);
173 buildsystems_list($step);
177 my $buildsystem = load_buildsystem($opt_buildsys, $step);
178 if (defined $buildsystem) {
179 $buildsystem->pre_building_step($step);
180 $buildsystem->$step(@_, @{$dh{U_PARAMS}});
181 $buildsystem->post_building_step($step);