Merge tag 'backlight-next-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/lee...
[sfrench/cifs-2.6.git] / scripts / adjust_autoksyms.sh
1 #!/bin/sh
2 # SPDX-License-Identifier: GPL-2.0-only
3
4 # Script to create/update include/generated/autoksyms.h and dependency files
5 #
6 # Copyright:    (C) 2016  Linaro Limited
7 # Created by:   Nicolas Pitre, January 2016
8 #
9
10 # Create/update the include/generated/autoksyms.h file from the list
11 # of all module's needed symbols as recorded on the third line of
12 # .tmp_versions/*.mod files.
13 #
14 # For each symbol being added or removed, the corresponding dependency
15 # file's timestamp is updated to force a rebuild of the affected source
16 # file. All arguments passed to this script are assumed to be a command
17 # to be exec'd to trigger a rebuild of those files.
18
19 set -e
20
21 cur_ksyms_file="include/generated/autoksyms.h"
22 new_ksyms_file="include/generated/autoksyms.h.tmpnew"
23
24 info() {
25         if [ "$quiet" != "silent_" ]; then
26                 printf "  %-7s %s\n" "$1" "$2"
27         fi
28 }
29
30 info "CHK" "$cur_ksyms_file"
31
32 # Use "make V=1" to debug this script.
33 case "$KBUILD_VERBOSE" in
34 *1*)
35         set -x
36         ;;
37 esac
38
39 # We need access to CONFIG_ symbols
40 . include/config/auto.conf
41
42 # Generate a new ksym list file with symbols needed by the current
43 # set of modules.
44 cat > "$new_ksyms_file" << EOT
45 /*
46  * Automatically generated file; DO NOT EDIT.
47  */
48
49 EOT
50 [ "$(ls -A "$MODVERDIR")" ] &&
51 for mod in "$MODVERDIR"/*.mod; do
52         sed -n -e '3{s/ /\n/g;/^$/!p;}' "$mod"
53 done | sort -u |
54 while read sym; do
55         echo "#define __KSYM_${sym} 1"
56 done >> "$new_ksyms_file"
57
58 # Special case for modversions (see modpost.c)
59 if [ -n "$CONFIG_MODVERSIONS" ]; then
60         echo "#define __KSYM_module_layout 1" >> "$new_ksyms_file"
61 fi
62
63 # Extract changes between old and new list and touch corresponding
64 # dependency files.
65 changed=$(
66 count=0
67 sort "$cur_ksyms_file" "$new_ksyms_file" | uniq -u |
68 sed -n 's/^#define __KSYM_\(.*\) 1/\1/p' | tr "A-Z_" "a-z/" |
69 while read sympath; do
70         if [ -z "$sympath" ]; then continue; fi
71         depfile="include/ksym/${sympath}.h"
72         mkdir -p "$(dirname "$depfile")"
73         touch "$depfile"
74         # Filesystems with coarse time precision may create timestamps
75         # equal to the one from a file that was very recently built and that
76         # needs to be rebuild. Let's guard against that by making sure our
77         # dep files are always newer than the first file we created here.
78         while [ ! "$depfile" -nt "$new_ksyms_file" ]; do
79                 touch "$depfile"
80         done
81         echo $((count += 1))
82 done | tail -1 )
83 changed=${changed:-0}
84
85 if [ $changed -gt 0 ]; then
86         # Replace the old list with tne new one
87         old=$(grep -c "^#define __KSYM_" "$cur_ksyms_file" || true)
88         new=$(grep -c "^#define __KSYM_" "$new_ksyms_file" || true)
89         info "KSYMS" "symbols: before=$old, after=$new, changed=$changed"
90         info "UPD" "$cur_ksyms_file"
91         mv -f "$new_ksyms_file" "$cur_ksyms_file"
92         # Then trigger a rebuild of affected source files
93         exec $@
94 else
95         rm -f "$new_ksyms_file"
96 fi