| 1 | # Copyright © 2020-2024 Guillem Jover <guillem@debian.org> | ||
| 2 | # | ||
| 3 | # This program is free software; you can redistribute it and/or modify | ||
| 4 | # it under the terms of the GNU General Public License as published by | ||
| 5 | # the Free Software Foundation; either version 2 of the License, or | ||
| 6 | # (at your option) any later version. | ||
| 7 | # | ||
| 8 | # This program is distributed in the hope that it will be useful, | ||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | # GNU General Public License for more details. | ||
| 12 | # | ||
| 13 | # You should have received a copy of the GNU General Public License | ||
| 14 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 15 | |||
| 16 | =encoding utf8 | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | Dpkg::BuildDriver - drive the build of a Debian package | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | This class is used by dpkg-buildpackage to drive the build of a Debian | ||
| 25 | package. | ||
| 26 | |||
| 27 | B<Note>: This is a private module, its API can change at any time. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | package Dpkg::BuildDriver 0.01; | ||
| 32 | |||
| 33 | use v5.36; | ||
| 34 | |||
| 35 | use Dpkg (); | ||
| 36 | use Dpkg::Gettext; | ||
| 37 | use Dpkg::ErrorHandling; | ||
| 38 | |||
| 39 | =head1 METHODS | ||
| 40 | |||
| 41 | =over 4 | ||
| 42 | |||
| 43 | =item $bd = Dpkg::BuildDriver->new(%opts) | ||
| 44 | |||
| 45 | Create a new Dpkg::BuildDriver object. | ||
| 46 | It will load a build driver module as requested in the B<Build-Drivers> field | ||
| 47 | in the $opts{ctrl} L<Dpkg::Control::Info> object or if not present, | ||
| 48 | it will fall back to load the default B<debian-rules> driver. | ||
| 49 | |||
| 50 | Options: | ||
| 51 | |||
| 52 | =over | ||
| 53 | |||
| 54 | =item B<ctrl> (required) | ||
| 55 | |||
| 56 | A L<Dpkg::Control::Info> object. | ||
| 57 | |||
| 58 | =item B<root_cmd> | ||
| 59 | |||
| 60 | A string with the gain-root-command to use when needing to execute a command | ||
| 61 | with root-like rights. | ||
| 62 | If needed and unset, it will default to L<fakeroot> if it is available or | ||
| 63 | the module will error out. | ||
| 64 | |||
| 65 | =item B<as_root> | ||
| 66 | |||
| 67 | A boolean to force F<debian/rules> target calls as root-like, even if they | ||
| 68 | would normally not require to be executed as root-like. | ||
| 69 | This option is applied to all targets globally. | ||
| 70 | |||
| 71 | B<Note>: This option is only relevant for drivers that use F<debian/rules>. | ||
| 72 | |||
| 73 | =item B<debian_rules> | ||
| 74 | |||
| 75 | An array containing the command to execute the F<debian/rules> file and any | ||
| 76 | additional arguments. | ||
| 77 | It defaults to B<debian/rules>. | ||
| 78 | |||
| 79 | B<Note>: This option is only relevant for drivers that use F<debian/rules>. | ||
| 80 | |||
| 81 | =item B<rrr_override> | ||
| 82 | |||
| 83 | A string that overrides the B<Rules-Requires-Root> field value. | ||
| 84 | |||
| 85 | B<Note>: This option is only relevant for drivers that use F<debian/rules>. | ||
| 86 | |||
| 87 | =back | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub _load_driver { | ||
| 92 | my ($name, %opts) = @_; | ||
| 93 | |||
| 94 | # Normalize the driver name. | ||
| 95 | $name = join q{}, map { ucfirst lc } split /-/, $name; | ||
| 96 | |||
| 97 | my $module = "Dpkg::BuildDriver::$name"; | ||
| 98 | eval qq{ | ||
| 99 | require $module; | ||
| 100 | }; | ||
| 101 | error(g_('build driver %s is unknown: %s'), $name, $@) if $@; | ||
| 102 | |||
| 103 | return $module->new(%opts); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub new { | ||
| 107 | my ($this, %opts) = @_; | ||
| 108 | my $class = ref($this) || $this; | ||
| 109 | |||
| 110 | my $ctrl_src = $opts{ctrl}->get_source(); | ||
| 111 | my $name = $ctrl_src->{'Build-Driver'} // 'debian-rules'; | ||
| 112 | my $self = { | ||
| 113 | driver => _load_driver($name, %opts), | ||
| 114 | }; | ||
| 115 | bless $self, $class; | ||
| 116 | return $self; | ||
| 117 | } | ||
| 118 | |||
| 119 | =item $bd->pre_check() | ||
| 120 | |||
| 121 | Perform build driver specific checks, before anything else. | ||
| 122 | |||
| 123 | This will run after the B<init> hook, and before C<dpkg-source --before-build>. | ||
| 124 | |||
| 125 | B<Note>: This is an optional method that can be omitted from the driver | ||
| 126 | implementation. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub pre_check { | ||
| 131 | my $self = shift; | ||
| 132 | |||
| 133 | return unless $self->{driver}->can('pre_check'); | ||
| 134 | return $self->{driver}->pre_check(); | ||
| 135 | } | ||
| 136 | |||
| 137 | =item $bool = $bd->need_build_task($build_task, binary_task) | ||
| 138 | |||
| 139 | Returns whether we need to use the build task. | ||
| 140 | |||
| 141 | B<Note>: This method is needed as long as we support building as root-like. | ||
| 142 | Once that is not needed this method will be deprecated. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub need_build_task { | ||
| 147 | my ($self, $build_task, $binary_task) = @_; | ||
| 148 | |||
| 149 | return $self->{driver}->need_build_task($build_task, $binary_task); | ||
| 150 | } | ||
| 151 | |||
| 152 | =item $bd->run_build_task($build_task, $binary_task) | ||
| 153 | |||
| 154 | Executes the build task for the build. | ||
| 155 | |||
| 156 | B<Note>: This is an optional method needed as long as we support building as | ||
| 157 | root-like. | ||
| 158 | Once that is not needed this method will be deprecated. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub run_build_task { | ||
| 163 | my ($self, $build_task, $binary_task) = @_; | ||
| 164 | |||
| 165 | $self->{driver}->run_build_task($build_task, $binary_task); | ||
| 166 | |||
| 167 | return; | ||
| 168 | } | ||
| 169 | |||
| 170 | =item $bd->run_task($task) | ||
| 171 | |||
| 172 | Executes the given task for the build. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | sub run_task { | ||
| 177 | my ($self, $task) = @_; | ||
| 178 | |||
| 179 | $self->{driver}->run_task($task); | ||
| 180 | |||
| 181 | return; | ||
| 182 | } | ||
| 183 | |||
| 184 | =back | ||
| 185 | |||
| 186 | =head1 CHANGES | ||
| 187 | |||
| 188 | =head2 Version 0.xx | ||
| 189 | |||
| 190 | This is a private module. | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | 1; |