From 5a96e1a9a5116917969afdd7adebfa895ff42617 Mon Sep 17 00:00:00 2001
From: Yaroslav Gurov <ygurov@proton.me>
Date: Mon, 30 Mar 2026 17:38:11 +0200
Subject: [PATCH] fix cloak go build

---
 cloak.cmake | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)

diff --git a/cloak.cmake b/cloak.cmake
index 8199ebc..d8ef1cb 100644
--- a/cloak.cmake
+++ b/cloak.cmake
@@ -9,36 +9,32 @@ list(APPEND CMAKE_PROGRAM_PATH "/usr/local/go/bin")
 find_program(GO_EXEC go)
 
 set(BUILD_CMD_ARGS build)
-list(APPEND BUILD_CMD_ARGS -buildmode=c-shared -o ${CMAKE_CURRENT_BINARY_DIR}/${CLOAK_LIB} ${CMAKE_GO_FLAGS} ./...)
-
-set(PREPARE_ENV_ARGS env)
-list(APPEND PREPARE_ENV_ARGS -w CGO_ENABLED=1 GOOS=android)
-
+list(APPEND BUILD_CMD_ARGS -buildmode=c-shared -v -o ${CMAKE_CURRENT_BINARY_DIR}/${CLOAK_LIB} ./...)
 
 string(REGEX MATCH "[0-9]+$" ANDROID_API_LEVEL ${ANDROID_PLATFORM})
 message(WARNING "build cloak plugin abi=${ANDROID_ABI}, ANDROID_API_LEVEL=${ANDROID_API_LEVEL}")
 
+list(APPEND GO_ENV CGO_ENABLED=1 GOOS=android)
+list(APPEND GO_ENV "CC=${CMAKE_C_COMPILER} --target=${CMAKE_C_COMPILER_TARGET}")
 if ("${ANDROID_ABI}" STREQUAL "x86")
-	list(APPEND PREPARE_ENV_ARGS GOARCH=386)
-    list(APPEND PREPARE_ENV_ARGS CC=${ANDROID_TOOLCHAIN_ROOT}/bin/i686-linux-android${ANDROID_API_LEVEL}-clang)
+	list(APPEND GO_ENV GOARCH=386)
 elseif ("${ANDROID_ABI}" STREQUAL "x86_64")
-	list(APPEND PREPARE_ENV_ARGS GOARCH=amd64)
-    list(APPEND PREPARE_ENV_ARGS CC=${ANDROID_TOOLCHAIN_ROOT}/bin/x86_64-linux-android${ANDROID_API_LEVEL}-clang)
+	list(APPEND GO_ENV GOARCH=amd64)
 elseif ("${ANDROID_ABI}" STREQUAL "arm64-v8a")
-	list(APPEND PREPARE_ENV_ARGS GOARCH=arm64)
-    list(APPEND PREPARE_ENV_ARGS CC=${ANDROID_TOOLCHAIN_ROOT}/bin/aarch64-linux-android${ANDROID_API_LEVEL}-clang)
+	list(APPEND GO_ENV GOARCH=arm64)
 elseif ("${ANDROID_ABI}" STREQUAL "armeabi-v7a")
-	list(APPEND PREPARE_ENV_ARGS GOARCH=arm)
-	list(APPEND PREPARE_ENV_ARGS GOARM=7)
-    list(APPEND PREPARE_ENV_ARGS CC=${ANDROID_TOOLCHAIN_ROOT}/bin/armv7a-linux-androideabi${ANDROID_API_LEVEL}-clang)
+	list(APPEND GO_ENV GOARCH=arm)
+	list(APPEND GO_ENV GOARM=7)
 endif ()
+string(REPLACE -mthumb -marm GO_C_FLAGS ${CMAKE_C_FLAGS})
+list(APPEND GO_ENV "CGO_CFLAGS=${GO_C_FLAGS}")
+list(APPEND GO_ENV "CGO_LDFLAGS=${GO_C_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS}")
 
 add_custom_command(
 		OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${CLOAK_LIB}
 		DEPENDS ${CLOAK_SRCS}
 		WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/cloak/cmd/ck-ovpn-plugin
-		COMMAND ${GO_EXEC} ${PREPARE_ENV_ARGS}
-		COMMAND ${GO_EXEC} ${BUILD_CMD_ARGS}
+		COMMAND ${CMAKE_COMMAND} -E env ${GO_ENV} ${GO_EXEC} ${BUILD_CMD_ARGS}
 		COMMENT "Building Go library")
 
 add_custom_target(${TARGET} DEPENDS ${CLOAK_LIB} ${HEADER})
-- 
2.50.1 (Apple Git-155)

