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.
32 sub create_buildsystem_instance {
35 my $module = "Debian::Debhelper::Buildsystem::$system";
39 error("unable to load buildsystem class '$system': $@");
42 if (!exists $bsopts{builddir} && defined $opt_builddir) {
43 $bsopts{builddir} = ($opt_builddir eq "") ? undef : $opt_builddir;
45 return $module->new(%bsopts);
48 sub load_buildsystem {
49 my ($step, $system)=@_;
50 if (defined $system) {
51 my $inst = create_buildsystem_instance($system);
55 # Try to determine build system automatically
56 for $system (@BUILDSYSTEMS) {
57 my $inst = create_buildsystem_instance($system, build_step=>$step);
58 if ($inst->is_buildable()) {
66 sub load_all_buildsystems {
67 my $incs=shift || \@INC;
68 my (%buildsystems, @buildsystems);
70 for my $inc (@$incs) {
71 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Buildsystem");
73 for my $module_path (glob "$path/*.pm") {
74 my $name = basename($module_path);
76 next if exists $buildsystems{$name};
77 $buildsystems{$name} = create_buildsystem_instance($name, @_);
82 # Push debhelper built-in buildsystems first
83 for my $name (@BUILDSYSTEMS) {
84 error("Debhelper built-in buildsystem '$name' could not be found/loaded")
85 if not exists $buildsystems{$name};
86 push @buildsystems, $buildsystems{$name};
87 delete $buildsystems{$name};
90 # The rest are 3rd party buildsystems
91 for my $name (keys %buildsystems) {
92 my $inst = $buildsystems{$name};
93 $inst->{thirdparty} = 1;
94 push @buildsystems, $inst;
100 sub buildsystems_init {
103 # Available command line options
105 "b:s" => \$opt_builddir,
106 "builddirectory:s" => \$opt_builddir,
108 "c=s" => \$opt_buildsys,
109 "buildsystem=s" => \$opt_buildsys,
112 "--list" => \$opt_list,
114 $args{options}{$_} = $options{$_} foreach keys(%options);
116 # Pass options from the DH_AUTO_OPTIONS environment variable
117 if (defined $ENV{DH_AUTO_OPTIONS}) {
118 $args{extra_args} = $ENV{DH_AUTO_OPTIONS};
120 Debian::Debhelper::Dh_Lib::init(%args);
123 sub buildsystems_list {
126 # List buildsystems (including auto and specified status)
127 my ($auto, $specified);
128 my @buildsystems = load_all_buildsystems(undef, build_step => undef);
129 for my $inst (@buildsystems) {
130 my $is_specified = defined $opt_buildsys && $opt_buildsys eq $inst->NAME();
131 if (! defined $specified && defined $opt_buildsys && $opt_buildsys eq $inst->NAME()) {
132 $specified = $inst->NAME();
134 elsif (! defined $auto && ! $inst->{thirdparty} && $inst->check_auto_buildable($step)) {
135 $auto = $inst->NAME();
137 printf("%s - %s", $inst->NAME(), $inst->DESCRIPTION());
138 print " [3rd party]" if $inst->{thirdparty};
142 print "Auto-selected: $auto\n" if defined $auto;
143 print "Specified: $specified\n" if defined $specified;
144 print "No system auto-selected or specified\n"
145 if ! defined $auto && ! defined $specified;
148 sub buildsystems_do {
151 if (!defined $step) {
152 $step = basename($0);
153 $step =~ s/^dh_auto_//;
156 if (grep(/^\Q$step\E$/, qw{configure build test install clean}) == 0) {
157 error("unrecognized build step: " . $step);
161 buildsystems_list($step);
165 my $buildsystem = load_buildsystem($step, $opt_buildsys);
166 if (defined $buildsystem) {
167 $buildsystem->pre_step($step);
168 $buildsystem->$step(@_, @{$dh{U_PARAMS}});
169 $buildsystem->post_step($step);