18c2ecf20Sopenharmony_ci#!/bin/sh
28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0-only
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci# Script to update include/generated/autoksyms.h and dependency files
58c2ecf20Sopenharmony_ci#
68c2ecf20Sopenharmony_ci# Copyright:	(C) 2016  Linaro Limited
78c2ecf20Sopenharmony_ci# Created by:	Nicolas Pitre, January 2016
88c2ecf20Sopenharmony_ci#
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci# Update the include/generated/autoksyms.h file.
118c2ecf20Sopenharmony_ci#
128c2ecf20Sopenharmony_ci# For each symbol being added or removed, the corresponding dependency
138c2ecf20Sopenharmony_ci# file's timestamp is updated to force a rebuild of the affected source
148c2ecf20Sopenharmony_ci# file. All arguments passed to this script are assumed to be a command
158c2ecf20Sopenharmony_ci# to be exec'd to trigger a rebuild of those files.
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ciset -e
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cicur_ksyms_file="include/generated/autoksyms.h"
208c2ecf20Sopenharmony_cinew_ksyms_file="include/generated/autoksyms.h.tmpnew"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ciinfo() {
238c2ecf20Sopenharmony_ci	if [ "$quiet" != "silent_" ]; then
248c2ecf20Sopenharmony_ci		printf "  %-7s %s\n" "$1" "$2"
258c2ecf20Sopenharmony_ci	fi
268c2ecf20Sopenharmony_ci}
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ciinfo "CHK" "$cur_ksyms_file"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci# Use "make V=1" to debug this script.
318c2ecf20Sopenharmony_cicase "$KBUILD_VERBOSE" in
328c2ecf20Sopenharmony_ci*1*)
338c2ecf20Sopenharmony_ci	set -x
348c2ecf20Sopenharmony_ci	;;
358c2ecf20Sopenharmony_ciesac
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci# We need access to CONFIG_ symbols
388c2ecf20Sopenharmony_ci. include/config/auto.conf
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci# Generate a new symbol list file
418c2ecf20Sopenharmony_ci$CONFIG_SHELL $srctree/scripts/gen_autoksyms.sh "$new_ksyms_file"
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci# Extract changes between old and new list and touch corresponding
448c2ecf20Sopenharmony_ci# dependency files.
458c2ecf20Sopenharmony_cichanged=$(
468c2ecf20Sopenharmony_cicount=0
478c2ecf20Sopenharmony_cisort "$cur_ksyms_file" "$new_ksyms_file" | uniq -u |
488c2ecf20Sopenharmony_cised -n 's/^#define __KSYM_\(.*\) 1/\1/p' | tr "A-Z_" "a-z/" |
498c2ecf20Sopenharmony_ciwhile read sympath; do
508c2ecf20Sopenharmony_ci	if [ -z "$sympath" ]; then continue; fi
518c2ecf20Sopenharmony_ci	depfile="include/ksym/${sympath}.h"
528c2ecf20Sopenharmony_ci	mkdir -p "$(dirname "$depfile")"
538c2ecf20Sopenharmony_ci	touch "$depfile"
548c2ecf20Sopenharmony_ci	# Filesystems with coarse time precision may create timestamps
558c2ecf20Sopenharmony_ci	# equal to the one from a file that was very recently built and that
568c2ecf20Sopenharmony_ci	# needs to be rebuild. Let's guard against that by making sure our
578c2ecf20Sopenharmony_ci	# dep files are always newer than the first file we created here.
588c2ecf20Sopenharmony_ci	while [ ! "$depfile" -nt "$new_ksyms_file" ]; do
598c2ecf20Sopenharmony_ci		touch "$depfile"
608c2ecf20Sopenharmony_ci	done
618c2ecf20Sopenharmony_ci	echo $((count += 1))
628c2ecf20Sopenharmony_cidone | tail -1 )
638c2ecf20Sopenharmony_cichanged=${changed:-0}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ciif [ $changed -gt 0 ]; then
668c2ecf20Sopenharmony_ci	# Replace the old list with tne new one
678c2ecf20Sopenharmony_ci	old=$(grep -c "^#define __KSYM_" "$cur_ksyms_file" || true)
688c2ecf20Sopenharmony_ci	new=$(grep -c "^#define __KSYM_" "$new_ksyms_file" || true)
698c2ecf20Sopenharmony_ci	info "KSYMS" "symbols: before=$old, after=$new, changed=$changed"
708c2ecf20Sopenharmony_ci	info "UPD" "$cur_ksyms_file"
718c2ecf20Sopenharmony_ci	mv -f "$new_ksyms_file" "$cur_ksyms_file"
728c2ecf20Sopenharmony_ci	# Then trigger a rebuild of affected source files
738c2ecf20Sopenharmony_ci	exec $@
748c2ecf20Sopenharmony_cielse
758c2ecf20Sopenharmony_ci	rm -f "$new_ksyms_file"
768c2ecf20Sopenharmony_cifi
77