From 91565886710f83062f0f2087a833282fc26add32 Mon Sep 17 00:00:00 2001 From: Sheldon Robinson Date: Sun, 3 Nov 2024 15:36:57 -0500 Subject: [PATCH] Added CMake install(), building shared/static libraries, export Windows Symbols --- CMakeLists.txt | 1 + cmake/CGraph-env-include.cmake | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 66af12d1..fd443911 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ set(CMAKE_CXX_STANDARD 11) option(CGRAPH_BUILD_FUNCTIONAL_TESTS "Enables builds of functional tests" OFF) option(CGRAPH_BUILD_PERFORMANCE_TESTS "Enables builds of performance tests" OFF) +option(BUILD_SHARED_LIBS "Build using shared libraries" ON) # 如果开启此宏定义,则CGraph执行过程中,不会在控制台打印任何信息 # add_definitions(-D_CGRAPH_SILENCE_) diff --git a/cmake/CGraph-env-include.cmake b/cmake/CGraph-env-include.cmake index 801fd0a7..192708e3 100644 --- a/cmake/CGraph-env-include.cmake +++ b/cmake/CGraph-env-include.cmake @@ -4,8 +4,16 @@ # 根据当前 CGraph-env-include.cmake 文件的位置,定位CGraph整体工程的路径 # 从而解决并兼容了直接引入和三方库引入的路径不匹配问题 get_filename_component(CGRAPH_PROJECT_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" DIRECTORY) -set(CGRAPH_PROJECT_ROOT_DIR "${CGRAPH_PROJECT_CMAKE_DIR}/../") + +set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables") +set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries") +set(INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "Installation directory for headers") +set(INSTALL_MAN_DIR "${CMAKE_INSTALL_PREFIX}/share/man" CACHE PATH "Installation directory for manual pages") +set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_PREFIX}/share/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files") + +set(CGRAPH_PROJECT_ROOT_DIR "${CGRAPH_PROJECT_CMAKE_DIR}/..") file(GLOB_RECURSE CGRAPH_PROJECT_SRC_LIST "${CGRAPH_PROJECT_ROOT_DIR}/src/*.cpp") +file(GLOB_RECURSE CGRAPH_PROJECT_HDR_LIST "${CGRAPH_PROJECT_ROOT_DIR}/src/*.h" "${CGRAPH_PROJECT_ROOT_DIR}/src/*.inl") IF(APPLE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -O2 \ @@ -17,6 +25,7 @@ ELSEIF(UNIX) add_definitions(-D_ENABLE_LIKELY_) ELSEIF(WIN32) IF(MSVC) + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS 1) # windows平台,加入utf-8设置。否则无法通过编译 add_definitions(/utf-8) add_compile_options("$<$:/utf-8>") @@ -33,6 +42,25 @@ ENDIF() include_directories(${CGRAPH_PROJECT_ROOT_DIR}/src/) # 直接加入"CGraph.h"文件对应的位置 # 以下三选一,本地编译执行,推荐OBJECT方式 -add_library(CGraph OBJECT ${CGRAPH_PROJECT_SRC_LIST}) # 通过代码编译 +# add_library(CGraph OBJECT ${CGRAPH_PROJECT_SRC_LIST}) # 通过代码编译 # add_library(CGraph SHARED ${CGRAPH_PROJECT_SRC_LIST}) # 编译libCGraph动态库 # add_library(CGraph STATIC ${CGRAPH_PROJECT_SRC_LIST}) # 编译libCGraph静态库 +if(BUILD_SHARED_LIBS) + add_library(CGraph SHARED ${CGRAPH_PROJECT_SRC_LIST}) +else() + add_library(CGraph STATIC ${CGRAPH_PROJECT_SRC_LIST}) +endif() + +if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL ) + install(TARGETS CGraph + RUNTIME DESTINATION "${INSTALL_BIN_DIR}" + ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" + LIBRARY DESTINATION "${INSTALL_LIB_DIR}" ) +endif() +if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL ) +foreach ( file ${CGRAPH_PROJECT_HDR_LIST} ) + get_filename_component( dir ${file} DIRECTORY ) + string(REGEX REPLACE "^${CGRAPH_PROJECT_ROOT_DIR}/src[/]?" "" output_path "${dir}") + install(FILES ${file} DESTINATION "${INSTALL_INC_DIR}/${output_path}" ) +endforeach() +endif()