diff --git a/.bazelrc b/.bazelrc index 59f854dc8..2ee414f72 100644 --- a/.bazelrc +++ b/.bazelrc @@ -188,6 +188,12 @@ build:apple-toolchain --apple_crosstool_top=@local_config_apple_cc//:toolchain build:apple-toolchain --crosstool_top=@local_config_apple_cc//:toolchain build:apple-toolchain --host_crosstool_top=@local_config_apple_cc//:toolchain +# Wheel builds need the LiteRT runtime dylib linked into python extensions. +# This config overrides the macos default above. +build:macos_wheel --config=macos +build:macos_wheel --define=resolve_symbols_in_exec=false +build:macos_wheel --define=litert_macos_wheel_use_so=true + # Settings for MacOS on ARM CPUs. build:macos_arm64 --config=macos build:macos_arm64 --cpu=darwin_arm64 diff --git a/ci/build_pip_package_with_bazel.sh b/ci/build_pip_package_with_bazel.sh index 1106af854..36c5cbf90 100755 --- a/ci/build_pip_package_with_bazel.sh +++ b/ci/build_pip_package_with_bazel.sh @@ -19,6 +19,7 @@ set -ex export TF_LOCAL_SOURCE_PATH=${TF_LOCAL_SOURCE_PATH:-"$(pwd)/third_party/tensorflow"} ARCH="$(uname -m)" +OS_NAME="$(uname -s)" TENSORFLOW_TARGET=${TENSORFLOW_TARGET:-$1} if [ "${TENSORFLOW_TARGET}" = "rpi" ]; then export TENSORFLOW_TARGET="armhf" @@ -59,6 +60,11 @@ case "${TENSORFLOW_TARGET}" in ;; esac +if [[ "${OS_NAME}" == "Darwin" ]]; then + # Ensure LiteRT runtime dylib is built and linked for macOS wheels. + BAZEL_FLAGS="${BAZEL_FLAGS} --config=macos_wheel" +fi + if [[ -n "${BAZEL_CONFIG_FLAGS}" ]]; then BAZEL_FLAGS="${BAZEL_FLAGS} ${BAZEL_CONFIG_FLAGS}" fi diff --git a/litert/c/BUILD b/litert/c/BUILD index 624b040dd..20bbfb597 100644 --- a/litert/c/BUILD +++ b/litert/c/BUILD @@ -650,7 +650,11 @@ cc_test( # TODO: b/437381008 cc_library( name = "litert_runtime_c_api_so_shim", - linkopts = ["-Wl,--undefined-glob=LiteRt*"], + linkopts = select({ + "@platforms//os:macos": [], + "@platforms//os:ios": [], + "//conditions:default": ["-Wl,--undefined-glob=LiteRt*"], + }), deps = LITERT_C_API_COMMON_DEPS, ) @@ -659,9 +663,10 @@ cc_shared_library( name = "litert_runtime_c_api_so", additional_linker_inputs = export_lrt_runtime_only_script(), shared_lib_name = "libLiteRt.so", - user_link_flags = export_lrt_runtime_only_linkopt() + [ - "-Wl,-soname=libLiteRt.so", - ] + litert_android_linkopts(), + user_link_flags = export_lrt_runtime_only_linkopt() + select({ + "@platforms//os:macos": ["-Wl,-install_name,@rpath/libLiteRt.so"], + "//conditions:default": ["-Wl,-soname=libLiteRt.so"], + }) + litert_android_linkopts(), deps = [":litert_runtime_c_api_so_shim"], ) @@ -747,7 +752,11 @@ exports_files(srcs = ["windows_exported_symbols.def"]) # TODO: b/437381008 cc_library( name = "litert_tflite_runtime_c_api_so_shim", - linkopts = ["-Wl,--undefined-glob=LiteRt*"], + linkopts = select({ + "@platforms//os:macos": [], + "@platforms//os:ios": [], + "//conditions:default": ["-Wl,--undefined-glob=LiteRt*"], + }), deps = LITERT_C_API_COMMON_DEPS + [ "//tflite/c:c_api", "//tflite/c:c_api_experimental", @@ -771,7 +780,8 @@ cc_shared_library( SELECT_LITERT_RUNTIME_C_API_SO = select({ ":resolve_symbols_in_exec": [], "@platforms//os:ios": [":litert_runtime_c_api_dylib"], - "@platforms//os:macos": [":litert_runtime_c_api_dylib"], + ":litert_macos_wheel_use_so": [":litert_runtime_c_api_so"], + ":litert_macos_default": [":litert_runtime_c_api_dylib"], "@platforms//os:windows": [], "//conditions:default": [":litert_runtime_c_api_so"], }) @@ -790,6 +800,18 @@ config_setting( define_values = {"resolve_symbols_in_exec": "true"}, ) +config_setting( + name = "litert_macos_wheel_use_so", + constraint_values = ["@platforms//os:macos"], + define_values = {"litert_macos_wheel_use_so": "true"}, +) + +config_setting( + name = "litert_macos_default", + constraint_values = ["@platforms//os:macos"], + define_values = {"litert_macos_wheel_use_so": "false"}, +) + filegroup( name = "c_api_headers", srcs = glob(["litert_*.h"]),