1*4882a593Smuzhiyun#!/bin/sh 2*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0 3*4882a593Smuzhiyun# Linux kernel symbol namespace import generator 4*4882a593Smuzhiyun# 5*4882a593Smuzhiyun# This script requires a minimum spatch version. 6*4882a593SmuzhiyunSPATCH_REQ_VERSION="1.0.4" 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunDIR="$(dirname $(readlink -f $0))/.." 9*4882a593SmuzhiyunSPATCH="`which ${SPATCH:=spatch}`" 10*4882a593Smuzhiyunif [ ! -x "$SPATCH" ]; then 11*4882a593Smuzhiyun echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/' 12*4882a593Smuzhiyun exit 1 13*4882a593Smuzhiyunfi 14*4882a593Smuzhiyun 15*4882a593SmuzhiyunSPATCH_REQ_VERSION_NUM=$(echo $SPATCH_REQ_VERSION | ${DIR}/scripts/ld-version.sh) 16*4882a593SmuzhiyunSPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}') 17*4882a593SmuzhiyunSPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh) 18*4882a593Smuzhiyun 19*4882a593Smuzhiyunif [ "$SPATCH_VERSION_NUM" -lt "$SPATCH_REQ_VERSION_NUM" ] ; then 20*4882a593Smuzhiyun echo "spatch needs to be version $SPATCH_REQ_VERSION or higher" 21*4882a593Smuzhiyun exit 1 22*4882a593Smuzhiyunfi 23*4882a593Smuzhiyun 24*4882a593Smuzhiyunif [ "$KBUILD_EXTMOD" ]; then 25*4882a593Smuzhiyun src_prefix= 26*4882a593Smuzhiyunelse 27*4882a593Smuzhiyun src_prefix=$srctree/ 28*4882a593Smuzhiyunfi 29*4882a593Smuzhiyun 30*4882a593Smuzhiyungenerate_deps_for_ns() { 31*4882a593Smuzhiyun $SPATCH --very-quiet --in-place --sp-file \ 32*4882a593Smuzhiyun $srctree/scripts/coccinelle/misc/add_namespace.cocci -D nsdeps -D ns=$1 $2 33*4882a593Smuzhiyun} 34*4882a593Smuzhiyun 35*4882a593Smuzhiyungenerate_deps() { 36*4882a593Smuzhiyun local mod=${1%.ko:} 37*4882a593Smuzhiyun shift 38*4882a593Smuzhiyun local namespaces="$*" 39*4882a593Smuzhiyun local mod_source_files="`cat $mod.mod | sed -n 1p \ 40*4882a593Smuzhiyun | sed -e 's/\.o/\.c/g' \ 41*4882a593Smuzhiyun | sed "s|[^ ]* *|${src_prefix}&|g"`" 42*4882a593Smuzhiyun for ns in $namespaces; do 43*4882a593Smuzhiyun echo "Adding namespace $ns to module $mod.ko." 44*4882a593Smuzhiyun generate_deps_for_ns $ns "$mod_source_files" 45*4882a593Smuzhiyun # sort the imports 46*4882a593Smuzhiyun for source_file in $mod_source_files; do 47*4882a593Smuzhiyun sed '/MODULE_IMPORT_NS/Q' $source_file > ${source_file}.tmp 48*4882a593Smuzhiyun offset=$(wc -l ${source_file}.tmp | awk '{print $1;}') 49*4882a593Smuzhiyun cat $source_file | grep MODULE_IMPORT_NS | LANG=C sort -u >> ${source_file}.tmp 50*4882a593Smuzhiyun tail -n +$((offset +1)) ${source_file} | grep -v MODULE_IMPORT_NS >> ${source_file}.tmp 51*4882a593Smuzhiyun if ! diff -q ${source_file} ${source_file}.tmp; then 52*4882a593Smuzhiyun mv ${source_file}.tmp ${source_file} 53*4882a593Smuzhiyun else 54*4882a593Smuzhiyun rm ${source_file}.tmp 55*4882a593Smuzhiyun fi 56*4882a593Smuzhiyun done 57*4882a593Smuzhiyun done 58*4882a593Smuzhiyun} 59*4882a593Smuzhiyun 60*4882a593Smuzhiyunwhile read line 61*4882a593Smuzhiyundo 62*4882a593Smuzhiyun generate_deps $line 63*4882a593Smuzhiyundone < $MODULES_NSDEPS 64