Skip to content

Commit

Permalink
feat: ➕ switch to boost asio; update CMakeLists.txt/config; update CI
Browse files Browse the repository at this point in the history
BREAKING CHANGES: Boost is now required as a dependency, which will be bundled if not found.
  • Loading branch information
jontitorr committed Dec 24, 2023
1 parent 8d7b81e commit e47a467
Show file tree
Hide file tree
Showing 44 changed files with 1,363 additions and 822 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ jobs:
- name: Install dependencies
run: sudo apt update && sudo apt install -y cmake ${{ matrix.gcc-version }} libssl-dev ninja-build rpm zlib1g-dev

- name: Install boost
uses: MarkusJx/install-boost@v2.4.4
id: install-boost
with:
boost_version: 1.81.0

- name: Configure CMake
run: cmake -S . -B build -G Ninja
env:
BOOST_ROOT: ${{ steps.install-boost.outputs.BOOST_ROOT }}
CXX: ${{ matrix.gcc-version }}

- name: Build
Expand Down
196 changes: 129 additions & 67 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,134 @@ endif()

include(cmake/CPM.cmake)

cpmfindpackage(NAME "fmt" GITHUB_REPOSITORY "fmtlib/fmt" GIT_TAG "13156e5")
file(GLOB_RECURSE sources CONFIGURE_DEPENDS "include/*.hpp" "src/*.cpp")

add_library(${PROJECT_NAME} ${sources})
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})

target_compile_definitions(
${PROJECT_NAME}
PUBLIC
$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:${PROJECT_NAME_UPPERCASE}_STATIC_DEFINE>
)

target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17)
target_compile_options(
${PROJECT_NAME} PUBLIC "$<$<COMPILE_LANG_AND_ID:CXX,MSVC>:/permissive->"
"$<$<PLATFORM_ID:Windows>:/bigobj>"
)

target_include_directories(
${PROJECT_NAME}
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/export>
$<INSTALL_INTERFACE:include>
)

if(WIN32)
if(MSVC)
target_compile_definitions(
fmt PUBLIC _SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING
${PROJECT_NAME}
PUBLIC -D_WIN32_WINNT=0x0601
-D_SCL_SECURE_NO_WARNINGS=1
-D_CRT_SECURE_NO_WARNINGS=1
-D_SILENCE_CXX17_ALLOCATOR_VOID_DEPRECATION_WARNING
-D_SILENCE_CXX17_ADAPTOR_TYPEDEFS_DEPRECATION_WARNING
)
endif()

set(${PROJECT_NAME}_WARNINGS_AS_ERRORS ON)
include(cmake/CompilerWarnings.cmake)

set_project_warnings(${PROJECT_NAME})

set(TRY_BOOST_VERSION "1.81.0")
set(BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
"coroutine;filesystem;thread;url"
)
set(BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED "asio;beast;outcome")

set(IS_BOOST_LOCAL OFF)
if(${CPM_LOCAL_PACKAGES_ONLY})
message(STATUS "Trying to find Boost...")
find_package(
Boost ${TRY_BOOST_VERSION} REQUIRED
COMPONENTS ${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}
)
set(IS_BOOST_LOCAL ON)
elseif(${CPM_USE_LOCAL_PACKAGES} OR NOT ${CPM_DOWNLOAD_ALL})
message(STATUS "Trying to use local Boost...")
find_package(
Boost ${TRY_BOOST_VERSION}
COMPONENTS ${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}
)
if(${BOOST_FOUND})
set(IS_BOOST_LOCAL ON)
message(DEBUG "boost include dir: ${Boost_INCLUDE_DIRS}")
endif()
endif()

if(NOT (${BOOST_FOUND}) OR (NOT DEFINED BOOST_FOUND))
message(STATUS "Trying to download Boost...")
set(BOOST_INCLUDE_LIBRARIES
"${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED};${BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
)
set(BOOST_SKIP_INSTALL_RULES OFF)
cpmaddpackage(
NAME
Boost
URL
"https://github.com/Xminent/boost-cmake/releases/download/1.84.0-release/boost-1.84.0.tar.xz"
)
set(IS_BOOST_LOCAL OFF)
endif()

