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