summaryrefslogtreecommitdiffstats
path: root/bin/sbuild-cross-resolver
blob: 110fd530f02ced90647dfd944b7a2555e7be2549 (plain)
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
#!/usr/bin/perl
#
# This script is in the public domain
#
# Author: Johannes Schauer Marin Rodrigues <josch@mister-muffin.de>
#
# Thin layer around /usr/lib/apt/solvers/apt which removes M-A:foreign and
# Essential:yes packages that are not arch:all and not arch:native from the
# EDSP before handing it to the apt solver. This is useful for resolving cross
# build dependencies as it makes sure that M-A:foreign packages and
# Essential:yes packages in the solution must come from the build architecture.

use strict;
use warnings;

if (! -e '/usr/lib/apt/solvers/apt') {
    printf STDOUT 'Error: ERR_NO_SOLVER\n';
    printf STDOUT 'Message: The external apt solver doesn\'t exist. You must install the apt-utils package.\n';
    exit 1;
}

my $buffer = '';
my $architecture = undef;
my $essential = 0;
my $multiarch = 'no';
my $build_arch;
sub keep {
    if ( $multiarch ne 'foreign' and !$essential ) {
        return 1;
    }
    if ( !defined $architecture ) {
        print STDOUT 'Error: ERR_NO_ARCH\n';
        print STDOUT 'Message: package without architecture\n';
        exit 1;
    }
    if ( $architecture eq 'all' or $architecture eq $build_arch ) {
        return 1;
    }
    return 0;
}
open my $fh, '|-', '/usr/lib/apt/solvers/apt';
my $first_stanza = 1;
while ( my $line = <STDIN> ) {
    $buffer .= $line;
    if ( $line eq "\n" ) {
        if ($first_stanza) {
            if (! defined $architecture) {
                print STDOUT 'ERROR: ERR_NO_ARCH';
                print STDOUT 'Message: no Architecture field in first stanza';
                exit 1;
            }
            $build_arch = $architecture;
            $first_stanza = 0;
        }
        if (keep) {
            print $fh $buffer;
        }
        $buffer       = '';
        $architecture = undef;
        $essential    = 0;
        $multiarch    = 'no';
        next;
    }
    if ( $line =~ /^Essential: yes\n$/ ) {
        $essential = 1;
    }
    if ( $line =~ /^Multi-Arch: (.*)\n$/ ) {
        $multiarch = $1;
    }
    if ( $line =~ /^Architecture: (.*)\n$/ ) {
        $architecture = $1;
    }
}
if (keep) {
    print $fh $buffer;
}
close $fh;