Skip to content

Commit

Permalink
export cmake option when developer use CMake FetchContent
Browse files Browse the repository at this point in the history
For example, use the following cmake config
```
include(FetchContent)
FetchContent_Declare(
        yalantinglibs
        GIT_REPOSITORY https://github.com/alibaba/yalantinglibs.git
        GIT_TAG xxx # use the least commit id
        GIT_SHALLOW 1 # optional ( --depth=1 )
)
FetchContent_MakeAvailable(yalantinglibs)
add_executable(demo main.cpp)
target_link_libraries(demo PRIVATE yalantinglibs)
```

you can use `cmake -DENABLE_STRUCT_PACK_UNPORTABLE_TYPE=ON ..`,
the option `ENABLE_STRUCT_PACK_UNPORTABLE_TYPE=ON` will be used when building yalantinglibs
  • Loading branch information
PikachuHyA committed Feb 4, 2024
1 parent a920fce commit c0e48cf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ if(CMAKE_PROJECT_NAME STREQUAL "yaLanTingLibs") # if ylt is top-level project
include(cmake/config.cmake)
# add project's source such as unit test, example & benchmark
include(cmake/subdir.cmake)
else ()
# add project config, such as enable_ssl.
include(cmake/config.cmake)
endif()

45 changes: 36 additions & 9 deletions cmake/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,70 @@ option(ENABLE_SSL "Enable ssl support" OFF)
message(STATUS "ENABLE_SSL: ${ENABLE_SSL}")
if (ENABLE_SSL)
find_package(OpenSSL REQUIRED)
add_compile_definitions(YLT_ENABLE_SSL)
link_libraries(OpenSSL::SSL OpenSSL::Crypto)
if(CMAKE_PROJECT_NAME STREQUAL "yaLanTingLibs")
add_compile_definitions(YLT_ENABLE_SSL)
link_libraries(OpenSSL::SSL OpenSSL::Crypto)
else ()
target_compile_definitions(yalantinglibs INTERFACE YLT_ENABLE_SSL)
target_link_libraries(yalantinglibs INTERFACE OpenSSL::SSL OpenSSL::Crypto)
endif ()
endif ()

option(ENABLE_PMR "Enable pmr support" OFF)
message(STATUS "ENABLE_PMR: ${ENABLE_PMR}")
if (ENABLE_PMR)
add_compile_definitions(YLT_ENABLE_PMR IGUANA_ENABLE_PMR)
if(CMAKE_PROJECT_NAME STREQUAL "yaLanTingLibs")
add_compile_definitions(YLT_ENABLE_PMR IGUANA_ENABLE_PMR)
else ()
target_compile_definitions(yalantinglibs INTERFACE YLT_ENABLE_PMR IGUANA_ENABLE_PMR)
endif ()
endif ()

option(ENABLE_IO_URING "Enable io_uring" OFF)
message(STATUS "ENABLE_IO_URING: ${ENABLE_IO_URING}")
if (ENABLE_IO_URING)
find_package(uring REQUIRED)
message(STATUS "Use IO_URING for all I/O in linux")
add_compile_definitions(ASIO_HAS_IO_URING ASIO_DISABLE_EPOLL ASIO_HAS_FILE YLT_ENABLE_FILE_IO_URING)
link_libraries(uring)
if(CMAKE_PROJECT_NAME STREQUAL "yaLanTingLibs")
add_compile_definitions(ASIO_HAS_IO_URING ASIO_DISABLE_EPOLL ASIO_HAS_FILE YLT_ENABLE_FILE_IO_URING)
link_libraries(uring)
else ()
target_compile_definitions(yalantinglibs INTERFACE ASIO_HAS_IO_URING ASIO_DISABLE_EPOLL ASIO_HAS_FILE YLT_ENABLE_FILE_IO_URING)
target_link_libraries(yalantinglibs INTERFACE uring)
endif ()
endif()

option(ENABLE_FILE_IO_URING "Enable file io_uring" OFF)
if (NOT ENABLE_IO_URING)
if(ENABLE_FILE_IO_URING)
find_package(uring REQUIRED)
message(STATUS "Enable io_uring for file I/O in linux")
add_compile_definitions(ASIO_HAS_IO_URING ASIO_HAS_FILE YLT_ENABLE_FILE_IO_URING)
link_libraries(uring)
if(CMAKE_PROJECT_NAME STREQUAL "yaLanTingLibs")
add_compile_definitions(ASIO_HAS_IO_URING ASIO_HAS_FILE YLT_ENABLE_FILE_IO_URING)
link_libraries(uring)
else ()
target_compile_definitions(yalantinglibs INTERFACE ASIO_HAS_IO_URING ASIO_HAS_FILE YLT_ENABLE_FILE_IO_URING)
target_link_libraries(yalantinglibs INTERFACE uring)
endif ()
endif()
endif()

option(ENABLE_STRUCT_PACK_UNPORTABLE_TYPE "enable struct_pack unportable type(like wchar_t)" OFF)
message(STATUS "ENABLE_STRUCT_PACK_UNPORTABLE_TYPE: ${ENABLE_STRUCT_PACK_UNPORTABLE_TYPE}")
if(ENABLE_STRUCT_PACK_UNPORTABLE_TYPE)
add_compile_definitions(STRUCT_PACK_ENABLE_UNPORTABLE_TYPE)
if(CMAKE_PROJECT_NAME STREQUAL "yaLanTingLibs")
add_compile_definitions(STRUCT_PACK_ENABLE_UNPORTABLE_TYPE)
else ()
target_compile_definitions(yalantinglibs INTERFACE STRUCT_PACK_ENABLE_UNPORTABLE_TYPE)
endif ()
endif()

option(ENABLE_STRUCT_PACK_OPTIMIZE "enable struct_pack optimize(but cost more compile time)" OFF)
message(STATUS "ENABLE_STRUCT_PACK_OPTIMIZE: ${ENABLE_STRUCT_PACK_OPTIMIZE}")
if(ENABLE_STRUCT_PACK_OPTIMIZE)
add_compile_definitions(ENABLE_STRUCT_PACK_OPTIMIZE)
if(CMAKE_PROJECT_NAME STREQUAL "yaLanTingLibs")
add_compile_definitions(ENABLE_STRUCT_PACK_OPTIMIZE)
else ()
target_compile_definitions(yalantinglibs INTERFACE ENABLE_STRUCT_PACK_OPTIMIZE)
endif ()
endif()

0 comments on commit c0e48cf

Please sign in to comment.