cpmfindpackage(
NAME
"net"
"OpenSSL"
GITHUB_REPOSITORY
"Xminent/net"
"Xminent/openssl-cmake"
GIT_TAG
"main"
"master"
OPTIONS
"BUILD_SHARED_LIBS OFF"
"WITH_APPS OFF"
)

if(OpenSSL_ADDED)
add_library(OpenSSL::Crypto ALIAS crypto)
add_library(OpenSSL::SSL ALIAS ssl)
install(TARGETS crypto ssl EXPORT ${PROJECT_NAME}Targets)
else()
set(SEPARATE_DEPENDENCIES "OpenSSL;SSL;")
endif()

cpmfindpackage(NAME "fmt" GITHUB_REPOSITORY "fmtlib/fmt" GIT_TAG "13156e5")

if(fmt_ADDED)
install(TARGETS fmt EXPORT ${PROJECT_NAME}Targets)

if(WIN32)
target_compile_definitions(
fmt PUBLIC _SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING
)
endif()
else()
set(SEPARATE_DEPENDENCIES "${SEPARATE_DEPENDENCIES}fmt;;")
endif()

cpmaddpackage(NAME certify GITHUB_REPOSITORY "Xminent/certify" GIT_TAG "master")

set(SEPARATE_DEPENDENCIES
"${SEPARATE_DEPENDENCIES}Threads;;Boost;coroutine,date_time,filesystem,system,thread,url"
)

if(${IS_BOOST_LOCAL})
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::boost)
else()
target_link_libraries(
${PROJECT_NAME} PUBLIC Boost::asio Boost::beast Boost::outcome
)
endif()

cpmaddpackage(
NAME
"nlohmann_json"
Expand All @@ -84,51 +193,14 @@ target_include_directories(
$<INSTALL_INTERFACE:include>
)

find_package(ZLIB)

file(GLOB_RECURSE sources CONFIGURE_DEPENDS "include/*.hpp" "src/*.cpp")

add_library(${PROJECT_NAME} ${sources})

include(GenerateExportHeader)

generate_export_header(
${PROJECT_NAME} EXPORT_FILE_NAME export/${PROJECT_NAME_LOWERCASE}/export.h
)

target_compile_definitions(
${PROJECT_NAME}
PUBLIC
$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:${PROJECT_NAME_UPPERCASE}_STATIC_DEFINE>
)
install(TARGETS core nlohmann_json EXPORT ${PROJECT_NAME}Targets)

target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17)
target_include_directories(
${PROJECT_NAME}
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/export>
$<INSTALL_INTERFACE:include>
)

set(${PROJECT_NAME}_TARGETS ${PROJECT_NAME} nlohmann_json)
set(SEPARATE_DEPENDENCIES "")

if(fmt_ADDED)
list(APPEND ${PROJECT_NAME}_TARGETS fmt)
else()
list(APPEND SEPARATE_DEPENDENCIES "fmt")
endif()

if(net_ADDED)
list(APPEND ${PROJECT_NAME}_TARGETS net)
else()
list(APPEND SEPARATE_DEPENDENCIES "net")
endif()
find_package(ZLIB)

