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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 cmake_minimum_required(VERSION 3.13)
16 set(CMAKE_C_COMPILER_NAMES clang gcc cc)
20 if(NOT DEFINED CMAKE_INSTALL_LIBDIR AND EXISTS "/etc/debian_version")
21 set(CMAKE_INSTALL_LIBDIR "lib/${CMAKE_LIBRARY_ARCHITECTURE}")
24 include(CheckCCompilerFlag)
25 include(CheckIPOSupported)
26 include(GNUInstallDirs)
27 include(cmake/misc.cmake)
28 include(cmake/cpu.cmake)
29 include(cmake/ccache.cmake)
31 ##############################################################################
33 ##############################################################################
35 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
36 COMMAND scripts/version
37 OUTPUT_VARIABLE VPP_VERSION
38 OUTPUT_STRIP_TRAILING_WHITESPACE
40 string(REPLACE "-" ";" VPP_LIB_VERSION ${VPP_VERSION})
41 list(GET VPP_LIB_VERSION 0 VPP_LIB_VERSION)
43 ##############################################################################
45 ##############################################################################
47 set(MIN_SUPPORTED_CLANG_C_COMPILER_VERSION 9.0.0)
48 set(MIN_SUPPORTED_GNU_C_COMPILER_VERSION 9.0.0)
50 if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
51 if (CMAKE_C_COMPILER_VERSION VERSION_LESS MIN_SUPPORTED_CLANG_C_COMPILER_VERSION)
52 set(COMPILER_TOO_OLD TRUE)
54 elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
55 if (CMAKE_C_COMPILER_VERSION VERSION_LESS MIN_SUPPORTED_GNU_C_COMPILER_VERSION)
56 set(COMPILER_TOO_OLD TRUE)
58 set(GCC_STRING_OVERFLOW_WARNING_DISABLE_VERSION 10.0.0)
59 if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL GCC_STRING_OVERFLOW_WARNING_DISABLE_VERSION)
60 add_compile_options(-Wno-stringop-overflow)
63 message(WARNING "WARNING: Unsupported C compiler `${CMAKE_C_COMPILER_ID}` is used")
64 set (PRINT_MIN_C_COMPILER_VER TRUE)
67 message(WARNING "WARNING: C compiler version is too old and it's usage may result")
68 message(WARNING " in sub-optimal binaries or lack of support for specific CPU types.")
69 set (PRINT_MIN_C_COMPILER_VER TRUE)
72 if (PRINT_MIN_C_COMPILER_VER)
73 string (APPEND _t "Supported C compilers are ")
74 string (APPEND _t "Clang ${MIN_SUPPORTED_CLANG_C_COMPILER_VERSION} or higher ")
75 string (APPEND _t "and GNU ${MIN_SUPPORTED_GNU_C_COMPILER_VERSION} or higher.")
76 message(WARNING " ${_t}")
80 ##############################################################################
82 ##############################################################################
83 if(CMAKE_CROSSCOMPILING)
85 /usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/
86 /usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/lib/
89 set(CMAKE_C_COMPILER_TARGET ${CMAKE_SYSTEM_PROCESSOR}-linux-gnu)
91 ##############################################################################
93 ##############################################################################
94 check_c_compiler_flag("-Wno-address-of-packed-member"
95 compiler_flag_no_address_of_packed_member)
96 set(VPP_RUNTIME_DIR ${CMAKE_INSTALL_BINDIR} CACHE STRING "Relative runtime directory path")
97 set(VPP_LIBRARY_DIR ${CMAKE_INSTALL_LIBDIR} CACHE STRING "Relative library directory path")
99 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${VPP_RUNTIME_DIR})
100 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${VPP_LIBRARY_DIR})
101 set(VPP_BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeFiles)
102 set(PYENV PYTHONPYCACHEPREFIX=${CMAKE_BINARY_DIR}/CMakeFiles/__pycache__)
104 if (CMAKE_BUILD_TYPE)
105 add_compile_options(-g -Werror -Wall)
108 if (compiler_flag_no_address_of_packed_member)
109 add_compile_options(-Wno-address-of-packed-member)
112 string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LC)
113 string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UC)
115 set(CMAKE_C_FLAGS_RELEASE "")
116 set(CMAKE_C_FLAGS_DEBUG "")
118 if (${CMAKE_BUILD_TYPE_LC} MATCHES "release")
119 add_compile_options(-O3 -fstack-protector -fno-common)
120 add_compile_definitions(_FORTIFY_SOURCE=2)
121 elseif (${CMAKE_BUILD_TYPE_LC} MATCHES "debug")
122 add_compile_options(-O0 -fstack-protector -fno-common)
123 add_compile_definitions(CLIB_DEBUG)
124 elseif (${CMAKE_BUILD_TYPE_LC} MATCHES "coverity")
125 add_compile_options(-O0)
126 add_compile_definitions(__COVERITY__)
127 elseif (${CMAKE_BUILD_TYPE_LC} MATCHES "gcov")
128 add_compile_options(-O0 -fprofile-arcs -ftest-coverage)
129 add_compile_definitions(CLIB_DEBUG CLIB_GCOV)
132 set(BUILD_TYPES release debug coverity gcov)
133 string(REPLACE ";" " " BUILD_TYPES_STR "${BUILD_TYPES}")
134 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
135 HELPSTRING "Build type - valid options are: ${BUILD_TYPES_STR}")
137 ##############################################################################
138 # link time optimizations
139 ##############################################################################
140 if (CMAKE_BUILD_TYPE_UC STREQUAL "RELEASE")
141 check_ipo_supported(RESULT _result)
143 option(VPP_USE_LTO "Link time optimization of release binaries" ON)
148 check_c_compiler_flag("-Wno-stringop-overflow"
149 compiler_flag_no_stringop_overflow)
151 ##############################################################################
153 ##############################################################################
155 option(VPP_ENABLE_SANITIZE_ADDR "Enable Address Sanitizer" OFF)
156 set(VPP_SANITIZE_ADDR_OPTIONS
157 "unmap_shadow_on_exit=1:disable_coredump=0:abort_on_error=1:detect_leaks=0"
159 STRING "Address sanitizer arguments"
162 if (VPP_ENABLE_SANITIZE_ADDR)
163 add_compile_options(-fsanitize=address)
164 add_link_options(-fsanitize=address)
165 endif (VPP_ENABLE_SANITIZE_ADDR)
167 ##############################################################################
169 ##############################################################################
171 option(VPP_ENABLE_TRAJECTORY_TRACE "Build vpp with trajectory tracing enabled" OFF)
172 if(VPP_ENABLE_TRAJECTORY_TRACE)
173 add_compile_definitions(VLIB_BUFFER_TRACE_TRAJECTORY=1)
176 ##############################################################################
177 # unittest with clang ode coverage
178 ##############################################################################
180 if("${CMAKE_VERSION}" VERSION_GREATER_EQUAL "3.13" AND "${CMAKE_C_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
181 option(VPP_BUILD_TESTS_WITH_COVERAGE "Build unit tests with code coverage" OFF)
184 ##############################################################################
186 ##############################################################################
187 option(VPP_SET_RPATH "Set rpath for resulting binaries and libraries." ON)
189 set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${VPP_LIBRARY_DIR}")
191 set(CMAKE_INSTALL_MESSAGE NEVER)
193 include_directories (
197 set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "vpp")
199 set(THREADS_PREFER_PTHREAD_FLAG ON)
200 find_package(Threads REQUIRED)
202 include(cmake/syscall.cmake)
203 include(cmake/api.cmake)
204 include(cmake/library.cmake)
205 include(cmake/exec.cmake)
206 include(cmake/plugin.cmake)
208 ##############################################################################
209 # subdirs - order matters
210 ##############################################################################
211 option(VPP_HOST_TOOLS_ONLY "Build only host tools" OFF)
212 if(VPP_HOST_TOOLS_ONLY)
213 set(SUBDIRS tools/vppapigen cmake)
214 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
215 find_package(OpenSSL)
217 vppinfra svm vlib vlibmemory vlibapi vnet vpp vat vat2 vcl vpp-api
218 plugins tools/vppapigen tools/g2 tools/perftool cmake pkg
221 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
222 set(SUBDIRS vppinfra)
224 message(FATAL_ERROR "Unsupported system: ${CMAKE_SYSTEM_NAME}")
227 foreach(DIR ${SUBDIRS})
228 add_subdirectory(${DIR} ${VPP_BINARY_DIR}/${DIR})
231 ##############################################################################
232 # detect if we are inside git repo and add configure dependency
233 ##############################################################################
235 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
236 COMMAND git rev-parse --show-toplevel
237 OUTPUT_VARIABLE VPP_GIT_TOPLEVEL_DIR
238 OUTPUT_STRIP_TRAILING_WHITESPACE
242 if (VPP_GIT_TOPLEVEL_DIR)
244 DIRECTORY APPEND PROPERTY
245 CMAKE_CONFIGURE_DEPENDS ${VPP_GIT_TOPLEVEL_DIR}/.git/index
249 ##############################################################################
251 ##############################################################################
253 add_custom_target(run
254 COMMAND ./${VPP_RUNTIME_DIR}/vpp -c startup.conf
255 COMMENT "Starting VPP..."
259 add_custom_target(debug
260 COMMAND gdb --args ./${VPP_RUNTIME_DIR}/vpp -c startup.conf
261 COMMENT "Starting VPP in the debugger..."
265 add_custom_target(config
266 COMMAND ccmake ${CMAKE_BINARY_DIR}
267 COMMENT "Starting Configuration TUI..."
271 foreach(bt ${BUILD_TYPES})
272 add_custom_target(set-build-type-${bt}
273 COMMAND cmake -DCMAKE_BUILD_TYPE:STRING=${bt} .
274 COMMENT "Changing build type to ${bt}"
279 mark_as_advanced(CLEAR
282 CMAKE_EXPORT_COMPILE_COMMANDS
285 CMAKE_SHARED_LINKER_FLAGS
286 CMAKE_VERBOSE_MAKEFILE
289 ##############################################################################
290 # print configuration
291 ##############################################################################
292 message(STATUS "Configuration:")
293 pr("VPP version" ${VPP_VERSION})
294 pr("VPP library version" ${VPP_LIB_VERSION})
295 pr("GIT toplevel dir" ${VPP_GIT_TOPLEVEL_DIR})
296 pr("Build type" ${CMAKE_BUILD_TYPE})
297 pr("C compiler" "${CMAKE_C_COMPILER} (${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION})")
298 pr("C flags" ${CMAKE_C_FLAGS}${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UC}})
299 pr("Linker flags (apps)" ${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UC}})
300 pr("Linker flags (libs)" ${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UC}})
301 pr("Host processor" ${CMAKE_HOST_SYSTEM_PROCESSOR})
302 pr("Target processor" ${CMAKE_SYSTEM_PROCESSOR})
303 pr("Prefix path" ${CMAKE_PREFIX_PATH})
304 pr("Install prefix" ${CMAKE_INSTALL_PREFIX})
305 pr("Library dir" ${VPP_LIBRARY_DIR})
306 pr("Multiarch variants" ${MARCH_VARIANTS_NAMES})