File Coverage

File:dpkg-buildflags.pl
Coverage:25.1%

linestmtbrancondsubpodtimecode
1#!/usr/bin/perl
2#
3# dpkg-buildflags
4#
5# Copyright © 2010-2011 Raphaël Hertzog <hertzog@debian.org>
6# Copyright © 2012-2013 Guillem Jover <guillem@debian.org>
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
21
1
1
1
1680
1
14
use strict;
22
1
1
1
2
0
16
use warnings;
23
24
1
1
1
108
0
10
use Dpkg ();
25
1
1
1
110
1
28
use Dpkg::Gettext;
26
1
1
1
117
0
61
use Dpkg::ErrorHandling qw(:DEFAULT report REPORT_STATUS);
27
1
1
1
111
1
12
use Dpkg::Build::Env;
28
1
1
1
122
1
15
use Dpkg::BuildFlags;
29
1
1
1
2
1
1027
use Dpkg::Vendor qw(get_current_vendor);
30
31
1
26905
textdomain('dpkg-dev');
32
33sub version {
34
0
    printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
35
36
0
    printf g_('
37This is free software; see the GNU General Public License version 2 or
38later for copying conditions. There is NO warranty.
39');
40}
41
42sub usage {
43
0
    printf g_(
44'Usage: %s [<command>]')
45    . "\n\n" . g_(
46'Commands:
47  --get <flag>       output the requested flag to stdout.
48  --origin <flag>    output the origin of the flag to stdout:
49                     value is one of vendor, system, user, env.
50  --status           output a synopsis with all parameters affecting the
51                     program behaviour, the resulting flags and their origin.
52  --query            like --status, but in deb822 format.
53  --query-features <area>
54                     output the status of features for the given area.
55  --list             output a list of the flags supported by the current vendor.
56  --export=(sh|make|cmdline|configure)
57                     output something convenient to import the compilation
58                     flags in a shell script, in make, or in a command line.
59  --dump             output all compilation flags with their values.
60  --help             show this help message.
61  --version          show the version.
62'), $Dpkg::PROGNAME;
63}
64
65
1
1
my ($param, $action);
66
1
1
my $load_config = 1;
67
68
1
2
while (@ARGV) {
69
0
0
    $_ = shift(@ARGV);
70
0
0
    if (m/^--(get|origin|query-features)$/) {
71
0
0
        usageerr(g_('two commands specified: --%s and --%s'), $1, $action)
72            if defined($action);
73
0
0
        $action = $1;
74
0
0
        $param = shift(@ARGV);
75
0
0
        usageerr(g_('%s needs a parameter'), $_) unless defined $param;
76    } elsif (m/^--export(?:=(sh|make|cmdline|configure))?$/) {
77
0
0
        usageerr(g_('two commands specified: --%s and --%s'), 'export', $action)
78            if defined($action);
79
0
0
        my $type = $1 || 'sh';
80        # Map legacy aliases.
81
0
0
        $type = 'cmdline' if $type eq 'configure';
82
0
0
        $action = "export-$type";
83    } elsif (m/^--(list|status|dump|query)$/) {
84
0
0
        usageerr(g_('two commands specified: --%s and --%s'), $1, $action)
85            if defined($action);
86
0
0
        $action = $1;
87
0
0
        $load_config = 0 if $action eq 'list';
88    } elsif (m/^-(?:\?|-help)$/) {
89
0
0
        usage();
90
0
0
        exit 0;
91    } elsif (m/^--version$/) {
92
0
0
        version();
93
0
0
        exit 0;
94    } else {
95
0
0
        usageerr(g_("unknown option '%s'"), $_);
96    }
97}
98
99
1
3
$action //= 'dump';
100
101
1
3
my $build_flags = Dpkg::BuildFlags->new();
102
103
1
2
$build_flags->load_config() if $load_config;
104
105
1
4
if ($action eq 'list') {
106
0
0
    foreach my $flag ($build_flags->list()) {
107
0
0
        print "$flag\n";
108    }
109} elsif ($action eq 'get') {
110
0
0
    exit 1 unless $build_flags->has($param);
111
112
0
0
    print $build_flags->get($param) . "\n";
113} elsif ($action eq 'origin') {
114
0
0
    exit 1 unless $build_flags->has($param);
115
116
0
0
    print $build_flags->get_origin($param) . "\n";
117} elsif ($action eq 'query-features') {
118
0
0
    exit 1 unless $build_flags->has_features($param);
119
120
0
0
    my %features = $build_flags->get_features($param);
121
0
0
    my $para_shown = 0;
122
0
0
    foreach my $feature (sort keys %features) {
123
0
0
        print $para_shown++ ? "\n" : '';
124
0
0
        printf "Feature: %s\n", $feature;
125
0
0
        printf "Enabled: %s\n", $features{$feature} ? 'yes' : 'no';
126    }
127} elsif ($action =~ m/^export-(.*)$/) {
128
0
0
    my $export_type = $1;
129
0
0
    foreach my $flag ($build_flags->list()) {
130
0
0
        next unless $flag =~ /^[A-Z]/; # Skip flags starting with lowercase
131
0
0
        my $value = $build_flags->get($flag);
132
0
0
        if ($export_type eq 'sh') {
133
0
0
            $value =~ s/"/\"/g;
134
0
0
            print "export $flag=\"$value\"\n";
135        } elsif ($export_type eq 'make') {
136
0
0
            $value =~ s/\$/\$\$/g;
137
0
0
            print "export $flag := $value\n";
138        } elsif ($export_type eq 'cmdline') {
139
0
0
            print "$flag=\"$value\" ";
140        }
141    }
142} elsif ($action eq 'dump') {
143
1
2
    foreach my $flag ($build_flags->list()) {
144
11
7
        my $value = $build_flags->get($flag);
145
11
11
        print "$flag=$value\n";
146    }
147} elsif ($action eq 'query') {
148    # First print all environment variables that might have changed the
149    # results (only existing ones, might make sense to add an option to
150    # also show which ones could have set to modify it).
151
0
0
    printf "Vendor: %s\n", Dpkg::Vendor::get_current_vendor() || 'undefined';
152
0
0
    print "Environment:\n";
153
0
0
    for my $envvar (Dpkg::Build::Env::list_accessed()) {
154
0
0
        print " $envvar=$ENV{$envvar}\n" if exists $ENV{$envvar};
155    }
156
157    # Then the resulting features:
158
0
0
    foreach my $area (sort $build_flags->get_feature_areas()) {
159
0
0
        print "\n";
160
0
0
        print "Area: $area\n";
161
0
0
        print "Features:\n";
162
0
0
        my %features = $build_flags->get_features($area);
163
0
0
        foreach my $feature (sort keys %features) {
164
0
0
            printf " %s=%s\n", $feature, $features{$feature} ? 'yes' : 'no';
165        }
166    }
167
168    # Then the resulting values (with their origin):
169
0
0
    foreach my $flag ($build_flags->list()) {
170
0
0
        print "\n";
171
0
0
        print "Flag: $flag\n";
172
0
0
        printf "Value: %s\n", $build_flags->get($flag);
173
0
0
        my $origin = $build_flags->get_origin($flag);
174
0
0
        if ($build_flags->is_maintainer_modified($flag)) {
175
0
0
            $origin .= '+maintainer';
176        }
177
0
0
        print "Origin: $origin\n";
178    }
179} elsif ($action eq 'status') {
180    # Prefix everything with "dpkg-buildflags: status: " to allow easy
181    # extraction from a build log. Thus we use report with a non-translated
182    # type string.
183
184    # First print all environment variables that might have changed the
185    # results (only existing ones, might make sense to add an option to
186    # also show which ones could have set to modify it).
187
0
0
    my @envvars = Dpkg::Build::Env::list_accessed();
188
0
0
    for my $envvar (@envvars) {
189
0
0
        if (exists $ENV{$envvar}) {
190            printf report(REPORT_STATUS, 'environment variable %s=%s',
191
0
0
                   $envvar, $ENV{$envvar});
192        }
193    }
194
0
0
    my $vendor = Dpkg::Vendor::get_current_vendor() || 'undefined';
195
0
0
    print report(REPORT_STATUS, "vendor is $vendor");
196    # Then the resulting features:
197
0
0
    foreach my $area (sort $build_flags->get_feature_areas()) {
198
0
0
        my $fs;
199
0
0
        my %features = $build_flags->get_features($area);
200
0
0
        foreach my $feature (sort keys %features) {
201
0
0
            $fs .= sprintf(' %s=%s', $feature, $features{$feature} ? 'yes' : 'no');
202        }
203
0
0
        print report(REPORT_STATUS, "$area features:$fs");
204    }
205    # Then the resulting values (with their origin):
206
0
0
    foreach my $flag ($build_flags->list()) {
207
0
0
        my $value = $build_flags->get($flag);
208
0
0
        my $origin = $build_flags->get_origin($flag);
209
0
0
        my $maintainer = $build_flags->is_maintainer_modified($flag) ? '+maintainer' : '';
210
0
0
        print report(REPORT_STATUS, "$flag [$origin$maintainer]: $value");
211    }
212}