115 lines
3.0 KiB
CMake
115 lines
3.0 KiB
CMake
cmake_minimum_required(VERSION 3.16)
|
|
|
|
project(cxxLibrary VERSION 1.0.1 LANGUAGES CXX)
|
|
set(CMAKE_CXX_STANDARD 17)
|
|
|
|
option(CXXLIBRARY_UTF8 "Add /utf-8 compile option for MSVC" ON)
|
|
option(CXXLIBRARY_TEST "Use test mode" OFF)
|
|
|
|
if (MSVC)
|
|
add_compile_definitions(-D_WIN32_WINNT=0x0601)
|
|
endif()
|
|
if(MSVC AND CXXLIBRARY_UTF8)
|
|
add_compile_options(/utf-8)
|
|
endif()
|
|
|
|
set(SOURCES
|
|
src/tinyxml2.cpp
|
|
)
|
|
|
|
file(GLOB_RECURSE BOOST_SOURCES
|
|
src/boost-src/*.cpp
|
|
src/boost-src/*.hpp
|
|
src/boost-src/*.h
|
|
)
|
|
file(GLOB_RECURSE ZOOST_SOURCES
|
|
src/zoost-src/zt.common.cpp
|
|
src/zoost-src/zt.utils.cpp
|
|
src/zoost-src/zt.mem.cpp
|
|
)
|
|
if(WIN32)
|
|
list(APPEND ZOOST_SOURCES
|
|
src/zoost-src/zt.os_win.cpp
|
|
)
|
|
endif()
|
|
|
|
add_library(cxxLibrary STATIC ${SOURCES} ${BOOST_SOURCES} ${ZOOST_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"
|
|
)
|
|
|
|
if (DEFINED CXXLIBRARY_TEST AND CXXLIBRARY_TEST)
|
|
message(STATUS "USE CXXLIBRARY_TEST ${CXXLIBRARY_TEST}")
|
|
include(CTest)
|
|
enable_testing()
|
|
add_subdirectory(test)
|
|
endif()
|
|
|
|
# 修正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
|
|
)
|