if(ZLIB_FOUND)
list(APPEND SEPARATE_DEPENDENCIES "ZLIB")
set(SEPARATE_DEPENDENCIES "${SEPARATE_DEPENDENCIES}ZLIB;;")
else()
cpmfindpackage(
cpmaddpackage(
NAME
"ZLIB"
GITHUB_REPOSITORY
Expand All @@ -140,33 +212,23 @@ else()
"ZLIB_ENABLE_TESTS OFF"
"SKIP_INSTALL_ALL ON"
)

add_library(ZLIB::ZLIB ALIAS zlib)
list(APPEND ${PROJECT_NAME}_TARGETS zlib)
install(TARGETS zlib EXPORT ${PROJECT_NAME}Targets)
endif()

target_link_libraries(
${PROJECT_NAME}
PUBLIC fmt::fmt net::net nlohmann_json
PRIVATE ZLIB::ZLIB
PUBLIC certify::core Boost::coroutine Boost::thread fmt::fmt nlohmann_json
PRIVATE Boost::url ZLIB::ZLIB
)
# target_precompile_headers( ${PROJECT_NAME} PRIVATE
# "${nlohmann_json_SOURCE_DIR}/include/nlohmann/json.hpp" )

find_program(CLANG_TIDY_EXE NAMES "clang-tidy")

if(CLANG_TIDY_EXE)
# set(CLANG_TIDY_COMMAND "${CLANG_TIDY_EXE}"
# "-checks=bugprone-*,cert-*,clang-analyzer-*,concurrency-*,cppcoreguidelines-*,google-build-using-namespace,google-explicit-constructor,google-global-names-in-headers,google-readability-casting,google-runtime-int,google-runtime-operator,hicpp-*,llvm-*,misc-*,modernize-*,performance-*,portability-*,readability-*,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-hicpp-signed-bitwise,-hicpp-uppercase-literal-suffix,-hicpp-vararg,-llvm-header-guard,-modernize-use-trailing-return-type,-readability-identifier-length,-readability-uppercase-literal-suffix,-cppcoreguidelines-avoid-const-or-ref-data-members,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-pro-type-reinterpret-cast,-misc-non-private-member-variables-in-classes"
# ) set_target_properties( ${PROJECT_NAME} PROPERTIES CXX_CLANG_TIDY
# "${CLANG_TIDY_COMMAND}" )
include(GenerateExportHeader)

message(
STATUS
"clang-tidy found: ${CLANG_TIDY_EXE} and applied to ${PROJECT_NAME}."
)
else()
message(STATUS "clang-tidy not found.")
endif()
generate_export_header(
${PROJECT_NAME} EXPORT_FILE_NAME export/${PROJECT_NAME_LOWERCASE}/export.h
)

if(${PROJECT_NAME}_BUILD_EXAMPLES)
add_subdirectory(examples)
Expand All @@ -176,7 +238,7 @@ if(${PROJECT_NAME}_INSTALL)
include(GNUInstallDirs)

install(
TARGETS ${${PROJECT_NAME}_TARGETS}
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
Expand Down
100 changes: 100 additions & 0 deletions cmake/CompilerWarnings.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# from here:
# https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md

function(set_project_warnings project_name)
set(MSVC_WARNINGS
/W4 # Baseline reasonable warnings
/w14242 # 'identifier': conversion from 'type1' to 'type1', possible
# loss of data
/w14254 # 'operator': conversion from 'type1:field_bits' to
# 'type2:field_bits', possible loss of data
/w14263 # 'function': member function does not override any base class
# virtual member function
/w14265 # 'classname': class has virtual functions, but destructor is
# not virtual instances of this class may not be destructed
# correctly
/w14287 # 'operator': unsigned/negative constant mismatch
/we4289 # nonstandard extension used: 'variable': loop control variable
# declared in the for-loop is used outside the for-loop scope
/w14296 # 'operator': expression is always 'boolean_value'
/w14311 # 'variable': pointer truncation from 'type1' to 'type2'
/w14545 # expression before comma evaluates to a function which is
# missing an argument list
/w14546 # function call before comma missing argument list
/w14547 # 'operator': operator before comma has no effect; expected
# operator with side-effect
/w14549 # 'operator': operator before comma has no effect; did you
# intend 'operator'?
/w14555 # expression has no effect; expected expression with side-
# effect
/w14619 # pragma warning: there is no warning number 'number'
/w14640 # Enable warning on thread un-safe static member initialization
/w14826 # Conversion from 'type1' to 'type_2' is sign-extended. This may
# cause unexpected runtime behavior.
/w14905 # wide string literal cast to 'LPSTR'
/w14906 # string literal cast to 'LPWSTR'
/w14928 # illegal copy-initialization; more than one user-defined
# conversion has been implicitly applied
/permissive- # standards conformance mode for MSVC compiler.
)

set(CLANG_WARNINGS
-Wall
-Wextra # reasonable and standard
-Wshadow # warn the user if a variable declaration shadows one from a
# parent context
-Wnon-virtual-dtor # warn the user if a class with virtual functions has
# a non-virtual destructor. This helps catch hard to
# track down memory errors
-Wold-style-cast # warn for c-style casts
-Wcast-align # warn for potential performance problem casts
-Wunused # warn on anything being unused
-Woverloaded-virtual # warn if you overload (not override) a virtual
# function
-Wpedantic # warn if non-standard C++ is used
-Wconversion # warn on type conversions that may lose data
-Wsign-conversion # warn on sign conversions
-Wnull-dereference # warn if a null dereference is detected
-Wdouble-promotion # warn if float is implicit promoted to double
-Wformat=2 # warn on security issues around functions that format output
# (ie printf)
)

if(${PROJECT_NAME}_WARNINGS_AS_ERRORS)
set(CLANG_WARNINGS ${CLANG_WARNINGS} -Werror)
set(MSVC_WARNINGS ${MSVC_WARNINGS} /WX)
endif()

set(GCC_WARNINGS
${CLANG_WARNINGS}
-Wmisleading-indentation # warn if indentation implies blocks where
# blocks do not exist
-Wduplicated-cond # warn if if / else chain has duplicated conditions
-Wduplicated-branches # warn if if / else branches have duplicated code
-Wlogical-op # warn about logical operations being used where bitwise
# were probably wanted
-Wuseless-cast # warn if you perform a cast to the same type
)

if(MSVC)
set(PROJECT_WARNINGS ${MSVC_WARNINGS})
elseif(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
set(PROJECT_WARNINGS ${CLANG_WARNINGS})
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(PROJECT_WARNINGS ${GCC_WARNINGS})
else()
message(
AUTHOR_WARNING
"No compiler warnings set for '${CMAKE_CXX_COMPILER_ID}' compiler."
)
endif()

if(NOT TARGET ${project_name})
message(
AUTHOR_WARNING
"${project_name} is not a target, thus no compiler warning were added."
)
endif()

target_compile_options(${project_name} INTERFACE ${PROJECT_WARNINGS})
endfunction()
21 changes: 18 additions & 3 deletions cmake/Config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,26 @@ set(@PROJECT_NAME@_VERSION @PROJECT_VERSION@)

set_and_check(@PROJECT_NAME@_INCLUDE_DIR "@CMAKE_INSTALL_FULL_INCLUDEDIR@")

set(_sep_deps "@SEPARATE_DEPENDENCIES@")
list(LENGTH _sep_deps _sep_deps_len)
math(EXPR len "${_sep_deps_len} - 1")

include(CMakeFindDependencyMacro)

foreach(_dep @SEPARATE_DEPENDENCIES@)
find_dependency(${_dep} REQUIRED)
get_filename_component(_BOOST_CMAKEDIR "${CMAKE_CURRENT_LIST_DIR}/../" ABSOLUTE)
list(APPEND CMAKE_PREFIX_PATH ${_BOOST_CMAKEDIR})

foreach(idx RANGE 0 ${len} 2)
list(GET _sep_deps ${idx} package_name)
math(EXPR components_idx "${idx} + 1")
list(GET _sep_deps ${components_idx} components)
string(REPLACE "," ";" components_list "${components}")
if(components_list)
find_dependency(${package_name} COMPONENTS ${components_list} REQUIRED)
else()
find_dependency(${package_name} REQUIRED)
endif()
endforeach()

include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
check_required_components(@PROJECT_NAME@)
check_required_components(@PROJECT_NAME@)
Loading

0 comments on commit e47a467

Please sign in to comment.