-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCMakeLists.txt
170 lines (143 loc) · 4.08 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# SPDX-License-Identifier: GPL-2.0-only
# Copyright (c) 2020 Facebook
cmake_minimum_required(VERSION 3.16)
project(examples)
# Tell cmake where to find BpfObject module
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake)
# Create directory beforehand, allows parallelization
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/libbpf/libbpf)
# Output .skel.h into generated include directory
set(SKEL_DIR include)
# Output .bpf.o into bpfobj/. Create the dir if not exists
set(BPFOBJECT_DIR obj)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${BPFOBJECT_DIR})
# Build vendored libbpf
include(ExternalProject)
ExternalProject_Add(libbpf
PREFIX libbpf
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/bpftool/libbpf/src
CONFIGURE_COMMAND ""
BUILD_COMMAND COMMAND $(MAKE)
BUILD_STATIC_ONLY=1
OBJDIR=${CMAKE_CURRENT_BINARY_DIR}/libbpf/libbpf
DESTDIR=${CMAKE_CURRENT_BINARY_DIR}/libbpf
INCLUDEDIR=
LIBDIR=
UAPIDIR=
install
BUILD_IN_SOURCE TRUE
INSTALL_COMMAND ""
STEP_TARGETS build
)
ExternalProject_Add(bpftool
PREFIX bpftool
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/bpftool/src
CONFIGURE_COMMAND ""
BUILD_COMMAND COMMAND $(MAKE)
OUTPUT=${CMAKE_CURRENT_BINARY_DIR}/bpftool/
BUILD_IN_SOURCE TRUE
INSTALL_COMMAND ""
STEP_TARGETS build
DEPENDS libbpf
)
# Set BpfObject input parameters -- note this is usually not necessary unless
# you're in a highly vendored environment (like libbpf-bootstrap)
set(BPFOBJECT_BPFTOOL_EXE ${CMAKE_CURRENT_BINARY_DIR}/bpftool/bpftool)
set(BPFOBJECT_VMLINUX_H ${CMAKE_CURRENT_BINARY_DIR}/bpftool/vmlinux.h)
set(LIBBPF_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/libbpf)
set(LIBBPF_LIBRARIES ${CMAKE_CURRENT_BINARY_DIR}/libbpf/libbpf.a)
find_package(BpfObject REQUIRED)
###################
###### CAPMON #####
###################
# SPDX-License-Identifier: GPL-2.0-only
# SPDX-FileCopyrightText: 2023 Casper Andersson <[email protected]>
set(SKEL_LIBRARIES "")
macro(make_bpf_object name)
set(SKEL_LIBRARIES ${SKEL_LIBRARIES} ${name}_skel)
bpf_object(${name} src/bpf/${name}.bpf.c)
add_dependencies(${name}_skel bpftool)
add_dependencies(${name}_skel libbpf)
endmacro()
# BPF Objects
make_bpf_object(capable_std)
make_bpf_object(capable_all)
make_bpf_object(proc_exec)
cmake_minimum_required(VERSION 3.12)
project(capmon VERSION 1.3)
set(CMAKE_C_FLAGS
"${CMAKE_C_FLAGS} -Wall -Wpedantic -Wextra -Wno-overlength-strings"
# -Wno-gnu-zero-variadic-macro-arguments"
)
set(PROJECT_NAME_VERSION "${CMAKE_PROJECT_NAME}-${CMAKE_PROJECT_VERSION}")
# Source Files
set(SRC_FILES
capmon.c
capabilities.c
libcapmon.c
monitor.c
proctrack.c
)
list(TRANSFORM SRC_FILES PREPEND "${PROJECT_SOURCE_DIR}/src/")
configure_file(include/version.h.in include/version.h)
add_executable(capmon ${SRC_FILES})
target_include_directories(capmon PUBLIC
${PROJECT_SOURCE_DIR}/include
${PROJECT_BINARY_DIR}/include
${PROJECT_BINARY_DIR}/bpftool
${PROJECT_BINARY_DIR}/libbpf
)
target_link_libraries(capmon ${SKEL_LIBRARIES})
install(TARGETS capmon
CONFIGURATIONS Debug
RUNTIME DESTINATION bin)
install(TARGETS capmon
CONFIGURATIONS Release
RUNTIME DESTINATION bin)
add_custom_target(dist
COMMAND
mkdir "-p" "${PROJECT_NAME_VERSION}"
COMMAND
cp
"-r"
"../CMakeLists.txt"
"../LICENSE"
"../LICENSES"
"../README.md"
"../.gitignore"
"../.gitmodules"
"../include"
"../src"
"../bpftool"
"../tools"
"../doc"
"../test"
"${PROJECT_NAME_VERSION}"
COMMAND
tar
"-cvzf"
"${PROJECT_NAME_VERSION}.tar.gz"
"${PROJECT_NAME_VERSION}/"
)
set_directory_properties(
PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
"${PROJECT_BINARY_DIR}/include"
)
# Remove dependency builds as well
add_custom_target(distclean
COMMAND
make clean
COMMAND
rm -r ${PROJECT_BINARY_DIR}/bpftool
COMMAND
rm -r ${PROJECT_BINARY_DIR}/libbpf
)
# ==== TEST ====
add_custom_target(test
COMMAND
sudo setcap cap_dac_override,cap_sys_admin+ep ${PROJECT_BINARY_DIR}/capmon
COMMAND
#unshare -r -n ${PROJECT_SOURCE_DIR}/test/test.sh ${PROJECT_BINARY_DIR}/capmon
${PROJECT_SOURCE_DIR}/test/main.sh ${PROJECT_BINARY_DIR}/capmon
DEPENDS ${PROJECT_BINARY_DIR}/capmon
)