# This file is part of Desktop App Toolkit,
# a set of libraries for developing nice desktop applications.
#
# For license and copyright information please follow this link:
# https://github.com/desktop-app/legal/blob/master/LEGAL

add_library(external_wallet_engine STATIC)
add_library(desktop-app::external_wallet_engine ALIAS external_wallet_engine)

init_target(external_wallet_engine "(external)")

if (DESKTOP_APP_USE_PACKAGED OR LINUX)
    if (NOT TARGET desktop-app::external_tdesktop_rust)
        find_library(DESKTOP_APP_WALLET_ENGINE_LIBRARY wallet_engine REQUIRED)
        target_link_libraries(external_wallet_engine PUBLIC "${DESKTOP_APP_WALLET_ENGINE_LIBRARY}")
    endif()
    find_path(DESKTOP_APP_WALLET_ENGINE_INCLUDE_DIR wallet_engine.hpp PATH_SUFFIXES wallet_engine REQUIRED)
    set(wallet_engine_include "${DESKTOP_APP_WALLET_ENGINE_INCLUDE_DIR}")
    set(wallet_engine_source "${DESKTOP_APP_WALLET_ENGINE_INCLUDE_DIR}/wallet_engine.cpp")
elseif (WIN32)
    set(wallet_engine_loc ${libs_loc}/tdesktop_rust/out)
    set(wallet_engine_include ${wallet_engine_loc}/include/wallet_engine)
    set(wallet_engine_source ${wallet_engine_loc}/src/wallet_engine/wallet_engine.cpp)
elseif (APPLE)
    set(wallet_engine_include ${libs_loc}/local/include/wallet_engine)
    set(wallet_engine_source ${libs_loc}/local/src/wallet_engine/wallet_engine.cpp)
endif()

target_sources(external_wallet_engine
PRIVATE
    ${wallet_engine_source}
)

# The binding is third-party code generated by UniFFI and its
# std::move-in-return pattern trips this repository's -Werror policy. The flag
# MUST ride on the source file: a target's own PRIVATE COMPILE_OPTIONS are
# ordered BEFORE the linked-library INTERFACE_COMPILE_OPTIONS, so
# common_options' -Wall would come last and re-enable the warning.
if (NOT MSVC)
    set_source_files_properties(${wallet_engine_source} PROPERTIES
        COMPILE_OPTIONS -Wno-pessimizing-move
    )
endif()

# Not SYSTEM, for the same reason as the bundled MicroTeX include paths:
# clang's -MMD leaves system headers out of generated dependency files, so a
# SYSTEM wallet_engine.hpp never reaches the .d files and an engine update
# leaves objects compiled against the previous binding silently retained. The
# generated source's own warning relaxation rides on the file above, so the
# ordinary path costs nothing here.
target_include_directories(external_wallet_engine
PUBLIC
    ${wallet_engine_include}
)

target_link_libraries(external_wallet_engine
PUBLIC
    $<TARGET_NAME_IF_EXISTS:desktop-app::external_tdesktop_rust>
)
