xref: /linux/scripts/misc-check (revision 08215f5486ec4d7c39cf14987ffb133e1e1f6c10)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-only
3
4set -e
5
6# Detect files that are tracked but ignored by git.
7check_tracked_ignored_files () {
8	git -C "${srctree:-.}" ls-files -i -c --exclude-per-directory=.gitignore 2>/dev/null |
9		sed 's/$/: warning: ignored by one of the .gitignore files/' >&2
10}
11
12# Check for missing #include <linux/export.h>
13#
14# The rule for including <linux/export.h> is very simple:
15# Include <linux/export.h> only when you use EXPORT_SYMBOL(). That's it.
16#
17# However, some headers include <linux/export.h> even though they are completely
18# unrelated to EXPORT_SYMBOL().
19#
20# One example is include/linux/module.h. Please note <linux/module.h> and
21# <linux/export.h> are orthogonal. <linux/module.h> should be included by files
22# that can be compiled as modules. In other words, <linux/module.h> should be
23# included by EXPORT_SYMBOL consumers. In contrast, <linux/export.h> should be
24# included from EXPORT_SYMBOL providers, which may or may not be modular.
25# Hence, include/linux/module.h should *not* include <linux/export.h>.
26#
27# Another example is include/linux/linkage.h, which is completely unrelated to
28# EXPORT_SYMBOL(). Worse, it is included by most C files, which means, most C
29# files end up including <linux/export.h>, even though only some of them
30# actually export symbols. Hence, include/linux/linkage.h should *not* include
31# <linux/export.h>.
32#
33# Before fixing such headers, we must ensure that C files using EXPORT_SYMBOL()
34# include <linux/export.h> directly, since many C files currently rely on
35# <linux/export.h> being included indirectly (likely, via <linux/linkage> etc.).
36#
37# Therefore, this check.
38#
39# The problem is simple - the warned files use EXPORT_SYMBOL(), but do not
40# include <linux/export.h>. Please add #include <linux/export.h> to them.
41#
42# If the included headers are sorted alphabetically, please insert
43# <linux/export.h> in the appropriate position to maintain the sort order.
44# For this reason, this script only checks missing <linux/export.h>, but
45# does not automatically fix it.
46check_missing_include_linux_export_h () {
47
48	git -C "${srctree:-.}" grep --files-with-matches -E 'EXPORT_SYMBOL((_NS)?(_GPL)?|_GPL_FOR_MODULES)\(.*\)' \
49	    -- '*.[ch]' :^tools/ :^include/linux/export.h |
50	xargs -r git -C "${srctree:-.}" grep --files-without-match '#include[[:space:]]*<linux/export\.h>' |
51	xargs -r printf "%s: warning: EXPORT_SYMBOL() is used, but #include <linux/export.h> is missing\n" >&2
52}
53
54# If you do not use EXPORT_SYMBOL(), please do not include <linux/export.h>.
55# Currently, this is checked for *.c files, but not for *.h files, because some
56# *.c files rely on <linux/export.h> being included indirectly.
57check_unnecessary_include_linux_export_h () {
58
59	git -C "${srctree:-.}" grep --files-with-matches '#include[[:space:]]*<linux/export\.h>' \
60	    -- '*.[c]' :^tools/ |
61	xargs -r git -C "${srctree:-.}" grep --files-without-match -E 'EXPORT_SYMBOL((_NS)?(_GPL)?|_GPL_FOR_MODULES)\(.*\)' |
62	xargs -r printf "%s: warning: EXPORT_SYMBOL() is not used, but #include <linux/export.h> is present\n" >&2
63}
64
65case "${KBUILD_EXTRA_WARN}" in
66*1*)
67	check_tracked_ignored_files
68	;;
69esac
70
71case "${KBUILD_EXTRA_WARN}" in
72*2*)
73	check_missing_include_linux_export_h
74	check_unnecessary_include_linux_export_h
75	;;
76esac
77