blob: 6e6d63957da348f6172ca7a7c5545e0058d8824d [file] [log] [blame]
Nicolas Pitre23121ca2016-01-26 21:50:18 -05001#!/bin/sh
2
3# Script to create/update include/generated/autoksyms.h and dependency files
4#
5# Copyright: (C) 2016 Linaro Limited
6# Created by: Nicolas Pitre, January 2016
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License version 2 as
10# published by the Free Software Foundation.
11
12# Create/update the include/generated/autoksyms.h file from the list
13# of all module's needed symbols as recorded on the third line of
14# .tmp_versions/*.mod files.
15#
16# For each symbol being added or removed, the corresponding dependency
17# file's timestamp is updated to force a rebuild of the affected source
18# file. All arguments passed to this script are assumed to be a command
19# to be exec'd to trigger a rebuild of those files.
20
21set -e
22
23cur_ksyms_file="include/generated/autoksyms.h"
24new_ksyms_file="include/generated/autoksyms.h.tmpnew"
25
26info() {
27 if [ "$quiet" != "silent_" ]; then
28 printf " %-7s %s\n" "$1" "$2"
29 fi
30}
31
32info "CHK" "$cur_ksyms_file"
33
34# Use "make V=1" to debug this script.
35case "$KBUILD_VERBOSE" in
36*1*)
37 set -x
38 ;;
39esac
40
41# We need access to CONFIG_ symbols
42case "${KCONFIG_CONFIG}" in
43*/*)
44 . "${KCONFIG_CONFIG}"
45 ;;
46*)
47 # Force using a file from the current directory
48 . "./${KCONFIG_CONFIG}"
49esac
50
Nicolas Pitre23121ca2016-01-26 21:50:18 -050051# Generate a new ksym list file with symbols needed by the current
52# set of modules.
53cat > "$new_ksyms_file" << EOT
54/*
55 * Automatically generated file; DO NOT EDIT.
56 */
57
58EOT
Nicolas Pitred0734722016-12-08 14:17:03 -050059[ "$(ls -A "$MODVERDIR")" ] &&
Michael Forney1fe7d2b2018-02-06 22:41:17 -080060for mod in "$MODVERDIR"/*.mod; do
61 sed -n -e '3{s/ /\n/g;/^$/!p;}' "$mod"
62done | sort -u |
Nicolas Pitre23121ca2016-01-26 21:50:18 -050063while read sym; do
Nicolas Pitre23121ca2016-01-26 21:50:18 -050064 echo "#define __KSYM_${sym} 1"
65done >> "$new_ksyms_file"
66
67# Special case for modversions (see modpost.c)
68if [ -n "$CONFIG_MODVERSIONS" ]; then
69 echo "#define __KSYM_module_layout 1" >> "$new_ksyms_file"
70fi
71
72# Extract changes between old and new list and touch corresponding
73# dependency files.
74changed=$(
75count=0
76sort "$cur_ksyms_file" "$new_ksyms_file" | uniq -u |
77sed -n 's/^#define __KSYM_\(.*\) 1/\1/p' | tr "A-Z_" "a-z/" |
78while read sympath; do
79 if [ -z "$sympath" ]; then continue; fi
Masahiro Yamadafbfa9be2018-03-16 16:37:14 +090080 depfile="include/ksym/${sympath}.h"
Nicolas Pitre23121ca2016-01-26 21:50:18 -050081 mkdir -p "$(dirname "$depfile")"
82 touch "$depfile"
Nicolas Pitre825d4872018-03-15 16:56:20 -040083 # Filesystems with coarse time precision may create timestamps
84 # equal to the one from a file that was very recently built and that
85 # needs to be rebuild. Let's guard against that by making sure our
86 # dep files are always newer than the first file we created here.
87 while [ ! "$depfile" -nt "$new_ksyms_file" ]; do
88 touch "$depfile"
89 done
Nicolas Pitre23121ca2016-01-26 21:50:18 -050090 echo $((count += 1))
91done | tail -1 )
92changed=${changed:-0}
93
94if [ $changed -gt 0 ]; then
95 # Replace the old list with tne new one
96 old=$(grep -c "^#define __KSYM_" "$cur_ksyms_file" || true)
97 new=$(grep -c "^#define __KSYM_" "$new_ksyms_file" || true)
98 info "KSYMS" "symbols: before=$old, after=$new, changed=$changed"
99 info "UPD" "$cur_ksyms_file"
100 mv -f "$new_ksyms_file" "$cur_ksyms_file"
101 # Then trigger a rebuild of affected source files
102 exec $@
103else
104 rm -f "$new_ksyms_file"
105fi