1 # rules -- lintian check script -*- perl -*-
3 # Copyright (C) 2006 Russ Allbery <rra@debian.org>
4 # Copyright (C) 2005 René van Bevern <rvb@pro-linux.de>
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.
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.
16 package Maemian::rules;
21 # The following targets are required per Policy.
22 my %required = map { $_ => 1 }
23 qw(build binary binary-arch binary-indep clean);
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.
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";
50 #get architecture field:
51 unless (-d "fields") {
52 fail("directory in lintian laboratory for $type package $pkg missing: fields");
55 my $architecture = $info->field('architecture') || '';
57 open(RULES, '<', 'debfiles/rules') or fail("Failed opening rules: $!");
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.
62 tag "debian-rules-not-a-makefile", ""
63 unless $start =~ m%^\#!\s*/usr/bin/make\s+-[re]?f[re]?\s*$%;
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.
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
80 $includes = 1 if m/^ *[s-]?include\s+/;
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
85 if (/^\s*(export\s+)?DH_COMPAT\s*:?=/ && keys(%seen) == 0) {
86 tag "debian-rules-sets-DH_COMPAT", "line $.";
89 # Check for problems that can occur anywhere in debian/rules.
90 if (/\$[\(\{]PWD[\)\}]/) {
91 tag "debian-rules-uses-pwd", "line $.";
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 $.";
97 if (/$[\(\{]DEB_BUILD_OPTS[\)\}]/) {
98 tag "debian-rules-uses-DEB_BUILD_OPTS", "line $.";
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
104 if (/^(?:[^:]+\s)?\.PHONY(?:\s[^:]+)?:(.+)/) {
105 my @targets = split (' ', $1);
107 $seen{$_}++ if $required{$_};
111 if (/^([^\s:][^:]*):/) {
112 @current_targets = split (' ', $1);
113 for (@current_targets) {
115 my $pattern = quotemeta $_;
116 $pattern =~ s/\\%/.*/g;
117 for my $target (keys %required) {
118 $seen{$target}++ if $target =~ m/$pattern/;
121 $seen{$_}++ if $required{$_};
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 = ();
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
136 foreach my $target (@current_targets) {
137 $rules_per_target{$target} ||= [];
138 push @{$rules_per_target{$target}}, $_;
140 if (m/^\s+(dh_\S+)\b/ and $debhelper_order{$1}) {
142 my $group = $debhelper_order{$command};
143 if ($group < $debhelper_group) {
144 tag "debian-rules-calls-debhelper-in-odd-order",
145 $command, "(line $.)";
147 $debhelper_group = $group;
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};
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'}}) {
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|\$\@)/) {
174 tag "binary-arch-rules-but-pkg-is-arch-indep" if $nonempty;
180 # indent-tabs-mode: nil
181 # cperl-indent-level: 4
183 # vim: syntax=perl sw=4 sts=4 ts=4 et shiftround