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 sub load_buildsystem {
53 my ($step, $system)=@_;
54 if (defined $system) {
55 my $inst = create_buildsystem_instance($system);
59 # Try to determine build system automatically
60 for $system (@BUILDSYSTEMS) {
61 my $inst = create_buildsystem_instance($system);
62 if ($inst->check_auto_buildable($step)) {
70 sub load_all_buildsystems {
71 my $incs=shift || \@INC;
72 my (%buildsystems, @buildsystems);
74 for my $inc (@$incs) {
75 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Buildsystem");
77 for my $module_path (glob "$path/*.pm") {
78 my $name = basename($module_path);
80 next if exists $buildsystems{$name};
81 $buildsystems{$name} = create_buildsystem_instance($name, @_);
86 # Push debhelper built-in buildsystems first
87 for my $name (@BUILDSYSTEMS) {
88 error("Debhelper built-in buildsystem '$name' could not be found/loaded")
89 if not exists $buildsystems{$name};
90 push @buildsystems, $buildsystems{$name};
91 delete $buildsystems{$name};
94 # The rest are 3rd party buildsystems
95 for my $name (keys %buildsystems) {
96 my $inst = $buildsystems{$name};
97 $inst->{thirdparty} = 1;
98 push @buildsystems, $inst;
101 return @buildsystems;
104 sub buildsystems_init {
107 # Available command line options
109 "d" => undef, # cancel default D_FLAG option spec
110 "d=s" => \$opt_sourcedir,
111 "sourcedirectory=s" => \$opt_sourcedir,
113 "b:s" => \$opt_builddir,
114 "builddirectory:s" => \$opt_builddir,
116 "c=s" => \$opt_buildsys,
117 "buildsystem=s" => \$opt_buildsys,
120 "--list" => \$opt_list,
122 $args{options}{$_} = $options{$_} foreach keys(%options);
124 # Pass options from the DH_AUTO_OPTIONS environment variable
125 if (defined $ENV{DH_AUTO_OPTIONS}) {
126 $args{extra_args} = $ENV{DH_AUTO_OPTIONS};
128 Debian::Debhelper::Dh_Lib::init(%args);
131 sub buildsystems_list {
134 # List buildsystems (including auto and specified status)
135 my ($auto, $specified);
136 my @buildsystems = load_all_buildsystems(undef);
137 for my $inst (@buildsystems) {
138 my $is_specified = defined $opt_buildsys && $opt_buildsys eq $inst->NAME();
139 if (! defined $specified && defined $opt_buildsys && $opt_buildsys eq $inst->NAME()) {
140 $specified = $inst->NAME();
142 elsif (! defined $auto && ! $inst->{thirdparty} && $inst->check_auto_buildable($step)) {
143 $auto = $inst->NAME();
145 printf("%s - %s", $inst->NAME(), $inst->DESCRIPTION());
146 print " [3rd party]" if $inst->{thirdparty};
150 print "Auto-selected: $auto\n" if defined $auto;
151 print "Specified: $specified\n" if defined $specified;
152 print "No system auto-selected or specified\n"
153 if ! defined $auto && ! defined $specified;
156 sub buildsystems_do {
159 if (!defined $step) {
160 $step = basename($0);
161 $step =~ s/^dh_auto_//;
164 if (grep(/^\Q$step\E$/, qw{configure build test install clean}) == 0) {
165 error("unrecognized build step: " . $step);
169 buildsystems_list($step);
173 my $buildsystem = load_buildsystem($step, $opt_buildsys);
174 if (defined $buildsystem) {
175 $buildsystem->pre_building_step($step);
176 $buildsystem->$step(@_, @{$dh{U_PARAMS}});
177 $buildsystem->post_building_step($step);