Sync with latest libbpf repo
[platform/upstream/bcc.git] / CMakeLists.txt
1 # Copyright (c) PLUMgrid, Inc.
2 # Licensed under the Apache License, Version 2.0 (the "License")
3 cmake_minimum_required(VERSION 2.8.7)
4
5 if (${CMAKE_VERSION} VERSION_EQUAL 3.12.0 OR ${CMAKE_VERSION} VERSION_GREATER 3.12.0)
6   cmake_policy(SET CMP0074 NEW)
7 endif()
8
9 project(bcc)
10 if(NOT CMAKE_BUILD_TYPE)
11   set(CMAKE_BUILD_TYPE Release)
12 endif()
13
14 if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
15   set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "path to install" FORCE)
16 endif()
17
18 enable_testing()
19
20 # populate submodules (libbpf)
21 if(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
24                   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()
29    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
34                     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()
39         else()
40                 message(WARNING "submodule libbpf dirty, so no sync")
41         endif()
42    endif()
43 endif()
44
45 # 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/ ...
51 include_directories(${KERNEL_INCLUDE_DIRS})
52
53 option(ENABLE_NO_PIE "Build bcc-lua without PIE" ON)
54
55 include(cmake/GetGitRevisionDescription.cmake)
56 include(cmake/version.cmake)
57 include(CMakeDependentOption)
58 include(GNUInstallDirs)
59 include(CheckCXXCompilerFlag)
60 include(cmake/FindCompilerFlag.cmake)
61
62 option(ENABLE_LLVM_NATIVECODEGEN "Enable use of llvm nativecodegen module (needed by rw-engine)" ON)
63 option(ENABLE_RTTI "Enable compiling with real time type information" OFF)
64 option(ENABLE_LLVM_SHARED "Enable linking LLVM as a shared library" OFF)
65 option(ENABLE_CLANG_JIT "Enable Loading BPF through Clang Frontend" ON)
66 option(ENABLE_USDT "Enable User-level Statically Defined Tracing" ON)
67 option(ENABLE_EXAMPLES "Build examples" ON)
68 option(ENABLE_MAN "Build man pages" ON)
69 option(ENABLE_TESTS "Build tests" ON)
70 option(RUN_LUA_TESTS "Run lua tests" ON)
71 CMAKE_DEPENDENT_OPTION(ENABLE_CPP_API "Enable C++ API" ON "ENABLE_USDT" OFF)
72
73 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
74
75 if (CMAKE_USE_LIBBPF_PACKAGE)
76   find_package(LibBpf)
77 endif()
78
79 if(NOT PYTHON_ONLY)
80 find_package(LLVM REQUIRED CONFIG)
81 message(STATUS "Found LLVM: ${LLVM_INCLUDE_DIRS} ${LLVM_PACKAGE_VERSION} (Use LLVM_ROOT envronment variable for another version of LLVM)")
82
83 if(ENABLE_CLANG_JIT)
84 find_package(BISON)
85 find_package(FLEX)
86 find_package(LibElf REQUIRED)
87 find_package(LibDebuginfod)
88 if(CLANG_DIR)
89   set(CMAKE_FIND_ROOT_PATH "${CLANG_DIR}")
90   include_directories("${CLANG_DIR}/include")
91 endif()
92
93 # clang is linked as a library, but the library path searching is
94 # primitively supported, unlike libLLVM
95 set(CLANG_SEARCH "/opt/local/llvm/lib;/usr/lib/llvm-3.7/lib;${LLVM_LIBRARY_DIRS}")
96 find_library(libclangAnalysis NAMES clangAnalysis clang-cpp HINTS ${CLANG_SEARCH})
97 find_library(libclangAST NAMES clangAST clang-cpp HINTS ${CLANG_SEARCH})
98 find_library(libclangBasic NAMES clangBasic clang-cpp HINTS ${CLANG_SEARCH})
99 find_library(libclangCodeGen NAMES clangCodeGen clang-cpp HINTS ${CLANG_SEARCH})
100 find_library(libclangDriver NAMES clangDriver clang-cpp HINTS ${CLANG_SEARCH})
101 find_library(libclangEdit NAMES clangEdit clang-cpp HINTS ${CLANG_SEARCH})
102 find_library(libclangFrontend NAMES clangFrontend clang-cpp HINTS ${CLANG_SEARCH})
103 find_library(libclangLex NAMES clangLex clang-cpp HINTS ${CLANG_SEARCH})
104 find_library(libclangParse NAMES clangParse clang-cpp HINTS ${CLANG_SEARCH})
105 find_library(libclangRewrite NAMES clangRewrite clang-cpp HINTS ${CLANG_SEARCH})
106 find_library(libclangSema NAMES clangSema clang-cpp HINTS ${CLANG_SEARCH})
107 find_library(libclangSerialization NAMES clangSerialization clang-cpp HINTS ${CLANG_SEARCH})
108 find_library(libclangASTMatchers NAMES clangASTMatchers clang-cpp HINTS ${CLANG_SEARCH})
109 find_library(libclang-shared libclang-cpp.so HINTS ${CLANG_SEARCH})
110 if(libclangBasic STREQUAL "libclangBasic-NOTFOUND")
111   message(FATAL_ERROR "Unable to find clang libraries")
112 endif()
113 FOREACH(DIR ${LLVM_INCLUDE_DIRS})
114   include_directories("${DIR}/../tools/clang/include")
115 ENDFOREACH()
116 endif(ENABLE_CLANG_JIT)
117
118 # Set to a string path if system places kernel lib directory in
119 # non-default location.
120 if(NOT DEFINED BCC_KERNEL_MODULES_DIR)
121   set(BCC_KERNEL_MODULES_DIR "/lib/modules")
122 endif()
123
124 if(NOT DEFINED BCC_PROG_TAG_DIR)
125   set(BCC_PROG_TAG_DIR "/var/tmp/bcc")
126 endif()
127
128 # 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.
131 option(USINGISYSTEM "using -isystem" ON)
132 execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
133 if (USINGISYSTEM AND GCC_VERSION VERSION_LESS 6.0)
134   # 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()
139 endif()
140
141 set(CMAKE_CXX_STANDARD_REQUIRED ON)
142 set(CMAKE_CXX_STANDARD 14)
143
144 endif(NOT PYTHON_ONLY)
145
146 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
147 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall ${CXX_ISYSTEM_DIRS}")
148
149 add_subdirectory(src)
150 add_subdirectory(introspection)
151 if(ENABLE_CLANG_JIT)
152 if(ENABLE_EXAMPLES)
153 add_subdirectory(examples)
154 endif(ENABLE_EXAMPLES)
155 if(ENABLE_MAN)
156 add_subdirectory(man)
157 endif(ENABLE_MAN)
158 if(ENABLE_TESTS)
159 add_subdirectory(tests)
160 endif(ENABLE_TESTS)
161 add_subdirectory(tools)
162 endif(ENABLE_CLANG_JIT)