xref: /OK3568_Linux_fs/kernel/scripts/headers_install.sh (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/bin/sh
2*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0
3*4882a593Smuzhiyun
4*4882a593Smuzhiyunif [ $# -ne 2 ]
5*4882a593Smuzhiyunthen
6*4882a593Smuzhiyun	echo "Usage: headers_install.sh INFILE OUTFILE"
7*4882a593Smuzhiyun	echo
8*4882a593Smuzhiyun	echo "Prepares kernel header files for use by user space, by removing"
9*4882a593Smuzhiyun	echo "all compiler.h definitions and #includes, removing any"
10*4882a593Smuzhiyun	echo "#ifdef __KERNEL__ sections, and putting __underscores__ around"
11*4882a593Smuzhiyun	echo "asm/inline/volatile keywords."
12*4882a593Smuzhiyun	echo
13*4882a593Smuzhiyun	echo "INFILE: header file to operate on"
14*4882a593Smuzhiyun	echo "OUTFILE: output file which the processed header is written to"
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun	exit 1
17*4882a593Smuzhiyunfi
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun# Grab arguments
20*4882a593SmuzhiyunINFILE=$1
21*4882a593SmuzhiyunOUTFILE=$2
22*4882a593SmuzhiyunTMPFILE=$OUTFILE.tmp
23*4882a593Smuzhiyun
24*4882a593Smuzhiyuntrap 'rm -f $OUTFILE $TMPFILE' EXIT
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun# SPDX-License-Identifier with GPL variants must have "WITH Linux-syscall-note"
27*4882a593Smuzhiyunif [ -n "$(sed -n -e "/SPDX-License-Identifier:.*GPL-/{/WITH Linux-syscall-note/!p}" $INFILE)" ]; then
28*4882a593Smuzhiyun	echo "error: $INFILE: missing \"WITH Linux-syscall-note\" for SPDX-License-Identifier" >&2
29*4882a593Smuzhiyun	exit 1
30*4882a593Smuzhiyunfi
31*4882a593Smuzhiyun
32*4882a593Smuzhiyunsed -E -e '
33*4882a593Smuzhiyun	s/([[:space:](])(__user|__force|__iomem)[[:space:]]/\1/g
34*4882a593Smuzhiyun	s/__attribute_const__([[:space:]]|$)/\1/g
35*4882a593Smuzhiyun	s@^#include <linux/compiler(|_types).h>@@
36*4882a593Smuzhiyun	s/(^|[^a-zA-Z0-9])__packed([^a-zA-Z0-9_]|$)/\1__attribute__((packed))\2/g
37*4882a593Smuzhiyun	s/(^|[[:space:](])(inline|asm|volatile)([[:space:](]|$)/\1__\2__\3/g
38*4882a593Smuzhiyun	s@#(ifndef|define|endif[[:space:]]*/[*])[[:space:]]*_UAPI@#\1 @
39*4882a593Smuzhiyun' $INFILE > $TMPFILE || exit 1
40*4882a593Smuzhiyun
41*4882a593Smuzhiyunscripts/unifdef -U__KERNEL__ -D__EXPORTED_HEADERS__ $TMPFILE > $OUTFILE
42*4882a593Smuzhiyun[ $? -gt 1 ] && exit 1
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun# Remove /* ... */ style comments, and find CONFIG_ references in code
45*4882a593Smuzhiyunconfigs=$(sed -e '
46*4882a593Smuzhiyun:comment
47*4882a593Smuzhiyun	s:/\*[^*][^*]*:/*:
48*4882a593Smuzhiyun	s:/\*\*\**\([^/]\):/*\1:
49*4882a593Smuzhiyun	t comment
50*4882a593Smuzhiyun	s:/\*\*/: :
51*4882a593Smuzhiyun	t comment
52*4882a593Smuzhiyun	/\/\*/! b check
53*4882a593Smuzhiyun	N
54*4882a593Smuzhiyun	b comment
55*4882a593Smuzhiyun:print
56*4882a593Smuzhiyun	P
57*4882a593Smuzhiyun	D
58*4882a593Smuzhiyun:check
59*4882a593Smuzhiyun	s:^\(CONFIG_[[:alnum:]_]*\):\1\n:
60*4882a593Smuzhiyun	t print
61*4882a593Smuzhiyun	s:^[[:alnum:]_][[:alnum:]_]*::
62*4882a593Smuzhiyun	s:^[^[:alnum:]_][^[:alnum:]_]*::
63*4882a593Smuzhiyun	t check
64*4882a593Smuzhiyun	d
65*4882a593Smuzhiyun' $OUTFILE)
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun# The entries in the following list do not result in an error.
68*4882a593Smuzhiyun# Please do not add a new entry. This list is only for existing ones.
69*4882a593Smuzhiyun# The list will be reduced gradually, and deleted eventually. (hopefully)
70*4882a593Smuzhiyun#
71*4882a593Smuzhiyun# The format is <file-name>:<CONFIG-option> in each line.
72*4882a593Smuzhiyunconfig_leak_ignores="
73*4882a593Smuzhiyuninclude/uapi/linux/iio/types.h:CONFIG_NO_GKI
74*4882a593Smuzhiyunarch/alpha/include/uapi/asm/setup.h:CONFIG_ALPHA_LEGACY_START_ADDRESS
75*4882a593Smuzhiyunarch/arc/include/uapi/asm/page.h:CONFIG_ARC_PAGE_SIZE_16K
76*4882a593Smuzhiyunarch/arc/include/uapi/asm/page.h:CONFIG_ARC_PAGE_SIZE_4K
77*4882a593Smuzhiyunarch/arc/include/uapi/asm/swab.h:CONFIG_ARC_HAS_SWAPE
78*4882a593Smuzhiyunarch/arm/include/uapi/asm/ptrace.h:CONFIG_CPU_ENDIAN_BE8
79*4882a593Smuzhiyunarch/hexagon/include/uapi/asm/ptrace.h:CONFIG_HEXAGON_ARCH_VERSION
80*4882a593Smuzhiyunarch/hexagon/include/uapi/asm/user.h:CONFIG_HEXAGON_ARCH_VERSION
81*4882a593Smuzhiyunarch/ia64/include/uapi/asm/cmpxchg.h:CONFIG_IA64_DEBUG_CMPXCHG
82*4882a593Smuzhiyunarch/m68k/include/uapi/asm/ptrace.h:CONFIG_COLDFIRE
83*4882a593Smuzhiyunarch/nios2/include/uapi/asm/swab.h:CONFIG_NIOS2_CI_SWAB_NO
84*4882a593Smuzhiyunarch/nios2/include/uapi/asm/swab.h:CONFIG_NIOS2_CI_SWAB_SUPPORT
85*4882a593Smuzhiyunarch/x86/include/uapi/asm/auxvec.h:CONFIG_IA32_EMULATION
86*4882a593Smuzhiyunarch/x86/include/uapi/asm/auxvec.h:CONFIG_X86_64
87*4882a593Smuzhiyunarch/x86/include/uapi/asm/mman.h:CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
88*4882a593Smuzhiyuninclude/uapi/asm-generic/fcntl.h:CONFIG_64BIT
89*4882a593Smuzhiyuninclude/uapi/linux/atmdev.h:CONFIG_COMPAT
90*4882a593Smuzhiyuninclude/uapi/linux/eventpoll.h:CONFIG_PM_SLEEP
91*4882a593Smuzhiyuninclude/uapi/linux/hw_breakpoint.h:CONFIG_HAVE_MIXED_BREAKPOINTS_REGS
92*4882a593Smuzhiyuninclude/uapi/linux/pktcdvd.h:CONFIG_CDROM_PKTCDVD_WCACHE
93*4882a593Smuzhiyun"
94*4882a593Smuzhiyun
95*4882a593Smuzhiyunfor c in $configs
96*4882a593Smuzhiyundo
97*4882a593Smuzhiyun	leak_error=1
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun	for ignore in $config_leak_ignores
100*4882a593Smuzhiyun	do
101*4882a593Smuzhiyun		if echo "$INFILE:$c" | grep -q "$ignore$"; then
102*4882a593Smuzhiyun			leak_error=
103*4882a593Smuzhiyun			break
104*4882a593Smuzhiyun		fi
105*4882a593Smuzhiyun	done
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun	if [ "$leak_error" = 1 ]; then
108*4882a593Smuzhiyun		echo "error: $INFILE: leak $c to user-space" >&2
109*4882a593Smuzhiyun		exit 1
110*4882a593Smuzhiyun	fi
111*4882a593Smuzhiyundone
112*4882a593Smuzhiyun
113*4882a593Smuzhiyunrm -f $TMPFILE
114*4882a593Smuzhiyuntrap - EXIT
115