Debian Codegraph @

cmake/debcontrol2cmake.pl

raw
1 #!/usr/bin/perl
2
3 # Copyright (C) 2011 Modestas Vainius <[email protected]>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>
17
18 use strict;
19 use warnings;
20
21 use Dpkg::Control::Info;
22 use Dpkg::Control;
23 use Dpkg::Arch;
24
25 sub samearch {
26 my $arch = shift;
27 my @archlist = split(/\s+/, shift);
28
29 foreach my $a (@archlist) {
30 if (system("dpkg-architecture", "-a$arch", "-i$a") == 0) {
31 return 1;
32 }
33 }
34
35 return 0;
36 }
37
38 # Parse command line arguments
39 my @fields;
40 my $prefix = "debcontrol_";
41 for (my $i = 0; $i < @ARGV; $i++) {
42 if ($ARGV[$i] eq "-F") {
43 push @fields, $ARGV[++$i];
44 } elsif ($ARGV[$i] =~ /^-F(.+)$/) {
45 push @fields, $1;
46 } elsif ($ARGV[$i] eq "-s") {
47 $prefix = $ARGV[++$i];
48 } elsif ($ARGV[$i] =~ /^-s(.+)$/) {
49 $prefix = $1;
50 }
51 }
52
53 my $arch = Dpkg::Arch::get_build_arch();
54
55 # Retrieve requested fields and generate set statements
56 my $control = Dpkg::Control::Info->new("debian/control");
57 foreach my $pkg ($control->{source}, @{$control->{packages}}) {
58 my $pkgok;
59 my $pkgname = ($pkg->get_type() == CTRL_INFO_SRC) ? "Source" : $pkg->{Package};
60 next if $pkg->get_type() == CTRL_INFO_PKG && !samearch($arch, $pkg->{"Architecture"});
61 foreach my $field (@fields) {
62 my $val;
63 if (exists $pkg->{$field}) {
64 $val = $pkg->{$field};
65 } elsif (my $f = $pkg->find_custom_field($field)) {
66 $val = $pkg->{$f};
67 }
68 if (defined $val) {
69 $pkgok = 1;
70 printf "set(%s%s_%s \"%s\")\n", $prefix, $pkgname, $field, $val;
71 }
72 }
73 if ($pkgok) {
74 printf "list(APPEND %spackages \"%s\")\n", $prefix, $pkgname;
75 }
76 }