From 7b84765925c8b08f6bb061e66aabebdc814f0168 Mon Sep 17 00:00:00 2001
From: jiang-dawei15 <jiangdawei15@huawei.com>
Date: Mon, 28 Feb 2022 16:27:22 +0800
Subject: [PATCH 3/3] libcare-patch-make: adapt libcare-patch-make to meson

Signed-off-by: Bihong Yu <yubihong@huawei.com>
---
 src/libcare-patch-make | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/src/libcare-patch-make b/src/libcare-patch-make
index 320ab88..ad1b566 100755
--- a/src/libcare-patch-make
+++ b/src/libcare-patch-make
@@ -53,6 +53,10 @@ prepare_env() {
 	LPMAKE_PATCHED_DIR="${LPMAKE_PATCHED_DIR-$PWD/.lpmaketmp/patched}"
 	LPMAKE_PATCHROOT="${LPMAKE_PATCHROOT-$PWD/patchroot}"
 
+        LIBCARE_CC=$CC
+        SYMBOLINK_CC=$(which cc)
+        REAL_CC=$(realpath $SYMBOLINK_CC)
+
 	export LPMAKE_ORIGINAL_DIR LPMAKE_PATCHED_DIR LPMAKE_PATCHROOT
 	mkdir -p "$LPMAKE_ORIGINAL_DIR" "$LPMAKE_PATCHED_DIR" "$LPMAKE_PATCHROOT"
 
@@ -79,6 +83,26 @@ restore_origs() {
 
 trap "restore_origs" 0
 
+change_qemu_ld_flags() {
+        qemu_ld_flags=$(grep "^QEMU_LDFLAGS=" config-host.mak)
+        ret=$(echo $qemu_ld_flags | grep "\-Wl,-q")
+        if [[ "$ret" == "" ]];then
+                qemu_ld_flags="${qemu_ld_flags} -Wl,-q"
+                echo "change QEMU_LDFLAGS to '${qemu_ld_flags}'"
+                sed -i "/^QEMU_LDFLAGS=/c\\${qemu_ld_flags}" config-host.mak
+        fi
+}
+
+replace_cc_symbolink() {
+        unlink $SYMBOLINK_CC
+        ln -s $LIBCARE_CC $SYMBOLINK_CC
+}
+
+recover_cc_symbolink() {
+        unlink $SYMBOLINK_CC
+        ln -s $REAL_CC $SYMBOLINK_CC
+}
+
 build_objects() {
 	restore_origs
 
@@ -93,6 +117,9 @@ build_objects() {
 	export KPATCH_STAGE=original
 	export KPCC_DBGFILTER_ARGS=""
 
+        #add '-Wl,-q' to LD_FLAGS
+        change_qemu_ld_flags
+
 	echo "${green}BUILDING ORIGINAL CODE${reset}"
 	make $LPMAKEFILE $JOBS_MAKE >$MAKE_OUTPUT 2>&1
 
@@ -237,10 +264,16 @@ main() {
 
 	prepare_env
 
+        # replace cc
+        replace_cc_symbolink
+
 	if test -z "$only_update"; then
 		build_objects "$@"
 	fi
 	build_kpatches
+
+        # recover cc
+        recover_cc_symbolink
 }
 
 main "$@"
-- 
2.23.0.windows.1

