cmake_minimum_required(VERSION 3.22.1)
project(stitch_needle LANGUAGES C CXX)

# Prebuilt Needle 2 engine from huggingface.co/Cactus-Compute/needle2 (android-arm64 / android-armv7).
# Fetched by scripts/fetch-needle.sh into needle/<ABI>/libneedle.a.
set(NEEDLE_LIB ${CMAKE_CURRENT_SOURCE_DIR}/needle/${ANDROID_ABI}/libneedle.a)

if(EXISTS ${NEEDLE_LIB})
    add_library(needle STATIC IMPORTED)
    set_target_properties(needle PROPERTIES IMPORTED_LOCATION ${NEEDLE_LIB})
    add_library(stitch_needle SHARED needle_jni.cpp)
    target_link_libraries(stitch_needle needle android log m)
else()
    # No Needle build for this CPU (e.g. x86_64 emulator): link a stub that reports "unavailable".
    add_library(stitch_needle SHARED needle_jni.cpp needle_stub.cpp)
    target_link_libraries(stitch_needle android log m)
endif()
target_include_directories(stitch_needle PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/needle)
