5 # Copyright © 2010-2011 Raphaël Hertzog <hertzog@debian.org>
6 # Copyright © 2012-2013 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
26 use Dpkg
::ErrorHandling
qw(:DEFAULT report REPORT_STATUS);
29 use Dpkg
::Vendor
qw(get_current_vendor);
31 textdomain
('dpkg-dev');
34 printf g_
("Debian %s version %s.\n"), $Dpkg::PROGNAME
, $Dpkg::PROGVERSION
;
37 This is free software; see the GNU General Public License version 2 or
38 later for copying conditions. There is NO warranty.
44 'Usage: %s [<command>]')
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.
70 if (m/^--(get|origin|query-features)$/) {
71 usageerr
(g_
('two commands specified: --%s and --%s'), $1, $action)
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)
79 my $type = $1 || 'sh';
81 $type = 'cmdline' if $type eq 'configure';
82 $action = "export-$type";
83 } elsif (m/^--(list|status|dump|query)$/) {
84 usageerr
(g_
('two commands specified: --%s and --%s'), $1, $action)
87 $load_config = 0 if $action eq 'list';
88 } elsif (m/^-(?:\?|-help)$/) {
91 } elsif (m/^--version$/) {
95 usageerr
(g_
("unknown option '%s'"), $_);
101 my $build_flags = Dpkg
::BuildFlags
->new();
103 $build_flags->load_config() if $load_config;
105 if ($action eq 'list') {
106 foreach my $flag ($build_flags->list()) {
109 } elsif ($action eq 'get') {
110 exit 1 unless $build_flags->has($param);
112 print $build_flags->get($param) . "\n";
113 } elsif ($action eq 'origin') {
114 exit 1 unless $build_flags->has($param);
116 print $build_flags->get_origin($param) . "\n";
117 } elsif ($action eq 'query-features') {
118 exit 1 unless $build_flags->has_features($param);
120 my %features = $build_flags->get_features($param);
121 my %builtins = $build_flags->get_builtins($param);
123 foreach my $feature (sort keys %features) {
124 print $para_shown++ ?
"\n" : '';
125 printf "Feature: %s\n", $feature;
126 printf "Enabled: %s\n", $features{$feature} // $builtins{$feature} ?
'yes' : 'no';
127 printf "Builtin: %s\n", $builtins{$feature} ?
'yes' : 'no' if exists $builtins{$feature};
129 } elsif ($action =~ m/^export-(.*)$/) {
130 my $export_type = $1;
131 foreach my $flag ($build_flags->list()) {
132 next unless $flag =~ /^[A-Z]/; # Skip flags starting with lowercase
133 my $value = $build_flags->get($flag);
134 if ($export_type eq 'sh') {
136 print "export $flag=\"$value\"\n";
137 } elsif ($export_type eq 'make') {
138 $value =~ s/\$/\$\$/g;
139 print "export $flag := $value\n";
140 } elsif ($export_type eq 'cmdline') {
141 print "$flag=\"$value\" ";
144 } elsif ($action eq 'dump') {
145 foreach my $flag ($build_flags->list()) {
146 my $value = $build_flags->get($flag);
147 print "$flag=$value\n";
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 printf "Vendor: %s\n", Dpkg
::Vendor
::get_current_vendor
() || 'undefined';
154 print "Environment:\n";
155 for my $envvar (Dpkg
::BuildEnv
::list_accessed
()) {
156 print " $envvar=$ENV{$envvar}\n" if exists $ENV{$envvar};
159 # Then the resulting features:
160 foreach my $area (sort $build_flags->get_feature_areas()) {
162 print "Area: $area\n";
164 my %features = $build_flags->get_features($area);
165 my %builtins = $build_flags->get_builtins($area);
166 foreach my $feature (sort keys %features) {
167 printf " %s=%s\n", $feature, $features{$feature} // $builtins{$feature} ?
'yes' : 'no';
170 foreach my $feature (sort keys %builtins) {
171 printf " %s=%s\n", $feature, $builtins{$feature} ?
'yes' : 'no';
175 # Then the resulting values (with their origin):
176 foreach my $flag ($build_flags->list()) {
178 print "Flag: $flag\n";
179 printf "Value: %s\n", $build_flags->get($flag);
180 my $origin = $build_flags->get_origin($flag);
181 if ($build_flags->is_maintainer_modified($flag)) {
182 $origin .= '+maintainer';
184 print "Origin: $origin\n";
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
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 my @envvars = Dpkg
::BuildEnv
::list_accessed
();
195 for my $envvar (@envvars) {
196 if (exists $ENV{$envvar}) {
197 printf report
(REPORT_STATUS
, 'environment variable %s=%s',
198 $envvar, $ENV{$envvar});
201 my $vendor = Dpkg
::Vendor
::get_current_vendor
() || 'undefined';
202 print report
(REPORT_STATUS
, "vendor is $vendor");
203 # Then the resulting features:
204 foreach my $area (sort $build_flags->get_feature_areas()) {
206 my %features = $build_flags->get_features($area);
207 my %builtins = $build_flags->get_builtins($area);
208 foreach my $feature (sort keys %features) {
209 $fs .= sprintf(' %s=%s', $feature, $features{$feature} // $builtins{$feature} ?
'yes' : 'no');
211 print report
(REPORT_STATUS
, "$area features:$fs");
213 foreach my $feature (sort keys %builtins) {
214 next if ! exists $builtins{$feature};
215 $bs .= sprintf(' %s=%s', $feature, $builtins{$feature} ?
'yes' : 'no');
217 print report
(REPORT_STATUS
, "$area builtins:$bs");
219 # Then the resulting values (with their origin):
220 foreach my $flag ($build_flags->list()) {
221 my $value = $build_flags->get($flag);
222 my $origin = $build_flags->get_origin($flag);
223 my $maintainer = $build_flags->is_maintainer_modified($flag) ?
'+maintainer' : '';
224 print report
(REPORT_STATUS
, "$flag [$origin$maintainer]: $value");