xref: /openbmc/linux/scripts/nsdeps (revision 49c3ca34)
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_VERSION=$($SPATCH --version | head -1 | awk '{print $3}')
16eb8305aeSMatthias Maennich
1733114c43SMasahiro Yamadaif ! { echo "$SPATCH_REQ_VERSION"; echo "$SPATCH_VERSION"; } | sort -CV ; then
18eb8305aeSMatthias Maennich	echo "spatch needs to be version $SPATCH_REQ_VERSION or higher"
19eb8305aeSMatthias Maennich	exit 1
20eb8305aeSMatthias Maennichfi
21eb8305aeSMatthias Maennich
22bc35d4bdSMasahiro Yamadaif [ "$KBUILD_EXTMOD" ]; then
23bc35d4bdSMasahiro Yamada	src_prefix=
24bc35d4bdSMasahiro Yamadaelse
25bc35d4bdSMasahiro Yamada	src_prefix=$srctree/
26bc35d4bdSMasahiro Yamadafi
27bc35d4bdSMasahiro Yamada
28eb8305aeSMatthias Maennichgenerate_deps_for_ns() {
29eb8305aeSMatthias Maennich	$SPATCH --very-quiet --in-place --sp-file \
3055c75498SMatthias Maennich		$srctree/scripts/coccinelle/misc/add_namespace.cocci -D nsdeps -D ns=$1 $2
31eb8305aeSMatthias Maennich}
32eb8305aeSMatthias Maennich
33eb8305aeSMatthias Maennichgenerate_deps() {
34bbc55bdeSMasahiro Yamada	local mod=${1%.ko:}
35bbc55bdeSMasahiro Yamada	shift
36bbc55bdeSMasahiro Yamada	local namespaces="$*"
37*49c3ca34SMasahiro Yamada	local mod_source_files=$(sed "s|^\(.*\)\.o$|${src_prefix}\1.c|" $mod.mod)
38*49c3ca34SMasahiro Yamada
39bbc55bdeSMasahiro Yamada	for ns in $namespaces; do
40bbc55bdeSMasahiro Yamada		echo "Adding namespace $ns to module $mod.ko."
4157baec7bSJessica Yu		generate_deps_for_ns $ns "$mod_source_files"
42eb8305aeSMatthias Maennich		# sort the imports
43eb8305aeSMatthias Maennich		for source_file in $mod_source_files; do
44eb8305aeSMatthias Maennich			sed '/MODULE_IMPORT_NS/Q' $source_file > ${source_file}.tmp
45eb8305aeSMatthias Maennich			offset=$(wc -l ${source_file}.tmp | awk '{print $1;}')
4677a88274SMasahiro Yamada			cat $source_file | grep MODULE_IMPORT_NS | LC_ALL=C sort -u >> ${source_file}.tmp
47eb8305aeSMatthias Maennich			tail -n +$((offset +1)) ${source_file} | grep -v MODULE_IMPORT_NS >> ${source_file}.tmp
48eb8305aeSMatthias Maennich			if ! diff -q ${source_file} ${source_file}.tmp; then
49eb8305aeSMatthias Maennich				mv ${source_file}.tmp ${source_file}
50eb8305aeSMatthias Maennich			else
51eb8305aeSMatthias Maennich				rm ${source_file}.tmp
52eb8305aeSMatthias Maennich			fi
53eb8305aeSMatthias Maennich		done
54eb8305aeSMatthias Maennich	done
55eb8305aeSMatthias Maennich}
56eb8305aeSMatthias Maennich
57bbc55bdeSMasahiro Yamadawhile read line
58bbc55bdeSMasahiro Yamadado
59bbc55bdeSMasahiro Yamada	generate_deps $line
60bc35d4bdSMasahiro Yamadadone < $MODULES_NSDEPS
61