xref: /openbmc/linux/scripts/nsdeps (revision bbc55bde)
140997fb8SMasahiro Yamada#!/bin/sh
2eb8305aeSMatthias Maennich# SPDX-License-Identifier: GPL-2.0
3eb8305aeSMatthias Maennich# Linux kernel symbol namespace import generator
4eb8305aeSMatthias Maennich#
5eb8305aeSMatthias Maennich# This script requires a minimum spatch version.
6eb8305aeSMatthias MaennichSPATCH_REQ_VERSION="1.0.4"
7eb8305aeSMatthias Maennich
8eb8305aeSMatthias MaennichDIR="$(dirname $(readlink -f $0))/.."
9eb8305aeSMatthias MaennichSPATCH="`which ${SPATCH:=spatch}`"
10eb8305aeSMatthias Maennichif [ ! -x "$SPATCH" ]; then
11eb8305aeSMatthias Maennich	echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
12eb8305aeSMatthias Maennich	exit 1
13eb8305aeSMatthias Maennichfi
14eb8305aeSMatthias Maennich
15eb8305aeSMatthias MaennichSPATCH_REQ_VERSION_NUM=$(echo $SPATCH_REQ_VERSION | ${DIR}/scripts/ld-version.sh)
16eb8305aeSMatthias MaennichSPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}')
17eb8305aeSMatthias MaennichSPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh)
18eb8305aeSMatthias Maennich
19eb8305aeSMatthias Maennichif [ "$SPATCH_VERSION_NUM" -lt "$SPATCH_REQ_VERSION_NUM" ] ; then
20eb8305aeSMatthias Maennich	echo "spatch needs to be version $SPATCH_REQ_VERSION or higher"
21eb8305aeSMatthias Maennich	exit 1
22eb8305aeSMatthias Maennichfi
23eb8305aeSMatthias Maennich
24eb8305aeSMatthias Maennichgenerate_deps_for_ns() {
25eb8305aeSMatthias Maennich	$SPATCH --very-quiet --in-place --sp-file \
26eb8305aeSMatthias Maennich		$srctree/scripts/coccinelle/misc/add_namespace.cocci -D ns=$1 $2
27eb8305aeSMatthias Maennich}
28eb8305aeSMatthias Maennich
29eb8305aeSMatthias Maennichgenerate_deps() {
30bbc55bdeSMasahiro Yamada	local mod=${1%.ko:}
31bbc55bdeSMasahiro Yamada	shift
32bbc55bdeSMasahiro Yamada	local namespaces="$*"
33bbc55bdeSMasahiro Yamada	local mod_source_files="`cat $mod.mod | sed -n 1p                      \
34eb8305aeSMatthias Maennich					      | sed -e 's/\.o/\.c/g'           \
3557baec7bSJessica Yu					      | sed "s|[^ ]* *|${srctree}/&|g"`"
36bbc55bdeSMasahiro Yamada	for ns in $namespaces; do
37bbc55bdeSMasahiro Yamada		echo "Adding namespace $ns to module $mod.ko."
3857baec7bSJessica Yu		generate_deps_for_ns $ns "$mod_source_files"
39eb8305aeSMatthias Maennich		# sort the imports
40eb8305aeSMatthias Maennich		for source_file in $mod_source_files; do
41eb8305aeSMatthias Maennich			sed '/MODULE_IMPORT_NS/Q' $source_file > ${source_file}.tmp
42eb8305aeSMatthias Maennich			offset=$(wc -l ${source_file}.tmp | awk '{print $1;}')
43df6f0987SMasahiro Yamada			cat $source_file | grep MODULE_IMPORT_NS | LANG=C sort -u >> ${source_file}.tmp
44eb8305aeSMatthias Maennich			tail -n +$((offset +1)) ${source_file} | grep -v MODULE_IMPORT_NS >> ${source_file}.tmp
45eb8305aeSMatthias Maennich			if ! diff -q ${source_file} ${source_file}.tmp; then
46eb8305aeSMatthias Maennich				mv ${source_file}.tmp ${source_file}
47eb8305aeSMatthias Maennich			else
48eb8305aeSMatthias Maennich				rm ${source_file}.tmp
49eb8305aeSMatthias Maennich			fi
50eb8305aeSMatthias Maennich		done
51eb8305aeSMatthias Maennich	done
52eb8305aeSMatthias Maennich}
53eb8305aeSMatthias Maennich
54bbc55bdeSMasahiro Yamadawhile read line
55bbc55bdeSMasahiro Yamadado
56bbc55bdeSMasahiro Yamada	generate_deps $line
57bbc55bdeSMasahiro Yamadadone < modules.nsdeps
58