summaryrefslogtreecommitdiffstats
path: root/scripts/t/Dpkg_BuildFlags.t
blob: 530871c8a889fa7ae0e8e11aa32ee878b79865ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/perl
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

use strict;
use warnings;

use Test::More tests => 40;

BEGIN {
    $ENV{DEB_BUILD_ARCH} = 'amd64';
    $ENV{DEB_HOST_ARCH} = 'amd64';
    use_ok('Dpkg::BuildFlags');
}

sub test_has_flag
{
    my ($bf, $flag, $optflag) = @_;

    my $value = $bf->get($flag);
    ok($value =~ m/$optflag/, "$flag contains $optflag: $value");
}

sub test_has_noflag
{
    my ($bf, $flag, $optflag) = @_;

    my $value = $bf->get($flag);
    ok($value !~ m/$optflag/, "$flag does not contain $optflag: $value");
}
my $bf = Dpkg::BuildFlags->new();

ok($bf->has('CPPFLAGS'), 'CPPFLAGS is present');
is($bf->get_origin('CPPFLAGS'), 'vendor', 'CPPFLAGS has a vendor origin');

$bf->set('DPKGFLAGS', '-Wflag -On -fsome', 'system');
is($bf->get('DPKGFLAGS'), '-Wflag -On -fsome', 'get flag');
is($bf->get_origin('DPKGFLAGS'), 'system', 'flag has a system origin');
ok(!$bf->is_maintainer_modified('DPKGFLAGS'), 'set marked flag as non-maint modified');

$bf->strip('DPKGFLAGS', '-On', 'user', undef);
is($bf->get('DPKGFLAGS'), '-Wflag -fsome', 'get stripped flag');
is($bf->get_origin('DPKGFLAGS'), 'user', 'flag has a user origin');
ok(!$bf->is_maintainer_modified('DPKGFLAGS'), 'strip marked flag as non-maint modified');

my @strip_tests = (
    {
        value => '-fsingle',
        strip => '-fsingle',
        exp => '',
    }, {
        value => '-fdupe -fdupe',
        strip => '-fdupe',
        exp => '',
    }, {
        value => '-Wa -fdupe -fdupe -Wb',
        strip => '-fdupe',
        exp => '-Wa -Wb',
    }, {
        value => '-fdupe -Wa -Wb -fdupe',
        strip => '-fdupe',
        exp => '-Wa -Wb',
    }, {
        value => '-fdupe -Wa -fdupe -Wb',
        strip => '-fdupe',
        exp => '-Wa -Wb',
    }, {
        value => '-Wa -fdupe -Wb -fdupe',
        strip => '-fdupe',
        exp => '-Wa -Wb',
    }
);

foreach my $test (@strip_tests) {
    $bf->set('DPKGSTRIPFLAGS', $test->{value}, 'system');
    $bf->strip('DPKGSTRIPFLAGS', $test->{strip}, 'user', undef);
    is($bf->get('DPKGSTRIPFLAGS'), $test->{exp},
        "strip flag '$test->{strip}' from '$test->{value}' to '$test->{exp}'");
}

$bf->append('DPKGFLAGS', '-Wl,other', 'vendor', 0);
is($bf->get('DPKGFLAGS'), '-Wflag -fsome -Wl,other', 'get appended flag');
is($bf->get_origin('DPKGFLAGS'), 'vendor', 'flag has a vendor origin');
ok(!$bf->is_maintainer_modified('DPKGFLAGS'), 'append marked flag as non-maint modified');

$bf->prepend('DPKGFLAGS', '-Idir', 'env', 1);
is($bf->get('DPKGFLAGS'), '-Idir -Wflag -fsome -Wl,other', 'get prepended flag');
is($bf->get_origin('DPKGFLAGS'), 'env', 'flag has an env origin');
ok($bf->is_maintainer_modified('DPKGFLAGS'), 'prepend marked flag as maint modified');

my %known_features = (
    future => [ qw(
        lfs
    ) ],
    abi => [ qw(
        lfs
        time64
    ) ],
    hardening => [ qw(
        bindnow
        branch
        format
        fortify
        pie
        relro
        stackclash
        stackprotector
        stackprotectorstrong
    ) ],
    qa => [ qw(
        bug
        bug-implicit-func
        canary
    ) ],
    reproducible => [ qw(
        fixdebugpath
        fixfilepath
        timeless
    ) ],
    optimize => [ qw(
        lto
    ) ],
    sanitize => [ qw(
        address
        leak
        thread
        undefined
    ) ],
);

is_deeply([ sort $bf->get_feature_areas() ], [ sort keys %known_features ],
          'supported feature areas');

foreach my $area (sort keys %known_features) {
    ok($bf->has_features($area), "has feature area $area");
    my %features = $bf->get_features($area);
    is_deeply([ sort keys %features ],
              $known_features{$area},
              "supported features for area $area");
}

# Test lfs alias from abi to future, we need a 32-bit arch that does does
# not currently have this flag built-in.
$ENV{DEB_BUILD_ARCH} = 'i386';
$ENV{DEB_HOST_ARCH} = 'i386';

$ENV{DEB_BUILD_MAINT_OPTIONS} = 'future=+lfs';
$bf = Dpkg::BuildFlags->new();
test_has_flag($bf, 'CPPFLAGS', '-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64');

$ENV{DEB_BUILD_MAINT_OPTIONS} = 'abi=+lfs';
$bf = Dpkg::BuildFlags->new();
test_has_flag($bf, 'CPPFLAGS', '-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64');

$ENV{DEB_BUILD_MAINT_OPTIONS} = 'future=-lfs abi=+lfs';
$bf = Dpkg::BuildFlags->new();
test_has_flag($bf, 'CPPFLAGS', '-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64');

$ENV{DEB_BUILD_MAINT_OPTIONS} = 'future=+lfs abi=-lfs';
$bf = Dpkg::BuildFlags->new();
test_has_noflag($bf, 'CPPFLAGS', '-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64');

# TODO: Add more test cases.