1# Copyright (c) PLUMgrid, Inc. 2# Licensed under the Apache License, Version 2.0 (the "License") 3cmake_minimum_required(VERSION 2.8.12) 4 5if(${CMAKE_VERSION} VERSION_EQUAL 3.12.0 OR ${CMAKE_VERSION} VERSION_GREATER 3.12.0) 6 cmake_policy(SET CMP0074 NEW) 7endif() 8 9if(${CMAKE_VERSION} VERSION_EQUAL 3.3.0 OR ${CMAKE_VERSION} VERSION_GREATER 3.3.0) 10 cmake_policy(SET CMP0057 NEW) 11endif() 12 13project(bcc) 14if(NOT CMAKE_BUILD_TYPE) 15 set(CMAKE_BUILD_TYPE Release) 16endif() 17 18if(CMAKE_SANITIZE_TYPE) 19 add_compile_options(-fsanitize=${CMAKE_SANITIZE_TYPE}) 20 add_link_options(-fsanitize=${CMAKE_SANITIZE_TYPE}) 21endif() 22 23if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) 24 set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "path to install" FORCE) 25endif() 26 27enable_testing() 28 29execute_process(COMMAND git config --global --add safe.directory ${CMAKE_CURRENT_SOURCE_DIR} 30 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 31 RESULT_VARIABLE CONFIG_RESULT) 32if(CONFIG_RESULT AND NOT CONFIG_RESULT EQUAL 0) 33 message(WARNING "Failed to add root source directory to safe.directory") 34endif() 35 36# populate submodule blazesym 37if(NOT NO_BLAZESYM) 38 execute_process(COMMAND git config --global --add safe.directory ${CMAKE_CURRENT_SOURCE_DIR}/libbpf-tools/blazesym 39 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 40 RESULT_VARIABLE CONFIG_RESULT) 41 if(CONFIG_RESULT AND NOT CONFIG_RESULT EQUAL 0) 42 message(WARNING "Failed to add blazesym source directory to safe.directory") 43 endif() 44 45 execute_process(COMMAND git submodule update --init --recursive -- libbpf-tools/blazesym 46 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 47 RESULT_VARIABLE UPDATE_RESULT) 48 if(UPDATE_RESULT AND NOT UPDATE_RESULT EQUAL 0) 49 message(WARNING "Failed to update submodule blazesym") 50 endif() 51endif() 52 53# populate submodules (libbpf) 54if(NOT CMAKE_USE_LIBBPF_PACKAGE) 55 execute_process(COMMAND git config --global --add safe.directory ${CMAKE_CURRENT_SOURCE_DIR}/src/cc/libbpf 56 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 57 RESULT_VARIABLE CONFIG_RESULT) 58 if(CONFIG_RESULT AND NOT CONFIG_RESULT EQUAL 0) 59 message(WARNING "Failed to add libbpf source directory to safe.directory") 60 endif() 61 execute_process(COMMAND git config --global --add safe.directory ${CMAKE_CURRENT_SOURCE_DIR}/libbpf-tools/bpftool 62 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 63 RESULT_VARIABLE CONFIG_RESULT) 64 if(CONFIG_RESULT AND NOT CONFIG_RESULT EQUAL 0) 65 message(WARNING "Failed to add bpftool source directory to safe.directory") 66 endif() 67 68 if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/cc/libbpf/src) 69 execute_process(COMMAND git submodule update --init --recursive 70 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 71 RESULT_VARIABLE UPDATE_RESULT) 72 if(UPDATE_RESULT AND NOT UPDATE_RESULT EQUAL 0) 73 message(WARNING "Failed to update submodule libbpf") 74 endif() 75 else() 76 execute_process(COMMAND git diff --shortstat ${CMAKE_CURRENT_SOURCE_DIR}/src/cc/libbpf/ 77 OUTPUT_VARIABLE DIFF_STATUS) 78 if("${DIFF_STATUS}" STREQUAL "") 79 execute_process(COMMAND git submodule update --init --recursive 80 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 81 RESULT_VARIABLE UPDATE_RESULT) 82 if(UPDATE_RESULT AND NOT UPDATE_RESULT EQUAL 0) 83 message(WARNING "Failed to update submodule libbpf") 84 endif() 85 else() 86 message(WARNING "submodule libbpf dirty, so no sync") 87 endif() 88 endif() 89endif() 90 91# It's possible to use other kernel headers with 92# KERNEL_INCLUDE_DIRS build variable, like: 93# $ cd <kernel-dir> 94# $ make INSTALL_HDR_PATH=/tmp/headers headers_install 95# $ cd <bcc-dir> 96# $ cmake -DKERNEL_INCLUDE_DIRS=/tmp/headers/include/ ... 97include_directories(${KERNEL_INCLUDE_DIRS}) 98 99option(ENABLE_NO_PIE "Build bcc-lua without PIE" ON) 100 101include(cmake/GetGitRevisionDescription.cmake) 102include(cmake/version.cmake) 103include(CMakeDependentOption) 104include(GNUInstallDirs) 105include(CheckCXXCompilerFlag) 106include(cmake/FindCompilerFlag.cmake) 107 108option(ENABLE_LLVM_NATIVECODEGEN "Enable use of llvm nativecodegen module (needed by rw-engine)" ON) 109option(ENABLE_RTTI "Enable compiling with real time type information" OFF) 110option(ENABLE_LLVM_SHARED "Enable linking LLVM as a shared library" OFF) 111option(ENABLE_CLANG_JIT "Enable Loading BPF through Clang Frontend" ON) 112option(ENABLE_USDT "Enable User-level Statically Defined Tracing" ON) 113option(ENABLE_EXAMPLES "Build examples" ON) 114option(ENABLE_MAN "Build man pages" ON) 115option(ENABLE_TESTS "Build tests" ON) 116option(RUN_LUA_TESTS "Run lua tests" ON) 117option(ENABLE_LIBDEBUGINFOD "Use libdebuginfod as a source of debug symbols" ON) 118CMAKE_DEPENDENT_OPTION(ENABLE_CPP_API "Enable C++ API" ON "ENABLE_USDT" OFF) 119 120set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake) 121 122if(ENABLE_TESTS) 123 find_package(KernelHeaders) 124endif() 125 126if(CMAKE_USE_LIBBPF_PACKAGE) 127 find_package(LibBpf) 128endif() 129 130if(NOT PYTHON_ONLY) 131 find_package(LLVM REQUIRED CONFIG) 132 message(STATUS "Found LLVM: ${LLVM_INCLUDE_DIRS} ${LLVM_PACKAGE_VERSION} (Use LLVM_ROOT envronment variable for another version of LLVM)") 133 134 if(ENABLE_CLANG_JIT) 135 find_package(BISON) 136 find_package(FLEX) 137 find_package(LibElf REQUIRED) 138 find_package(LibDebuginfod) 139 find_package(LibLzma) 140 if(CLANG_DIR) 141 set(CMAKE_FIND_ROOT_PATH "${CLANG_DIR}") 142 include_directories("${CLANG_DIR}/include") 143 endif() 144 145 # clang is linked as a library, but the library path searching is 146 # primitively supported, unlike libLLVM 147 set(CLANG_SEARCH "/opt/local/llvm/lib;/usr/lib/llvm-3.7/lib;${LLVM_LIBRARY_DIRS}") 148 find_library(libclangAnalysis NAMES clangAnalysis clang-cpp HINTS ${CLANG_SEARCH}) 149 find_library(libclangAST NAMES clangAST clang-cpp HINTS ${CLANG_SEARCH}) 150 find_library(libclangBasic NAMES clangBasic clang-cpp HINTS ${CLANG_SEARCH}) 151 find_library(libclangCodeGen NAMES clangCodeGen clang-cpp HINTS ${CLANG_SEARCH}) 152 find_library(libclangDriver NAMES clangDriver clang-cpp HINTS ${CLANG_SEARCH}) 153 find_library(libclangEdit NAMES clangEdit clang-cpp HINTS ${CLANG_SEARCH}) 154 find_library(libclangFrontend NAMES clangFrontend clang-cpp HINTS ${CLANG_SEARCH}) 155 find_library(libclangLex NAMES clangLex clang-cpp HINTS ${CLANG_SEARCH}) 156 find_library(libclangParse NAMES clangParse clang-cpp HINTS ${CLANG_SEARCH}) 157 find_library(libclangRewrite NAMES clangRewrite clang-cpp HINTS ${CLANG_SEARCH}) 158 find_library(libclangSema NAMES clangSema clang-cpp HINTS ${CLANG_SEARCH}) 159 find_library(libclangSerialization NAMES clangSerialization clang-cpp HINTS ${CLANG_SEARCH}) 160 find_library(libclangASTMatchers NAMES clangASTMatchers clang-cpp HINTS ${CLANG_SEARCH}) 161 162 if(${LLVM_PACKAGE_VERSION} VERSION_EQUAL 15 OR ${LLVM_PACKAGE_VERSION} VERSION_GREATER 15) 163 find_library(libclangSupport NAMES clangSupport clang-cpp HINTS ${CLANG_SEARCH}) 164 endif() 165 166 find_library(libclang-shared libclang-cpp.so HINTS ${CLANG_SEARCH}) 167 168 if(libclangBasic STREQUAL "libclangBasic-NOTFOUND") 169 message(FATAL_ERROR "Unable to find clang libraries") 170 endif() 171 172 FOREACH(DIR ${LLVM_INCLUDE_DIRS}) 173 include_directories("${DIR}/../tools/clang/include") 174 ENDFOREACH() 175 176 endif(ENABLE_CLANG_JIT) 177 178 # Set to a string path if system places kernel lib directory in 179 # non-default location. 180 if(NOT DEFINED BCC_KERNEL_MODULES_DIR) 181 set(BCC_KERNEL_MODULES_DIR "/lib/modules") 182 endif() 183 184 if(NOT DEFINED BCC_PROG_TAG_DIR) 185 set(BCC_PROG_TAG_DIR "/var/tmp/bcc") 186 endif() 187 188 # As reported in issue #735, GCC 6 has some behavioral problems when 189 # dealing with -isystem. Hence, skip the warning optimization 190 # altogether on that compiler. 191 option(USINGISYSTEM "using -isystem" ON) 192 execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION) 193 if(USINGISYSTEM AND GCC_VERSION VERSION_LESS 6.0) 194 # iterate over all available directories in LLVM_INCLUDE_DIRS to 195 # generate a correctly tokenized list of parameters 196 foreach(ONE_LLVM_INCLUDE_DIR ${LLVM_INCLUDE_DIRS}) 197 set(CXX_ISYSTEM_DIRS "${CXX_ISYSTEM_DIRS} -isystem ${ONE_LLVM_INCLUDE_DIR}") 198 endforeach() 199 endif() 200 201 set(CMAKE_CXX_STANDARD_REQUIRED ON) 202 if(${LLVM_PACKAGE_VERSION} VERSION_EQUAL 16 OR ${LLVM_PACKAGE_VERSION} VERSION_GREATER 16) 203 set(CMAKE_CXX_STANDARD 17) 204 else() 205 set(CMAKE_CXX_STANDARD 14) 206 endif() 207 208endif(NOT PYTHON_ONLY) 209 210set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") 211set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall ${CXX_ISYSTEM_DIRS}") 212 213add_subdirectory(src) 214add_subdirectory(introspection) 215 216if(ENABLE_CLANG_JIT) 217 if(ENABLE_EXAMPLES) 218 add_subdirectory(examples) 219 endif(ENABLE_EXAMPLES) 220 221 if(ENABLE_MAN) 222 add_subdirectory(man) 223 endif(ENABLE_MAN) 224 225 if(ENABLE_TESTS) 226 add_subdirectory(tests) 227 endif(ENABLE_TESTS) 228 229 add_subdirectory(tools) 230endif(ENABLE_CLANG_JIT) 231 232if(NOT TARGET uninstall) 233 configure_file( 234 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CmakeUninstall.cmake.in" 235 "${CMAKE_CURRENT_BINARY_DIR}/CmakeUninstall.cmake" 236 IMMEDIATE @ONLY) 237 238 add_custom_target(uninstall 239 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/CmakeUninstall.cmake) 240endif() 241