From 8889db96913366bf4d9b843fda2f1e2410dbc21e Mon Sep 17 00:00:00 2001 From: "Tomi P. Hakala" Date: Sun, 19 Jan 2025 18:54:53 +0200 Subject: [PATCH] fix: enhance Makefile for improved cross-platform library handling - Updated the TFLITE_LIB_DIR logic to prioritize Homebrew installation on macOS, falling back to the HOME directory if not found. - Added conditional CGO_LDFLAGS for Darwin to ensure proper linking of TensorFlow Lite libraries. - Improved the download-tflite target with clearer echo statements for file movements, enhancing user feedback during the build process. These changes ensure better compatibility and user experience when building on macOS and Linux. --- Makefile | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ae43222..949f623 100644 --- a/Makefile +++ b/Makefile @@ -54,6 +54,9 @@ define get_cgo_flags $(strip \ CGO_ENABLED=1 \ CGO_CFLAGS="-I$(HOME)/src/tensorflow" \ + $(if $(filter darwin%,$1), \ + CGO_LDFLAGS="-L$(TFLITE_LIB_DIR)" \ + ) \ $(if $(filter linux_arm64,$1), \ $(if $(filter x86_64,$(UNAME_M)), \ CC=aarch64-linux-gnu-gcc \ @@ -69,7 +72,12 @@ ifeq ($(UNAME_S),Linux) TFLITE_LIB_EXT := .so else ifeq ($(UNAME_S),Darwin) NATIVE_TARGET := darwin_$(if $(filter x86_64,$(UNAME_M)),amd64,arm64) - TFLITE_LIB_DIR := /usr/local/lib + # Try Homebrew location first, fall back to HOME directory + ifeq ($(wildcard /opt/homebrew/lib),) + TFLITE_LIB_DIR := $(HOME)/lib + else + TFLITE_LIB_DIR := /opt/homebrew/lib + endif TFLITE_LIB_EXT := .dylib else $(error Build is supported only on Linux and macOS) @@ -136,16 +144,19 @@ download-tflite: sudo mv ./tensorflowlite_c-$(patsubst v%,%,$(TFLITE_VERSION)).dll $(TFLITE_LIB_DIR)/; \ rm -f tensorflowlite_c-$(patsubst v%,%,$(TFLITE_VERSION)).dll; \ else \ + echo "Extracting $(TFLITE_C_FILE)..."; \ tar -xzf $(TFLITE_C_FILE) -C .; \ if [ "$(UNAME_S)" = "Darwin" ]; then \ if [ -f "$(TFLITE_LIB_DIR)/libtensorflowlite_c.dylib" ]; then \ sudo mv "$(TFLITE_LIB_DIR)/libtensorflowlite_c.dylib" "$(TFLITE_LIB_DIR)/libtensorflowlite_c.dylib.old"; \ fi; \ + echo "Moving libtensorflowlite_c.dylib to $(TFLITE_LIB_DIR)"; \ sudo mv libtensorflowlite_c.dylib $(TFLITE_LIB_DIR)/; \ else \ if [ -f "$(TFLITE_LIB_DIR)/libtensorflowlite_c.so" ]; then \ sudo mv "$(TFLITE_LIB_DIR)/libtensorflowlite_c.so" "$(TFLITE_LIB_DIR)/libtensorflowlite_c.so.old"; \ fi; \ + echo "Moving libtensorflowlite_c.so to $(TFLITE_LIB_DIR)"; \ sudo mv libtensorflowlite_c.so.$(patsubst v%,%,$(TFLITE_VERSION)) $(TFLITE_LIB_DIR)/; \ fi; \ fi; \