Skip to content

Commit

Permalink
cmake: Add jemalloc support
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuhiro-t committed Sep 27, 2022
1 parent 242f517 commit b4f7810
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 16 deletions.
40 changes: 24 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ foreach(_build_type "Release" "MinSizeRel" "RelWithDebInfo")
endforeach()
endforeach()

include(CMakePushCheckState)
include(ExtractValidFlags)

if(NOT CMAKE_C_COMPILER_ID MATCHES "MSVC")
if(ENABLE_ASAN)
cmake_push_check_state()
set(CMAKE_REQUIRED_LIBRARIES "-fsanitize=address")
check_c_compiler_flag(-fsanitize=address C__fsanitize_address_VALID)
check_cxx_compiler_flag(-fsanitize=address CXX__fsanitize_address_VALID)
cmake_pop_check_state()
if(NOT C__fsanitize_address_VALID OR NOT CXX__fsanitize_address_VALID)
message(WARNING "ENABLE_ASAN was requested, but not supported!")
else()
set(CMAKE_C_FLAGS "-fsanitize=address ${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "-fsanitize=address ${CMAKE_CXX_FLAGS}")
endif()
endif()
endif()

if(ENABLE_GNUTLS)
find_package(GnuTLS 3.7.2)
Expand All @@ -104,6 +122,9 @@ endif()
if(ENABLE_WOLFSSL)
find_package(wolfssl 5.5.0)
endif()
if(ENABLE_JEMALLOC)
find_package(Jemalloc)
endif()
find_package(Libev 4.11)
find_package(Libnghttp3 0.0.0)
find_package(CUnit 2.1)
Expand All @@ -115,7 +136,6 @@ endif()

# OpenSSL (required for libngtcp2_crypto_openssl,
# libngtcp2_crypto_picotls and examples)
include(CMakePushCheckState)
include(CheckSymbolExists)
if(ENABLE_OPENSSL AND OPENSSL_FOUND)
set(VANILLA_OPENSSL_INCLUDE_DIRS ${OPENSSL_INCLUDE_DIR})
Expand Down Expand Up @@ -166,6 +186,8 @@ else()
set(BORINGSSL_LIBRARIES "")
endif()

# jemalloc
set(HAVE_JEMALLOC ${JEMALLOC_FOUND})
# libev (required for examples)
set(HAVE_LIBEV ${LIBEV_FOUND})
# libnghttp3 (required for examples)
Expand Down Expand Up @@ -251,7 +273,6 @@ endif()

check_symbol_exists(bswap_64 "byteswap.h" HAVE_BSWAP_64)

include(ExtractValidFlags)
set(WARNCFLAGS)
set(WARNCXXFLAGS)
if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
Expand Down Expand Up @@ -328,20 +349,6 @@ else()
# long as all source files are compiled with the same compiler.
-Wno-noexcept-type
)

