-
Notifications
You must be signed in to change notification settings - Fork 43
/
CMakeLists.txt
211 lines (174 loc) · 8.17 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Copyright (c) Prevail Verifier contributors.
# SPDX-License-Identifier: MIT
cmake_minimum_required(VERSION 3.14)
project(ebpf_verifier)
include(FetchContent)
if (IS_DIRECTORY "${PROJECT_SOURCE_DIR}/.git")
# Install Git pre-commit hook
file(COPY scripts/pre-commit scripts/commit-msg
DESTINATION "${PROJECT_SOURCE_DIR}/.git/hooks")
option(VERIFIER_ENABLE_TESTS "Build tests" ON)
else()
option(VERIFIER_ENABLE_TESTS "Build tests" OFF)
endif ()
message("Building tests: ${VERIFIER_ENABLE_TESTS}")
FetchContent_Declare(GSL
GIT_REPOSITORY "https://github.com/microsoft/GSL"
GIT_TAG "v4.0.0"
GIT_SHALLOW ON
)
FetchContent_MakeAvailable(GSL)
get_target_property(GSL_INCLUDE_DIRS Microsoft.GSL::GSL INTERFACE_INCLUDE_DIRECTORIES)
if (VERIFIER_ENABLE_TESTS)
FetchContent_Declare(Catch2
GIT_REPOSITORY "https://github.com/catchorg/Catch2.git"
GIT_TAG "v3.7.1"
GIT_SHALLOW ON
)
FetchContent_MakeAvailable(Catch2)
endif()
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
"${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
if (VERIFIER_ENABLE_TESTS)
find_package(yaml-cpp REQUIRED)
endif()
find_package(Boost REQUIRED)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
list(APPEND CMAKE_CONFIGURATION_TYPES FuzzerDebug)
list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING
"Add the configurations that we need"
FORCE)
set(CMAKE_EXE_LINKER_FLAGS_FUZZERDEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
set(CMAKE_SHARED_LINKER_FLAGS_FUZZERDEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
set(CMAKE_C_FLAGS_FUZZERDEBUG "${CMAKE_C_FLAGS_DEBUG} /fsanitize=address /fsanitize=fuzzer /fsanitize-coverage=inline-bool-flag /fsanitize-coverage=edge /fsanitize-coverage=trace-cmp /fsanitize-coverage=trace-div /ZH:SHA_256")
set(CMAKE_CXX_FLAGS_FUZZERDEBUG "${CMAKE_CXX_FLAGS_DEBUG} /fsanitize=address /fsanitize=fuzzer /fsanitize-coverage=inline-bool-flag /fsanitize-coverage=edge /fsanitize-coverage=trace-cmp /fsanitize-coverage=trace-div /ZH:SHA_256")
find_program(NUGET nuget)
if (NOT NUGET)
message("ERROR: You must first install nuget.exe from https://www.nuget.org/downloads")
else ()
execute_process(COMMAND ${NUGET} install "Boost" -Version 1.81.0 -ExcludeVersion -OutputDirectory ${CMAKE_BINARY_DIR}/packages)
set(BOOST_VERSION 1.81.0)
endif ()
set(Boost_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/packages/boost/lib/native/include)
set(Boost_LIBRARY_DIRS ${CMAKE_BINARY_DIR}/packages/boost_filesystem-vc143/lib/native)
if (VERIFIER_ENABLE_TESTS)
# MSVC's "std:c++20" option is the current standard that supports all the C++17
# features we use. However, cmake can't deal with that here, so we set it below.
set(EXTERNAL_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/packages/yaml-cpp)
include(ExternalProject)
ExternalProject_Add(yaml-cpp
GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
GIT_TAG "yaml-cpp-0.7.0"
GIT_SHALLOW true
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
-DYAML_MSVC_SHARED_RT=ON
-DYAML_CPP_BUILD_TESTS=OFF
-DYAML_CPP_BUILD_TOOLS=OFF
)
set(YAML_CPP_LIBRARIES ${EXTERNAL_INSTALL_LOCATION}/lib/yaml-cpp$<$<CONFIG:DEBUG>:d>.lib)
set(YAML_CPP_INCLUDE_DIR ${EXTERNAL_INSTALL_LOCATION}/include/)
endif()
endif ()
include_directories(./external)
include_directories(./external/bpf_conformance/external/elfio)
include_directories(./src)
include_directories(./external/libbtf)
include_directories(${GSL_INCLUDE_DIRS})
include_directories(${Boost_INCLUDE_DIRS})
include_directories(${YAML_CPP_INCLUDE_DIR})
link_directories(${YAML_CPP_LIBRARY_DIR})
link_directories(${Boost_LIBRARY_DIRS})
file(GLOB LIB_SRC
"./src/*.cpp"
"./src/crab/*.cpp"
"./src/crab_utils/*.cpp"
"./src/linux/gpl/spec_prototypes.cpp"
"./src/linux/linux_platform.cpp"
)
if(VERIFIER_ENABLE_TESTS)
file(GLOB ALL_TEST
"./src/test/test_conformance.cpp"
"./src/test/test_marshal.cpp"
"./src/test/test_print.cpp"
"./src/test/test_verify.cpp"
"./src/test/test_wto.cpp"
"./src/test/test_yaml.cpp"
)
set(LIB_SRC ${LIB_SRC} "./src/test/ebpf_yaml.cpp")
endif()
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
"${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(COMMON_FLAGS -Wall -Wfatal-errors -DSIZEOF_VOID_P=8 -DSIZEOF_LONG=8)
set(RELEASE_FLAGS -O2 -flto=auto -ffat-lto-objects)
set(DEBUG_FLAGS -O0 -g3 -fno-omit-frame-pointer)
set(SANITIZE_FLAGS -fsanitize=address -O1 -fno-omit-frame-pointer)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++20")
endif ()
add_library(ebpfverifier ${LIB_SRC})
if(VERIFIER_ENABLE_TESTS)
add_executable(check src/main/check.cpp src/main/linux_verifier.cpp)
add_executable(tests ${ALL_TEST})
add_executable(run_yaml src/main/run_yaml.cpp)
add_executable(conformance_check src/test/conformance_check.cpp)
endif()
target_include_directories(ebpfverifier PRIVATE ${GSL_INCLUDE_DIRS})
if(VERIFIER_ENABLE_TESTS)
target_include_directories(run_yaml PRIVATE src/test)
set_target_properties(check
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/..")
set_target_properties(tests
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/..")
set_target_properties(run_yaml
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/..")
set_target_properties(conformance_check
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/..")
add_dependencies(tests conformance_check)
endif()
target_compile_options(ebpfverifier PRIVATE ${COMMON_FLAGS})
target_compile_options(ebpfverifier PUBLIC "$<$<CONFIG:DEBUG>:${DEBUG_FLAGS}>")
target_compile_options(ebpfverifier PUBLIC "$<$<CONFIG:RELEASE>:${RELEASE_FLAGS}>")
target_compile_options(ebpfverifier PUBLIC "$<$<CONFIG:SANITIZE>:${SANITIZE_FLAGS}>")
add_subdirectory("external/bpf_conformance/external/elfio")
if(VERIFIER_ENABLE_TESTS)
add_subdirectory("external/bpf_conformance/src")
endif()
add_subdirectory("external/libbtf")
# CMake derives a Visual Studio project GUID from the file path but can be overridden via a property
# (see https://gitlab.kitware.com/cmake/cmake/-/commit/c85367f4). Using a non-constant GUID
# can cause problems if other projects/repos want to reference the ebpfverifier vcxproj file,
# so we force a constant GUID here.
set(ebpfverifier_GUID_CMAKE "7d5b4e68-c0fa-3f86-9405-f6400219b440" CACHE INTERNAL "Project GUID")
set(yaml-cpp_GUID_CMAKE "98d56b8a-d8eb-3d98-b8ee-c83696b4d58a" CACHE INTERNAL "Project GUID")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(ebpfverifier PRIVATE ${YAML_CPP_LIBRARIES})
target_link_libraries(ebpfverifier PRIVATE libbtf)
target_link_libraries(ebpfverifier PRIVATE Microsoft.GSL::GSL)
target_compile_options(ebpfverifier PRIVATE ${COMMON_FLAGS})
if(VERIFIER_ENABLE_TESTS)
target_compile_options(check PRIVATE ${COMMON_FLAGS})
target_compile_options(check PUBLIC "$<$<CONFIG:DEBUG>:${DEBUG_FLAGS}>")
target_compile_options(check PUBLIC "$<$<CONFIG:RELEASE>:${RELEASE_FLAGS}>")
target_compile_options(check PUBLIC "$<$<CONFIG:SANITIZE>:${SANITIZE_FLAGS}>")
target_link_libraries(check PRIVATE ebpfverifier)
message("Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}")
target_compile_options(tests PRIVATE ${COMMON_FLAGS})
target_compile_options(tests PUBLIC "$<$<CONFIG:DEBUG>:${DEBUG_FLAGS}>")
target_compile_options(tests PUBLIC "$<$<CONFIG:RELEASE>:${RELEASE_FLAGS}>")
target_compile_options(tests PUBLIC "$<$<CONFIG:SANITIZE>:${SANITIZE_FLAGS}>")
target_link_libraries(tests PRIVATE ebpfverifier)
target_link_libraries(tests PRIVATE bpf_conformance)
target_link_libraries(tests PRIVATE ${CMAKE_DL_LIBS})
target_link_libraries(tests PRIVATE Threads::Threads)
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)
target_link_libraries(run_yaml PRIVATE ebpfverifier)
target_link_libraries(conformance_check PRIVATE ebpfverifier)
endif()