Adding latest version of minimae.
[maemian] / checks / rules
1 # rules -- lintian check script -*- perl -*-
2
3 # Copyright (C) 2006 Russ Allbery <rra@debian.org>
4 # Copyright (C) 2005 RenĂ© van Bevern <rvb@pro-linux.de>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 package Maemian::rules;
17 use strict;
18 use Tags;
19 use Util;
20
21 # The following targets are required per Policy.
22 my %required = map { $_ => 1 }
23     qw(build binary binary-arch binary-indep clean);
24
25 # Rules about required debhelper command ordering.  Each command is put into a
26 # class and the tag is issued if they're called in the wrong order for the
27 # classes.  Unknown commands won't trigger this flag.
28 my %debhelper_order =
29     (dh_makeshlibs => 1,
30      dh_shlibdeps  => 2,
31      dh_installdeb => 2,
32      dh_gencontrol => 2,
33      dh_builddeb   => 3);
34
35 sub run {
36
37 my $pkg = shift;
38 my $type = shift;
39 my $info = shift;
40
41 # Policy could be read as allowing debian/rules to be a symlink to some other
42 # file, and in a native Debian package it could be a symlink to a file that we
43 # didn't unpack.  Warn if it's a symlink (dpkg-source does as well) and skip
44 # all the tests if we then can't read it.
45 if (-l "debfiles/rules") {
46     tag "debian-rules-is-symlink", "";
47     return 0 unless -f "debfiles/rules";
48 }
49
50 #get architecture field:
51 unless (-d "fields") {
52     fail("directory in lintian laboratory for $type package $pkg missing: fields");
53 }
54
55 my $architecture = $info->field('architecture') || '';
56
57 open(RULES, '<', 'debfiles/rules') or fail("Failed opening rules: $!");
58
59 # Check for required #!/usr/bin/make -f opening line.  Allow -r or -e; a
60 # strict reading of Policy doesn't allow either, but they seem harmless.
61 my $start = <RULES>;
62 tag "debian-rules-not-a-makefile", ""
63     unless $start =~ m%^\#!\s*/usr/bin/make\s+-[re]?f[re]?\s*$%;
64
65 # Scan debian/rules.  We would really like to let make do this for us, but
66 # unfortunately there doesn't seem to be a way to get make to syntax-check and
67 # analyze a makefile without running at least $(shell) commands.
68 #
69 # We skip some of the rule analysis if debian/rules includes any other files,
70 # since to chase all includes we'd have to have all of its build dependencies
71 # installed.
72 my $includes = 0;
73 my %seen;
74 local $_;
75 my @current_targets;
76 my %rules_per_target;
77 my $debhelper_group;
78 while (<RULES>) {
79     next if /^\s*\#/;
80     $includes = 1 if m/^ *[s-]?include\s+/;
81
82     # Check for DH_COMPAT settings outside of any rule, which are now
83     # deprecated.  It's a bit easier structurally to do this here than in
84     # debhelper.
85     if (/^\s*(export\s+)?DH_COMPAT\s*:?=/ && keys(%seen) == 0) {
86         tag "debian-rules-sets-DH_COMPAT", "line $.";
87     }
88
89     # Check for problems that can occur anywhere in debian/rules.
90     if (/\$[\(\{]PWD[\)\}]/) {
91         tag "debian-rules-uses-pwd", "line $.";
92     }
93     if (/^\t\s*-(?:\$[\(\{]MAKE[\}\)]|make)\s.*(?:dist)?clean/ ||
94         /^\t\s*(?:\$[\(\{]MAKE[\}\)]|make)\s(?:.*\s)?-\w*i.*(?:dist)?clean/) {
95         tag "debian-rules-ignores-make-clean-error", "line $.";
96     }
97     if (/$[\(\{]DEB_BUILD_OPTS[\)\}]/) {
98         tag "debian-rules-uses-DEB_BUILD_OPTS", "line $.";
99     }
100
101     # Listing a rule as a dependency of .PHONY is sufficient to make it
102     # present for the purposes of GNU make and therefore the Policy
103     # requirement.
104     if (/^(?:[^:]+\s)?\.PHONY(?:\s[^:]+)?:(.+)/) {
105         my @targets = split (' ', $1);
106         for (@targets) {
107             $seen{$_}++ if $required{$_};
108         }
109     }
110
111     if (/^([^\s:][^:]*):/) {
112         @current_targets = split (' ', $1);
113         for (@current_targets) {
114             if (m/%/) {
115                 my $pattern = quotemeta $_;
116                 $pattern =~ s/\\%/.*/g;
117                 for my $target (keys %required) {
118                     $seen{$target}++ if $target =~ m/$pattern/;
119                 }
120             } else {
121                 $seen{$_}++ if $required{$_};
122             }
123         }
124         $debhelper_group = 0;
125     } elsif (/^define /) {
126         # We don't want to think the body of the define is part of the
127         # previous rule or we'll get false positives on tags like
128         # binary-arch-rules-but-pkg-is-arch-indep.  Treat a define as the
129         # end of the current rule, although that isn't very accurate either.
130         @current_targets = ();
131     } else {
132         # If we have non-empty, non-comment lines, store them for all current
133         # targets and check whether debhelper programs are called in a
134         # reasonable order.
135         if (m/^\s+[^\#]/) {
136             foreach my $target (@current_targets) {
137                 $rules_per_target{$target} ||= [];
138                 push @{$rules_per_target{$target}}, $_;
139             }
140             if (m/^\s+(dh_\S+)\b/ and $debhelper_order{$1}) {
141                 my $command = $1;
142                 my $group = $debhelper_order{$command};
143                 if ($group < $debhelper_group) {
144                     tag "debian-rules-calls-debhelper-in-odd-order",
145                         $command, "(line $.)";
146                 } else {
147                     $debhelper_group = $group;
148                 }
149             }
150         }
151     }
152 }
153 close RULES;
154
155 unless ($includes) {
156     # Make sure all the required rules were seen.
157     for my $target (sort keys %required) {
158         tag "debian-rules-missing-required-target", $target
159             unless $seen{$target};
160     }
161 }
162
163 # Make sure we have no content for binary-arch if we are arch-indep:
164 $rules_per_target{'binary-arch'} ||= [];
165 if ($architecture eq "all" && scalar @{$rules_per_target{'binary-arch'}}) {
166     my $nonempty = 0;
167     foreach (@{$rules_per_target{'binary-arch'}}) {
168         # dh binary-arch is actually a no-op if there is no
169         # Architecture: any package in the control file
170         unless (m/^\s*dh\s+(?:binary-arch|\$\@)/) {
171             $nonempty = 1;
172         }
173     }
174     tag "binary-arch-rules-but-pkg-is-arch-indep" if $nonempty;
175 }
176 }
177 1;
178
179 # Local Variables:
180 # indent-tabs-mode: nil
181 # cperl-indent-level: 4
182 # End:
183 # vim: syntax=perl sw=4 sts=4 ts=4 et shiftround