project(OPSQLite)
cmake_minimum_required(VERSION 3.9.0)

set (PACKAGE_NAME "op-sqlite")

if (USE_SQLCIPHER)
  # Put ../cpp/sqlcipher ahead of ../cpp on the include path so the SQLCipher
  # header (which exposes sqlite3_key_v2 and friends) wins over the plain
  # sqlite3.h that also lives in ../cpp. Quoted #include search has its own
  # rules, but with this ordering both quoted and angle-bracket forms resolve
  # to the SQLCipher header under USE_SQLCIPHER; without it the SQLCipher
  # build was empirically picking up the plain header.
  include_directories(../cpp/sqlcipher)
endif()

include_directories(
  ../cpp
)

if (USE_LIBSQL)
  include_directories(src/main/jniLibs/include)
endif()

if (USE_TURSO)
  include_directories(src/main/tursoLibs/include)
endif()

# Apply library-default SQLite flags (driven by package.json toggles such as
# performanceMode, fts5 and rtree) FIRST, then user-supplied sqliteFlags. clang's
# last-define-wins rule lets the user override any default when both define the
# same macro. This mirrors iOS, where the podspec appends sqliteFlags after the
# optimizedCflags. Use add_compile_options (not add_definitions) because CMake
# sorts COMPILE_DEFINITIONS alphabetically before emitting them, which would
# break the override ordering for pairs like -DSQLITE_FOO=1 vs -DSQLITE_FOO=0.
separate_arguments(DEFAULT_SQLITE_FLAGS_LIST UNIX_COMMAND "${DEFAULT_SQLITE_FLAGS}")
separate_arguments(SQLITE_FLAGS_LIST UNIX_COMMAND "${SQLITE_FLAGS}")

add_compile_options(${DEFAULT_SQLITE_FLAGS_LIST})
add_compile_options(${SQLITE_FLAGS_LIST})

add_library(
  ${PACKAGE_NAME}
  SHARED
  ../cpp/OPSqlite.cpp
  ../cpp/utils.cpp
  ../cpp/OPThreadPool.cpp
  ../cpp/SmartHostObject.cpp
  ../cpp/PreparedStatementHostObject.cpp
  ../cpp/DumbHostObject.cpp
  ../cpp/DBHostObject.cpp
  cpp-adapter.cpp
)

if (USE_SQLCIPHER)
  target_sources(${PACKAGE_NAME} PRIVATE ../cpp/sqlcipher/sqlite3.h ../cpp/sqlcipher/sqlite3.c ../cpp/bridge.cpp ../cpp/bridge.hpp)

  add_definitions(
    -DOP_SQLITE_USE_SQLCIPHER=1
    -DSQLITE_HAS_CODEC
    -DSQLITE_TEMP_STORE=3
    -DSQLITE_EXTRA_INIT=sqlcipher_extra_init
    -DSQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown
  )

  find_package(openssl REQUIRED CONFIG)
elseif (USE_LIBSQL)
  target_sources(${PACKAGE_NAME} PRIVATE ../cpp/libsql/bridge.cpp)

  add_definitions(
    -DOP_SQLITE_USE_LIBSQL=1
  )
elseif (USE_TURSO)
  target_sources(${PACKAGE_NAME} PRIVATE ../cpp/turso_bridge.cpp)

  add_definitions(
    -DOP_SQLITE_USE_TURSO=1
  )
else()
 target_sources(${PACKAGE_NAME} PRIVATE ../cpp/sqlite3.c ../cpp/bridge.cpp)
endif()

if (USE_CRSQLITE)
  add_definitions(
    -DOP_SQLITE_USE_CRSQLITE=1
  )
endif()

if (USE_SQLITE_VEC)
  add_definitions(
    -DOP_SQLITE_USE_SQLITE_VEC=1
  )
endif()

find_package(ReactAndroid REQUIRED CONFIG)
find_package(fbjni REQUIRED CONFIG)
find_library(LOG_LIB log)

# Add user defined files (the generated tokenizers)
if (USER_DEFINED_SOURCE_FILES)
  target_sources(${PACKAGE_NAME} PRIVATE ${USER_DEFINED_SOURCE_FILES})

  add_definitions("-DTOKENIZERS_HEADER_PATH=\"${USER_DEFINED_TOKENIZERS_HEADER_PATH}\"")
endif()

if (USE_SQLCIPHER)
  target_link_libraries(
    ${PACKAGE_NAME}
    ${LOG_LIB}
    ReactAndroid::reactnative
    ReactAndroid::jsi
    fbjni::fbjni
    openssl::crypto
  )
elseif (USE_LIBSQL)
  cmake_path(SET LIBSQL_PATH ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libsql_experimental.so NORMALIZE)
  add_library(libsql_experimental SHARED IMPORTED)
  set_target_properties(libsql_experimental PROPERTIES
    IMPORTED_LOCATION ${LIBSQL_PATH}
    IMPORTED_NO_SONAME TRUE
  )

  target_link_libraries(
    ${PACKAGE_NAME}
    libsql_experimental
    ${LOG_LIB}
    ReactAndroid::reactnative
    ReactAndroid::jsi
    fbjni::fbjni
  )
elseif (USE_TURSO)
  cmake_path(SET TURSO_PATH ${CMAKE_CURRENT_SOURCE_DIR}/src/main/tursoLibs/${ANDROID_ABI}/libturso_sdk_kit.so NORMALIZE)
  add_library(turso_sdk_kit SHARED IMPORTED)
  set_target_properties(turso_sdk_kit PROPERTIES
    IMPORTED_LOCATION ${TURSO_PATH}
    IMPORTED_NO_SONAME TRUE
  )

  target_link_libraries(
    ${PACKAGE_NAME}
    turso_sdk_kit
    ${LOG_LIB}
    ReactAndroid::reactnative
    ReactAndroid::jsi
    fbjni::fbjni
  )
else ()
  target_link_libraries(
    ${PACKAGE_NAME}
    ${LOG_LIB}
    ReactAndroid::reactnative
    ReactAndroid::jsi
    fbjni::fbjni
  )
endif()
