move api and create dependent option
authorBrenden Blanco <bblanco@gmail.com>
Thu, 29 Jun 2017 00:37:06 +0000 (17:37 -0700)
committerBrenden Blanco <bblanco@gmail.com>
Fri, 25 Aug 2017 23:41:36 +0000 (16:41 -0700)
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 <bblanco@gmail.com>
CMakeLists.txt
examples/cpp/CMakeLists.txt
src/cc/CMakeLists.txt
src/cc/api/BPF.cc [moved from src/cc/BPF.cc with 100% similarity]
src/cc/api/BPF.h [moved from src/cc/BPF.h with 100% similarity]
src/cc/api/BPFTable.cc [moved from src/cc/BPFTable.cc with 100% similarity]
src/cc/api/BPFTable.h [moved from src/cc/BPFTable.h with 100% similarity]
src/cc/api/CMakeLists.txt [new file with mode: 0644]
src/cc/link_all.cc [new file with mode: 0644]
tests/cc/CMakeLists.txt

index b23261c..f2bdd55 100644 (file)
@@ -11,6 +11,7 @@ enable_testing()
 
 include(cmake/GetGitRevisionDescription.cmake)
 include(cmake/version.cmake)
+include(CMakeDependentOption)
 include(GNUInstallDirs)
 include(CheckCXXCompilerFlag)
 include(cmake/FindCompilerFlag.cmake)
index 49da829..7d6ccee 100644 (file)
@@ -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)
 
index 799f548..8d4b7dc 100644 (file)
@@ -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
similarity index 100%
rename from src/cc/BPF.cc
rename to src/cc/api/BPF.cc
similarity index 100%
rename from src/cc/BPF.h
rename to src/cc/api/BPF.h
similarity index 100%
rename from src/cc/BPFTable.cc
rename to src/cc/api/BPFTable.cc
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 (file)
index 0000000..4234e20
--- /dev/null
@@ -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 (file)
index 0000000..1105776
--- /dev/null
@@ -0,0 +1,21 @@
+// Copyright (c) 2017 VMware, Inc.
+// Licensed under the Apache License, Version 2.0 (the "License")
+#include <cstdlib>
+
+#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
+}
index be986bf..373bfcb 100644 (file)
@@ -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)