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.
34 sub create_buildsystem_instance {
37 my $module = "Debian::Debhelper::Buildsystem::$system";
41 error("unable to load build system class '$system': $@");
44 if (!exists $bsopts{builddir} && defined $opt_builddir) {
45 $bsopts{builddir} = ($opt_builddir eq "") ? undef : $opt_builddir;
47 if (!exists $bsopts{sourcedir} && defined $opt_sourcedir) {
48 $bsopts{sourcedir} = ($opt_sourcedir eq "") ? undef : $opt_sourcedir;
50 return $module->new(%bsopts);
53 # Similar to create_build system_instance(), but it attempts to autoselect
54 # a build system if none was specified. In case autoselection fails, undef
56 sub load_buildsystem {
59 if (defined $system) {
60 my $inst = create_buildsystem_instance($system, @_);
64 # Try to determine build system automatically
65 for $system (@BUILDSYSTEMS) {
66 my $inst = create_buildsystem_instance($system, @_);
67 if ($inst->check_auto_buildable($step)) {
75 sub load_all_buildsystems {
76 my $incs=shift || \@INC;
77 my (%buildsystems, @buildsystems);
79 for my $inc (@$incs) {
80 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Buildsystem");
82 for my $module_path (glob "$path/*.pm") {
83 my $name = basename($module_path);
85 next if exists $buildsystems{$name};
86 $buildsystems{$name} = create_buildsystem_instance($name, @_);
91 # Standard debhelper build systems first
92 for my $name (@BUILDSYSTEMS) {
93 error("standard debhelper build system '$name' could not be found/loaded")
94 if not exists $buildsystems{$name};
95 push @buildsystems, $buildsystems{$name};
96 delete $buildsystems{$name};
99 # The rest are 3rd party build systems
100 for my $name (keys %buildsystems) {
101 my $inst = $buildsystems{$name};
102 $inst->{thirdparty} = 1;
103 push @buildsystems, $inst;
106 return @buildsystems;
109 sub buildsystems_init {
112 # Available command line options
114 "D=s" => \$opt_sourcedir,
115 "sourcedirectory=s" => \$opt_sourcedir,
117 "B:s" => \$opt_builddir,
118 "builddirectory:s" => \$opt_builddir,
120 "S=s" => \$opt_buildsys,
121 "buildsystem=s" => \$opt_buildsys,
124 "list" => \$opt_list,
126 $args{options}{$_} = $options{$_} foreach keys(%options);
127 Debian::Debhelper::Dh_Lib::init(%args);
130 sub buildsystems_list {
133 # List build systems (including auto and specified status)
134 my ($auto, $specified);
135 for my $inst (load_all_buildsystems()) {
136 my $is_specified = defined $opt_buildsys && $opt_buildsys eq $inst->NAME();
137 if (! defined $specified && defined $opt_buildsys && $opt_buildsys eq $inst->NAME()) {
138 $specified = $inst->NAME();
140 elsif (! defined $auto && ! $inst->{thirdparty} && $inst->check_auto_buildable($step)) {
141 $auto = $inst->NAME();
143 printf("%-20s %s", $inst->NAME(), $inst->DESCRIPTION());
144 print " [3rd party]" if $inst->{thirdparty};
148 print "Auto-selected: $auto\n" if defined $auto;
149 print "Specified: $specified\n" if defined $specified;
150 print "No system auto-selected or specified\n"
151 if ! defined $auto && ! defined $specified;
154 sub buildsystems_do {
157 if (!defined $step) {
158 $step = basename($0);
159 $step =~ s/^dh_auto_//;
162 if (grep(/^\Q$step\E$/, qw{configure build test install clean}) == 0) {
163 error("unrecognized build step: " . $step);
167 buildsystems_list($step);
171 my $buildsystem = load_buildsystem($opt_buildsys, $step);
172 if (defined $buildsystem) {
173 $buildsystem->pre_building_step($step);
174 $buildsystem->$step(@_, @{$dh{U_PARAMS}});
175 $buildsystem->post_building_step($step);