Skip to content

Commit

Permalink
[compile] Added CMake install(), building shared/static libraries, ex…
Browse files Browse the repository at this point in the history
…port Windows Symbols (#444)
  • Loading branch information
sheldonrobinson authored Nov 4, 2024
1 parent f1b8ff5 commit cf91f2b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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_)
Expand Down
32 changes: 30 additions & 2 deletions cmake/CGraph-env-include.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
Expand All @@ -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()

0 comments on commit cf91f2b

Please sign in to comment.