cmake: Build bpf

This commit is contained in:
Tatsuhiro Tsujikawa 2021-08-31 15:26:39 +09:00
parent a87ea20b7c
commit f0d1e50d5a
5 changed files with 70 additions and 0 deletions

View File

@ -64,6 +64,7 @@ find_package(ZLIB 1.2.3)
find_package(Libngtcp2 0.0.0) find_package(Libngtcp2 0.0.0)
find_package(Libngtcp2_crypto_openssl 0.0.0) find_package(Libngtcp2_crypto_openssl 0.0.0)
find_package(Libnghttp3 0.0.0) find_package(Libnghttp3 0.0.0)
find_package(Libbpf 0.4.0)
if(OPENSSL_FOUND AND LIBEV_FOUND AND ZLIB_FOUND) if(OPENSSL_FOUND AND LIBEV_FOUND AND ZLIB_FOUND)
set(ENABLE_APP_DEFAULT ON) set(ENABLE_APP_DEFAULT ON)
else() else()
@ -235,6 +236,20 @@ if(ENABLE_ASIO_LIB)
find_package(Boost 1.54.0 REQUIRED system thread) find_package(Boost 1.54.0 REQUIRED system thread)
endif() endif()
# libbpf (for bpf)
set(HAVE_LIBBPF ${LIBBPF_FOUND})
if(LIBBPF_FOUND)
set(BPFCFLAGS -Wall -O2 -g)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
# For Debian/Ubuntu
set(EXTRABPFCFLAGS -I/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu)
endif()
check_c_source_compiles("
#include <linux/bpf.h>
int main() { enum bpf_stats_type foo; (void)foo; }" HAVE_BPF_STATS_TYPE)
endif()
# The nghttp, nghttpd and nghttpx under src depend on zlib, OpenSSL and libev # The nghttp, nghttpd and nghttpx under src depend on zlib, OpenSSL and libev
if(ENABLE_APP AND NOT (ZLIB_FOUND AND OPENSSL_FOUND AND LIBEV_FOUND)) if(ENABLE_APP AND NOT (ZLIB_FOUND AND OPENSSL_FOUND AND LIBEV_FOUND))
message(FATAL_ERROR "Applications were requested (ENABLE_APP=1) but dependencies are not met.") message(FATAL_ERROR "Applications were requested (ENABLE_APP=1) but dependencies are not met.")
@ -492,6 +507,7 @@ add_subdirectory(integration-tests)
add_subdirectory(doc) add_subdirectory(doc)
add_subdirectory(contrib) add_subdirectory(contrib)
add_subdirectory(script) add_subdirectory(script)
add_subdirectory(bpf)
string(TOUPPER "${CMAKE_BUILD_TYPE}" _build_type) string(TOUPPER "${CMAKE_BUILD_TYPE}" _build_type)
@ -525,6 +541,7 @@ message(STATUS "summary of build options:
Libngtcp2: ${HAVE_LIBNGTCP2} (LIBS='${LIBNGTCP2_LIBRARIES}') Libngtcp2: ${HAVE_LIBNGTCP2} (LIBS='${LIBNGTCP2_LIBRARIES}')
Libngtcp2_crypto_openssl: ${HAVE_LIBNGTCP2_CRYPTO_OPENSSL} (LIBS='${LIBNGTCP2_CRYPTO_OPENSSL_LIBRARIES}') Libngtcp2_crypto_openssl: ${HAVE_LIBNGTCP2_CRYPTO_OPENSSL} (LIBS='${LIBNGTCP2_CRYPTO_OPENSSL_LIBRARIES}')
Libnghttp3: ${HAVE_LIBNGHTTP3} (LIBS='${LIBNGHTTP3_LIBRARIES}') Libnghttp3: ${HAVE_LIBNGHTTP3} (LIBS='${LIBNGHTTP3_LIBRARIES}')
Libbpf: ${HAVE_LIBBPF} (LIBS='${LIBBPF_LIBRARIES}')
Libevent(SSL): ${HAVE_LIBEVENT_OPENSSL} (LIBS='${LIBEVENT_OPENSSL_LIBRARIES}') Libevent(SSL): ${HAVE_LIBEVENT_OPENSSL} (LIBS='${LIBEVENT_OPENSSL_LIBRARIES}')
Jansson: ${HAVE_JANSSON} (LIBS='${JANSSON_LIBRARIES}') Jansson: ${HAVE_JANSSON} (LIBS='${JANSSON_LIBRARIES}')
Jemalloc: ${HAVE_JEMALLOC} (LIBS='${JEMALLOC_LIBRARIES}') Jemalloc: ${HAVE_JEMALLOC} (LIBS='${JEMALLOC_LIBRARIES}')

13
bpf/CMakeLists.txt Normal file
View File

@ -0,0 +1,13 @@
if(LIBBPF_FOUND)
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/reuseport_kern.o"
COMMAND ${CMAKE_C_COMPILER} ${BPFCFLAGS} ${EXTRABPFCFLAGS} -I${LIBBPF_INCLUDE_DIRS} -target bpf -c reuseport_kern.c -o "${CMAKE_CURRENT_BINARY_DIR}/reuseport_kern.o"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM)
add_custom_target(bpf ALL
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/reuseport_kern.o"
VERBATIM)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/reuseport_kern.o"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/${CMAKE_PROJECT_NAME}")
endif()

