cmake_minimum_required(VERSION 3.16)

project(cxxLibrary VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)

set(SOURCES
    src/tinyxml2.cpp
)

file(GLOB_RECURSE BOOST_SOURCES
    src/boost-src/*.cpp
    src/boost-src/*.hpp
    src/boost-src/*.h
)

add_library(cxxLibrary STATIC ${SOURCES} ${BOOST_SOURCES})

# 修正1：使用相对路径，避免绝对路径警告
target_include_directories(cxxLibrary PUBLIC 
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
)

if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_SYSTEM_NAME MATCHES "Windows")
    target_link_libraries(cxxLibrary PUBLIC ws2_32 bcrypt ntdll)
endif()

target_compile_definitions(cxxLibrary PUBLIC
    BOOST_ALL_NO_LIB
    BOOST_ALL_NO_LIBRARY
    BOOST_FILESYSTEM_NO_CXX20_ATOMIC_REF
)

set_target_properties(cxxLibrary PROPERTIES 
    DEBUG_POSTFIX "d"
    # 可选：设置其他配置的后缀
    # RELEASE_POSTFIX ""
    # RELWITHDEBINFO_POSTFIX "rd"
    # MINSIZEREL_POSTFIX "s"
)

# 修正2：添加头文件安装
# 如果include目录中有头文件，需要单独安装它们
install(DIRECTORY include/
    DESTINATION include
    FILES_MATCHING PATTERN "*.h"
    PATTERN "*.hpp" PATTERN "*.ipp"
)

# 安装目标
install(TARGETS cxxLibrary
    EXPORT cxxLibraryTargets           # 用于生成导出文件
    ARCHIVE DESTINATION lib            # 静态库安装到lib目录
    LIBRARY DESTINATION lib            # 动态库安装到lib目录
    RUNTIME DESTINATION bin            # Windows的DLL
    INCLUDES DESTINATION include       # 头文件安装位置
)

# 安装导出文件
install(EXPORT cxxLibraryTargets
    FILE cxxLibraryTargets.cmake
    NAMESPACE cxxLibrary::             # 添加命名空间
    DESTINATION lib/cmake/cxxLibrary
)

include(CMakePackageConfigHelpers)

# 生成配置文件
configure_package_config_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cxxLibrary.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cxxLibraryConfig.cmake"
    INSTALL_DESTINATION lib/cmake/cxxLibrary
)

# 生成版本文件
write_basic_package_version_file(
    "${CMAKE_CURRENT_BINARY_DIR}/cxxLibraryConfigVersion.cmake"
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY SameMajorVersion
)

# 安装生成的文件
install(FILES
    "${CMAKE_CURRENT_BINARY_DIR}/cxxLibraryConfig.cmake"
    "${CMAKE_CURRENT_BINARY_DIR}/cxxLibraryConfigVersion.cmake"
    DESTINATION lib/cmake/cxxLibrary
)