#
# Copyright (c) KylinSoft  Co., Ltd. 2024. All rights reserved.
#
# kaiming is licensed under the GPL v2.0+.
# 
# See the LICENSE file for more details.
#

set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")

pkg_search_module(elf_static REQUIRED IMPORTED_TARGET libelf)
# pkg_search_module(deflate_static REQUIRED IMPORTED_TARGET libdeflate)
pkg_search_module(lz4_static REQUIRED IMPORTED_TARGET liblz4)
pkg_search_module(lzma_static REQUIRED IMPORTED_TARGET liblzma)
pkg_search_module(zlib_static REQUIRED IMPORTED_TARGET zlib)
pkg_search_module(zstd_static REQUIRED IMPORTED_TARGET libzstd)
pkg_search_module(uuid_static REQUIRED IMPORTED_TARGET uuid)

# 查找库文件，V10 SP1中libdeflate-dev中没有.pc文件，不能用pkgconfig查找
find_library(FOUND_deflate_static NAMES deflate)
if("${FOUND_deflate_static}" STREQUAL "")
  message(FATAL_ERROR "libdeflate.a not fount")
endif()
get_filename_component(LIBDEFLATE_ABS_FILE ${FOUND_deflate_static} ABSOLUTE)
message(STATUS "found libdeflate.a: ${LIBDEFLATE_ABS_FILE}")

# 查找头文件路径
find_path(deflate_static_INCLUDE_DIRS NAMES libdeflate.h)
if (NOT deflate_static_INCLUDE_DIRS)
    message(FATAL_ERROR "Could not find libdeflate.h")
endif()
message(STATUS "Found libdeflate.h at: ${deflate_static_INCLUDE_DIRS}")

add_link_options(-static -static-libgcc -static-libstdc++)

set(ONLY_TO_TERMINAL 1)
configure_file(config.h.in config.h)

add_executable(ok-engine
    main.cpp
    KMOABApplication.h
    KMOABApplication.cpp
    KMOABContext.h
    KMOABElf.h
    KMOABElf.cpp
    KMOABExtract.h
    KMOABExtract.cpp
    KMOABPrintMeta.h
    KMOABPrintMeta.cpp
    KMOABUtils.h
    KMOABUtils.cpp
    KMOABRepackage.h
    KMOABRepackage.cpp
    KMOABPrintDataDir.h
    KMOABPrintDataDir.cpp
    KMOABMount.h
    KMOABMount.cpp
    KMOABRun.h
    KMOABRun.cpp
    ../../common/KMBuildinOptions.h
    ../../common/KMBuildinOptions.cpp
    ../../common/KMException.h
    ../../common/KMException.cpp
    ../../common/KMObject.h
    ../../common/KMObject.cpp
    ../../common/KMSingleton.h
    ../../common/KMSubCommand.h
    ../../common/KMSubCommand.cpp
    ../../common/KMDynamicCreator.h
    ../../common/KMDynamicCreator.cpp
    ../../common/KMOABMetadata.h
    ../../common/KMOABMetadata.cpp
    ../../common/KMStringUtils.h
    ../../common/KMStringUtils.cpp
    ../../common/KMJsonHelper.h
    ../../common/KMJsonHelper.cpp
    ../../common/KMInfoJson.h
    ../../common/KMInfoJson.cpp
    ../../common/KMLogger.h
    ../../common/KMLogger.cpp
    ../../common/KMConfigData.h
    ../../common/KMCrun.h
    ../../common/KMCrun.cpp
    ../../common/KMContainer.h
    ../../common/KMContainer.cpp
)

target_include_directories(ok-engine PRIVATE 
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/../../
    ${elf_static_INCLUDE_DIRS}
    ${deflate_static_INCLUDE_DIRS}
    ${lz4_static_INCLUDE_DIRS}
    ${lzma_static_INCLUDE_DIRS}
    ${zlib_static_INCLUDE_DIRS}
    ${zstd_static_INCLUDE_DIRS}
    ${uuid_static_INCLUDE_DIRS}
)

target_link_libraries(ok-engine PUBLIC
    # ${elf_static_LIBRARIES}
    PkgConfig::elf_static
    # PkgConfig::deflate_static
    ${LIBDEFLATE_ABS_FILE}
    PkgConfig::lz4_static
    PkgConfig::lzma_static
    PkgConfig::zlib_static
    PkgConfig::zstd_static
    PkgConfig::uuid_static
    stdc++fs
)

install( TARGETS ok-engine
    RUNTIME DESTINATION  /opt/kaiming-tools/bin
)
