bpftrace/bpftrace-add-support-to-link-bpftrace-against-the-system-inst.patch
2018-11-09 16:51:46 +01:00

240 lines
8.3 KiB
Diff

From c931287579bc2f83a76bd2511285b79f95f22628 Mon Sep 17 00:00:00 2001
From: Augusto Caringi <acaringi@redhat.com>
Date: Wed, 31 Oct 2018 20:32:17 +0100
Subject: [PATCH] Add support to link bpftrace against the system installed bcc
library
- Add "-DSYSTEM_BCC_LIBRARY:BOOL=ON" CMake option to link bpftrace
executable against the system installed bcc library
---
CMakeLists.txt | 50 ++++++++++++++++++++++++++----------------
INSTALL.md | 2 ++
cmake/FindLibBcc.cmake | 44 +++++++++++++++++++++++++++++++++++++
src/CMakeLists.txt | 15 ++++++++-----
src/ast/CMakeLists.txt | 8 ++++---
tests/CMakeLists.txt | 15 ++++++++-----
6 files changed, 100 insertions(+), 34 deletions(-)
create mode 100644 cmake/FindLibBcc.cmake
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 18875ff..3b01a21 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 2.8.12)
project(bpftrace)
set(STATIC_LINKING OFF CACHE BOOL "Build bpftrace as a statically linked executable")
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
+set(ENABLE_TESTS ON CACHE BOOL "Enable tests")
add_compile_options("-std=c++14")
add_compile_options("-Wno-format-security")
@@ -20,25 +22,34 @@ add_compile_options("-Wno-format-security")
#add_compile_options("-Wstrict-overflow=5")
#add_compile_options("-Wdisabled-optimization")
-enable_testing()
+if (ENABLE_TESTS)
+ enable_testing()
+endif()
-if (OFFLINE_BUILDS)
- include(ExternalProject)
- ExternalProject_Add(bcc
- GIT_REPOSITORY https://github.com/iovisor/bcc
- STEP_TARGETS build update
- EXCLUDE_FROM_ALL 1
- UPDATE_DISCONNECTED 1
- BUILD_COMMAND ${CMAKE_COMMAND} --build . --target bcc-static
- )
+if (SYSTEM_BCC_LIBRARY)
+ find_package(LibBcc REQUIRED)
+ include_directories(${LIBBCC_INCLUDE_DIRS})
else()
- include(ExternalProject)
- ExternalProject_Add(bcc
- GIT_REPOSITORY https://github.com/iovisor/bcc
- STEP_TARGETS build update
- EXCLUDE_FROM_ALL 1
- BUILD_COMMAND ${CMAKE_COMMAND} --build . --target bcc-static
- )
+ if (OFFLINE_BUILDS)
+ include(ExternalProject)
+ ExternalProject_Add(bcc
+ GIT_REPOSITORY https://github.com/iovisor/bcc
+ STEP_TARGETS build update
+ EXCLUDE_FROM_ALL 1
+ UPDATE_DISCONNECTED 1
+ BUILD_COMMAND ${CMAKE_COMMAND} --build . --target bcc-static
+ )
+ else()
+ include(ExternalProject)
+ ExternalProject_Add(bcc
+ GIT_REPOSITORY https://github.com/iovisor/bcc
+ STEP_TARGETS build update
+ EXCLUDE_FROM_ALL 1
+ BUILD_COMMAND ${CMAKE_COMMAND} --build . --target bcc-static
+ )
+ endif()
+ ExternalProject_Get_Property(bcc source_dir)
+ include_directories(${source_dir}/src/cc)
endif()
if (STATIC_LINKING)
@@ -48,7 +59,6 @@ if (STATIC_LINKING)
set(CMAKE_LINK_SEARCH_END_STATIC TRUE)
endif()
-set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
find_package(LibElf REQUIRED)
include_directories(${LIBELF_INCLUDE_DIRS})
@@ -75,7 +85,9 @@ include_directories(${CLANG_INCLUDE_DIRS})
add_subdirectory(src/arch)
add_subdirectory(src/ast)
add_subdirectory(src)
-add_subdirectory(tests)
+if (ENABLE_TESTS)
+ add_subdirectory(tests)
+endif()
add_subdirectory(resources)
add_subdirectory(tools)
add_subdirectory(man)
diff --git a/INSTALL.md b/INSTALL.md
index 7058dd7..d936ee6 100644
--- a/INSTALL.md
+++ b/INSTALL.md
@@ -170,6 +170,8 @@ By default bpftrace will be built as a dynamically linked executable. If a stati
The latest versions of BCC and Google Test will be downloaded on each build. To speed up builds and only download their sources on the first run, use the CMake option `-DOFFLINE_BUILDS:BOOL=ON`.
+There is also an experimental support to link the bpftrace executable against the system installed bcc library instead of downloading and building bcc from source. This can be enabled through the CMake option `-DSYSTEM_BCC_LIBRARY:BOOL=ON`.
+
To test that the build works, you can try running the test suite, and a one-liner:
```
diff --git a/cmake/FindLibBcc.cmake b/cmake/FindLibBcc.cmake
new file mode 100644
index 0000000..860bc91
--- /dev/null
+++ b/cmake/FindLibBcc.cmake
@@ -0,0 +1,44 @@
+# - Try to find libbcc
+# Once done this will define
+#
+# LIBBCC_FOUND - system has libbcc
+# LIBBCC_INCLUDE_DIRS - the libbcc include directory
+# LIBBCC_LIBRARIES - Link these to use libbcc
+# LIBBCC_DEFINITIONS - Compiler switches required for using libbcc
+
+if (LIBBCC_LIBRARIES AND LIBBCC_INCLUDE_DIRS)
+ set (LibBcc_FIND_QUIETLY TRUE)
+endif (LIBBCC_LIBRARIES AND LIBBCC_INCLUDE_DIRS)
+
+find_path (LIBBCC_INCLUDE_DIRS
+ NAMES
+ libbpf.h
+ PATHS
+ /usr/include
+ /usr/include/bcc
+ /usr/local/include
+ /usr/local/include/libbcc
+ /opt/local/include
+ /opt/local/include/libbcc
+ /sw/include
+ /sw/include/libbcc
+ ENV CPATH)
+
+find_library (LIBBCC_LIBRARIES
+ NAMES
+ bcc
+ PATHS
+ /usr/lib
+ /usr/local/lib
+ /opt/local/lib
+ /sw/lib
+ ENV LIBRARY_PATH
+ ENV LD_LIBRARY_PATH)
+
+include (FindPackageHandleStandardArgs)
+
+
+# handle the QUIETLY and REQUIRED arguments and set LIBBCC_FOUND to TRUE if all listed variables are TRUE
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibBcc DEFAULT_MSG
+ LIBBCC_LIBRARIES
+ LIBBCC_INCLUDE_DIRS)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 861f899..e275416 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -19,12 +19,15 @@ if(HAVE_NAME_TO_HANDLE_AT)
endif(HAVE_NAME_TO_HANDLE_AT)
target_link_libraries(bpftrace arch ast parser resources)
-ExternalProject_Get_Property(bcc source_dir binary_dir)
-target_include_directories(bpftrace PUBLIC ${source_dir}/src/cc)
-target_link_libraries(bpftrace ${binary_dir}/src/cc/libbpf.a)
-target_link_libraries(bpftrace ${binary_dir}/src/cc/libbcc-loader-static.a)
-target_link_libraries(bpftrace ${binary_dir}/src/cc/libbcc.a)
-target_link_libraries(bpftrace ${binary_dir}/src/cc/frontends/clang/libclang_frontend.a)
+if (SYSTEM_BCC_LIBRARY)
+ target_link_libraries(bpftrace ${LIBBCC_LIBRARIES})
+else()
+ ExternalProject_Get_Property(bcc binary_dir)
+ target_link_libraries(bpftrace ${binary_dir}/src/cc/libbpf.a)
+ target_link_libraries(bpftrace ${binary_dir}/src/cc/libbcc-loader-static.a)
+ target_link_libraries(bpftrace ${binary_dir}/src/cc/libbcc.a)
+ target_link_libraries(bpftrace ${binary_dir}/src/cc/frontends/clang/libclang_frontend.a)
+endif()
target_link_libraries(bpftrace ${LIBELF_LIBRARIES})
install(TARGETS bpftrace DESTINATION bin)
diff --git a/src/ast/CMakeLists.txt b/src/ast/CMakeLists.txt
index 1214a38..262179e 100644
--- a/src/ast/CMakeLists.txt
+++ b/src/ast/CMakeLists.txt
@@ -11,9 +11,11 @@ target_include_directories(ast PUBLIC ${CMAKE_SOURCE_DIR}/src/ast)
target_include_directories(ast PUBLIC ${CMAKE_BINARY_DIR})
target_link_libraries(ast arch)
-add_dependencies(ast bcc-build parser)
-ExternalProject_Get_Property(bcc source_dir)
-target_include_directories(ast PUBLIC ${source_dir}/src/cc)
+if (SYSTEM_BCC_LIBRARY)
+ add_dependencies(ast parser)
+else()
+ add_dependencies(ast bcc-build parser)
+endif()
if (STATIC_LINKING)
set(clang_libs
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 0938d5a..82e7d28 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -108,12 +108,15 @@ if(HAVE_NAME_TO_HANDLE_AT)
endif(HAVE_NAME_TO_HANDLE_AT)
target_link_libraries(bpftrace_test arch ast parser resources)
-ExternalProject_Get_Property(bcc source_dir binary_dir)
-target_include_directories(bpftrace_test PUBLIC ${source_dir}/src/cc)
-target_link_libraries(bpftrace_test ${binary_dir}/src/cc/libbpf.a)
-target_link_libraries(bpftrace_test ${binary_dir}/src/cc/libbcc-loader-static.a)
-target_link_libraries(bpftrace_test ${binary_dir}/src/cc/libbcc.a)
-target_link_libraries(bpftrace_test ${binary_dir}/src/cc/frontends/clang/libclang_frontend.a)
+if (SYSTEM_BCC_LIBRARY)
+ target_link_libraries(bpftrace_test ${LIBBCC_LIBRARIES})
+else()
+ ExternalProject_Get_Property(bcc binary_dir)
+ target_link_libraries(bpftrace_test ${binary_dir}/src/cc/libbpf.a)
+ target_link_libraries(bpftrace_test ${binary_dir}/src/cc/libbcc-loader-static.a)
+ target_link_libraries(bpftrace_test ${binary_dir}/src/cc/libbcc.a)
+ target_link_libraries(bpftrace_test ${binary_dir}/src/cc/frontends/clang/libclang_frontend.a)
+endif()
target_link_libraries(bpftrace_test ${LIBELF_LIBRARIES})
find_package(Threads REQUIRED)
--
2.17.2