blob: 1d7dabe58aca027834cd3afe2febe8a6ed740867 [file] [log] [blame]
Brenden Blanco246b9422015-06-05 11:15:27 -07001# Copyright (c) PLUMgrid, Inc.
2# Licensed under the Apache License, Version 2.0 (the "License")
Brenden Blancoa94bd932015-04-26 00:56:42 -07003cmake_minimum_required(VERSION 2.8.7)
4
FUJI Goro9fc04932021-11-19 13:40:01 +09005if (${CMAKE_VERSION} VERSION_EQUAL 3.12.0 OR ${CMAKE_VERSION} VERSION_GREATER 3.12.0)
6 cmake_policy(SET CMP0074 NEW)
7endif()
8
Brenden Blancof275d3d2015-07-06 23:41:23 -07009project(bcc)
Gabor Buella31d4a592017-05-19 15:33:08 +020010if(NOT CMAKE_BUILD_TYPE)
11 set(CMAKE_BUILD_TYPE Release)
12endif()
Brenden Blancocd5cb412015-04-26 09:41:58 -070013
FUJI Goro4c1136e2020-03-18 14:26:43 +090014if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
15 set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "path to install" FORCE)
16endif()
17
Brenden Blancocd5cb412015-04-26 09:41:58 -070018enable_testing()
Brenden Blancoa94bd932015-04-26 00:56:42 -070019
Yonghong Songfbe94dd2019-01-16 15:47:24 -080020# populate submodules (libbpf)
Jianpeng Ma3df26a52021-02-04 11:33:30 +080021if(NOT CMAKE_USE_LIBBPF_PACKAGE)
22 if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/cc/libbpf/src)
23 execute_process(COMMAND git submodule update --init --recursive
Jacky_Yin2ddafc22021-09-10 18:54:27 +080024 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
25 RESULT_VARIABLE UPDATE_RESULT)
26 if(UPDATE_RESULT AND NOT UPDATE_RESULT EQUAL 0)
27 message(WARNING "Failed to update submodule libbpf")
28 endif()
Jianpeng Ma3df26a52021-02-04 11:33:30 +080029 else()
30 execute_process(COMMAND git diff --shortstat ${CMAKE_CURRENT_SOURCE_DIR}/src/cc/libbpf/
31 OUTPUT_VARIABLE DIFF_STATUS)
32 if("${DIFF_STATUS}" STREQUAL "")
33 execute_process(COMMAND git submodule update --init --recursive
Jacky_Yin2ddafc22021-09-10 18:54:27 +080034 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
35 RESULT_VARIABLE UPDATE_RESULT)
36 if(UPDATE_RESULT AND NOT UPDATE_RESULT EQUAL 0)
37 message(WARNING "Failed to update submodule libbpf")
38 endif()
Jianpeng Ma3df26a52021-02-04 11:33:30 +080039 else()
40 message(WARNING "submodule libbpf dirty, so no sync")
41 endif()
42 endif()
Yonghong Songfbe94dd2019-01-16 15:47:24 -080043endif()
44
Jiri Olsa82abd2f2020-05-21 16:51:30 +020045# It's possible to use other kernel headers with
46# KERNEL_INCLUDE_DIRS build variable, like:
47# $ cd <kernel-dir>
48# $ make INSTALL_HDR_PATH=/tmp/headers headers_install
49# $ cd <bcc-dir>
50# $ cmake -DKERNEL_INCLUDE_DIRS=/tmp/headers/include/ ...
51include_directories(${KERNEL_INCLUDE_DIRS})
52
Gary Lin24eeae22021-03-12 11:32:19 +080053option(ENABLE_NO_PIE "Build bcc-lua without PIE" ON)
54
Brenden Blanco974e8912015-09-04 09:36:37 -070055include(cmake/GetGitRevisionDescription.cmake)
56include(cmake/version.cmake)
Brenden Blanco71fc3d52017-06-28 17:37:06 -070057include(CMakeDependentOption)
Brenden Blanco6470bbe2015-09-23 07:23:35 -070058include(GNUInstallDirs)
Simon Liu2d82c8e2017-04-17 09:53:58 -050059include(CheckCXXCompilerFlag)
Yonghong Song75e2f372017-08-23 13:40:47 -070060include(cmake/FindCompilerFlag.cmake)
Brenden Blancod8acf6f2015-06-07 22:32:33 -070061
torgil61c063a2018-12-23 08:20:21 +010062option(ENABLE_LLVM_NATIVECODEGEN "Enable use of llvm nativecodegen module (needed by rw-engine)" ON)
torgil4a7717d2018-12-12 08:12:42 +010063option(ENABLE_RTTI "Enable compiling with real time type information" OFF)
Brenden Blancoe8001c32018-07-23 08:15:56 -070064option(ENABLE_LLVM_SHARED "Enable linking LLVM as a shared library" OFF)
Brenden Blanco7fef6952017-08-22 15:47:12 -070065option(ENABLE_CLANG_JIT "Enable Loading BPF through Clang Frontend" ON)
66option(ENABLE_USDT "Enable User-level Statically Defined Tracing" ON)
Marcelo Juchem8fac7102020-12-23 17:18:41 -080067option(ENABLE_EXAMPLES "Build examples" ON)
Romain Naour910ddaf2018-12-07 22:36:21 +010068option(ENABLE_MAN "Build man pages" ON)
Marcelo Juchem8fac7102020-12-23 17:18:41 -080069option(ENABLE_TESTS "Build tests" ON)
Dave Marchevskyddfedaa2021-12-28 22:45:22 -050070option(RUN_LUA_TESTS "Run lua tests" ON)
Brenden Blanco7fef6952017-08-22 15:47:12 -070071CMAKE_DEPENDENT_OPTION(ENABLE_CPP_API "Enable C++ API" ON "ENABLE_USDT" OFF)
72
Vicent Martiff9ff5d2016-04-20 13:24:54 +020073set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
74
Jiri Olsad4b3bf02019-10-22 21:01:00 +020075if (CMAKE_USE_LIBBPF_PACKAGE)
76 find_package(LibBpf)
77endif()
78
Marcelo Juchemc06208f2020-12-29 11:25:35 -080079if(NOT PYTHON_ONLY)
Brenden Blancoa94bd932015-04-26 00:56:42 -070080find_package(LLVM REQUIRED CONFIG)
FUJI Goro9fc04932021-11-19 13:40:01 +090081message(STATUS "Found LLVM: ${LLVM_INCLUDE_DIRS} ${LLVM_PACKAGE_VERSION} (Use LLVM_ROOT envronment variable for another version of LLVM)")
Brenden Blanco25245bf2015-05-10 12:15:06 -070082
Marcelo Juchemc06208f2020-12-29 11:25:35 -080083if(ENABLE_CLANG_JIT)
84find_package(BISON)
85find_package(FLEX)
86find_package(LibElf REQUIRED)
Andreas Ziegler8ddbf3b2021-04-29 12:18:40 +020087find_package(LibDebuginfod)
Dale Hamel2d099cd2020-02-11 21:10:52 -050088if(CLANG_DIR)
89 set(CMAKE_FIND_ROOT_PATH "${CLANG_DIR}")
90 include_directories("${CLANG_DIR}/include")
91endif()
92
Brenden Blanco25245bf2015-05-10 12:15:06 -070093# clang is linked as a library, but the library path searching is
94# primitively supported, unlike libLLVM
Brenden Blancof275d3d2015-07-06 23:41:23 -070095set(CLANG_SEARCH "/opt/local/llvm/lib;/usr/lib/llvm-3.7/lib;${LLVM_LIBRARY_DIRS}")
Dominique Martinet1599c2e2020-04-17 21:16:03 +020096find_library(libclangAnalysis NAMES clangAnalysis clang-cpp HINTS ${CLANG_SEARCH})
97find_library(libclangAST NAMES clangAST clang-cpp HINTS ${CLANG_SEARCH})
98find_library(libclangBasic NAMES clangBasic clang-cpp HINTS ${CLANG_SEARCH})
99find_library(libclangCodeGen NAMES clangCodeGen clang-cpp HINTS ${CLANG_SEARCH})
100find_library(libclangDriver NAMES clangDriver clang-cpp HINTS ${CLANG_SEARCH})
101find_library(libclangEdit NAMES clangEdit clang-cpp HINTS ${CLANG_SEARCH})
102find_library(libclangFrontend NAMES clangFrontend clang-cpp HINTS ${CLANG_SEARCH})
103find_library(libclangLex NAMES clangLex clang-cpp HINTS ${CLANG_SEARCH})
104find_library(libclangParse NAMES clangParse clang-cpp HINTS ${CLANG_SEARCH})
105find_library(libclangRewrite NAMES clangRewrite clang-cpp HINTS ${CLANG_SEARCH})
106find_library(libclangSema NAMES clangSema clang-cpp HINTS ${CLANG_SEARCH})
107find_library(libclangSerialization NAMES clangSerialization clang-cpp HINTS ${CLANG_SEARCH})
108find_library(libclangASTMatchers NAMES clangASTMatchers clang-cpp HINTS ${CLANG_SEARCH})
Luca Boccassie46997e2021-01-01 19:04:37 +0000109find_library(libclang-shared libclang-cpp.so HINTS ${CLANG_SEARCH})
Brenden Blanco83102912015-06-09 17:43:27 -0700110if(libclangBasic STREQUAL "libclangBasic-NOTFOUND")
111 message(FATAL_ERROR "Unable to find clang libraries")
112endif()
Alexei Starovoitov955f3b62015-06-09 19:37:38 -0700113FOREACH(DIR ${LLVM_INCLUDE_DIRS})
114 include_directories("${DIR}/../tools/clang/include")
115ENDFOREACH()
Marcelo Juchemc06208f2020-12-29 11:25:35 -0800116endif(ENABLE_CLANG_JIT)
Brenden Blancoa94bd932015-04-26 00:56:42 -0700117
Ragnar Dahléndbc21ea2016-04-05 23:32:02 +0100118# Set to a string path if system places kernel lib directory in
119# non-default location.
120if(NOT DEFINED BCC_KERNEL_MODULES_DIR)
121 set(BCC_KERNEL_MODULES_DIR "/lib/modules")
122endif()
123
Alexei Starovoitov4f47e3b2017-09-06 19:57:21 -0700124if(NOT DEFINED BCC_PROG_TAG_DIR)
125 set(BCC_PROG_TAG_DIR "/var/tmp/bcc")
126endif()
127
Marco Leogrande0c461c92016-10-06 12:28:40 -0700128# As reported in issue #735, GCC 6 has some behavioral problems when
129# dealing with -isystem. Hence, skip the warning optimization
130# altogether on that compiler.
Simon Liu1b38b9a2017-04-14 12:50:57 -0500131option(USINGISYSTEM "using -isystem" ON)
Marco Leogrande0c461c92016-10-06 12:28:40 -0700132execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
Simon Liu1b38b9a2017-04-14 12:50:57 -0500133if (USINGISYSTEM AND GCC_VERSION VERSION_LESS 6.0)
Marco Leogrande0c461c92016-10-06 12:28:40 -0700134 # iterate over all available directories in LLVM_INCLUDE_DIRS to
135 # generate a correctly tokenized list of parameters
136 foreach(ONE_LLVM_INCLUDE_DIR ${LLVM_INCLUDE_DIRS})
137 set(CXX_ISYSTEM_DIRS "${CXX_ISYSTEM_DIRS} -isystem ${ONE_LLVM_INCLUDE_DIR}")
138 endforeach()
139endif()
140
Teng Qin84757ce2017-08-25 15:56:23 -0700141set(CMAKE_CXX_STANDARD_REQUIRED ON)
yonghong-song903513e2019-09-24 20:08:31 -0700142set(CMAKE_CXX_STANDARD 14)
Simon Liu2d82c8e2017-04-17 09:53:58 -0500143
Marcelo Juchemc06208f2020-12-29 11:25:35 -0800144endif(NOT PYTHON_ONLY)
Brenden Blancoa94bd932015-04-26 00:56:42 -0700145
Brenden Blanco7fef6952017-08-22 15:47:12 -0700146set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
147set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall ${CXX_ISYSTEM_DIRS}")
148
149add_subdirectory(src)
Martin KaFai Lauf89cb402017-10-19 23:52:54 -0700150add_subdirectory(introspection)
Brenden Blanco7fef6952017-08-22 15:47:12 -0700151if(ENABLE_CLANG_JIT)
Marcelo Juchem8fac7102020-12-23 17:18:41 -0800152if(ENABLE_EXAMPLES)
Brenden Blanco46176a12015-07-07 13:05:22 -0700153add_subdirectory(examples)
Marcelo Juchem8fac7102020-12-23 17:18:41 -0800154endif(ENABLE_EXAMPLES)
Romain Naour910ddaf2018-12-07 22:36:21 +0100155if(ENABLE_MAN)
Brenden Blancoc175cf82015-11-25 18:22:42 -0800156add_subdirectory(man)
Romain Naour910ddaf2018-12-07 22:36:21 +0100157endif(ENABLE_MAN)
Marcelo Juchem8fac7102020-12-23 17:18:41 -0800158if(ENABLE_TESTS)
Brenden Blancocd5cb412015-04-26 09:41:58 -0700159add_subdirectory(tests)
Marcelo Juchem8fac7102020-12-23 17:18:41 -0800160endif(ENABLE_TESTS)
Brenden Blancoc175cf82015-11-25 18:22:42 -0800161add_subdirectory(tools)
Brenden Blanco7fef6952017-08-22 15:47:12 -0700162endif(ENABLE_CLANG_JIT)