File Coverage

blib/lib/App/gimpgitbuild/API/Worker.pm
Criterion Covered Total %
statement 20 139 14.3
branch 0 32 0.0
condition 0 22 0.0
subroutine 7 27 25.9
pod n/a
total 27 220 12.2


line stmt bran cond sub pod time code
1             package App::gimpgitbuild::API::Worker;
2             $App::gimpgitbuild::API::Worker::VERSION = '0.32.1';
3 3     3   246862 use strict;
  3         6  
  3         110  
4 3     3   14 use warnings;
  3         6  
  3         167  
5 3     3   51 use 5.014;
  3         35  
6              
7 3     3   1128 use Moo;
  3         17208  
  3         14  
8              
9 3     3   6038 use Path::Tiny qw/ cwd path /;
  3         37735  
  3         266  
10 3     3   1878 use Git::Sync::App ();
  3         49866  
  3         84  
11 3     3   934 use App::gimpgitbuild::API::GitBuild ();
  3         8  
  3         5764  
12              
13             has '_api_obj' => (
14             is => 'rw',
15             default => sub { return App::gimpgitbuild::API::GitBuild->new(); }
16             );
17             has '_mode' => ( is => 'ro', required => 1, );
18             has '_override_mode' => ( is => 'rw', default => "", );
19             has '_process_executor' => ( is => 'ro', required => 1, );
20              
21             sub _do_system
22             {
23 0     0     my ($args) = @_;
24              
25 0           my $cmd = $args->{cmd};
26 0           print "Running [@$cmd]\n";
27 0 0         if ( system(@$cmd) )
28             {
29 0           die "Running [@$cmd] failed!";
30             }
31             }
32              
33             my $PAR_JOBS = ( $ENV{GIMPGITBUILD__PAR_JOBS_FLAGS} // '-j4' );
34             my $skip_builds_re = $ENV{"GIMPGITBUILD__SKIP_BUILDS_RE"} // "^(*FAIL)";
35             $skip_builds_re = qr/$skip_builds_re/;
36             my $forcify_tests_re = $ENV{"GIMPGITBUILD__FORCE_TESTS_RE"} // "^(*FAIL)";
37             $forcify_tests_re = qr/$forcify_tests_re/;
38              
39             my $BUILD_DIR = ( $ENV{GIMPGITBUILD__MESON_BUILD_DIR}
40             // "to-del--gimpgitbuild--build-dir" );
41              
42             # See:
43             # https://github.com/libfuse/libfuse/issues/212
44             # Ubuntu/etc. places it under $prefix/lib/$arch by default.
45             my $UBUNTU_MESON_LIBDIR_OVERRIDE = "-D libdir=lib";
46              
47             sub _wrap_check
48             {
49 0     0     my ( $self, $id, $cmd ) = @_;
50             my $ret = $cmd
51             . (
52             (
53             length( $ENV{SKIP_CHECK} )
54 0 0 0       or ( $id =~ $forcify_tests_re )
55             ) ? " || true" : ''
56             );
57 0           return ($ret);
58             }
59              
60             sub _check
61             {
62 0     0     my ( $self, $id ) = @_;
63 0           return $self->_wrap_check( $id, "make check" );
64             }
65              
66             sub _ninja_check
67             {
68 0     0     my ( $self, $id ) = @_;
69 0           return $self->_wrap_check( $id, "ninja test" );
70             }
71              
72             sub _git_sync
73             {
74 0     0     my ( $self, $args ) = @_;
75             return
76 0           qq#$^X -MGit::Sync::App -e "Git::Sync::App->new->run" -- sync origin "$args->{branch}"#;
77             }
78              
79             sub _git_build
80             {
81 0     0     my $self = shift;
82 0           my $args = shift;
83 0           my $orig_cwd = cwd()->absolute();
84 0           my $id = $args->{id};
85 0   0       my $extra_configure_args = ( $args->{extra_configure_args} // [] );
86 0   0       my $extra_meson_args = ( $args->{extra_meson_args} // [] );
87 0           my $SHELL_PREFIX = "set -e -x";
88 0   0       my $install_before_test = ( $args->{install_before_test} // '' );
89              
90 0 0         if ( $id =~ $skip_builds_re )
91             {
92 0           return;
93             }
94 0   0       $args->{branch} //= 'master';
95 0   0       $args->{tag} //= 'false';
96              
97             my $git_co = (
98             $args->{git_co} // (
99             $self->_api_obj()->base_git_clones_dir() . "/"
100             . $args->{git_checkout_subdir}
101             )
102 0   0       );
103 0 0         if ( !-e $git_co )
104             {
105 0           path($git_co)->parent->mkpath;
106 0           _do_system( { cmd => [qq#git clone "$args->{url}" "$git_co"#] } );
107             }
108              
109             my $_autodie_chdir = sub {
110 0     0     my $dirname = shift;
111 0 0         if ( not chdir($dirname) )
112             {
113 0           die qq#Failed changing directory to "$dirname"!#;
114             }
115 0           return;
116 0           };
117             my $shell_cmd = sub {
118 0     0     return shift;
119 0           };
120             my $chdir_cmd = sub {
121 0     0     return $shell_cmd->( qq#cd "# . shift(@_) . qq#"# );
122 0           };
123 0           my $clean_install = ( $self->_override_mode() eq "clean_install" );
124 0   0       my $PERL_EXECUTE =
125             ( $clean_install or $self->_process_executor() eq 'perl' );
126 0 0         if ($PERL_EXECUTE)
127             {
128             $shell_cmd = sub {
129 0     0     my $cmd = shift;
130             return sub {
131 0           return _do_system(
132             {
133             cmd => ["$SHELL_PREFIX ; $cmd"],
134             }
135             );
136 0           };
137 0           };
138             $chdir_cmd = sub {
139 0     0     my $dirname = shift;
140             return sub {
141 0           return $_autodie_chdir->($dirname);
142 0           };
143 0           };
144             }
145              
146 0           my $prefix = $args->{prefix};
147              
148 0 0         if ($clean_install)
149             {
150 0           $shell_cmd->(qq#rm -fr "$prefix"#)->();
151 0           return;
152             }
153              
154             my $test_install_order = sub {
155 0     0     my ( $test_cmd, $install_cmd ) = @_;
156 0 0         return $install_before_test
157             ? [ @$install_cmd, @$test_cmd ]
158             : [ @$test_cmd, @$install_cmd ];
159 0           };
160              
161             my $gen_meson_build_cmds = sub {
162             return [
163             $shell_cmd->(qq#mkdir -p "$BUILD_DIR"#),
164             $chdir_cmd->($BUILD_DIR),
165             $shell_cmd->(
166 0           qq#meson setup --prefix="$prefix" $UBUNTU_MESON_LIBDIR_OVERRIDE @{$extra_meson_args} ..#
167             ),
168             $shell_cmd->(qq#ninja $PAR_JOBS#),
169             @{
170 0     0     $test_install_order->(
  0            
171 0           [ $shell_cmd->(qq#@{[$self->_ninja_check($id)]}#), ],
172             [ $shell_cmd->(qq#ninja $PAR_JOBS install#), ]
173             )
174             },
175             ];
176 0           };
177             my $gen_autoconf_build_cmds = sub {
178             return [
179             $shell_cmd->(qq#NOCONFIGURE=1 ./autogen.sh#),
180             $shell_cmd->(qq#mkdir -p "$BUILD_DIR"#),
181             $chdir_cmd->($BUILD_DIR),
182             $shell_cmd->(
183 0           qq#../configure @{$extra_configure_args} --prefix="$prefix"#),
184             $shell_cmd->(qq#make $PAR_JOBS#),
185             @{
186 0     0     $test_install_order->(
  0            
187 0           [ $shell_cmd->(qq#@{[$self->_check($id)]}#), ],
188             [ $shell_cmd->(qq#make install#), ]
189             )
190             },
191             ];
192 0           };
193             my $gen_clean_mode_cmds =
194 0     0     sub { return [ $shell_cmd->(qq#git clean -dxf .#), ]; };
  0            
195             my $commands_gen = (
196             ( $self->_mode() eq 'clean' ) ? $gen_clean_mode_cmds
197             : (
198 0 0         $args->{use_meson} ? $gen_meson_build_cmds
    0          
199             : $gen_autoconf_build_cmds
200             )
201             );
202 0           my $sync_cmd = $self->_git_sync( { branch => $args->{branch}, } );
203             my @commands = (
204             $chdir_cmd->($git_co),
205             $shell_cmd->(qq#git checkout "$args->{branch}"#),
206             $shell_cmd->(qq#( $args->{tag} || $sync_cmd )#),
207 0           @{ $commands_gen->() },
  0            
208             );
209              
210             my $run = sub {
211 0 0   0     if ($PERL_EXECUTE)
212             {
213 0           foreach my $cb (@commands)
214             {
215 0           $cb->();
216             }
217 0           return;
218             }
219 0           my $aggregate_shell_command =
220             "$SHELL_PREFIX ; " . join( " ; ", @commands );
221 0           return _do_system(
222             {
223             cmd => [ $aggregate_shell_command, ]
224             }
225             );
226 0           };
227              
228 0           my $on_failure = $args->{on_failure};
229              
230 0 0         if ( !$on_failure )
231             {
232 0           $run->();
233             }
234             else
235             {
236 0           eval { $run->(); };
  0            
237 0           my $Err = $@;
238              
239 0 0         if ($Err)
240             {
241 0           $on_failure->( { exception => $Err, }, );
242             }
243             }
244 0           $_autodie_chdir->($orig_cwd);
245 0           return;
246             }
247              
248             sub _get_gnome_git_url
249             {
250 0     0     my ( $self, $proj ) = @_;
251 0           my $GNOME_GIT = 'https://gitlab.gnome.org/GNOME';
252              
253 0           return "${GNOME_GIT}/${proj}.git/";
254             }
255              
256             sub _run_all
257             {
258 0     0     my ($worker) = @_;
259 0           my $obj = $worker->_api_obj();
260 0           $worker->_git_build(
261             {
262             id => "babl",
263             git_checkout_subdir => "babl/git/babl",
264             url => $worker->_get_gnome_git_url("babl"),
265             prefix => $obj->babl_p,
266             use_meson => 1,
267             }
268             );
269 0           $worker->_git_build(
270             {
271             id => "gegl",
272             git_checkout_subdir => "gegl/git/gegl",
273             extra_meson_args => [ qw# -Dlua=disabled #, ],
274             url => $worker->_get_gnome_git_url("gegl"),
275             prefix => $obj->gegl_p,
276             use_meson => 1,
277             }
278             );
279              
280             # Override python3_girdir in gexiv2 in order to avoid having
281             # to run polkit's pkexec to install files as root/superuser.
282 0           my @gexiv2_girdir_override =
283             ( "-Dpython3_girdir=" . ( $obj->gexiv2_p() . "/lib/python3" ), );
284 0           $worker->_git_build(
285             {
286             id => "gexiv2",
287             git_checkout_subdir => "gexiv2/git/gexiv2",
288              
289             # extra_meson_args => [ qw# -Dlua=disabled #, ],
290             # extra_meson_args => [ @gexiv2_girdir_override, ],
291             extra_meson_args => [],
292             url => $worker->_get_gnome_git_url("gexiv2"),
293             prefix => $obj->gexiv2_p,
294             use_meson => 1,
295             }
296             );
297 0           $worker->_git_build(
298             {
299             id => "libmypaint",
300             git_checkout_subdir => "libmypaint/git/libmypaint",
301             url => "https://github.com/mypaint/libmypaint.git",
302             prefix => $obj->mypaint_p,
303             use_meson => 0,
304             branch => "v1.6.1",
305             tag => "true",
306             }
307             );
308 0           $worker->_git_build(
309             {
310             id => "mypaint-brushes",
311             git_checkout_subdir => "libmypaint/git/mypaint-brushes",
312             url => "https://github.com/Jehan/mypaint-brushes.git",
313             prefix => $obj->mypaint_p,
314             use_meson => 0,
315             branch => "v1.3.x",
316             }
317             );
318              
319 0           my $KEY = 'GIMPGITBUILD__BUILD_GIMP_USING_MESON';
320 0 0         my $BUILD_GIMP_USING_MESON = ( exists( $ENV{$KEY} ) ? $ENV{$KEY} : 1 );
321              
322             $worker->_git_build(
323             {
324             id => "gimp",
325             extra_configure_args => [ qw# --enable-debug --with-lua=no #, ],
326             extra_meson_args => [ qw# #, ],
327             git_checkout_subdir => "git/gimp",
328             install_before_test => 1,
329             url => $worker->_get_gnome_git_url("gimp"),
330             prefix => $obj->gimp_p,
331             use_meson => $BUILD_GIMP_USING_MESON,
332             on_failure => sub {
333 0     0     my ($args) = @_;
334 0           my $Err = $args->{exception};
335 0 0 0       if ( ( $worker->_mode() eq 'clean' )
336             or ( !$BUILD_GIMP_USING_MESON ) )
337             {
338 0           die $Err;
339             }
340 0           STDERR->print( $Err, "\n" );
341 0           STDERR->print(<<"EOF");
342             Meson-using builds of GIMP are known to be error prone. Please try setting
343             the "$KEY" environment variable to "0", and run gimpgitbuild again, e.g using:
344              
345             export $KEY="0"
346              
347             EOF
348 0           die "Meson build failure";
349             },
350             }
351 0           );
352 0           return;
353             }
354              
355             sub _run_the_mode_on_all_repositories
356             {
357 0     0     my ($worker) = @_;
358              
359 0 0         if ( $worker->_mode() eq 'build' )
360             {
361 0           $worker->_override_mode("clean_install");
362 0           $worker->_run_all();
363             }
364 0           $worker->_override_mode("");
365 0           $worker->_run_all();
366              
367 0           return;
368             }
369              
370             1;
371              
372             __END__