From d86044aa709617186fa4eeb2dff9540395f77c8e Mon Sep 17 00:00:00 2001
From: ctyunsystem <ctyuncommiter05@chinatelecom.cn>
Date: Tue, 5 Jul 2022 10:13:15 +0800
Subject: [PATCH 1/2] Revert "fix probably restore cc symbol link fail when
 kill patch building uncourteous" ==> Revert "libcare-patch-make: fix some
 bugs"

---
 src/libcare-patch-make | 45 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 41 insertions(+), 4 deletions(-)

diff --git a/src/libcare-patch-make b/src/libcare-patch-make
index 41e5926..03aa1d6 100755
--- a/src/libcare-patch-make
+++ b/src/libcare-patch-make
@@ -83,6 +83,32 @@ restore_origs() {
 
 trap "restore_origs" 0
 
+replace_qemu_ld_flags() {
+	local qemu_ld_flags_old=$1
+	ret=$(echo $qemu_ld_flags_old | grep "\-Wl,-q")
+	if [[ "$ret" == "" ]]; then
+		local qemu_ld_flags="${qemu_ld_flags_old} -Wl,-q"
+		echo "replace QEMU_LDFLAGS to '${qemu_ld_flags}'"
+		sed -i "/^QEMU_LDFLAGS=/c\\${qemu_ld_flags}" config-host.mak
+	fi
+}
+
+recover_qemu_ld_flags() {
+	local qemu_ld_flags=$1
+	echo "recover QEMU_LDFLAGS to '${qemu_ld_flags}'"
+	sed -i "/^QEMU_LDFLAGS=/c\\${qemu_ld_flags}" config-host.mak
+}
+
+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
 
@@ -98,8 +124,7 @@ build_objects() {
 	export KPCC_DBGFILTER_ARGS=""
 
 	echo "${green}BUILDING ORIGINAL CODE${reset}"
-	local lp_make_env_original="CC=${CC}"
-	make $LPMAKEFILE $JOBS_MAKE ${lp_make_env_original} >$MAKE_OUTPUT 2>&1
+	make $LPMAKEFILE $JOBS_MAKE >$MAKE_OUTPUT 2>&1
 
 	echo "${green}INSTALLING ORIGINAL OBJECTS INTO $LPMAKE_ORIGINAL_DIR${reset}"
 	make $LPMAKEFILE $JOBS_MAKE install			\
@@ -124,14 +149,20 @@ build_objects() {
 	export KPATCH_STAGE=patched
 	export KPCC_APPEND_ARGS="-Wl,-q"
 
+	qemu_ld_flags_bak=$(grep "^QEMU_LDFLAGS=" config-host.mak)
+	#add '-Wl,-q' to LD_FLAGS
+	replace_qemu_ld_flags "$qemu_ld_flags_bak"
+
 	echo "${green}BUILDING PATCHED CODE${reset}"
-	local lp_make_env_patched="CC=${CC}"
-	make $LPMAKEFILE $JOBS_MAKE ${lp_make_env_patched} >$MAKE_OUTPUT 2>&1
+	make $LPMAKEFILE $JOBS_MAKE >$MAKE_OUTPUT 2>&1
 
 	echo "${green}INSTALLING PATCHED OBJECTS INTO $LPMAKE_PATCHED_DIR${reset}"
 	make $LPMAKEFILE $JOBS_MAKE install			\
 		"$destdir=$LPMAKE_PATCHED_DIR"			\
 		>$MAKE_OUTPUT 2>&1
+
+	# recover LD_FLAGS
+	recover_qemu_ld_flags "$qemu_ld_flags_bak"
 }
 
 build_kpatches() {
@@ -243,10 +274,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.27.0

