From 21507cd3e97bc5692d97201ee68df044c6767e9a Mon Sep 17 00:00:00 2001 From: Andrii Nakryiko Date: Thu, 7 Feb 2019 11:00:36 -0800 Subject: [PATCH] pahole: add libbpf as submodule under lib/bpf This change allows to use libbpf definitions and APIs from pahole. Signed-off-by: Andrii Nakryiko Cc: Alexei Starovoitov Cc: Martin KaFai Lau Cc: Yonghong Song Cc: dwarves@vger.kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- .gitmodules | 3 +++ CMakeLists.txt | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- lib/bpf | 1 + 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 .gitmodules create mode 160000 lib/bpf diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..6be99dc --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/bpf"] + path = lib/bpf + url = https://github.com/libbpf/libbpf diff --git a/CMakeLists.txt b/CMakeLists.txt index b41e91d..0cc383a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,15 +31,60 @@ add_definitions(-D_GNU_SOURCE -DDWARVES_VERSION="v1.12") find_package(DWARF REQUIRED) find_package(ZLIB REQUIRED) +# make sure git submodule(s) are checked out +find_package(Git QUIET) +if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git") + # Update submodules as needed + option(GIT_SUBMODULE "Check submodules during build" ON) + if(GIT_SUBMODULE) + message(STATUS "Submodule update") + execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + RESULT_VARIABLE GIT_SUBMOD_RESULT) + if(NOT GIT_SUBMOD_RESULT EQUAL "0") + message(FATAL_ERROR "git submodule update --init failed with ${GIT_SUBMOD_RESULT}, please checkout submodules") + else() + message(STATUS "Submodule update - done") + endif() + endif() +endif() +if(NOT EXISTS "${PROJECT_SOURCE_DIR}/lib/bpf/src/btf.h") + message(FATAL_ERROR "The submodules were not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.") +endif() + _set_fancy(LIB_INSTALL_DIR "${EXEC_INSTALL_PREFIX}${CMAKE_INSTALL_PREFIX}/${__LIB}" "libdir") +# libbpf uses reallocarray, which is not available in all versions of glibc +# libbpf's include/tools/libc_compat.h provides implementation, but needs +# COMPACT_NEED_REALLOCARRAY to be set +INCLUDE(CheckCSourceCompiles) +CHECK_C_SOURCE_COMPILES( +" +#define _GNU_SOURCE +#include +int main(void) +{ + return !!reallocarray(NULL, 1, 1); +} +" HAVE_REALLOCARRAY_SUPPORT) +if (NOT HAVE_REALLOCARRAY_SUPPORT) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOMPAT_NEED_REALLOCARRAY") +endif() + +file(GLOB libbpf_sources "lib/bpf/src/*.c") +add_library(bpf STATIC ${libbpf_sources}) +set_target_properties(bpf PROPERTIES OUTPUT_NAME bpf) +target_include_directories(bpf PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include + ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include/uapi) + set(dwarves_LIB_SRCS dwarves.c dwarves_fprintf.c gobuffer strings ctf_encoder.c ctf_loader.c libctf.c btf_encoder.c btf_loader.c libbtf.c dwarf_loader.c dutil.c elf_symtab.c rbtree.c) add_library(dwarves SHARED ${dwarves_LIB_SRCS}) set_target_properties(dwarves PROPERTIES VERSION 1.0.0 SOVERSION 1) set_target_properties(dwarves PROPERTIES INTERFACE_LINK_LIBRARIES "") -target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES}) +target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} bpf) set(dwarves_emit_LIB_SRCS dwarves_emit.c) add_library(dwarves_emit SHARED ${dwarves_emit_LIB_SRCS}) @@ -75,7 +120,7 @@ set(pglobal_SRCS pglobal.c) add_executable(pglobal ${pglobal_SRCS}) target_link_libraries(pglobal dwarves) -set(pfunct_SRCS pfunct.c ) +set(pfunct_SRCS pfunct.c) add_executable(pfunct ${pfunct_SRCS}) target_link_libraries(pfunct dwarves dwarves_emit ${ELF_LIBRARY}) diff --git a/lib/bpf b/lib/bpf new file mode 160000 index 0000000..b19c6dc --- /dev/null +++ b/lib/bpf @@ -0,0 +1 @@ +Subproject commit b19c6dcf623a7adc9e538ddbe2964c2f58dd2417