Skip to content

Commit

Permalink
move api and create dependent option
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
drzaeus77 committed Aug 25, 2017
1 parent 1614ce7 commit 71fc3d5
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enable_testing()

include(cmake/GetGitRevisionDescription.cmake)
include(cmake/version.cmake)
include(CMakeDependentOption)
include(GNUInstallDirs)
include(CheckCXXCompilerFlag)
include(cmake/FindCompilerFlag.cmake)
Expand Down
1 change: 1 addition & 0 deletions examples/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 11 additions & 5 deletions src/cc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions src/cc/api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions src/cc/link_all.cc
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions tests/cc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 71fc3d5

Please sign in to comment.