Skip to content

Commit

Permalink
build(cmake): support install & find_package()
Browse files Browse the repository at this point in the history
  • Loading branch information
Codesire-Deng committed Jun 22, 2023
1 parent 12335d6 commit f604c85
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ project(co_context

include(cmake/Policy.cmake NO_POLICY_SCOPE)

add_library(co_context OBJECT)
target_include_directories(co_context PUBLIC include)
add_library(co_context STATIC)
add_library(co_context::co_context ALIAS co_context)
target_include_directories(co_context
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include>"
)

include(cmake/Option.cmake)
include(cmake/CompileOption.cmake)
include(cmake/Platform.cmake)
include(cmake/Develop.cmake)
include(cmake/Extra.cmake)
include(cmake/Install.cmake)

add_subdirectory(lib)
8 changes: 5 additions & 3 deletions cmake/CompileOption.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ string(REGEX MATCH "^([0-9]+)" kernel_version_major ${kernel_version})
string(REGEX REPLACE "^([0-9]+)\\." "" kernel_version_minor ${kernel_version})
message(STATUS "kernel_version_major = ${kernel_version_major}")
message(STATUS "kernel_version_minor = ${kernel_version_minor}")
add_compile_definitions(LIBURINGCXX_KERNEL_VERSION_MAJOR=${kernel_version_major})
add_compile_definitions(LIBURINGCXX_KERNEL_VERSION_MINOR=${kernel_version_minor})
target_compile_definitions(co_context
PUBLIC "$<BUILD_INTERFACE:LIBURINGCXX_KERNEL_VERSION_MAJOR=${kernel_version_major}>"
PUBLIC "$<BUILD_INTERFACE:LIBURINGCXX_KERNEL_VERSION_MINOR=${kernel_version_minor}>"
)
unset(kernel_version)

# Optional IPO/LTO.
Expand Down Expand Up @@ -58,7 +60,7 @@ endif()

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(co_context PRIVATE Threads::Threads)
target_link_libraries(co_context PUBLIC Threads::Threads)

if (USE_MIMALLOC)
target_link_libraries(co_context PUBLIC mimalloc)
Expand Down
40 changes: 40 additions & 0 deletions cmake/Install.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
include(GNUInstallDirs)

install(TARGETS co_context
EXPORT co_context_targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(DIRECTORY "${co_context_SOURCE_DIR}/include/co_context"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(DIRECTORY "${co_context_SOURCE_DIR}/include/uring"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(EXPORT co_context_targets
FILE co_context_targets.cmake
NAMESPACE co_context::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/co_context
)

include(CMakePackageConfigHelpers)

configure_package_config_file(${co_context_SOURCE_DIR}/cmake/templates/Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/co_context-config.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/co_context
)

write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/co_context-config-version.cmake"
COMPATIBILITY SameMinorVersion)

install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/co_context-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/co_context-config-version.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/co_context
)
21 changes: 21 additions & 0 deletions cmake/templates/Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)
find_dependency(Threads REQUIRED)
find_dependency(mimalloc QUIET)

# Get the linux kernel version to liburingcxx
message(STATUS "target_kernel_version = ${CMAKE_SYSTEM_VERSION}")
set(kernel_version ${CMAKE_SYSTEM_VERSION})
string(REGEX MATCH "^([0-9]+)\.([0-9]+)" kernel_version ${kernel_version})
string(REGEX MATCH "^([0-9]+)" kernel_version_major ${kernel_version})
string(REGEX REPLACE "^([0-9]+)\\." "" kernel_version_minor ${kernel_version})
message(STATUS "kernel_version_major = ${kernel_version_major}")
message(STATUS "kernel_version_minor = ${kernel_version_minor}")
add_compile_definitions(LIBURINGCXX_KERNEL_VERSION_MAJOR=${kernel_version_major})
add_compile_definitions(LIBURINGCXX_KERNEL_VERSION_MINOR=${kernel_version_minor})
unset(kernel_version)

include("${CMAKE_CURRENT_LIST_DIR}/co_context_targets.cmake")

check_required_components(co_context)

0 comments on commit f604c85

Please sign in to comment.