chiark / gitweb /
dpkg (1.18.25) stretch; urgency=medium
[dpkg] / scripts / dpkg-buildflags.pl
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 use strict;
22 use warnings;
23
24 use Dpkg ();
25 use Dpkg::Gettext;
26 use Dpkg::ErrorHandling qw(:DEFAULT report REPORT_STATUS);
27 use Dpkg::Build::Env;
28 use Dpkg::BuildFlags;
29 use Dpkg::Vendor qw(get_current_vendor);
30
31 textdomain('dpkg-dev');
32
33 sub version {
34     printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
35
36     printf g_('
37 This is free software; see the GNU General Public License version 2 or
38 later for copying conditions. There is NO warranty.
39 ');
40 }
41
42 sub usage {
43     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   --query-features <area>
51                      output the status of features for the given area.
52   --list             output a list of the flags supported by the current vendor.
53   --export=(sh|make|cmdline|configure)
54                      output something convenient to import the compilation
55                      flags in a shell script, in make, or in a command line.
56   --dump             output all compilation flags with their values
57   --status           print a synopsis with all parameters affecting the
58                      behaviour of dpkg-buildflags and the resulting flags
59                      and their origin.
60   --help             show this help message.
61   --version          show the version.
62 '), $Dpkg::PROGNAME;
63 }
64
65 my ($param, $action);
66 my $load_config = 1;
67
68 while (@ARGV) {
69     $_ = shift(@ARGV);
70     if (m/^--(get|origin|query-features)$/) {
71         usageerr(g_('two commands specified: --%s and --%s'), $1, $action)
72             if defined($action);
73         $action = $1;
74         $param = shift(@ARGV);
75         usageerr(g_('%s needs a parameter'), $_) unless defined $param;
76     } elsif (m/^--export(?:=(sh|make|cmdline|configure))?$/) {
77         usageerr(g_('two commands specified: --%s and --%s'), 'export', $action)
78             if defined($action);
79         my $type = $1 || 'sh';
80         # Map legacy aliases.
81         $type = 'cmdline' if $type eq 'configure';
82         $action = "export-$type";
83     } elsif (m/^--(list|status|dump)$/) {
84         usageerr(g_('two commands specified: --%s and --%s'), $1, $action)
85             if defined($action);
86         $action = $1;
87         $load_config = 0 if $action eq 'list';
88     } elsif (m/^-(?:\?|-help)$/) {
89         usage();
90         exit 0;
91     } elsif (m/^--version$/) {
92         version();
93         exit 0;
94     } else {
95         usageerr(g_("unknown option '%s'"), $_);
96     }
97 }
98
99 $action //= 'dump';
100
101 my $build_flags = Dpkg::BuildFlags->new();
102
103 $build_flags->load_config() if $load_config;
104
105 if ($action eq 'list') {
106     foreach my $flag ($build_flags->list()) {
107         print "$flag\n";
108     }
109 } elsif ($action eq 'get') {
110     exit 1 unless $build_flags->has($param);
111
112     print $build_flags->get($param) . "\n";
113 } elsif ($action eq 'origin') {
114     exit 1 unless $build_flags->has($param);
115
116     print $build_flags->get_origin($param) . "\n";
117 } elsif ($action eq 'query-features') {
118     exit 1 unless $build_flags->has_features($param);
119
120     my %features = $build_flags->get_features($param);
121     my $para_shown = 0;
122     foreach my $feature (sort keys %features) {
123         print $para_shown++ ? "\n" : '';
124         printf "Feature: %s\n", $feature;
125         printf "Enabled: %s\n", $features{$feature} ? 'yes' : 'no';
126     }
127 } elsif ($action =~ m/^export-(.*)$/) {
128     my $export_type = $1;
129     foreach my $flag ($build_flags->list()) {
130         next unless $flag =~ /^[A-Z]/; # Skip flags starting with lowercase
131         my $value = $build_flags->get($flag);
132         if ($export_type eq 'sh') {
133             $value =~ s/"/\"/g;
134             print "export $flag=\"$value\"\n";
135         } elsif ($export_type eq 'make') {
136             $value =~ s/\$/\$\$/g;
137             print "export $flag := $value\n";
138         } elsif ($export_type eq 'cmdline') {
139             print "$flag=\"$value\" ";
140         }
141     }
142 } elsif ($action eq 'dump') {
143     foreach my $flag ($build_flags->list()) {
144         my $value = $build_flags->get($flag);
145         print "$flag=$value\n";
146     }
147 } elsif ($action eq 'status') {
148     # Prefix everything with "dpkg-buildflags: status: " to allow easy
149     # extraction from a build log. Thus we use report with a non-translated
150     # type string.
151
152     # First print all environment variables that might have changed the
153     # results (only existing ones, might make sense to add an option to
154     # also show which ones could have set to modify it).
155     my @envvars = Dpkg::Build::Env::list_accessed();
156     for my $envvar (@envvars) {
157         if (exists $ENV{$envvar}) {
158             printf report(REPORT_STATUS, 'environment variable %s=%s',
159                    $envvar, $ENV{$envvar});
160         }
161     }
162     my $vendor = Dpkg::Vendor::get_current_vendor() || 'undefined';
163     print report(REPORT_STATUS, "vendor is $vendor");
164     # Then the resulting features:
165     foreach my $area (sort $build_flags->get_feature_areas()) {
166         my $fs;
167         my %features = $build_flags->get_features($area);
168         foreach my $feature (sort keys %features) {
169             $fs .= sprintf(' %s=%s', $feature, $features{$feature} ? 'yes' : 'no');
170         }
171         print report(REPORT_STATUS, "$area features:$fs");
172     }
173     # Then the resulting values (with their origin):
174     foreach my $flag ($build_flags->list()) {
175         my $value = $build_flags->get($flag);
176         my $origin = $build_flags->get_origin($flag);
177         my $maintainer = $build_flags->is_maintainer_modified($flag) ? '+maintainer' : '';
178         print report(REPORT_STATUS, "$flag [$origin$maintainer]: $value");
179     }
180 }