blob: 46948a2aff35fa1a718247d237934cb92fea1ada [file] [log] [blame]
Damjan Marion612dd6a2018-07-30 12:45:07 +02001# 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 Marionc30f3002021-12-02 14:03:02 +010014cmake_minimum_required(VERSION 3.13)
Damjan Marion612dd6a2018-07-30 12:45:07 +020015
Damjan Mariond22f1902021-04-14 18:48:22 +020016set(CMAKE_C_COMPILER_NAMES
Damjan Marion21feecf2021-10-29 22:08:39 +020017 clang-13
Damjan Mariond22f1902021-04-14 18:48:22 +020018 clang-12
19 clang-11
20 clang-10
21 clang-9
22 gcc-10
23 gcc-9
24 cc
25)
Damjan Marion4ba16a42020-04-28 13:29:37 +020026
Damjan Marion612dd6a2018-07-30 12:45:07 +020027project(vpp C)
28
Damjan Marion5546e432021-09-30 20:04:14 +020029if(NOT DEFINED CMAKE_INSTALL_LIBDIR AND EXISTS "/etc/debian_version")
30 set(CMAKE_INSTALL_LIBDIR "lib/${CMAKE_LIBRARY_ARCHITECTURE}")
31endif()
32
Damjan Marion612dd6a2018-07-30 12:45:07 +020033include(CheckCCompilerFlag)
Damjan Marionaf7892c2020-10-22 14:23:47 +020034include(CheckIPOSupported)
Nick Browne3cf4d02021-09-15 14:25:40 +010035include(GNUInstallDirs)
Damjan Marionff426932019-01-26 14:12:25 +010036include(cmake/misc.cmake)
Damjan Mariond16004d2018-08-26 10:14:52 +020037include(cmake/cpu.cmake)
38include(cmake/ccache.cmake)
Damjan Marion612dd6a2018-07-30 12:45:07 +020039
40##############################################################################
Damjan Marionc6c02462018-08-31 17:38:57 +020041# VPP Version
42##############################################################################
43execute_process(
44 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
45 COMMAND scripts/version
46 OUTPUT_VARIABLE VPP_VERSION
47 OUTPUT_STRIP_TRAILING_WHITESPACE
48)
49string(REPLACE "-" ";" VPP_LIB_VERSION ${VPP_VERSION})
50list(GET VPP_LIB_VERSION 0 VPP_LIB_VERSION)
51
52##############################################################################
Damjan Mariondd395292019-01-15 00:36:03 +010053# cross compiling
54##############################################################################
55if(CMAKE_CROSSCOMPILING)
56 set(CMAKE_IGNORE_PATH
57 /usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/
58 /usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/lib/
59 )
60endif()
Damjan Marion64911362019-01-18 17:09:44 +010061set(CMAKE_C_COMPILER_TARGET ${CMAKE_SYSTEM_PROCESSOR}-linux-gnu)
Damjan Mariondd395292019-01-15 00:36:03 +010062
63##############################################################################
Damjan Marion612dd6a2018-07-30 12:45:07 +020064# build config
65##############################################################################
Damjan Marion847d5282019-01-17 18:25:11 +010066check_c_compiler_flag("-Wno-address-of-packed-member"
67 compiler_flag_no_address_of_packed_member)
Nick Browne3cf4d02021-09-15 14:25:40 +010068set(VPP_RUNTIME_DIR ${CMAKE_INSTALL_BINDIR} CACHE STRING "Relative runtime directory path")
69set(VPP_LIBRARY_DIR ${CMAKE_INSTALL_LIBDIR} CACHE STRING "Relative library directory path")
Damjan Marion847d5282019-01-17 18:25:11 +010070
Damjan Marion599efc62020-05-07 16:49:45 +020071set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${VPP_RUNTIME_DIR})
72set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${VPP_LIBRARY_DIR})
Damjan Marion88b2e362021-04-29 18:47:25 +020073set(VPP_BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeFiles)
Damjan Marion7cf80af2021-05-25 19:28:21 +020074set(PYENV PYTHONPYCACHEPREFIX=${CMAKE_BINARY_DIR}/CMakeFile/__pycache__)
Damjan Marion612dd6a2018-07-30 12:45:07 +020075
Damjan Marion847d5282019-01-17 18:25:11 +010076if (CMAKE_BUILD_TYPE)
Damjan Marion0d39cba2021-05-10 14:51:44 +020077 add_compile_options(-g -fPIC -Werror -Wall)
Damjan Marion33ed3e42018-08-27 15:59:30 +020078endif()
79
Damjan Marion847d5282019-01-17 18:25:11 +010080if (compiler_flag_no_address_of_packed_member)
Damjan Marion0d39cba2021-05-10 14:51:44 +020081 add_compile_options(-Wno-address-of-packed-member)
Damjan Marion847d5282019-01-17 18:25:11 +010082endif()
83
Damjan Marion0d39cba2021-05-10 14:51:44 +020084string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LC)
Damjan Marion847d5282019-01-17 18:25:11 +010085string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UC)
86
Damjan Marion0d39cba2021-05-10 14:51:44 +020087set(CMAKE_C_FLAGS_RELEASE "")
88set(CMAKE_C_FLAGS_DEBUG "")
Damjan Marion952a7b82019-11-19 17:58:36 +010089
Damjan Marion0d39cba2021-05-10 14:51:44 +020090if (${CMAKE_BUILD_TYPE_LC} MATCHES "release")
91 add_compile_options(-O3 -fstack-protector -fno-common)
92 add_compile_definitions(_FORTIFY_SOURCE=2)
Damjan Marionc30f3002021-12-02 14:03:02 +010093 add_link_options("-pie")
Damjan Marion0d39cba2021-05-10 14:51:44 +020094elseif (${CMAKE_BUILD_TYPE_LC} MATCHES "debug")
95 add_compile_options(-O0 -fstack-protector -fno-common)
96 add_compile_definitions(CLIB_DEBUG)
97elseif (${CMAKE_BUILD_TYPE_LC} MATCHES "coverity")
98 add_compile_options(-O0)
99 add_compile_definitions(__COVERITY__)
100elseif (${CMAKE_BUILD_TYPE_LC} MATCHES "gcov")
101 add_compile_options(-O0 -fprofile-arcs -ftest-coverage)
102 add_compile_definitions(CLIB_DEBUG CLIB_GCOV)
103endif()
104
105set(BUILD_TYPES release debug coverity gcov)
Damjan Marion3a533cd2021-05-03 12:40:27 +0200106string(REPLACE ";" " " BUILD_TYPES_STR "${BUILD_TYPES}")
Damjan Marion952a7b82019-11-19 17:58:36 +0100107set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
Damjan Marion3a533cd2021-05-03 12:40:27 +0200108 HELPSTRING "Build type - valid options are: ${BUILD_TYPES_STR}")
Damjan Marion952a7b82019-11-19 17:58:36 +0100109
Damjan Marion612dd6a2018-07-30 12:45:07 +0200110##############################################################################
Damjan Marionaf7892c2020-10-22 14:23:47 +0200111# link time optimizations
112##############################################################################
113if (CMAKE_BUILD_TYPE_UC STREQUAL "RELEASE")
114 check_ipo_supported(RESULT _result)
115 if (_result)
116 option(VPP_USE_LTO "Link time optimization of release binaries" ON)
117 endif()
118endif()
119
Damjan Marion1e267242021-11-06 17:09:37 +0100120if(VPP_USE_LTO)
121 check_c_compiler_flag("-Wno-stringop-overflow"
122 compiler_flag_no_stringop_overflow)
123endif()
Damjan Marionaf7892c2020-10-22 14:23:47 +0200124##############################################################################
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200125# sanitizers
126##############################################################################
127
Damjan Marion599efc62020-05-07 16:49:45 +0200128option(VPP_ENABLE_SANITIZE_ADDR "Enable Address Sanitizer" OFF)
Benoît Ganne173484f2020-06-16 12:05:07 +0200129set(VPP_SANITIZE_ADDR_OPTIONS
Damjan Marionec3a3f12020-05-21 19:09:13 +0200130 "unmap_shadow_on_exit=1:disable_coredump=0:abort_on_error=1:detect_leaks=0"
131 CACHE
132 STRING "Address sanitizer arguments"
133)
134
Damjan Marion599efc62020-05-07 16:49:45 +0200135if (VPP_ENABLE_SANITIZE_ADDR)
Damjan Marion0d39cba2021-05-10 14:51:44 +0200136 add_compile_options(-fsanitize=address)
137 add_compile_definitions(CLIB_SANITIZE_ADDR)
Damjan Marionc30f3002021-12-02 14:03:02 +0100138 add_link_options(-fsanitize=address)
Damjan Marion599efc62020-05-07 16:49:45 +0200139endif (VPP_ENABLE_SANITIZE_ADDR)
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200140
141##############################################################################
Benoît Gannef89bbbe2021-03-04 14:31:03 +0100142# trajectory trace
143##############################################################################
144
145option(VPP_ENABLE_TRAJECTORY_TRACE "Build vpp with trajectory tracing enabled" OFF)
146if(VPP_ENABLE_TRAJECTORY_TRACE)
Damjan Marion0d39cba2021-05-10 14:51:44 +0200147 add_compile_definitions(VLIB_BUFFER_TRACE_TRAJECTORY=1)
Benoît Gannef89bbbe2021-03-04 14:31:03 +0100148endif()
149
150##############################################################################
Ole Troan94323402021-11-11 19:22:12 +0100151# unittest with clang ode coverage
152##############################################################################
153
154if("${CMAKE_VERSION}" VERSION_GREATER_EQUAL "3.13" AND "${CMAKE_C_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
155 option(VPP_BUILD_TESTS_WITH_COVERAGE "Build unit tests with code coverage" OFF)
156endif()
157
158##############################################################################
Damjan Marion612dd6a2018-07-30 12:45:07 +0200159# install config
160##############################################################################
Nathan Moos2c919222021-01-15 13:28:34 -0800161option(VPP_SET_RPATH "Set rpath for resulting binaries and libraries." ON)
162if(VPP_SET_RPATH)
Damjan Marion5546e432021-09-30 20:04:14 +0200163 set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${VPP_LIBRARY_DIR}")
Nathan Moos2c919222021-01-15 13:28:34 -0800164endif()
Damjan Marion612dd6a2018-07-30 12:45:07 +0200165set(CMAKE_INSTALL_MESSAGE NEVER)
166
Damjan Marion612dd6a2018-07-30 12:45:07 +0200167include_directories (
168 ${CMAKE_SOURCE_DIR}
Damjan Marion88b2e362021-04-29 18:47:25 +0200169 ${VPP_BINARY_DIR}
Damjan Marion612dd6a2018-07-30 12:45:07 +0200170)
171set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "vpp")
172
173set(THREADS_PREFER_PTHREAD_FLAG ON)
174find_package(Threads REQUIRED)
Damjan Marion612dd6a2018-07-30 12:45:07 +0200175
Damjan Marion1ee346a2019-03-18 17:06:51 +0100176include(cmake/syscall.cmake)
Damjan Mariond16004d2018-08-26 10:14:52 +0200177include(cmake/api.cmake)
Damjan Marion4553c952018-08-26 11:04:40 +0200178include(cmake/library.cmake)
179include(cmake/exec.cmake)
Damjan Mariond16004d2018-08-26 10:14:52 +0200180include(cmake/plugin.cmake)
Damjan Marion612dd6a2018-07-30 12:45:07 +0200181
182##############################################################################
Damjan Marion0abd4a22018-08-28 12:57:29 +0200183# subdirs - order matters
Damjan Marion612dd6a2018-07-30 12:45:07 +0200184##############################################################################
Damjan Marion599efc62020-05-07 16:49:45 +0200185option(VPP_HOST_TOOLS_ONLY "Build only host tools" OFF)
186if(VPP_HOST_TOOLS_ONLY)
187 set(SUBDIRS tools/vppapigen cmake)
188elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
Klement Sekeraefd4d702021-04-20 19:21:36 +0200189 find_package(OpenSSL)
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200190 set(SUBDIRS
Arthur de Kerhorb2819dd2021-09-20 14:47:47 +0200191 vppinfra svm vlib vlibmemory vlibapi vnet vpp vat vat2 vcl vpp-api
192 plugins tools/vppapigen tools/g2 tools/perftool cmake pkg
Dave Barach0729f642019-03-24 16:25:03 -0400193 tools/appimage
Damjan Marion0fa900e2018-09-12 12:12:36 +0200194 )
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200195elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
196 set(SUBDIRS vppinfra)
197else()
198 message(FATAL_ERROR "Unsupported system: ${CMAKE_SYSTEM_NAME}")
199endif()
200
201foreach(DIR ${SUBDIRS})
Damjan Marion88b2e362021-04-29 18:47:25 +0200202 add_subdirectory(${DIR} ${VPP_BINARY_DIR}/${DIR})
Damjan Marion0abd4a22018-08-28 12:57:29 +0200203endforeach()
Damjan Marion612dd6a2018-07-30 12:45:07 +0200204
Damjan Marioneeadc142018-09-13 20:02:12 +0200205##############################################################################
Damjan Marionc6c02462018-08-31 17:38:57 +0200206# detect if we are inside git repo and add configure dependency
207##############################################################################
208execute_process(
209 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
210 COMMAND git rev-parse --show-toplevel
211 OUTPUT_VARIABLE VPP_GIT_TOPLEVEL_DIR
212 OUTPUT_STRIP_TRAILING_WHITESPACE
Damjan Marion6077c972019-01-28 17:55:59 +0100213 ERROR_QUIET
Damjan Marionc6c02462018-08-31 17:38:57 +0200214)
215
216if (VPP_GIT_TOPLEVEL_DIR)
217 set_property(
218 DIRECTORY APPEND PROPERTY
219 CMAKE_CONFIGURE_DEPENDS ${VPP_GIT_TOPLEVEL_DIR}/.git/index
220 )
221endif()
222
223##############################################################################
Damjan Marion88b2e362021-04-29 18:47:25 +0200224# custom targets
225##############################################################################
226
227add_custom_target(run
228 COMMAND ./${VPP_RUNTIME_DIR}/vpp -c startup.conf
229 COMMENT "Starting VPP..."
230 USES_TERMINAL
231)
232
233add_custom_target(debug
234 COMMAND gdb --args ./${VPP_RUNTIME_DIR}/vpp -c startup.conf
235 COMMENT "Starting VPP in the debugger..."
236 USES_TERMINAL
237)
238
Damjan Marionc8983242021-04-30 19:31:09 +0200239add_custom_target(config
Damjan Marion88b2e362021-04-29 18:47:25 +0200240 COMMAND ccmake ${CMAKE_BINARY_DIR}
241 COMMENT "Starting Configuration TUI..."
242 USES_TERMINAL
243)
244
Damjan Marion3a533cd2021-05-03 12:40:27 +0200245foreach(bt ${BUILD_TYPES})
246 add_custom_target(set-build-type-${bt}
247 COMMAND cmake -DCMAKE_BUILD_TYPE:STRING=${bt} .
248 COMMENT "Changing build type to ${bt}"
249 USES_TERMINAL
250 )
251endforeach()
252
Damjan Marion0d39cba2021-05-10 14:51:44 +0200253mark_as_advanced(CLEAR
254 CMAKE_C_FLAGS
255 CMAKE_C_COMPILER
256 CMAKE_EXPORT_COMPILE_COMMANDS
257 CMAKE_INSTALL_PREFIX
258 CMAKE_LINKER
259 CMAKE_SHARED_LINKER_FLAGS
260 CMAKE_VERBOSE_MAKEFILE
261)
262
Damjan Marion88b2e362021-04-29 18:47:25 +0200263##############################################################################
Damjan Marionc6c02462018-08-31 17:38:57 +0200264# print configuration
265##############################################################################
266message(STATUS "Configuration:")
Damjan Marionf2912e02021-07-16 12:44:22 +0200267pr("VPP version" ${VPP_VERSION})
268pr("VPP library version" ${VPP_LIB_VERSION})
269pr("GIT toplevel dir" ${VPP_GIT_TOPLEVEL_DIR})
270pr("Build type" ${CMAKE_BUILD_TYPE})
Damjan Marion2e3a79f2021-11-05 20:08:05 +0100271pr("C compiler" ${CMAKE_C_COMPILER})
Damjan Marionf2912e02021-07-16 12:44:22 +0200272pr("C flags" ${CMAKE_C_FLAGS}${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UC}})
273pr("Linker flags (apps)" ${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UC}})
274pr("Linker flags (libs)" ${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UC}})
275pr("Host processor" ${CMAKE_HOST_SYSTEM_PROCESSOR})
276pr("Target processor" ${CMAKE_SYSTEM_PROCESSOR})
277pr("Prefix path" ${CMAKE_PREFIX_PATH})
278pr("Install prefix" ${CMAKE_INSTALL_PREFIX})
Damjan Marion2e3a79f2021-11-05 20:08:05 +0100279pr("Library dir" ${VPP_LIBRARY_DIR})