From 71fc3d5c890f57fb7f979c6176d97da0f04d0801 Mon Sep 17 00:00:00 2001 From: Brenden Blanco Date: Wed, 28 Jun 2017 17:37:06 -0700 Subject: [PATCH] move api and create dependent option Move the C++ api files under a new subdirectory. Use CMAKE_DEPENDENT_OPTION to enforce sdt->cpp_api relationship. Since linking .a into a .so can cause global symbols to be dropped, add a helper file to force exported symbols to be retained (link_all.cc). This problem doesn't exist for building the static cpp examples. Signed-off-by: Brenden Blanco --- CMakeLists.txt | 1 + examples/cpp/CMakeLists.txt | 1 + src/cc/CMakeLists.txt | 16 +++++++++++----- src/cc/{ => api}/BPF.cc | 0 src/cc/{ => api}/BPF.h | 0 src/cc/{ => api}/BPFTable.cc | 0 src/cc/{ => api}/BPFTable.h | 0 src/cc/api/CMakeLists.txt | 3 +++ src/cc/link_all.cc | 21 +++++++++++++++++++++ tests/cc/CMakeLists.txt | 1 + 10 files changed, 38 insertions(+), 5 deletions(-) rename src/cc/{ => api}/BPF.cc (100%) rename src/cc/{ => api}/BPF.h (100%) rename src/cc/{ => api}/BPFTable.cc (100%) rename src/cc/{ => api}/BPFTable.h (100%) create mode 100644 src/cc/api/CMakeLists.txt create mode 100644 src/cc/link_all.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index b23261cc..f2bdd554 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ enable_testing() include(cmake/GetGitRevisionDescription.cmake) include(cmake/version.cmake) +include(CMakeDependentOption) include(GNUInstallDirs) include(CheckCXXCompilerFlag) include(cmake/FindCompilerFlag.cmake) diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt index 49da8296..7d6ccee2 100644 --- a/examples/cpp/CMakeLists.txt +++ b/examples/cpp/CMakeLists.txt @@ -2,6 +2,7 @@ # Licensed under the Apache License, Version 2.0 (the "License") include_directories(${CMAKE_SOURCE_DIR}/src/cc) +include_directories(${CMAKE_SOURCE_DIR}/src/cc/api) option(INSTALL_CPP_EXAMPLES "Install C++ examples. Those binaries are statically linked and can take plenty of disk space" OFF) diff --git a/src/cc/CMakeLists.txt b/src/cc/CMakeLists.txt index 799f5481..8d4b7dc2 100644 --- a/src/cc/CMakeLists.txt +++ b/src/cc/CMakeLists.txt @@ -28,18 +28,17 @@ set(bcc_common_sources bpf_common.cc bpf_module.cc exported_files.cc) set(bcc_table_sources table_storage.cc shared_table.cc bpffs_table.cc json_map_decl_visitor.cc) set(bcc_util_sources ns_guard.cc common.cc) set(bcc_sym_sources bcc_syms.cc bcc_elf.c bcc_perf_map.c bcc_proc.c) -set(bcc_api_sources BPF.cc BPFTable.cc) add_library(bcc-shared SHARED + link_all.cc ${bcc_common_sources} ${bcc_table_sources} ${bcc_sym_sources} - ${bcc_util_sources} ${bcc_api_sources}) + ${bcc_util_sources}) set_target_properties(bcc-shared PROPERTIES VERSION ${REVISION_LAST} SOVERSION 0) set_target_properties(bcc-shared PROPERTIES OUTPUT_NAME bcc) add_library(bcc-loader-static STATIC ${bcc_sym_sources} ${bcc_util_sources}) add_library(bcc-static STATIC - ${bcc_common_sources} ${bcc_table_sources} - ${bcc_util_sources} ${bcc_api_sources}) + ${bcc_common_sources} ${bcc_table_sources} ${bcc_util_sources}) set_target_properties(bcc-static PROPERTIES OUTPUT_NAME bcc) include(clang_libs) @@ -50,6 +49,13 @@ set(bcc_common_libs b_frontend clang_frontend bpf-static ${clang_libs} ${llvm_libs} ${LIBELF_LIBRARIES}) option(ENABLE_USDT "Enable User-level Statically Defined Tracing" ON) +CMAKE_DEPENDENT_OPTION(ENABLE_CPP_API "Enable C++ API" ON "ENABLE_USDT" OFF) + +if(ENABLE_CPP_API) + add_subdirectory(api) + list(APPEND bcc_common_libs api-static) +endif() + if(ENABLE_USDT) add_subdirectory(usdt) list(APPEND bcc_common_libs usdt-static) @@ -62,7 +68,7 @@ target_link_libraries(bcc-static ${bcc_common_libs} bcc-loader-static) install(TARGETS bcc-shared LIBRARY COMPONENT libbcc DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES bpf_common.h bpf_module.h bcc_syms.h bcc_exception.h file_desc.h - libbpf.h perf_reader.h BPF.h BPFTable.h + libbpf.h perf_reader.h table_desc.h table_storage.h COMPONENT libbcc DESTINATION include/bcc) install(DIRECTORY compat/linux/ COMPONENT libbcc diff --git a/src/cc/BPF.cc b/src/cc/api/BPF.cc similarity index 100% rename from src/cc/BPF.cc rename to src/cc/api/BPF.cc diff --git a/src/cc/BPF.h b/src/cc/api/BPF.h similarity index 100% rename from src/cc/BPF.h rename to src/cc/api/BPF.h diff --git a/src/cc/BPFTable.cc b/src/cc/api/BPFTable.cc similarity index 100% rename from src/cc/BPFTable.cc rename to src/cc/api/BPFTable.cc diff --git a/src/cc/BPFTable.h b/src/cc/api/BPFTable.h similarity index 100% rename from src/cc/BPFTable.h rename to src/cc/api/BPFTable.h diff --git a/src/cc/api/CMakeLists.txt b/src/cc/api/CMakeLists.txt new file mode 100644 index 00000000..4234e20c --- /dev/null +++ b/src/cc/api/CMakeLists.txt @@ -0,0 +1,3 @@ +set(bcc_api_sources BPF.cc BPFTable.cc) +add_library(api-static STATIC ${bcc_api_sources}) +install(FILES BPF.h BPFTable.h COMPONENT libbcc DESTINATION include/bcc) diff --git a/src/cc/link_all.cc b/src/cc/link_all.cc new file mode 100644 index 00000000..11057766 --- /dev/null +++ b/src/cc/link_all.cc @@ -0,0 +1,21 @@ +// Copyright (c) 2017 VMware, Inc. +// Licensed under the Apache License, Version 2.0 (the "License") +#include + +#include "bcc_usdt.h" + +namespace { + // Take this trick from llvm for forcing exported functions in helper + // libraries to be included in the final .so + struct LinkAll { + LinkAll() { + // getenv never returns -1, but compiler doesn't know! + if (::getenv("bar") != (char *)-1) + return; + + (void)bcc_usdt_new_frompid(-1); + (void)bcc_usdt_new_frompath(nullptr); + (void)bcc_usdt_close(nullptr); + } + } LinkAll; // declare one instance to invoke the constructor +} diff --git a/tests/cc/CMakeLists.txt b/tests/cc/CMakeLists.txt index be986bfa..373bfcb7 100644 --- a/tests/cc/CMakeLists.txt +++ b/tests/cc/CMakeLists.txt @@ -2,6 +2,7 @@ # Licensed under the Apache License, Version 2.0 (the "License") include_directories(${CMAKE_SOURCE_DIR}/src/cc) +include_directories(${CMAKE_SOURCE_DIR}/src/cc/api) add_executable(test_static test_static.c) target_link_libraries(test_static bcc-static) -- 2.34.1