From ea7475d09c65d6682f4f20edcf62ec8198335c33 Mon Sep 17 00:00:00 2001 From: Reuben Morais Date: Tue, 28 Apr 2020 12:54:33 +0200 Subject: [PATCH] Ugly, very ugly, incredibly ugly static linking of libsox on macOS All of the brew installed dependencies have static libraries as well, but the macOS linker will always prefer a dynamic library if both exist under the same `-L/foo -lbar` resolution. The only way to force static linking is to include a full path to the static library. These changes basically reverse engineer the static library locations and then pass those to the linker. --- native_client/Makefile | 3 +-- native_client/definitions.mk | 12 +++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/native_client/Makefile b/native_client/Makefile index 2b0d093e..98dcb8a0 100644 --- a/native_client/Makefile +++ b/native_client/Makefile @@ -18,10 +18,9 @@ default: $(DEEPSPEECH_BIN) clean: rm -f deepspeech -$(DEEPSPEECH_BIN): client.cc +$(DEEPSPEECH_BIN): client.cc Makefile $(CXX) $(CFLAGS) $(CFLAGS_DEEPSPEECH) $(SOX_CFLAGS) client.cc $(LDFLAGS) $(SOX_LDFLAGS) ifeq ($(OS),Darwin) - install_name_tool -change $$TASKCLUSTER_TASK_DIR/homebrew-builds/opt/sox/lib/libsox.3.dylib @rpath/libsox.3.dylib deepspeech install_name_tool -change bazel-out/local-opt/bin/native_client/libdeepspeech.so @rpath/libdeepspeech.so deepspeech endif diff --git a/native_client/definitions.mk b/native_client/definitions.mk index 85592956..41acf5f3 100644 --- a/native_client/definitions.mk +++ b/native_client/definitions.mk @@ -12,6 +12,8 @@ TOOL_LD := ld TOOL_LDD := ldd TOOL_LIBEXE := +OS := $(shell uname -s) + ifeq ($(findstring _NT,$(OS)),_NT) PLATFORM_EXE_SUFFIX := .exe endif @@ -30,7 +32,13 @@ SOX_CFLAGS := `pkg-config --cflags sox` ifeq ($(OS),Linux) SOX_CFLAGS += -fopenmp SOX_LDFLAGS := -Wl,-Bstatic `pkg-config --static --libs sox` -lgsm `pkg-config --static --libs libpng | cut -d' ' -f1` -lz -lmagic -lltdl -Wl,-Bdynamic -ldl -else # OS == Linux +else ifeq ($(OS),Darwin) +LIBSOX_PATH := $(shell echo `pkg-config --libs-only-L sox | sed -e 's/^-L//'`/lib`pkg-config --libs-only-l sox | sed -e 's/^-l//'`.dylib) +LIBOPUSFILE_PATH := $(shell echo `pkg-config --libs-only-L opusfile | sed -e 's/^-L//'`/lib`pkg-config --libs-only-l opusfile | sed -e 's/^-l//'`.dylib) +LIBSOX_STATIC_DEPS := $(shell echo `otool -L $(LIBSOX_PATH) | tail -n +2 | cut -d' ' -f1 | grep /opt/ | sed -E "s/\.[[:digit:]]+\.dylib/\.a/" | tr '\n' ' '`) +LIBOPUSFILE_STATIC_DEPS := $(shell echo `otool -L $(LIBOPUSFILE_PATH) | tail -n +2 | cut -d' ' -f1 | grep /opt/ | sed -E "s/\.[[:digit:]]+\.dylib/\.a/" | tr '\n' ' '`) +SOX_LDFLAGS := $(LIBSOX_STATIC_DEPS) $(LIBOPUSFILE_STATIC_DEPS) -framework CoreAudio -lz +else SOX_LDFLAGS := `pkg-config --libs sox` endif # OS others PYTHON_PACKAGES := numpy${NUMPY_BUILD_VERSION} @@ -93,8 +101,6 @@ NODE_PLATFORM_TARGET := --target_arch=arm64 --target_platform=linux TOOLCHAIN_LDD_OPTS := --root $(RASPBIAN)/ endif # ($(TARGET),rpi3-armv8) -OS := $(shell uname -s) - # -Wl,--no-as-needed is required to force linker not to evict libs it thinks we # dont need ; will fail the build on OSX because that option does not exists ifeq ($(OS),Linux)