1# Copyright (c) PLUMgrid, Inc. 2# Licensed under the Apache License, Version 2.0 (the "License") 3 4include_directories(${CMAKE_CURRENT_BINARY_DIR}) 5include_directories(${CMAKE_CURRENT_SOURCE_DIR}) 6# to be removed 7include_directories(${CMAKE_CURRENT_BINARY_DIR}/frontends/b) 8include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frontends/b) 9include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frontends/clang) 10include_directories(${LLVM_INCLUDE_DIRS}) 11include_directories(${LIBELF_INCLUDE_DIRS}) 12if (LIBDEBUGINFOD_FOUND) 13 include_directories(${LIBDEBUGINFOD_INCLUDE_DIRS}) 14endif (LIBDEBUGINFOD_FOUND) 15# todo: if check for kernel version 16if (CMAKE_USE_LIBBPF_PACKAGE AND LIBBPF_FOUND) 17 include_directories(${LIBBPF_INCLUDE_DIRS}) 18else() 19 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libbpf/include) 20 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libbpf/include/uapi) 21endif() 22 23# add_definitions has a problem parsing "-D_GLIBCXX_USE_CXX11_ABI=0", this is safer 24separate_arguments(LLVM_DEFINITIONS) 25add_compile_options(${LLVM_DEFINITIONS}) 26 27configure_file(libbcc.pc.in libbcc.pc @ONLY) 28configure_file(bcc_version.h.in bcc_version.h @ONLY) 29 30set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -DBCC_PROG_TAG_DIR='\"${BCC_PROG_TAG_DIR}\"'") 31set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-result") 32set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -Wno-unused-result") 33 34if (NOT HAVE_REALLOCARRAY_SUPPORT) 35 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOMPAT_NEED_REALLOCARRAY") 36endif() 37 38string(REGEX MATCH "^([0-9]+).*" _ ${LLVM_PACKAGE_VERSION}) 39set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DLLVM_MAJOR_VERSION=${CMAKE_MATCH_1}") 40 41include(static_libstdc++) 42 43if(LIBBPF_INCLUDE_DIR) 44 # Add user libbpf include and notify compilation 45 # that we are using external libbpf. It's checked 46 # in src/cc/libbpf.h and proper files are included. 47 include_directories(${LIBBPF_INCLUDE_DIR}) 48 add_definitions(-DHAVE_EXTERNAL_LIBBPF) 49endif() 50 51if(NOT CMAKE_USE_LIBBPF_PACKAGE) 52 if(LIBBPF_FOUND) 53 set(extract_dir ${CMAKE_CURRENT_BINARY_DIR}/libbpf_a_extract) 54 execute_process(COMMAND sh -c "mkdir -p ${extract_dir} && cd ${extract_dir} && ${CMAKE_AR} x ${LIBBPF_STATIC_LIBRARIES}") 55 file(GLOB libbpf_sources "${extract_dir}/*.o") 56 else() 57 file(GLOB libbpf_sources "libbpf/src/*.c") 58 endif() 59 60 set(libbpf_uapi libbpf/include/uapi/linux/) 61endif() 62 63set(bcc_common_sources bcc_common.cc bpf_module.cc bcc_btf.cc exported_files.cc) 64if (${LLVM_PACKAGE_VERSION} VERSION_EQUAL 6 OR ${LLVM_PACKAGE_VERSION} VERSION_GREATER 6) 65 set(bcc_common_sources ${bcc_common_sources} bcc_debug.cc) 66endif() 67 68if(ENABLE_LLVM_NATIVECODEGEN) 69set(bcc_common_sources ${bcc_common_sources} bpf_module_rw_engine.cc) 70else() 71set(bcc_common_sources ${bcc_common_sources} bpf_module_rw_engine_disabled.cc) 72endif() 73 74set(bcc_table_sources table_storage.cc shared_table.cc bpffs_table.cc json_map_decl_visitor.cc) 75set(bcc_util_sources common.cc) 76set(bcc_sym_sources bcc_syms.cc bcc_elf.c bcc_perf_map.c bcc_proc.c) 77set(bcc_common_headers libbpf.h perf_reader.h "${CMAKE_CURRENT_BINARY_DIR}/bcc_version.h") 78set(bcc_table_headers file_desc.h table_desc.h table_storage.h) 79set(bcc_api_headers bcc_common.h bpf_module.h bcc_exception.h bcc_syms.h bcc_proc.h bcc_elf.h) 80if(LIBBPF_FOUND) 81 set(bcc_common_sources ${bcc_common_sources} libbpf.c perf_reader.c) 82endif() 83 84if(ENABLE_CLANG_JIT) 85add_library(bcc-shared SHARED 86 link_all.cc ${bcc_common_sources} ${bcc_table_sources} ${bcc_sym_sources} 87 ${bcc_util_sources}) 88set_target_properties(bcc-shared PROPERTIES VERSION ${REVISION_LAST} SOVERSION 0) 89set_target_properties(bcc-shared PROPERTIES OUTPUT_NAME bcc) 90 91if(ENABLE_USDT) 92 add_definitions(-DEXPORT_USDT) 93 set(bcc_usdt_sources usdt/usdt.cc usdt/usdt_args.cc) 94 # else undefined 95endif() 96 97add_library(bcc-loader-static STATIC ${bcc_sym_sources} ${bcc_util_sources}) 98target_link_libraries(bcc-loader-static elf z) 99add_library(bcc-static STATIC 100 ${bcc_common_sources} ${bcc_table_sources} ${bcc_util_sources} ${bcc_usdt_sources} ${bcc_sym_sources} ${bcc_util_sources}) 101set_target_properties(bcc-static PROPERTIES OUTPUT_NAME bcc) 102set(bcc-lua-static 103 ${bcc_common_sources} ${bcc_table_sources} ${bcc_sym_sources} ${bcc_util_sources}) 104 105set(bpf_sources libbpf.c perf_reader.c ${libbpf_sources} ${bcc_sym_sources} ${bcc_util_sources} ${bcc_usdt_sources}) 106add_library(bpf-static STATIC ${bpf_sources}) 107set_target_properties(bpf-static PROPERTIES OUTPUT_NAME bcc_bpf) 108target_link_libraries(bpf-static elf z) 109add_library(bpf-shared SHARED ${bpf_sources}) 110set_target_properties(bpf-shared PROPERTIES VERSION ${REVISION_LAST} SOVERSION 0) 111set_target_properties(bpf-shared PROPERTIES OUTPUT_NAME bcc_bpf) 112target_link_libraries(bpf-shared elf z) 113if(LIBDEBUGINFOD_FOUND) 114 target_link_libraries(bpf-shared ${LIBDEBUGINFOD_LIBRARIES}) 115endif(LIBDEBUGINFOD_FOUND) 116if(CMAKE_USE_LIBBPF_PACKAGE AND LIBBPF_FOUND) 117 target_link_libraries(bpf-shared ${LIBBPF_LIBRARIES}) 118endif() 119 120include(clang_libs) 121set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${clang_lib_exclude_flags}") 122set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${llvm_lib_exclude_flags}") 123 124# bcc_common_libs_for_a for archive libraries 125# bcc_common_libs_for_s for shared libraries 126set(bcc_common_libs clang_frontend 127 -Wl,--whole-archive ${clang_libs} ${llvm_libs} -Wl,--no-whole-archive 128 ${LIBELF_LIBRARIES}) 129if (LIBDEBUGINFOD_FOUND) 130 list(APPEND bcc_common_libs ${LIBDEBUGINFOD_LIBRARIES}) 131endif (LIBDEBUGINFOD_FOUND) 132set(bcc_common_libs_for_a ${bcc_common_libs}) 133set(bcc_common_libs_for_s ${bcc_common_libs}) 134set(bcc_common_libs_for_lua clang_frontend 135 ${clang_libs} ${llvm_libs} ${LIBELF_LIBRARIES}) 136if(LIBBPF_FOUND) 137 list(APPEND bcc_common_libs_for_a ${LIBBPF_LIBRARIES}) 138 list(APPEND bcc_common_libs_for_s ${LIBBPF_LIBRARIES}) 139 list(APPEND bcc_common_libs_for_lua ${LIBBPF_LIBRARIES}) 140else() 141 list(APPEND bcc_common_libs_for_a bpf-static) 142 list(APPEND bcc_common_libs_for_s bpf-static) 143 list(APPEND bcc_common_libs_for_lua bpf-static) 144endif() 145 146if(ENABLE_CPP_API) 147 add_subdirectory(api) 148 list(APPEND bcc_common_libs_for_a api-static) 149 # Keep all API functions 150 list(APPEND bcc_common_libs_for_s -Wl,--whole-archive api-static -Wl,--no-whole-archive) 151endif() 152 153if(ENABLE_USDT) 154 list(APPEND bcc_api_headers bcc_usdt.h) 155 add_subdirectory(usdt) 156 list(APPEND bcc_common_libs_for_a usdt-static) 157 list(APPEND bcc_common_libs_for_s usdt-static) 158 list(APPEND bcc_common_libs_for_lua usdt-static) 159endif() 160 161add_subdirectory(frontends) 162 163# Link against LLVM libraries 164target_link_libraries(bcc-shared ${bcc_common_libs_for_s}) 165target_link_libraries(bcc-static ${bcc_common_libs_for_a} bcc-loader-static) 166set(bcc-lua-static ${bcc-lua-static} ${bcc_common_libs_for_lua}) 167 168install(TARGETS bcc-shared bcc-static bcc-loader-static bpf-static LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) 169install(FILES ${bcc_table_headers} DESTINATION include/bcc) 170install(FILES ${bcc_api_headers} DESTINATION include/bcc) 171install(DIRECTORY ${libbpf_uapi} DESTINATION include/bcc/compat/linux FILES_MATCHING PATTERN "*.h") 172install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libbcc.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) 173endif(ENABLE_CLANG_JIT) 174install(FILES ${bcc_common_headers} DESTINATION include/bcc) 175install(TARGETS bpf-shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) 176