summaryrefslogtreecommitdiffstats
path: root/refresh-package
blob: 8f6e4a9866e71b280e75f5ee258e238479ef8331 (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
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
#!/usr/bin/perl -w
undef $/;
my $DEB_HOST_MULTIARCH = (split /\n/, qx/dpkg-architecture -qDEB_HOST_MULTIARCH/)[0];
my $DEB_HOST_ARCH = (split /\n/, qx/dpkg-architecture -qDEB_HOST_ARCH/)[0];
my $DEB_HOST_ARCH_BITS = (split /\n/, qx/dpkg-architecture -qDEB_HOST_ARCH_BITS/)[0];

sub file_replace
{
    my ($f,$name,$lcname)= @_;
    my $foutname = $f;
    $foutname =~ s/[@]NAME[@]/$name/;
    $foutname =~ s/[@]LCNAME[@]/$lcname/;
    open IN, "<debian/$f.in" or die "Can't read debian/$f.in: $!\n";
    local $_=<IN>;
    close IN;

    s/[@]NAME[@]/$name/g;
    s/[@]LCNAME[@]/$lcname/g;
    s/[@]DEB_HOST_MULTIARCH[@]/$DEB_HOST_MULTIARCH/g;
    open OUT, ">debian/$foutname"
	or die "Can't write debian/$foutname: $!\n";
    print OUT;
    close OUT;
}

my $control;

my $ctemplate;
my $cfilename = 'test-@NAME@.c';
open(my $fctemplate, '<', $cfilename) or die "cannot open file $cfilename";
{
    local $/ = undef;
    $ctemplate = <$fctemplate>;
}
close($fctemplate);

# dpkg-shlibdeps now looks inside, but is ok with an empty file.
system("touch debian/control");

my @control_in;
open IN, "<debian/control.d/control.in" or die "Can't read debian/control.d/control.in: $!\n";
$_=<IN>;
@control_in = grep !/^\s*$/s, split /\n\s*\n/s;
close IN;

open ISA_LIST, "<isa-list" or die "Can't read isa-list: $!\n";
$_=<ISA_LIST>;
close ISA_LIST;
my %archlist;

my @structs;
foreach my $block (split /\n\s*\n/s)
{
    my $lastfield = undef;
    my %data = ();
    foreach (split /\n/s, $block) {
	next if /^#.*/;
	next if /^\s*$/;
	if (/^([^\s:]+?)\s*:\s*(.*?)\s*$/) {
	    $key = $1;
	    $value = $2 // '';
	    $data{"$key"} = "$value";
	    $lastfield = "$key";
	} elsif (/^(\s\S.*?)\s*$/) {
	    $data{$lastfield}  .= "\n$1";
	}

	next;
    }
    push @structs, \%data if %data;

}

for (@structs) {
    my %entry = %$_;
    $archlist{$entry{'Architecture'}} = 1;
    my $name=$entry{'Name'};
    my $lcname=lc($name);
    $name=~/^[a-zA-Z0-9\.+_]+$/ or die "Bad package/isa name: \"$name\".\n";
    my $description=$entry{'Description'};
    $description=~s/\A\s*\n+//m;
    $description=~s/\A\s+//m;

    my @c = @control_in;
    foreach (@c) {
	s/[@]NAME[@]/$name/g;
	s/[@]LCNAME[@]/$lcname/g;
	s/[@]ARCHITECTURE[@]/$entry{'Architecture'}/g;
	s/[@]DEB_HOST_MULTIARCH[@]/$DEB_HOST_MULTIARCH/g;
	s/[@]DESCRIPTION[@]/$description/g;
	$control.="\n".$_;
    }

    open C, '>', "test-$name.c";
    my $test=$entry{'Test'}//"return !__builtin_cpu_supports(\"$lcname\");";
    my $cfile = $ctemplate;
    $cfile =~ s/[@]TEST[@]/$test/g;
    print C $cfile;
    close C;

    for my $qemu (qw(qemu-good qemu-bad)) {
	if (exists $entry{$qemu}) {
	    my $finname = $qemu.'-@NAME@';
	    my $foutname = $finname;
	    $foutname =~ s/[@]NAME[@]/$name/;
	    open IN, "<$finname" or die "Can't read $finname: $!\n";
	    local $_=<IN>;
	    close IN;

	    s/[@]NAME[@]/$name/g;
	    s/[@]DEB_HOST_MULTIARCH[@]/$DEB_HOST_MULTIARCH/g;
	    s/[@]DEB_HOST_ARCH[@]/$DEB_HOST_ARCH/g;
	    s/[@]DEB_HOST_ARCH_BITS[@]/$DEB_HOST_ARCH_BITS/g;
	    s/[@]QEMU_GOOD[@]/$entry{'qemu-good'}/g;
	    s/[@]QEMU_BAD[@]/$entry{'qemu-bad'}/g;

	    open OUT, ">$foutname"
		or die "Can't write $foutname: $!\n";
	    print OUT;
	    close OUT;
	}
    }

    foreach (qw(@LCNAME@-support.preinst @LCNAME@-support.templates @LCNAME@-support.lintian-overrides @LCNAME@-support.maintscript)) {
	file_replace($_,$name,$lcname);
    }
}

my $all_architectures = join(' ',sort(keys %archlist));

my $control_base;
my $filename = "debian/control.d/control-base.in";
open(my $fh, '<', $filename) or die "cannot open file $filename";
{
    local $/ = undef;
    $control_base = <$fh>;
}
close($fh);
$control_base =~ s/[@]ALL_ARCHITECTURES[@]/$all_architectures/g;
$control=$control_base.$control;

open CONTROL, ">debian/control" or die "Can't write to debian/control: $!\n";
print CONTROL $control;
close CONTROL;