Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 1 | # Copyright (c) 2018 Cisco and/or its affiliates. |
| 2 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | # you may not use this file except in compliance with the License. |
| 4 | # You may obtain a copy of the License at: |
| 5 | # |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # |
| 8 | # Unless required by applicable law or agreed to in writing, software |
| 9 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | # See the License for the specific language governing permissions and |
| 12 | # limitations under the License. |
| 13 | |
Damjan Marion | af7892c | 2020-10-22 14:23:47 +0200 | [diff] [blame] | 14 | cmake_minimum_required(VERSION 3.10) |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 15 | |
Damjan Marion | d22f190 | 2021-04-14 18:48:22 +0200 | [diff] [blame] | 16 | set(CMAKE_C_COMPILER_NAMES |
| 17 | clang-12 |
| 18 | clang-11 |
| 19 | clang-10 |
| 20 | clang-9 |
| 21 | gcc-10 |
| 22 | gcc-9 |
| 23 | cc |
| 24 | ) |
Damjan Marion | 4ba16a4 | 2020-04-28 13:29:37 +0200 | [diff] [blame] | 25 | |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 26 | project(vpp C) |
| 27 | |
Damjan Marion | 0d39cba | 2021-05-10 14:51:44 +0200 | [diff] [blame] | 28 | if(CMAKE_VERSION VERSION_LESS 3.12) |
| 29 | macro(add_compile_definitions defs) |
| 30 | foreach(d ${defs}) |
| 31 | add_compile_options(-D${d}) |
| 32 | endforeach() |
| 33 | endmacro() |
| 34 | endif() |
Damjan Marion | 49a2925 | 2021-05-05 10:24:38 +0200 | [diff] [blame] | 35 | |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 36 | include(CheckCCompilerFlag) |
Damjan Marion | af7892c | 2020-10-22 14:23:47 +0200 | [diff] [blame] | 37 | include(CheckIPOSupported) |
Nick Brown | e3cf4d0 | 2021-09-15 14:25:40 +0100 | [diff] [blame] | 38 | include(GNUInstallDirs) |
Damjan Marion | ff42693 | 2019-01-26 14:12:25 +0100 | [diff] [blame] | 39 | include(cmake/misc.cmake) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 40 | include(cmake/cpu.cmake) |
| 41 | include(cmake/ccache.cmake) |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 42 | |
| 43 | ############################################################################## |
Damjan Marion | c6c0246 | 2018-08-31 17:38:57 +0200 | [diff] [blame] | 44 | # VPP Version |
| 45 | ############################################################################## |
| 46 | execute_process( |
| 47 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 48 | COMMAND scripts/version |
| 49 | OUTPUT_VARIABLE VPP_VERSION |
| 50 | OUTPUT_STRIP_TRAILING_WHITESPACE |
| 51 | ) |
| 52 | string(REPLACE "-" ";" VPP_LIB_VERSION ${VPP_VERSION}) |
| 53 | list(GET VPP_LIB_VERSION 0 VPP_LIB_VERSION) |
| 54 | |
| 55 | ############################################################################## |
Damjan Marion | dd39529 | 2019-01-15 00:36:03 +0100 | [diff] [blame] | 56 | # cross compiling |
| 57 | ############################################################################## |
| 58 | if(CMAKE_CROSSCOMPILING) |
| 59 | set(CMAKE_IGNORE_PATH |
| 60 | /usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/ |
| 61 | /usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/lib/ |
| 62 | ) |
| 63 | endif() |
Damjan Marion | 6491136 | 2019-01-18 17:09:44 +0100 | [diff] [blame] | 64 | set(CMAKE_C_COMPILER_TARGET ${CMAKE_SYSTEM_PROCESSOR}-linux-gnu) |
Damjan Marion | dd39529 | 2019-01-15 00:36:03 +0100 | [diff] [blame] | 65 | |
| 66 | ############################################################################## |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 67 | # build config |
| 68 | ############################################################################## |
Damjan Marion | 847d528 | 2019-01-17 18:25:11 +0100 | [diff] [blame] | 69 | check_c_compiler_flag("-Wno-address-of-packed-member" |
| 70 | compiler_flag_no_address_of_packed_member) |
Nick Brown | e3cf4d0 | 2021-09-15 14:25:40 +0100 | [diff] [blame] | 71 | set(VPP_RUNTIME_DIR ${CMAKE_INSTALL_BINDIR} CACHE STRING "Relative runtime directory path") |
| 72 | set(VPP_LIBRARY_DIR ${CMAKE_INSTALL_LIBDIR} CACHE STRING "Relative library directory path") |
Damjan Marion | 847d528 | 2019-01-17 18:25:11 +0100 | [diff] [blame] | 73 | |
Damjan Marion | 599efc6 | 2020-05-07 16:49:45 +0200 | [diff] [blame] | 74 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${VPP_RUNTIME_DIR}) |
| 75 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${VPP_LIBRARY_DIR}) |
Damjan Marion | 88b2e36 | 2021-04-29 18:47:25 +0200 | [diff] [blame] | 76 | set(VPP_BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeFiles) |
Damjan Marion | 7cf80af | 2021-05-25 19:28:21 +0200 | [diff] [blame] | 77 | set(PYENV PYTHONPYCACHEPREFIX=${CMAKE_BINARY_DIR}/CMakeFile/__pycache__) |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 78 | |
Damjan Marion | 847d528 | 2019-01-17 18:25:11 +0100 | [diff] [blame] | 79 | if (CMAKE_BUILD_TYPE) |
Damjan Marion | 0d39cba | 2021-05-10 14:51:44 +0200 | [diff] [blame] | 80 | add_compile_options(-g -fPIC -Werror -Wall) |
Damjan Marion | 33ed3e4 | 2018-08-27 15:59:30 +0200 | [diff] [blame] | 81 | endif() |
| 82 | |
Damjan Marion | 847d528 | 2019-01-17 18:25:11 +0100 | [diff] [blame] | 83 | if (compiler_flag_no_address_of_packed_member) |
Damjan Marion | 0d39cba | 2021-05-10 14:51:44 +0200 | [diff] [blame] | 84 | add_compile_options(-Wno-address-of-packed-member) |
Damjan Marion | 847d528 | 2019-01-17 18:25:11 +0100 | [diff] [blame] | 85 | endif() |
| 86 | |
Damjan Marion | 0d39cba | 2021-05-10 14:51:44 +0200 | [diff] [blame] | 87 | string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LC) |
Damjan Marion | 847d528 | 2019-01-17 18:25:11 +0100 | [diff] [blame] | 88 | string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UC) |
| 89 | |
Damjan Marion | 0d39cba | 2021-05-10 14:51:44 +0200 | [diff] [blame] | 90 | set(CMAKE_C_FLAGS_RELEASE "") |
| 91 | set(CMAKE_C_FLAGS_DEBUG "") |
Damjan Marion | 952a7b8 | 2019-11-19 17:58:36 +0100 | [diff] [blame] | 92 | |
Damjan Marion | 0d39cba | 2021-05-10 14:51:44 +0200 | [diff] [blame] | 93 | if (${CMAKE_BUILD_TYPE_LC} MATCHES "release") |
| 94 | add_compile_options(-O3 -fstack-protector -fno-common) |
| 95 | add_compile_definitions(_FORTIFY_SOURCE=2) |
Damjan Marion | f2912e0 | 2021-07-16 12:44:22 +0200 | [diff] [blame] | 96 | set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-pie") |
Damjan Marion | 0d39cba | 2021-05-10 14:51:44 +0200 | [diff] [blame] | 97 | elseif (${CMAKE_BUILD_TYPE_LC} MATCHES "debug") |
| 98 | add_compile_options(-O0 -fstack-protector -fno-common) |
| 99 | add_compile_definitions(CLIB_DEBUG) |
| 100 | elseif (${CMAKE_BUILD_TYPE_LC} MATCHES "coverity") |
| 101 | add_compile_options(-O0) |
| 102 | add_compile_definitions(__COVERITY__) |
| 103 | elseif (${CMAKE_BUILD_TYPE_LC} MATCHES "gcov") |
| 104 | add_compile_options(-O0 -fprofile-arcs -ftest-coverage) |
| 105 | add_compile_definitions(CLIB_DEBUG CLIB_GCOV) |
| 106 | endif() |
| 107 | |
| 108 | set(BUILD_TYPES release debug coverity gcov) |
Damjan Marion | 3a533cd | 2021-05-03 12:40:27 +0200 | [diff] [blame] | 109 | string(REPLACE ";" " " BUILD_TYPES_STR "${BUILD_TYPES}") |
Damjan Marion | 952a7b8 | 2019-11-19 17:58:36 +0100 | [diff] [blame] | 110 | set_property(CACHE CMAKE_BUILD_TYPE PROPERTY |
Damjan Marion | 3a533cd | 2021-05-03 12:40:27 +0200 | [diff] [blame] | 111 | HELPSTRING "Build type - valid options are: ${BUILD_TYPES_STR}") |
Damjan Marion | 952a7b8 | 2019-11-19 17:58:36 +0100 | [diff] [blame] | 112 | |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 113 | ############################################################################## |
Damjan Marion | af7892c | 2020-10-22 14:23:47 +0200 | [diff] [blame] | 114 | # link time optimizations |
| 115 | ############################################################################## |
| 116 | if (CMAKE_BUILD_TYPE_UC STREQUAL "RELEASE") |
| 117 | check_ipo_supported(RESULT _result) |
| 118 | if (_result) |
| 119 | option(VPP_USE_LTO "Link time optimization of release binaries" ON) |
| 120 | endif() |
| 121 | endif() |
| 122 | |
| 123 | ############################################################################## |
Benoît Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame] | 124 | # sanitizers |
| 125 | ############################################################################## |
| 126 | |
Damjan Marion | 599efc6 | 2020-05-07 16:49:45 +0200 | [diff] [blame] | 127 | option(VPP_ENABLE_SANITIZE_ADDR "Enable Address Sanitizer" OFF) |
Benoît Ganne | 173484f | 2020-06-16 12:05:07 +0200 | [diff] [blame] | 128 | set(VPP_SANITIZE_ADDR_OPTIONS |
Damjan Marion | ec3a3f1 | 2020-05-21 19:09:13 +0200 | [diff] [blame] | 129 | "unmap_shadow_on_exit=1:disable_coredump=0:abort_on_error=1:detect_leaks=0" |
| 130 | CACHE |
| 131 | STRING "Address sanitizer arguments" |
| 132 | ) |
| 133 | |
Damjan Marion | 599efc6 | 2020-05-07 16:49:45 +0200 | [diff] [blame] | 134 | if (VPP_ENABLE_SANITIZE_ADDR) |
Damjan Marion | 0d39cba | 2021-05-10 14:51:44 +0200 | [diff] [blame] | 135 | add_compile_options(-fsanitize=address) |
| 136 | add_compile_definitions(CLIB_SANITIZE_ADDR) |
Benoît Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame] | 137 | set(CMAKE_EXE_LINKER_FLAGS "-fsanitize=address ${CMAKE_EXE_LINKER_FLAGS}") |
| 138 | set(CMAKE_SHARED_LINKER_FLAGS "-fsanitize=address ${CMAKE_SHARED_LINKER_FLAGS}") |
Damjan Marion | 599efc6 | 2020-05-07 16:49:45 +0200 | [diff] [blame] | 139 | endif (VPP_ENABLE_SANITIZE_ADDR) |
Benoît Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame] | 140 | |
| 141 | ############################################################################## |
Benoît Ganne | f89bbbe | 2021-03-04 14:31:03 +0100 | [diff] [blame] | 142 | # trajectory trace |
| 143 | ############################################################################## |
| 144 | |
| 145 | option(VPP_ENABLE_TRAJECTORY_TRACE "Build vpp with trajectory tracing enabled" OFF) |
| 146 | if(VPP_ENABLE_TRAJECTORY_TRACE) |
Damjan Marion | 0d39cba | 2021-05-10 14:51:44 +0200 | [diff] [blame] | 147 | add_compile_definitions(VLIB_BUFFER_TRACE_TRAJECTORY=1) |
Benoît Ganne | f89bbbe | 2021-03-04 14:31:03 +0100 | [diff] [blame] | 148 | endif() |
| 149 | |
| 150 | ############################################################################## |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 151 | # install config |
| 152 | ############################################################################## |
Nathan Moos | 2c91922 | 2021-01-15 13:28:34 -0800 | [diff] [blame] | 153 | option(VPP_SET_RPATH "Set rpath for resulting binaries and libraries." ON) |
| 154 | if(VPP_SET_RPATH) |
| 155 | set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") |
| 156 | endif() |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 157 | set(CMAKE_INSTALL_MESSAGE NEVER) |
| 158 | |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 159 | include_directories ( |
| 160 | ${CMAKE_SOURCE_DIR} |
Damjan Marion | 88b2e36 | 2021-04-29 18:47:25 +0200 | [diff] [blame] | 161 | ${VPP_BINARY_DIR} |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 162 | ) |
| 163 | set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "vpp") |
| 164 | |
| 165 | set(THREADS_PREFER_PTHREAD_FLAG ON) |
| 166 | find_package(Threads REQUIRED) |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 167 | |
Damjan Marion | 1ee346a | 2019-03-18 17:06:51 +0100 | [diff] [blame] | 168 | include(cmake/syscall.cmake) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 169 | include(cmake/api.cmake) |
Damjan Marion | 4553c95 | 2018-08-26 11:04:40 +0200 | [diff] [blame] | 170 | include(cmake/library.cmake) |
| 171 | include(cmake/exec.cmake) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 172 | include(cmake/plugin.cmake) |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 173 | |
| 174 | ############################################################################## |
Damjan Marion | 0abd4a2 | 2018-08-28 12:57:29 +0200 | [diff] [blame] | 175 | # subdirs - order matters |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 176 | ############################################################################## |
Damjan Marion | 599efc6 | 2020-05-07 16:49:45 +0200 | [diff] [blame] | 177 | option(VPP_HOST_TOOLS_ONLY "Build only host tools" OFF) |
| 178 | if(VPP_HOST_TOOLS_ONLY) |
| 179 | set(SUBDIRS tools/vppapigen cmake) |
| 180 | elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") |
Klement Sekera | efd4d70 | 2021-04-20 19:21:36 +0200 | [diff] [blame] | 181 | find_package(OpenSSL) |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 182 | set(SUBDIRS |
Arthur de Kerhor | b2819dd | 2021-09-20 14:47:47 +0200 | [diff] [blame] | 183 | vppinfra svm vlib vlibmemory vlibapi vnet vpp vat vat2 vcl vpp-api |
| 184 | plugins tools/vppapigen tools/g2 tools/perftool cmake pkg |
Dave Barach | 0729f64 | 2019-03-24 16:25:03 -0400 | [diff] [blame] | 185 | tools/appimage |
Damjan Marion | 0fa900e | 2018-09-12 12:12:36 +0200 | [diff] [blame] | 186 | ) |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 187 | elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin") |
| 188 | set(SUBDIRS vppinfra) |
| 189 | else() |
| 190 | message(FATAL_ERROR "Unsupported system: ${CMAKE_SYSTEM_NAME}") |
| 191 | endif() |
| 192 | |
| 193 | foreach(DIR ${SUBDIRS}) |
Damjan Marion | 88b2e36 | 2021-04-29 18:47:25 +0200 | [diff] [blame] | 194 | add_subdirectory(${DIR} ${VPP_BINARY_DIR}/${DIR}) |
Damjan Marion | 0abd4a2 | 2018-08-28 12:57:29 +0200 | [diff] [blame] | 195 | endforeach() |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 196 | |
Damjan Marion | eeadc14 | 2018-09-13 20:02:12 +0200 | [diff] [blame] | 197 | ############################################################################## |
Damjan Marion | c6c0246 | 2018-08-31 17:38:57 +0200 | [diff] [blame] | 198 | # detect if we are inside git repo and add configure dependency |
| 199 | ############################################################################## |
| 200 | execute_process( |
| 201 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 202 | COMMAND git rev-parse --show-toplevel |
| 203 | OUTPUT_VARIABLE VPP_GIT_TOPLEVEL_DIR |
| 204 | OUTPUT_STRIP_TRAILING_WHITESPACE |
Damjan Marion | 6077c97 | 2019-01-28 17:55:59 +0100 | [diff] [blame] | 205 | ERROR_QUIET |
Damjan Marion | c6c0246 | 2018-08-31 17:38:57 +0200 | [diff] [blame] | 206 | ) |
| 207 | |
| 208 | if (VPP_GIT_TOPLEVEL_DIR) |
| 209 | set_property( |
| 210 | DIRECTORY APPEND PROPERTY |
| 211 | CMAKE_CONFIGURE_DEPENDS ${VPP_GIT_TOPLEVEL_DIR}/.git/index |
| 212 | ) |
| 213 | endif() |
| 214 | |
| 215 | ############################################################################## |
Damjan Marion | 88b2e36 | 2021-04-29 18:47:25 +0200 | [diff] [blame] | 216 | # custom targets |
| 217 | ############################################################################## |
| 218 | |
| 219 | add_custom_target(run |
| 220 | COMMAND ./${VPP_RUNTIME_DIR}/vpp -c startup.conf |
| 221 | COMMENT "Starting VPP..." |
| 222 | USES_TERMINAL |
| 223 | ) |
| 224 | |
| 225 | add_custom_target(debug |
| 226 | COMMAND gdb --args ./${VPP_RUNTIME_DIR}/vpp -c startup.conf |
| 227 | COMMENT "Starting VPP in the debugger..." |
| 228 | USES_TERMINAL |
| 229 | ) |
| 230 | |
Damjan Marion | c898324 | 2021-04-30 19:31:09 +0200 | [diff] [blame] | 231 | add_custom_target(config |
Damjan Marion | 88b2e36 | 2021-04-29 18:47:25 +0200 | [diff] [blame] | 232 | COMMAND ccmake ${CMAKE_BINARY_DIR} |
| 233 | COMMENT "Starting Configuration TUI..." |
| 234 | USES_TERMINAL |
| 235 | ) |
| 236 | |
Damjan Marion | 3a533cd | 2021-05-03 12:40:27 +0200 | [diff] [blame] | 237 | foreach(bt ${BUILD_TYPES}) |
| 238 | add_custom_target(set-build-type-${bt} |
| 239 | COMMAND cmake -DCMAKE_BUILD_TYPE:STRING=${bt} . |
| 240 | COMMENT "Changing build type to ${bt}" |
| 241 | USES_TERMINAL |
| 242 | ) |
| 243 | endforeach() |
| 244 | |
Damjan Marion | 0d39cba | 2021-05-10 14:51:44 +0200 | [diff] [blame] | 245 | mark_as_advanced(CLEAR |
| 246 | CMAKE_C_FLAGS |
| 247 | CMAKE_C_COMPILER |
| 248 | CMAKE_EXPORT_COMPILE_COMMANDS |
| 249 | CMAKE_INSTALL_PREFIX |
| 250 | CMAKE_LINKER |
| 251 | CMAKE_SHARED_LINKER_FLAGS |
| 252 | CMAKE_VERBOSE_MAKEFILE |
| 253 | ) |
| 254 | |
Damjan Marion | 88b2e36 | 2021-04-29 18:47:25 +0200 | [diff] [blame] | 255 | ############################################################################## |
Damjan Marion | c6c0246 | 2018-08-31 17:38:57 +0200 | [diff] [blame] | 256 | # print configuration |
| 257 | ############################################################################## |
| 258 | message(STATUS "Configuration:") |
Damjan Marion | f2912e0 | 2021-07-16 12:44:22 +0200 | [diff] [blame] | 259 | pr("VPP version" ${VPP_VERSION}) |
| 260 | pr("VPP library version" ${VPP_LIB_VERSION}) |
| 261 | pr("GIT toplevel dir" ${VPP_GIT_TOPLEVEL_DIR}) |
| 262 | pr("Build type" ${CMAKE_BUILD_TYPE}) |
| 263 | pr("C flags" ${CMAKE_C_FLAGS}${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UC}}) |
| 264 | pr("Linker flags (apps)" ${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UC}}) |
| 265 | pr("Linker flags (libs)" ${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UC}}) |
| 266 | pr("Host processor" ${CMAKE_HOST_SYSTEM_PROCESSOR}) |
| 267 | pr("Target processor" ${CMAKE_SYSTEM_PROCESSOR}) |
| 268 | pr("Prefix path" ${CMAKE_PREFIX_PATH}) |
| 269 | pr("Install prefix" ${CMAKE_INSTALL_PREFIX}) |