From 25f3ccee72f50f14fd51166a9839f86570be9f72 Mon Sep 17 00:00:00 2001 From: Brenden Blanco Date: Mon, 26 Jun 2017 13:37:34 -0700 Subject: [PATCH] src/cc: cmake file cleanup and split usdt into subdir Move two usdt related files to a new subdirectory and link them into the final product. Introduce a cmake option to disable that feature (turning it off currently fails to compile). Move some of the cmake flag computation into a helper cmake file and include that rather than inlining it. Signed-off-by: Brenden Blanco --- cmake/clang_libs.cmake | 42 +++++++++++++++++++++++ cmake/static_libstdc++.cmake | 15 ++++++++ src/cc/CMakeLists.txt | 63 +++++++++------------------------- src/cc/usdt/CMakeLists.txt | 1 + src/cc/{ => usdt}/usdt.cc | 0 src/cc/{ => usdt}/usdt_args.cc | 0 6 files changed, 74 insertions(+), 47 deletions(-) create mode 100644 cmake/clang_libs.cmake create mode 100644 cmake/static_libstdc++.cmake create mode 100644 src/cc/usdt/CMakeLists.txt rename src/cc/{ => usdt}/usdt.cc (100%) rename src/cc/{ => usdt}/usdt_args.cc (100%) diff --git a/cmake/clang_libs.cmake b/cmake/clang_libs.cmake new file mode 100644 index 00000000..907b784f --- /dev/null +++ b/cmake/clang_libs.cmake @@ -0,0 +1,42 @@ +set(llvm_raw_libs bitwriter bpfcodegen irreader linker + mcjit objcarcopts option passes nativecodegen lto) +list(FIND LLVM_AVAILABLE_LIBS "LLVMCoverage" _llvm_coverage) +if (${_llvm_coverage} GREATER -1) + list(APPEND llvm_raw_libs coverage) +endif() +list(FIND LLVM_AVAILABLE_LIBS "LLVMCoroutines" _llvm_coroutines) +if (${_llvm_coroutines} GREATER -1) + list(APPEND llvm_raw_libs coroutines) +endif() +llvm_map_components_to_libnames(_llvm_libs ${llvm_raw_libs}) +llvm_expand_dependencies(llvm_libs ${_llvm_libs}) + +# order is important +set(clang_libs + ${libclangFrontend} + ${libclangSerialization} + ${libclangDriver} + ${libclangParse} + ${libclangSema} + ${libclangCodeGen} + ${libclangAnalysis} + ${libclangRewrite} + ${libclangEdit} + ${libclangAST} + ${libclangLex} + ${libclangBasic}) + +# prune unused llvm static library stuff when linking into the new .so +set(_exclude_flags) +foreach(_lib ${clang_libs}) + get_filename_component(_lib ${_lib} NAME) + set(_exclude_flags "${_exclude_flags} -Wl,--exclude-libs=${_lib}") +endforeach(_lib) +set(clang_lib_exclude_flags "${_exclude_flags}") + +set(_exclude_flags) +foreach(_lib ${llvm_libs}) + get_filename_component(_lib ${_lib} NAME) + set(_exclude_flags "${_exclude_flags} -Wl,--exclude-libs=lib${_lib}.a") +endforeach(_lib) +set(llvm_lib_exclude_flags "${_exclude_flags}") diff --git a/cmake/static_libstdc++.cmake b/cmake/static_libstdc++.cmake new file mode 100644 index 00000000..3c8ac179 --- /dev/null +++ b/cmake/static_libstdc++.cmake @@ -0,0 +1,15 @@ +# only turn on static-libstdc++ if also linking statically against clang +string(REGEX MATCH ".*[.]a$" LIBCLANG_ISSTATIC "${libclangBasic}") +# if gcc 4.9 or higher is used, static libstdc++ is a good option +if (CMAKE_COMPILER_IS_GNUCC AND LIBCLANG_ISSTATIC) + execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION) + if (GCC_VERSION VERSION_GREATER 4.9 OR GCC_VERSION VERSION_EQUAL 4.9) + execute_process(COMMAND ${CMAKE_C_COMPILER} -print-libgcc-file-name OUTPUT_VARIABLE GCC_LIB) + get_filename_component(GCC_DIR "${GCC_LIB}" DIRECTORY) + find_library(GCC_LIBSTDCPP libstdc++.a PATHS "${GCC_DIR}" NO_DEFAULT_PATH) + if (GCC_LIBSTDCPP) + message(STATUS "Using static-libstdc++") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libstdc++") + endif() + endif() +endif() diff --git a/src/cc/CMakeLists.txt b/src/cc/CMakeLists.txt index 5c4cfa5a..bc25a8db 100644 --- a/src/cc/CMakeLists.txt +++ b/src/cc/CMakeLists.txt @@ -17,21 +17,7 @@ configure_file(libbcc.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libbcc.pc @ONLY) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") -# only turn on static-libstdc++ if also linking statically against clang -string(REGEX MATCH ".*[.]a$" LIBCLANG_ISSTATIC "${libclangBasic}") -# if gcc 4.9 or higher is used, static libstdc++ is a good option -if (CMAKE_COMPILER_IS_GNUCC AND LIBCLANG_ISSTATIC) - execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION) - if (GCC_VERSION VERSION_GREATER 4.9 OR GCC_VERSION VERSION_EQUAL 4.9) - execute_process(COMMAND ${CMAKE_C_COMPILER} -print-libgcc-file-name OUTPUT_VARIABLE GCC_LIB) - get_filename_component(GCC_DIR "${GCC_LIB}" DIRECTORY) - find_library(GCC_LIBSTDCPP libstdc++.a PATHS "${GCC_DIR}" NO_DEFAULT_PATH) - if (GCC_LIBSTDCPP) - message(STATUS "Using static-libstdc++") - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libstdc++") - endif() - endif() -endif() +include(static_libstdc++) add_library(bpf-static STATIC libbpf.c perf_reader.c) set_target_properties(bpf-static PROPERTIES OUTPUT_NAME bpf) @@ -40,8 +26,8 @@ set_target_properties(bpf-shared PROPERTIES OUTPUT_NAME bpf) add_library(bcc-shared SHARED bpf_common.cc bpf_module.cc table_storage.cc shared_table.cc bpffs_table.cc json_map_decl_visitor.cc exported_files.cc - bcc_elf.c bcc_perf_map.c bcc_proc.c bcc_syms.cc ns_guard.cc usdt_args.cc - usdt.cc common.cc BPF.cc BPFTable.cc) + bcc_elf.c bcc_perf_map.c bcc_proc.c bcc_syms.cc ns_guard.cc common.cc BPF.cc + BPFTable.cc) set_target_properties(bcc-shared PROPERTIES VERSION ${REVISION_LAST} SOVERSION 0) set_target_properties(bcc-shared PROPERTIES OUTPUT_NAME bcc) @@ -49,42 +35,25 @@ add_library(bcc-loader-static STATIC bcc_elf.c bcc_perf_map.c bcc_proc.c bcc_syms.cc ns_guard.cc) add_library(bcc-static STATIC bpf_common.cc bpf_module.cc shared_table.cc bpffs_table.cc json_map_decl_visitor.cc table_storage.cc exported_files.cc - usdt_args.cc usdt.cc common.cc BPF.cc BPFTable.cc) + common.cc BPF.cc BPFTable.cc) set_target_properties(bcc-static PROPERTIES OUTPUT_NAME bcc) -set(llvm_raw_libs bitwriter bpfcodegen irreader linker - mcjit objcarcopts option passes nativecodegen lto) -list(FIND LLVM_AVAILABLE_LIBS "LLVMCoverage" _llvm_coverage) -if (${_llvm_coverage} GREATER -1) - list(APPEND llvm_raw_libs coverage) -endif() -list(FIND LLVM_AVAILABLE_LIBS "LLVMCoroutines" _llvm_coroutines) -if (${_llvm_coroutines} GREATER -1) - list(APPEND llvm_raw_libs coroutines) -endif() -llvm_map_components_to_libnames(llvm_libs ${llvm_raw_libs}) -llvm_expand_dependencies(expanded_libs ${llvm_libs}) +include(clang_libs) +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${clang_lib_exclude_flags}") +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${llvm_lib_exclude_flags}") -# order is important -set(clang_libs ${libclangFrontend} ${libclangSerialization} ${libclangDriver} ${libclangParse} - ${libclangSema} ${libclangCodeGen} ${libclangAnalysis} ${libclangRewrite} ${libclangEdit} - ${libclangAST} ${libclangLex} ${libclangBasic}) +set(bcc_common_libs b_frontend clang_frontend bpf-static + ${clang_libs} ${llvm_libs} ${LIBELF_LIBRARIES}) -# prune unused llvm static library stuff when linking into the new .so -foreach(lib ${clang_libs}) - get_filename_component(lib ${lib} NAME) - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--exclude-libs=${lib}") -endforeach(lib) -foreach(lib ${expanded_libs}) - get_filename_component(lib ${lib} NAME) - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--exclude-libs=lib${lib}.a") -endforeach(lib) +option(ENABLE_USDT "Enable User-level Statically Defined Tracing" ON) +if(ENABLE_USDT) + add_subdirectory(usdt) + list(APPEND bcc_common_libs usdt-static) +endif() # Link against LLVM libraries -target_link_libraries(bcc-shared b_frontend clang_frontend bpf-static - ${clang_libs} ${expanded_libs} ${LIBELF_LIBRARIES}) -target_link_libraries(bcc-static b_frontend clang_frontend bcc-loader-static - bpf-static ${clang_libs} ${expanded_libs} ${LIBELF_LIBRARIES}) +target_link_libraries(bcc-shared ${bcc_common_libs}) +target_link_libraries(bcc-static ${bcc_common_libs} bcc-loader-static) install(TARGETS bcc-shared LIBRARY COMPONENT libbcc DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/src/cc/usdt/CMakeLists.txt b/src/cc/usdt/CMakeLists.txt new file mode 100644 index 00000000..6a2e8958 --- /dev/null +++ b/src/cc/usdt/CMakeLists.txt @@ -0,0 +1 @@ +add_library(usdt-static STATIC usdt_args.cc usdt.cc) diff --git a/src/cc/usdt.cc b/src/cc/usdt/usdt.cc similarity index 100% rename from src/cc/usdt.cc rename to src/cc/usdt/usdt.cc diff --git a/src/cc/usdt_args.cc b/src/cc/usdt/usdt_args.cc similarity index 100% rename from src/cc/usdt_args.cc rename to src/cc/usdt/usdt_args.cc -- 2.34.1