-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpixiewood
executable file
·732 lines (620 loc) · 24.8 KB
/
pixiewood
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#!/usr/bin/perl
# Pixiewood - GTK Android Builder
# Copyright (C) 2024 Florian "sp1rit" <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
use strict;
use warnings;
use Archive::Tar;
use Cwd qw(abs_path);
use File::Basename qw(fileparse);
use File::Copy qw(copy);
use File::Find qw(find);
use File::Path qw(make_path);
use File::Spec::Functions qw(catfile rel2abs);
use Getopt::Long qw(GetOptions);
use Glib;
use Glib::Object::Introspection;
use IPC::Run qw(run);
use JSON;
use List::Util qw(max reduce);
use POSIX ":sys_wait_h";
use Set::Scalar;
use XML::LibXML;
use XML::LibXSLT;
Glib::Object::Introspection->setup(basename => 'AppStream', version => '1.0', package => 'As');
Getopt::Long::Configure("default", "require_order", "bundling");
my $version = "0";
my $pixiewood_dirname = ".pixiewood";
my (undef, $asset_dir) = fileparse(abs_path(__FILE__));
my $ninja = "ninja";
my $dir = ".";
my $verbose = 0;
my $help = 0;
my $cversion = 0;
sub usage {
my ($error) = @_;
print STDERR "Error: $error\n" if $error;
print << "EOF";
Usage: $0 [options] <action>
Global Options:
-C, --change-dir <DIR> The project source root of the project to build
-v, --verbose Enable verbose output
--help Display this help message
-V, --version Display version and debug information
Actions:
prepare [options] <manifest> Prepare the project for building with GTK Android. This includes
patching the subprojects and configuring the build directories.
--release Create a release instead of debug build
-a, --android-studio-dir <DIR> Location where Android Studio is installed
-s, --sdk <DIR> Install location of the Android SDK
-t, --toolchain <DIR> Install location of the Android NDK (typically within the SDK)
--meson <EXECUTABLE> Specify the executable for the meson buildsystem
generate Generate an Android application skeleton based on the manifest.
build [--skip-gradle] Build the project and wrap it into and Android package.
EOF
exit 1 if $error;
exit 0;
}
sub do_version {
print << "EOF";
Pixiewood (version $version)
Copyright (c) 2024 Florian "sp1rit" <sp1rit\@disroot.org>
Licensed under the GNU General Public License version 3 or later.
You should have received a copy of it along with this program.
If not, see <https://www.gnu.org/licenses/>.
This is free software: you are free to change and redistribute it.
This program comes with ABSOLUTELY NO WARRANTY, to the extent permitted by law.
EOF
exit 0;
}
GetOptions(
"change-dir|C=s" => \$dir,
"verbose|v" => \$verbose,
"help" => \$help,
"version|V" => \$cversion,
) or usage("Incorrect usage of command line options");
usage() if $help;
do_version() if $cversion;
my $action = shift @ARGV;
usage("Action not specified") unless defined $action;
my $original_dir = abs_path '.';
my $pixiewood_dir = catfile($dir, $pixiewood_dirname);
make_path($pixiewood_dir);
chdir $dir or die("Failed to enter source directory: $!");
sub path_from_uri {
my $uri = shift;
my ($dir,$path) = $uri =~ m/^(src|build):(.*)$/;
return $uri unless defined $dir and defined $path;
if ($dir eq "src") {
return catfile ".", $path;
} elsif ($dir eq "build") {
my ($arch,$path) = $path =~ m/^\/*([a-z0-9_]*)\/(.*)$/;
my $res = catfile ".", $pixiewood_dirname, "bin-$arch", $path;
do {
# Run build:// URL as target in case it is generated during build
my @cmd = ($ninja,
"-C", "$pixiewood_dirname/bin-$arch",
"$path");
run \@cmd, '>', "/dev/null", '2>&1';
} if -e catfile ".", $pixiewood_dirname, "bin-$arch", "build.ninja";
return $res;
}
return $path;
};
my $xml_protos = XML::LibXML::InputCallback->new();
$xml_protos->register_callbacks([
sub {
my $uri = shift;
return $uri =~ /^(src|build):/;
},
sub {
my $uri = shift;
my $fh;
open $fh, "<", path_from_uri($uri) or do {
my $default = '<?xml version="1.0" encoding="UTF-8"?><undef/>';
open $fh, '<', \$default or die "Failed to create handle for default metainfo document: $!";
};
return $fh;
},
sub {
my ($fh, $len) = @_;
my $buffer;
read($fh, $buffer, $len);
return $buffer;
},
sub {
my $fh = shift;
close $fh;
}
]);
my $parser = XML::LibXML->new();
$parser->input_callbacks($xml_protos);
XML::LibXSLT->register_function("https://sp1rit.arpa/pixiewood/", "vermax", sub {
reduce { As::vercmp ($a, $b, "none") > 0 ? $a : $b } map { $_->value } shift->get_nodelist
});
XML::LibXSLT->register_function("https://sp1rit.arpa/pixiewood/", "semver2code", sub {
my $semver = shift;
$semver =~ m/(\d+)(?:\.(\d+)(?:\.(\d+))?)?/ or die "Failed to match semver: $semver";
($1 << 20) + (($2 or 0) << 10) + ($3 or 0)
});
sub xpc_for_manifest {
my $appx = XML::LibXML::XPathContext->new(shift);
$appx->registerNs("pw", "https://sp1rit.arpa/pixiewood/");
$appx->registerNs("meta", "https://specifications.freedesktop.org/metainfo/1.0");
return $appx;
}
sub read_configuration {
print "Reading configuration\n" if $verbose;
my $cfg = Glib::KeyFile->new;
$cfg->load_from_file("$pixiewood_dirname/pixiewood.ini", "none");
my $app_path = $cfg->get_string("pixiewood", "manifest");
my $studio_dir = $cfg->get_string("pixiewood", "android-studio-dir");
my $sdk = $cfg->get_string("pixiewood", "android-sdk");
my $toolchain = $cfg->get_string("pixiewood", "android-ndk");
my $meson = $cfg->get_string("pixiewood", "meson-bin");
my $release = $cfg->get_boolean("pixiewood", "release");
my $app_doc = $parser->load_xml(location => $app_path, xinclude => 1);
return (xpc_for_manifest($app_doc), {
'manifest' => $app_doc,
'studio-dir' => $studio_dir,
'android-sdk' => $sdk,
'android-ndk' => $toolchain,
'meson' => $meson,
'release' => $release
});
}
sub get_target_arches {
my $appx = shift;
my $arches = Set::Scalar->new("aarch64", "x86_64");
my $architectures = $appx->findnodes("/pw:app/pw:build/pw:architectures")->shift;
if (defined $architectures) {
my @listed = map { $_->textContent } $appx->findnodes("pw:arch", $architectures);
my $listed_arches = Set::Scalar->new(@listed);
my $archtype = $architectures->getAttribute("mode");
if (not defined $archtype or $archtype eq "whitelist") {
$arches = $arches->intersection($listed_arches);
} elsif ($archtype eq "blacklist") {
$arches = $arches->difference($listed_arches);
} else {
die("Unknown architectures modes: $archtype");
}
}
die "Can't build for no architectures" if $arches->size == 0;
return $arches;
}
{
package ParallelRunner;
sub new {
my ($class) = @_;
my $self = {
pids => [],
};
bless $self, $class;
return $self;
}
sub launch {
my ($self, @cmd) = @_;
my $pid = fork;
die "fork failed: $!" if not defined $pid;
if ($pid == 0) {
#open(STDOUT, '>', '/dev/null') or die $!;
#open(STDERR, '>', '/dev/null') or die $!;
exec @cmd or die "exec failed: $!";
} else {
push @{$self->{pids}}, $pid;
}
}
sub wait {
my ($self) = @_;
while (scalar @{$self->{pids}} > 0) {
my $child = waitpid(-1, 0);
my $status = $?;
die "Unexpected child process $child died" unless (grep { $_ == $child } @{$self->{pids}});
if ($status != 0) {
foreach my $pid (@{$self->{pids}}) {
kill QUIT => $pid;
}
return 0;
}
@{$self->{pids}} = grep { !/$child/ } @{$self->{pids}};
}
1;
}
}
my %actions = (
"prepare" => sub {
my $meson = "meson";
my $studio_dir = "/opt/android-studio/";
my $sdk;
my $toolchain;
my $release = 0;
GetOptions(
"android-studio-dir|a=s" => \$studio_dir,
"sdk|s=s" => \$sdk,
"toolchain|t=s" => \$toolchain,
"meson=s" => \$meson,
"release" => \$release,
) or usage("Incorrect usage of command line options");
my $app_path = rel2abs(shift @ARGV, $original_dir);
usage("Manifest not specified") unless defined $app_path;
usage("Android SDK directory not specified") unless defined $sdk;
$meson = rel2abs($meson, $original_dir) if $meson =~ /^\.{1,2}\//;
$studio_dir = rel2abs($studio_dir, $original_dir);
$sdk = rel2abs($sdk, $original_dir);
$toolchain = rel2abs($toolchain, $original_dir) if defined $toolchain;
$toolchain = do {
opendir(my $dir, "$sdk/ndk") or die "Failed to open NDK toolchain dir: $!";
my $newest = reduce { As::vercmp ($a, $b, "none") > 0 ? $a : $b } grep { !/^\./ } readdir($dir);
closedir($dir);
defined $newest ? catfile $sdk, "ndk", $newest : undef
} unless defined $toolchain;
usage("Unable to detect Android NDK (Toolchain), try specifing it") unless defined $toolchain;
my $app_doc = $parser->load_xml(location => $app_path, xinclude => 1);
my $appx = xpc_for_manifest $app_doc;
my $arches = get_target_arches $appx;
print "Generating toolchain crossfile\n" if $verbose;
my $tcc = Glib::KeyFile->new;
$tcc->set_string("constants", "toolchain", "'$toolchain/toolchains/llvm/prebuilt/linux-x86_64/'");
open my $tccfh, ">", "$pixiewood_dirname/toolchain.cross" or die("Failed to open toolchain crossfile for writing");
print $tccfh $tcc->to_data;
close $tccfh;
print "Patching dependencies\n" if $verbose;
my @dependencies = $appx->findnodes("//pw:app/pw:dependencies/pw:*");
foreach my $dependency (@dependencies) {
make_path "subprojects";
my $depname = $dependency->localname;
my $wrap = Glib::KeyFile->new;
$wrap->set_list_separator(ord(','));
$wrap->load_from_file("$asset_dir/prepare/wraps/$depname/$depname.wrap", "keep-comments");
my $revision = $dependency->getAttribute("revision");
if (defined $revision) {
my @valid_for_revision = qw(wrap-git wrap-hg wrap-svn);
foreach my $section (@valid_for_revision) {
$wrap->set_string($section, "revision", $revision) if $wrap->has_group($section);
}
}
my @patches = map { $_->textContent } $appx->findnodes("pw:patch", $dependency);
if (scalar @patches > 0) {
foreach my $patch (@patches) {
make_path "subprojects/packagefiles/$depname/";
copy("$asset_dir/prepare/wraps/$depname/$patch.patch", "subprojects/packagefiles/$depname/") or die("Failed to copy $depname patch $patch: $!");
}
my @valid_for_diff = qw(wrap-file wrap-git wrap-hg wrap-svn);
foreach my $section (@valid_for_diff) {
$wrap->set_string($section, "diff_files", join(',', map { "$depname/$_.patch" } @patches)) if $wrap->has_group($section);
}
}
open my $wrapfh, ">", "subprojects/$depname.wrap" or die("Failed to open wrapfile $depname for writing");
print $wrapfh $wrap->to_data;
close $wrapfh;
}
print "Configuring build directories\n" if $verbose;
my $configures = ParallelRunner->new;
my @options = map { $_->textContent } $appx->findnodes('//pw:app/pw:build/pw:configure-options/pw:option');
while (defined(my $arch = $arches->each)) {
my @cmd = ($meson, "setup",
"--cross-file", "$pixiewood_dirname/toolchain.cross",
"--cross-file", "$asset_dir/prepare/arch/$arch.cross",
"--cross-file", "$asset_dir/prepare/android.cross",
"--buildtype", $release ? "release" : "debug",
$release ? "--strip" : (),
@options,
"$pixiewood_dirname/bin-$arch", ".");
$configures->launch(@cmd);
}
die "Configuration failed, see logs for more info" unless $configures->wait();
print "Writing configuration\n" if $verbose;
my $cfg = Glib::KeyFile->new;
$cfg->set_string("pixiewood", "manifest", $app_path);
$cfg->set_string("pixiewood", "android-studio-dir", $studio_dir);
$cfg->set_string("pixiewood", "android-sdk", $sdk);
$cfg->set_string("pixiewood", "android-ndk", $toolchain);
$cfg->set_string("pixiewood", "meson-bin", $meson);
$cfg->set_boolean("pixiewood", "release", $release);
open my $cfgfh, ">", "$pixiewood_dirname/pixiewood.ini" or die("Failed to open build configuration for writing");
print $cfgfh $cfg->to_data;
close $cfgfh;
},
"generate" => sub {
our ($appx, $cfg) = read_configuration;
my $arches = get_target_arches $appx;
my $id = $appx->findvalue("/pw:app/pw:metainfo/meta:component/meta:id[not(\@xml:lang)]");
die("Unable to find component id in manifest, if you are using XInclude ensure that the path is correct") unless $id;
my @id_parts = split /\./, $id;
my $package = lc(join('/', @id_parts));
my $class = $id_parts[-1] . "App";
my @targets = do {
my $arch = $arches->each;
$arches->size; # reset the iterator
my $file = "$pixiewood_dirname/bin-$arch/meson-info/intro-targets.json";
open(my $fh, "<", $file) or die("Failed to open target introspection $file: $!");
local $/;
my $json = decode_json <$fh>;
close $fh;
# TODO: filter out anything that wasn't produced by gnome.executable (not possible yet)
@$json;
};
my $target = $appx->findvalue('//pw:app/pw:build/@target');
@targets = grep { $_->{name} eq $target } @targets if $target;
# TODO: heuristic (case insensitive .name contains $name)
die "Unable to find target, have you set android_usecase?" if scalar @targets == 0;
die "Found more than one target, please set /pw:app/pw:build/\@target" if scalar @targets > 1;
my $libname = $targets[0]->{name};
our $output_dir = catfile($pixiewood_dirname, "android");
my $skeleton = Archive::Tar->new;
$skeleton->setcwd($output_dir);
$skeleton->read("$asset_dir/generate/skel.tar");
$skeleton->extract();
sub create_handle {
my ($dest) = @_;
my (undef, $dirs) = fileparse($dest);
make_path(catfile($output_dir, $dirs));
my $path = catfile($output_dir, $dest);
open (my $fh, '>', $path) or die("Unable to create/open file $path for writing: $!");
return $fh;
}
# ABIs for ndkFilter, expects abi value in crossfile to be quoted
my $abis = do {
sub abi_for_arch {
my $arch = shift;
my $cf = Glib::KeyFile->new;
$cf->load_from_file("$asset_dir/prepare/arch/$arch.cross", "none");
return $cf->get_string("constants", "abi");
}
join(',', map { abi_for_arch $_ } $arches->members)
};
my @stylesheets = (
("settings.xsl", "settings.gradle"),
("gradle.xsl", "app/build.gradle"),
("manifest.xsl", "app/src/main/AndroidManifest.xml"),
("strings.xsl", "app/src/main/res/values/strings.xml"),
("application.xsl", catfile("app/src/main/java/", $package, "$class.java"))
);
my $xslt = XML::LibXSLT->new();
for my ($stylesheet_file, $dest) (@stylesheets) {
my $style_doc = XML::LibXML->load_xml(location => catfile("$asset_dir/generate", $stylesheet_file), no_cdata => 1);
my $stylesheet = $xslt->parse_stylesheet($style_doc);
my $results = $stylesheet->transform($cfg->{'manifest'}, XML::LibXSLT::xpath_to_string(libname => $libname, abis => $abis));
my $fh = create_handle $dest;
$stylesheet->output_fh($results, $fh);
close $fh;
}
print "Generating translations\n" if $verbose;
do {
my $lang_doc = XML::LibXML->load_xml(location => catfile("$asset_dir/generate", "langs.xsl"), no_cdata => 1);
my $langsheet = $xslt->parse_stylesheet($lang_doc);
my $langres = $langsheet->transform($cfg->{'manifest'});
my $langs = $langsheet->output_as_chars($langres);
my $strings_doc = XML::LibXML->load_xml(location => catfile("$asset_dir/generate", "strings.xsl"), no_cdata => 1);
my $stringssheet = $xslt->parse_stylesheet($strings_doc);
open my $fh, '<', \$langs or die "Could not open string: $!";
while (my $lang = <$fh>) {
chomp $lang;
my $results = $stringssheet->transform($cfg->{'manifest'}, XML::LibXSLT::xpath_to_string(lang => "$lang"));
my $ofh = create_handle "app/src/main/res/values-$lang/strings.xml";
$stringssheet->output_fh($results, $ofh);
close $ofh;
}
close $fh;
};
my $styledef = $appx->findnodes("/pw:app/pw:style")->shift or die("Missing style definition");
# TODO: implement
#print "Generating style\n" if $verbose;
print "Generating application icon\n" if $verbose;
do {
my $icondef = $appx->findnodes("pw:icon", $styledef)->shift or return;
my $kind = $icondef->getAttribute("type") or die("Unspecified icon type");
if ($kind eq "generate") {
sub handle_drawable {
my ($drawable, $dest) = @_;
my $type = $drawable->getAttribute("type") or die("Missing type definition from drawable");
my $output = create_handle($dest);
if ($type eq "avd") {
my $path = path_from_uri $drawable->getAttribute("path") or die("Drawable is missing path");
open (my $fh, '<', $path);
while (my $line = <$fh>) {
print $output $line;
}
close $fh;
} elsif ($type eq "svg") {
$ENV{'STUDIO_DIR'} = $cfg->{'studio-dir'};
my $path = path_from_uri $drawable->getAttribute("path") or die("Drawable is missing path");
my @cmd = (catfile("$asset_dir/generate", "Svg2Avd"), $path);
my $scale = $drawable->getAttribute("scale");
if (defined $scale) {
run \@cmd, '>', \my $converted or die('Svg2Avd failed');
my $doc = XML::LibXML->load_xml(string => $converted, no_blanks => 1);
my $vector = $doc->documentElement();
my $transform = $doc->createElement("group");
my $width = $vector->getAttribute("android:viewportWidth");
my $height = $vector->getAttribute("android:viewportHeight");
my $xoff = ($width *(1-$scale)) / 2.0;
my $yoff = ($height*(1-$scale)) / 2.0;
$transform->setAttribute("android:scaleX", $scale+0);
$transform->setAttribute("android:scaleY", $scale+0);
$transform->setAttribute("android:translateX", $xoff);
$transform->setAttribute("android:translateY", $yoff);
foreach my $node ($vector->childNodes) {
$transform->appendChild($node);
}
$vector->appendChild($transform);
$doc->toFH($output, 1);
} else {
run \@cmd, '>', $output or die('Svg2Avd failed');
}
} else {
die("Unsupported drawable type: $type");
}
close $output;
}
sub handle_color {
my ($color, $dest, $name) = @_;
my $doc = XML::LibXML->createDocument("1.0", "utf-8");
my $resources = $doc->createElement("resources");
my $entry = $doc->createElement("color");
$entry->setAttribute("name", $name);
my $color_entry = $doc->createTextNode($color->textContent);
$entry->addChild($color_entry);
$resources->addChild($entry);
$doc->setDocumentElement($resources);
my $output = create_handle($dest);
$doc->toFH($output, 1);
close $output;
}
my $fg = $appx->findnodes("pw:*[\@target='foreground']", $icondef)->shift or die("Missing foreground from icon");
my $bg = $appx->findnodes("pw:*[\@target='background']", $icondef)->shift or die("Missing background from icon");
my $mono = $appx->findnodes("pw:*[\@target='monochrome']", $icondef)->shift;
if ($fg->localname eq "drawable") {
handle_drawable($fg, "app/src/main/res/drawable/ic_launcher_foreground.xml");
} else {
die("Unsupported node for foreground: " . $fg->nodeName);
}
if ($bg->localname eq "drawable") {
handle_drawable($bg, "app/src/main/res/drawable/ic_launcher_background.xml");
} elsif ($bg->localname eq "color") {
handle_color($bg, "app/src/main/res/values/ic_launcher_background.xml", "ic_launcher_background");
} else {
die("Unsupported node for foreground: " . $fg->nodeName);
}
if (defined $mono) {
if ($mono->localname eq "drawable") {
handle_drawable($mono, "app/src/main/res/drawable/ic_launcher_monochrome.xml");
} else {
die("Unsupported node for monochrome: " . $mono->nodeName);
}
}
my $icondoc = XML::LibXML->createDocument("1.0", "utf-8");
my $icon = $icondoc->createElement("adaptive-icon");
$icon->setNamespace("http://schemas.android.com/apk/res/android", "android", 0);
my $background = $icondoc->createElement("background");
$background->setAttribute("android:drawable", "\@" . $bg->localname . "/ic_launcher_background");
my $foreground = $icondoc->createElement("foreground");
$foreground->setAttribute("android:drawable", "\@" . $fg->localname . "/ic_launcher_foreground");
$icon->addChild($background);
$icon->addChild($foreground);
if (defined $mono) {
my $monochrome = $icondoc->createElement("monochrome");
$monochrome->setAttribute("android:drawable", "\@" . $fg->localname . "/ic_launcher_monochrome");
$icon->addChild($monochrome);
}
$icondoc->setDocumentElement($icon);
my $output = create_handle("app/src/main/res/mipmap-anydpi/ic_launcher.xml");
$icondoc->toFH($output, 1);
close $output;
my $output_round = create_handle("app/src/main/res/mipmap-anydpi/ic_launcher_round.xml");
$icondoc->toFH($output_round, 1);
close $output_round;
} else {
die("Unknown icon type: ", $kind);
}
};
sub force_symlink {
my ($target, $new) = @_;
return 1 if symlink($target, $new);
if ($!{EEXIST}) {
unlink($new) or return undef;
symlink($target, $new) or return undef;
return 1;
}
return undef;
}
# A small hack to let pixiewood build applications within the gtk codebase
my $java_sources = $appx->findvalue('//pw:app/pw:build/@java-sources');
$java_sources = "subprojects/gtk/gdk/android/glue/java/" unless $java_sources;
force_symlink rel2abs(catfile($java_sources, "org/gtk/android")), catfile($output_dir, "app/src/main/java/org/gtk/android") or die "Failed to symlink java sources: $!";
force_symlink "../../../../root/lib", catfile($output_dir, "app/src/main/jniLibs") or die "Failed to symlink libraries: $!";
},
"build" => sub {
my $skip_gradle = 0;
GetOptions(
"skip-gradle" => \$skip_gradle,
) or usage("Incorrect usage of command line options");
my ($appx, $cfg) = read_configuration;
my $arches = get_target_arches $appx;
my $num = $arches->size;
my $parallel = `nproc`;
$parallel = 1 if $? != 0;
$parallel = max ($parallel / $num, 1);
print "Building $num instances a $parallel threads" if $verbose;
my $builds = ParallelRunner->new;
while (defined(my $arch = $arches->each)) {
my @cmd = ($ninja, "-j", $parallel,
"-C", "$pixiewood_dirname/bin-$arch");
$builds->launch(@cmd);
}
die "Ninja build failed, see logs for more info" unless $builds->wait();
# Lets not run the install in parallel
while (defined(my $arch = $arches->each)) {
my @cmd = ($cfg->{'meson'}, "install",
"-C", "$pixiewood_dirname/bin-$arch",
"--destdir", rel2abs("$pixiewood_dirname/root"),
"--tags", "runtime");
run \@cmd or die('Failed to install files');
}
our $asset_install_dir = "$pixiewood_dirname/android/app/src/main/assets/";
our $root = "$pixiewood_dirname/root";
our @filtered = ("bin", "lib", "share/glib-2.0/schemas");
sub pre_asset_copy {
my $pattern = join('|', map { quotemeta($_) } @filtered);
grep { catfile($File::Find::dir, $_) !~ /($pattern)$/ } @_
}
sub do_asset_copy {
my $path = $File::Find::name;
$path =~ s/^\Q$root\E//;
$path = catfile $asset_install_dir, $path;
if (-d $_) {
make_path $path;
} elsif (-f $_) {
copy($_, $path) or die "Failed to copy file from $_ to $path: $!";
}
}
find { preprocess => \&pre_asset_copy, wanted => \&do_asset_copy, no_chdir => 1 }, $root;
do {
my $root_schemas = catfile $pixiewood_dirname, "root", "share/glib-2.0/schemas";
if (-d $root_schemas) {
my $schemadir = catfile $asset_install_dir, "share/glib-2.0/schemas";
make_path $schemadir;
my @cmd = ("glib-compile-schemas", # this is best run on a system with the same endianess as the target
"--targetdir", $schemadir,
$root_schemas);
run \@cmd or die("Failed to compile schemas");
}
};
do {
open my $fh, '>', catfile($asset_install_dir, "afpr") or die "Failed to create asset fingerprint file: $!";
binmode $fh;
print $fh map { pack("C", rand(256)) } 1..128; # file fingerprint with 128 random bytes
close $fh;
};
unless ($skip_gradle) {
my $gradle = fork;
die "fork failed: $!" unless defined $gradle;
if ($gradle == 0) {
$ENV{ANDROID_HOME} = $cfg->{'android-sdk'};
chdir "$pixiewood_dirname/android" or die("Failed to enter android directory: $!");
exec ("./gradlew", "--no-daemon",
$cfg->{'release'} ? "assembleRelease" : "assembleDebug"
) or die "exec failed: $!";
}
waitpid($gradle, 0);
die "Gradle build failed" unless $? == 0;
}
}
);
if (exists $actions{$action}) {
$actions{$action}->();
} else {
usage("Invalid action specified: $action");
}