32
cmake/FindLibbpf.cmake Normal file
View File

@ -0,0 +1,32 @@
# - Try to find libbpf
# Once done this will define
# LIBBPF_FOUND - System has libbpf
# LIBBPF_INCLUDE_DIRS - The libbpf include directories
# LIBBPF_LIBRARIES - The libraries needed to use libbpf
find_package(PkgConfig QUIET)
pkg_check_modules(PC_LIBBPF QUIET libbpf)
find_path(LIBBPF_INCLUDE_DIR
NAMES bpf/bpf.h
HINTS ${PC_LIBBPF_INCLUDE_DIRS}
)
find_library(LIBBPF_LIBRARY
NAMES bpf
HINTS ${PC_LIBBPF_LIBRARY_DIRS}
)
include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set LIBBPF_FOUND
# to TRUE if all listed variables are TRUE and the requested version
# matches.
find_package_handle_standard_args(Libbpf REQUIRED_VARS
LIBBPF_LIBRARY LIBBPF_INCLUDE_DIR
VERSION_VAR LIBBPF_VERSION)
if(LIBBPF_FOUND)
set(LIBBPF_LIBRARIES ${LIBBPF_LIBRARY})
set(LIBBPF_INCLUDE_DIRS ${LIBBPF_INCLUDE_DIR})
endif()
mark_as_advanced(LIBBPF_INCLUDE_DIR LIBBPF_LIBRARY)

View File

@ -81,3 +81,9 @@
/* Define to 1 if HTTP/3 is enabled. */ /* Define to 1 if HTTP/3 is enabled. */
#cmakedefine ENABLE_HTTP3 1 #cmakedefine ENABLE_HTTP3 1
/* Define to 1 if you have `libbpf` library. */
#cmakedefine HAVE_LIBBPF 1
/* Define to 1 if you have enum bpf_stats_type in linux/bpf.h. */
#cmakedefine HAVE_BPF_STATS_TYPE 1

View File

@ -22,6 +22,7 @@ include_directories(
${LIBCARES_INCLUDE_DIRS} ${LIBCARES_INCLUDE_DIRS}
${JANSSON_INCLUDE_DIRS} ${JANSSON_INCLUDE_DIRS}
${ZLIB_INCLUDE_DIRS} ${ZLIB_INCLUDE_DIRS}
${LIBBPF_INCLUDE_DIRS}
) )
# XXX per-target? # XXX per-target?
@ -38,6 +39,7 @@ link_libraries(
${JANSSON_LIBRARIES} ${JANSSON_LIBRARIES}
${ZLIB_LIBRARIES} ${ZLIB_LIBRARIES}
${APP_LIBRARIES} ${APP_LIBRARIES}
${LIBBPF_LIBRARIES}
) )
if(ENABLE_APP) if(ENABLE_APP)