if(ENABLE_ASAN)
cmake_push_check_state()
set(CMAKE_REQUIRED_LIBRARIES "-fsanitize=address")
check_c_compiler_flag(-fsanitize=address C__fsanitize_address_VALID)
check_cxx_compiler_flag(-fsanitize=address CXX__fsanitize_address_VALID)
cmake_pop_check_state()
if(NOT C__fsanitize_address_VALID OR NOT CXX__fsanitize_address_VALID)
message(WARNING "ENABLE_ASAN was requested, but not supported!")
else()
set(CMAKE_C_FLAGS "-fsanitize=address ${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "-fsanitize=address ${CMAKE_CXX_FLAGS}")
endif()
endif()
endif()

if(ENABLE_DEBUG)
Expand Down Expand Up @@ -420,4 +427,5 @@ message(STATUS "summary of build options:
BoringSSL: ${HAVE_BORINGSSL} (LIBS='${BORINGSSL_LIBRARIES}')
Picotls: ${HAVE_PICOTLS} (LIBS='${PICOTLS_LIBRARIES}')
wolfSSL: ${HAVE_WOLFSSL} (LIBS='${WOLFSSL_LIBRARIES}')
Jemalloc: ${HAVE_JEMALLOC} (LIBS='${JEMALLOC_LIBRARIES}')
")
1 change: 1 addition & 0 deletions CMakeOptions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ option(ENABLE_SHARED_LIB "Build as shared libraries" ON)
option(ENABLE_WERROR "Make compiler warnings fatal" OFF)
option(ENABLE_DEBUG "Turn on debug output" OFF)
option(ENABLE_ASAN "Enable AddressSanitizer (ASAN)" OFF)
option(ENABLE_JEMALLOC "Enable Jemalloc" OFF)

option(ENABLE_GNUTLS "Enable GnuTLS crypto backend" OFF)
option(ENABLE_OPENSSL "Enable OpenSSL crypto backend (required for examples)" ON)
Expand Down
40 changes: 40 additions & 0 deletions cmake/FindJemalloc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# - Try to find jemalloc
# Once done this will define
# JEMALLOC_FOUND - System has jemalloc
# JEMALLOC_INCLUDE_DIRS - The jemalloc include directories
# JEMALLOC_LIBRARIES - The libraries needed to use jemalloc

find_package(PkgConfig QUIET)
pkg_check_modules(PC_JEMALLOC QUIET jemalloc)

find_path(JEMALLOC_INCLUDE_DIR
NAMES jemalloc/jemalloc.h
HINTS ${PC_JEMALLOC_INCLUDE_DIRS}
)
find_library(JEMALLOC_LIBRARY
NAMES jemalloc
HINTS ${PC_JEMALLOC_LIBRARY_DIRS}
)

if(JEMALLOC_INCLUDE_DIR)
set(_version_regex "^#define[ \t]+JEMALLOC_VERSION[ \t]+\"([^\"]+)\".*")
file(STRINGS "${JEMALLOC_INCLUDE_DIR}/jemalloc/jemalloc.h"
JEMALLOC_VERSION REGEX "${_version_regex}")
string(REGEX REPLACE "${_version_regex}" "\\1"
JEMALLOC_VERSION "${JEMALLOC_VERSION}")
unset(_version_regex)
endif()

include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set JEMALLOC_FOUND to TRUE
# if all listed variables are TRUE and the requested version matches.
find_package_handle_standard_args(Jemalloc REQUIRED_VARS
JEMALLOC_LIBRARY JEMALLOC_INCLUDE_DIR
VERSION_VAR JEMALLOC_VERSION)

if(JEMALLOC_FOUND)
set(JEMALLOC_LIBRARIES ${JEMALLOC_LIBRARY})
set(JEMALLOC_INCLUDE_DIRS ${JEMALLOC_INCLUDE_DIR})
endif()

mark_as_advanced(JEMALLOC_INCLUDE_DIR JEMALLOC_LIBRARY)
10 changes: 10 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ if(LIBEV_FOUND AND HAVE_OPENSSL AND LIBNGHTTP3_FOUND)
${CMAKE_SOURCE_DIR}/third-party
${CMAKE_SOURCE_DIR}/crypto/includes

${JEMALLOC_INCLUDE_DIRS}
${OPENSSL_INCLUDE_DIRS}
${LIBEV_INCLUDE_DIRS}
${LIBNGHTTP3_INCLUDE_DIRS}
Expand All @@ -61,6 +62,7 @@ if(LIBEV_FOUND AND HAVE_OPENSSL AND LIBNGHTTP3_FOUND)
set(ossl_LIBS
ngtcp2_crypto_openssl
ngtcp2
${JEMALLOC_LIBRARIES}
${OPENSSL_LIBRARIES}
${LIBEV_LIBRARIES}
${LIBNGHTTP3_LIBRARIES}
Expand Down Expand Up @@ -118,6 +120,7 @@ if(LIBEV_FOUND AND HAVE_GNUTLS AND LIBNGHTTP3_FOUND)
${CMAKE_SOURCE_DIR}/third-party
${CMAKE_SOURCE_DIR}/crypto/includes

${JEMALLOC_INCLUDE_DIRS}
${GNUTLS_INCLUDE_DIRS}
${LIBEV_INCLUDE_DIRS}
${LIBNGHTTP3_INCLUDE_DIRS}
Expand All @@ -126,6 +129,7 @@ if(LIBEV_FOUND AND HAVE_GNUTLS AND LIBNGHTTP3_FOUND)
set(gtls_LIBS
ngtcp2_crypto_gnutls
ngtcp2
${JEMALLOC_LIBRARIES}
${GNUTLS_LIBRARIES}
${LIBEV_LIBRARIES}
${LIBNGHTTP3_LIBRARIES}
Expand Down Expand Up @@ -183,6 +187,7 @@ if(LIBEV_FOUND AND HAVE_BORINGSSL AND LIBNGHTTP3_FOUND)
${CMAKE_SOURCE_DIR}/third-party
${CMAKE_SOURCE_DIR}/crypto/includes

${JEMALLOC_INCLUDE_DIRS}
${BORINGSSL_INCLUDE_DIRS}
${LIBEV_INCLUDE_DIRS}
${LIBNGHTTP3_INCLUDE_DIRS}
Expand All @@ -191,6 +196,7 @@ if(LIBEV_FOUND AND HAVE_BORINGSSL AND LIBNGHTTP3_FOUND)
set(bssl_LIBS
ngtcp2_crypto_boringssl_static
ngtcp2
${JEMALLOC_LIBRARIES}
${BORINGSSL_LIBRARIES}
${LIBEV_LIBRARIES}
${LIBNGHTTP3_LIBRARIES}
Expand Down Expand Up @@ -248,6 +254,7 @@ if(LIBEV_FOUND AND HAVE_PICOTLS AND LIBNGHTTP3_FOUND)
${CMAKE_SOURCE_DIR}/third-party
${CMAKE_SOURCE_DIR}/crypto/includes

${JEMALLOC_INCLUDE_DIRS}
${PICOTLS_INCLUDE_DIRS}
${VANILLA_OPENSSL_INCLUDE_DIRS}
${LIBEV_INCLUDE_DIRS}
Expand All @@ -257,6 +264,7 @@ if(LIBEV_FOUND AND HAVE_PICOTLS AND LIBNGHTTP3_FOUND)
set(ptls_LIBS
ngtcp2_crypto_picotls_static
ngtcp2
${JEMALLOC_LIBRARIES}
${PICOTLS_LIBRARIES}
${VANILLA_OPENSSL_LIBRARIES}
${LIBEV_LIBRARIES}
Expand Down Expand Up @@ -315,6 +323,7 @@ if(LIBEV_FOUND AND HAVE_WOLFSSL AND LIBNGHTTP3_FOUND)
${CMAKE_SOURCE_DIR}/third-party
${CMAKE_SOURCE_DIR}/crypto/includes

${JEMALLOC_INCLUDE_DIRS}
${WOLFSSL_INCLUDE_DIRS}
${LIBEV_INCLUDE_DIRS}
${LIBNGHTTP3_INCLUDE_DIRS}
Expand All @@ -323,6 +332,7 @@ if(LIBEV_FOUND AND HAVE_WOLFSSL AND LIBNGHTTP3_FOUND)
set(wolfssl_LIBS
ngtcp2_crypto_wolfssl_static
ngtcp2
${JEMALLOC_LIBRARIES}
${WOLFSSL_LIBRARIES}
${LIBEV_LIBRARIES}
${LIBNGHTTP3_LIBRARIES}
Expand Down

0 comments on commit b4f7810

Please sign in to comment.