File Coverage

File:dpkg-buildflags.pl
Coverage:21.8%

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
3
3
3
5116
2
47
use strict;
22
3
3
3
5
1
59
use warnings;
23
24
3
3
3
342
3
32
use Dpkg ();
25
3
3
3
328
4
96
use Dpkg::Gettext;
26
3
3
3
440
3
228
use Dpkg::ErrorHandling qw(:DEFAULT report REPORT_STATUS);
27
3
3
3
363
3
43
use Dpkg::BuildEnv;
28
3
3
3
385
5
81
use Dpkg::BuildFlags;
29
3
3
3
12
2
4060
use Dpkg::Vendor qw(get_current_vendor);
30
31
3
101837
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 behavior, 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
3
4
my ($param, $action);
66
3
2
my $load_config = 1;
67
68
3
6
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
3
13
$action //= 'dump';
100
101
3
11
my $build_flags = Dpkg::BuildFlags->new();
102
103
3
6
$build_flags->load_config() if $load_config;
104
105
3
15
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 %builtins = $build_flags->get_builtins($param);
122
0
0
    my $para_shown = 0;
123
0
0
    foreach my $feature (sort keys %features) {
124
0
0
        print $para_shown++ ? "\n" : '';
125
0
0
        printf "Feature: %s\n", $feature;
126
0
0
        printf "Enabled: %s\n", $features{$feature} // $builtins{$feature} ? 'yes' : 'no';
127
0
0
        printf "Builtin: %s\n", $builtins{$feature} ? 'yes' : 'no' if exists $builtins{$feature};
128    }
129} elsif ($action =~ m/^export-(.*)$/) {
130
0
0
    my $export_type = $1;
131
0
0
    foreach my $flag ($build_flags->list()) {
132
0
0
        next unless $flag =~ /^[A-Z]/; # Skip flags starting with lowercase
133
0
0
        my $value = $build_flags->get($flag);
134
0
0
        if ($export_type eq 'sh') {
135
0
0
            $value =~ s/"/\"/g;
136
0
0
            print "export $flag=\"$value\"\n";
137        } elsif ($export_type eq 'make') {
138
0
0
            $value =~ s/\$/\$\$/g;
139
0
0
            print "export $flag := $value\n";
140        } elsif ($export_type eq 'cmdline') {
141
0
0
            print "$flag=\"$value\" ";
142        }
143    }
144} elsif ($action eq 'dump') {
145
3
7
    foreach my $flag ($build_flags->list()) {
146
60
51
        my $value = $build_flags->get($flag);
147
60
42
        print "$flag=$value\n";
148    }
149} elsif ($action eq 'query') {
150    # First print all environment variables that might have changed the
151    # results (only existing ones, might make sense to add an option to
152    # also show which ones could have set to modify it).
153
0
0
    printf "Vendor: %s\n", Dpkg::Vendor::get_current_vendor() || 'undefined';
154
0
0
    print "Environment:\n";
155
0
0
    for my $envvar (Dpkg::BuildEnv::list_accessed()) {
156
0
0
        print " $envvar=$ENV{$envvar}\n" if exists $ENV{$envvar};
157    }
158
159    # Then the resulting features:
160
0
0
    foreach my $area (sort $build_flags->get_feature_areas()) {
161
0
0
        print "\n";
162
0
0
        print "Area: $area\n";
163
0
0
        print "Features:\n";
164
0
0
        my %features = $build_flags->get_features($area);
165
0
0
        my %builtins = $build_flags->get_builtins($area);
166
0
0
        foreach my $feature (sort keys %features) {
167
0
0
            printf " %s=%s\n", $feature, $features{$feature} // $builtins{$feature} ? 'yes' : 'no';
168        }
169
0
0
        print "Builtins:\n";
170
0
0
        foreach my $feature (sort keys %builtins) {
171
0
0
            printf " %s=%s\n", $feature, $builtins{$feature} ? 'yes' : 'no';
172        }
173    }
174
175    # Then the resulting values (with their origin):
176
0
0
    foreach my $flag ($build_flags->list()) {
177
0
0
        print "\n";
178
0
0
        print "Flag: $flag\n";
179
0
0
        printf "Value: %s\n", $build_flags->get($flag);
180
0
0
        my $origin = $build_flags->get_origin($flag);
181
0
0
        if ($build_flags->is_maintainer_modified($flag)) {
182
0
0
            $origin .= '+maintainer';
183        }
184
0
0
        print "Origin: $origin\n";
185    }
186} elsif ($action eq 'status') {
187    # Prefix everything with "dpkg-buildflags: status: " to allow easy
188    # extraction from a build log. Thus we use report with a non-translated
189    # type string.
190
191    # First print all environment variables that might have changed the
192    # results (only existing ones, might make sense to add an option to
193    # also show which ones could have set to modify it).
194
0
0
    my @envvars = Dpkg::BuildEnv::list_accessed();
195
0
0
    for my $envvar (@envvars) {
196
0
0
        if (exists $ENV{$envvar}) {
197            printf report(REPORT_STATUS, 'environment variable %s=%s',
198
0
0
                   $envvar, $ENV{$envvar});
199        }
200    }
201
0
0
    my $vendor = Dpkg::Vendor::get_current_vendor() || 'undefined';
202
0
0
    print report(REPORT_STATUS, "vendor is $vendor");
203    # Then the resulting features:
204
0
0
    foreach my $area (sort $build_flags->get_feature_areas()) {
205
0
0
        my $fs;
206
0
0
        my %features = $build_flags->get_features($area);
207
0
0
        my %builtins = $build_flags->get_builtins($area);
208
0
0
        foreach my $feature (sort keys %features) {
209
0
0
            $fs .= sprintf(' %s=%s', $feature, $features{$feature} // $builtins{$feature} ? 'yes' : 'no');
210        }
211
0
0
        print report(REPORT_STATUS, "$area features:$fs");
212
0
0
        my $bs = q{};
213
0
0
        foreach my $feature (sort keys %builtins) {
214
0
0
            next if ! exists $builtins{$feature};
215
0
0
            $bs .= sprintf(' %s=%s', $feature, $builtins{$feature} ? 'yes' : 'no');
216        }
217
0
0
        print report(REPORT_STATUS, "$area builtins:$bs");
218    }
219    # Then the resulting values (with their origin):
220
0
0
    foreach my $flag ($build_flags->list()) {
221
0
0
        my $value = $build_flags->get($flag);
222
0
0
        my $origin = $build_flags->get_origin($flag);
223
0
0
        my $maintainer = $build_flags->is_maintainer_modified($flag) ? '+maintainer' : '';
224
0
0
        print report(REPORT_STATUS, "$flag [$origin$maintainer]: $value");
225    }
226}