[PATCH] kernel: Add support for cross-build kernels

Andrew Murray <[email protected]>
Newsgroups org.kernel.vger.smatch
Message-ID <[email protected]>
The smatch scripts such as kchecker invoke the Linux kernel build
system, however it doesn't pass commonly used environment variables
which allow for cross-compilation.

Let's pass the ARCH and CROSS_COMPILE environment variables to the
kernel build system to support cross-built kernels. E.g.

ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- \
	~/smatch/smatch_scripts/build_kernel_data.sh

Signed-off-by: Andrew Murray <[email protected]>
---
 Documentation/smatch.txt             |  4 ++++
 smatch_scripts/build_generic_data.sh |  9 ++++++++-
 smatch_scripts/kchecker              | 10 +++++++++-
 smatch_scripts/test_generic.sh       | 11 +++++++++--
 smatch_scripts/test_kernel.sh        | 11 +++++++++--
 5 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/Documentation/smatch.txt b/Documentation/smatch.txt
index ae9a454dd023..b62e4507ee1d 100644
--- a/Documentation/smatch.txt
+++ b/Documentation/smatch.txt
@@ -55,6 +55,10 @@ You can also build a directory like this:
 
 The kchecker script prints its warnings to stdout.
 
+The above scripts will ensure that any ARCH or CROSS_COMPILE environment
+variables are passed to kernel build system - thus allowing for the use of
+Smatch with kernels that are normally built with cross-compilers.
+
 If you are building something else (which is not the Linux kernel) then use
 something like:
 
diff --git a/smatch_scripts/build_generic_data.sh b/smatch_scripts/build_generic_data.sh
index 27ad013bf55b..c8f98d9bdb62 100755
--- a/smatch_scripts/build_generic_data.sh
+++ b/smatch_scripts/build_generic_data.sh
@@ -52,7 +52,14 @@ if [ ! -e smatch_db.sqlite ] ; then
     fi
 fi
 
-make -j${NR_CPU} CHECK="$BIN_DIR/smatch --call-tree --info --param-mapper --spammy --file-output" $TARGET
+if [[ ! -z $ARCH ]]; then
+	KERNEL_ARCH="ARCH=$ARCH"
+fi
+if [[ ! -z $CROSS_COMPILE ]] ; then
+	KERNEL_CROSS_COMPILE="CROSS_COMPILE=$CROSS_COMPILE"
+fi
+
+make $KERNEL_ARCH $KERNEL_CROSS_COMPILE -j${NR_CPU} CHECK="$BIN_DIR/smatch --call-tree --info --param-mapper --spammy --file-output" $TARGET
 
 find -name \*.c.smatch -exec cat \{\} \; -exec rm \{\} \; > smatch_warns.txt
 
diff --git a/smatch_scripts/kchecker b/smatch_scripts/kchecker
index 9754b21f93d0..2ca9b0f00f49 100755
--- a/smatch_scripts/kchecker
+++ b/smatch_scripts/kchecker
@@ -70,4 +70,12 @@ if  echo $oname | grep -q .o$ ; then
     rm -f $oname
 fi
 
-make C=2 $ENDIAN CHECK="$PRE $CMD $POST" $oname
+if [[ ! -z $ARCH ]]; then
+	echo "yes"
+	KERNEL_ARCH="ARCH=$ARCH"
+fi
+if [[ ! -z $CROSS_COMPILE ]] ; then
+	KERNEL_CROSS_COMPILE="CROSS_COMPILE=$CROSS_COMPILE"
+fi
+
+make $KERNEL_CROSS_COMPILE $KERNEL_ARCH C=2 $ENDIAN CHECK="$PRE $CMD $POST" $oname
diff --git a/smatch_scripts/test_generic.sh b/smatch_scripts/test_generic.sh
index 4655ba74930a..5707d7c6ae70 100755
--- a/smatch_scripts/test_generic.sh
+++ b/smatch_scripts/test_generic.sh
@@ -52,9 +52,16 @@ else
     exit 1
 fi
 
-make clean
+if [[ ! -z $ARCH ]]; then
+	KERNEL_ARCH="ARCH=$ARCH"
+fi
+if [[ ! -z $CROSS_COMPILE ]] ; then
+	KERNEL_CROSS_COMPILE="CROSS_COMPILE=$CROSS_COMPILE"
+fi
+
+make $KERNEL_ARCH $KERNEL_CROSS_COMPILE clean
 find -name \*.c.smatch -exec rm \{\} \;
-make -j${NR_CPU} $ENDIAN -k CHECK="$CMD --file-output $*" \
+make $KERNEL_ARCH $KERNEL_CROSS_COMPILE -j${NR_CPU} $ENDIAN -k CHECK="$CMD --file-output $*" \
 	C=1 $TARGET 2>&1 | tee $LOG
 find -name \*.c.smatch -exec cat \{\} \; -exec rm \{\} \; > $WLOG
 
diff --git a/smatch_scripts/test_kernel.sh b/smatch_scripts/test_kernel.sh
index b31c61047cfe..d254c56a638c 100755
--- a/smatch_scripts/test_kernel.sh
+++ b/smatch_scripts/test_kernel.sh
@@ -56,9 +56,16 @@ else
     exit 1
 fi
 
-make clean
+if [[ ! -z $ARCH ]]; then
+	KERNEL_ARCH="ARCH=$ARCH"
+fi
+if [[ ! -z $CROSS_COMPILE ]] ; then
+	KERNEL_CROSS_COMPILE="CROSS_COMPILE=$CROSS_COMPILE"
+fi
+
+make $KERNEL_ARCH $KERNEL_CROSS_COMPILE clean
 find -name \*.c.smatch -exec rm \{\} \;
-make -j${NR_CPU} $ENDIAN -k CHECK="$CMD -p=kernel --file-output --succeed $*" \
+make $KERNEL_ARCH $KERNEL_CROSS_COMPILE -j${NR_CPU} $ENDIAN -k CHECK="$CMD -p=kernel --file-output --succeed $*" \
 	C=1 $BUILD_PARAM $TARGET 2>&1 | tee $LOG
 BUILD_STATUS=${PIPESTATUS[0]}
 find -name \*.c.smatch -exec cat \{\} \; -exec rm \{\} \; > $WLOG
-- 
2.21.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.