blob: 1730e7b669e5207e7c19c7235b6aa4eab289e9a8 [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 Marionaf7892c2020-10-22 14:23:47 +020014cmake_minimum_required(VERSION 3.10)
Damjan Marion612dd6a2018-07-30 12:45:07 +020015
Damjan Mariond22f1902021-04-14 18:48:22 +020016set(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 Marion4ba16a42020-04-28 13:29:37 +020025
Damjan Marion612dd6a2018-07-30 12:45:07 +020026project(vpp C)
27
Damjan Marion0d39cba2021-05-10 14:51:44 +020028if(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()
34endif()
Damjan Marion49a29252021-05-05 10:24:38 +020035
Damjan Marion612dd6a2018-07-30 12:45:07 +020036include(CheckCCompilerFlag)
Damjan Marionaf7892c2020-10-22 14:23:47 +020037include(CheckIPOSupported)
Damjan Marionff426932019-01-26 14:12:25 +010038include(cmake/misc.cmake)
Damjan Mariond16004d2018-08-26 10:14:52 +020039include(cmake/cpu.cmake)
40include(cmake/ccache.cmake)
Damjan Marion612dd6a2018-07-30 12:45:07 +020041
42##############################################################################
Damjan Marionc6c02462018-08-31 17:38:57 +020043# VPP Version
44##############################################################################
45execute_process(
46 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
47 COMMAND scripts/version
48 OUTPUT_VARIABLE VPP_VERSION
49 OUTPUT_STRIP_TRAILING_WHITESPACE
50)
51string(REPLACE "-" ";" VPP_LIB_VERSION ${VPP_VERSION})
52list(GET VPP_LIB_VERSION 0 VPP_LIB_VERSION)
53
54##############################################################################
Damjan Mariondd395292019-01-15 00:36:03 +010055# cross compiling
56##############################################################################
57if(CMAKE_CROSSCOMPILING)
58 set(CMAKE_IGNORE_PATH
59 /usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/
60 /usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/lib/
61 )
62endif()
Damjan Marion64911362019-01-18 17:09:44 +010063set(CMAKE_C_COMPILER_TARGET ${CMAKE_SYSTEM_PROCESSOR}-linux-gnu)
Damjan Mariondd395292019-01-15 00:36:03 +010064
65##############################################################################
Damjan Marion612dd6a2018-07-30 12:45:07 +020066# build config
67##############################################################################
Damjan Marion847d5282019-01-17 18:25:11 +010068check_c_compiler_flag("-Wno-address-of-packed-member"
69 compiler_flag_no_address_of_packed_member)
Damjan Marion599efc62020-05-07 16:49:45 +020070set(VPP_RUNTIME_DIR "bin" CACHE STRING "Relative runtime directory path")
71set(VPP_LIBRARY_DIR "lib" CACHE STRING "Relative library directory path")
Damjan Marion847d5282019-01-17 18:25:11 +010072
Damjan Marion599efc62020-05-07 16:49:45 +020073set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${VPP_RUNTIME_DIR})
74set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${VPP_LIBRARY_DIR})
Damjan Marion88b2e362021-04-29 18:47:25 +020075set(VPP_BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeFiles)
Damjan Marion612dd6a2018-07-30 12:45:07 +020076
Damjan Marion847d5282019-01-17 18:25:11 +010077if (CMAKE_BUILD_TYPE)
Damjan Marion0d39cba2021-05-10 14:51:44 +020078 add_compile_options(-g -fPIC -Werror -Wall)
Damjan Marion33ed3e42018-08-27 15:59:30 +020079endif()
80
Damjan Marion847d5282019-01-17 18:25:11 +010081if (compiler_flag_no_address_of_packed_member)
Damjan Marion0d39cba2021-05-10 14:51:44 +020082 add_compile_options(-Wno-address-of-packed-member)
Damjan Marion847d5282019-01-17 18:25:11 +010083endif()
84
Damjan Marion0d39cba2021-05-10 14:51:44 +020085string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LC)
Damjan Marion847d5282019-01-17 18:25:11 +010086string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UC)
87
Damjan Marion0d39cba2021-05-10 14:51:44 +020088set(CMAKE_C_FLAGS_RELEASE "")
89set(CMAKE_C_FLAGS_DEBUG "")
Damjan Marion952a7b82019-11-19 17:58:36 +010090
Damjan Marion0d39cba2021-05-10 14:51:44 +020091if (${CMAKE_BUILD_TYPE_LC} MATCHES "release")
92 add_compile_options(-O3 -fstack-protector -fno-common)
93 add_compile_definitions(_FORTIFY_SOURCE=2)
94 string(CONCAT CMAKE_EXE_LINKER_FLAGS_RELEASE "-pie")
95elseif (${CMAKE_BUILD_TYPE_LC} MATCHES "debug")
96 add_compile_options(-O0 -fstack-protector -fno-common)
97 add_compile_definitions(CLIB_DEBUG)
98elseif (${CMAKE_BUILD_TYPE_LC} MATCHES "coverity")
99 add_compile_options(-O0)
100 add_compile_definitions(__COVERITY__)
101elseif (${CMAKE_BUILD_TYPE_LC} MATCHES "gcov")
102 add_compile_options(-O0 -fprofile-arcs -ftest-coverage)
103 add_compile_definitions(CLIB_DEBUG CLIB_GCOV)
104endif()
105
106set(BUILD_TYPES release debug coverity gcov)
Damjan Marion3a533cd2021-05-03 12:40:27 +0200107string(REPLACE ";" " " BUILD_TYPES_STR "${BUILD_TYPES}")
Damjan Marion952a7b82019-11-19 17:58:36 +0100108set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
Damjan Marion3a533cd2021-05-03 12:40:27 +0200109 HELPSTRING "Build type - valid options are: ${BUILD_TYPES_STR}")
Damjan Marion952a7b82019-11-19 17:58:36 +0100110
Damjan Marion612dd6a2018-07-30 12:45:07 +0200111##############################################################################
Damjan Marionaf7892c2020-10-22 14:23:47 +0200112# link time optimizations
113##############################################################################
114if (CMAKE_BUILD_TYPE_UC STREQUAL "RELEASE")
115 check_ipo_supported(RESULT _result)
116 if (_result)
117 option(VPP_USE_LTO "Link time optimization of release binaries" ON)
118 endif()
119endif()
120
121##############################################################################
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200122# sanitizers
123##############################################################################
124
Damjan Marion599efc62020-05-07 16:49:45 +0200125option(VPP_ENABLE_SANITIZE_ADDR "Enable Address Sanitizer" OFF)
Benoît Ganne173484f2020-06-16 12:05:07 +0200126set(VPP_SANITIZE_ADDR_OPTIONS
Damjan Marionec3a3f12020-05-21 19:09:13 +0200127 "unmap_shadow_on_exit=1:disable_coredump=0:abort_on_error=1:detect_leaks=0"
128 CACHE
129 STRING "Address sanitizer arguments"
130)
131
Damjan Marion599efc62020-05-07 16:49:45 +0200132if (VPP_ENABLE_SANITIZE_ADDR)
Damjan Marion0d39cba2021-05-10 14:51:44 +0200133 add_compile_options(-fsanitize=address)
134 add_compile_definitions(CLIB_SANITIZE_ADDR)
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200135 set(CMAKE_EXE_LINKER_FLAGS "-fsanitize=address ${CMAKE_EXE_LINKER_FLAGS}")
136 set(CMAKE_SHARED_LINKER_FLAGS "-fsanitize=address ${CMAKE_SHARED_LINKER_FLAGS}")
Damjan Marion599efc62020-05-07 16:49:45 +0200137endif (VPP_ENABLE_SANITIZE_ADDR)
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200138
139##############################################################################
Benoît Gannef89bbbe2021-03-04 14:31:03 +0100140# trajectory trace
141##############################################################################
142
143option(VPP_ENABLE_TRAJECTORY_TRACE "Build vpp with trajectory tracing enabled" OFF)
144if(VPP_ENABLE_TRAJECTORY_TRACE)
Damjan Marion0d39cba2021-05-10 14:51:44 +0200145 add_compile_definitions(VLIB_BUFFER_TRACE_TRAJECTORY=1)
Benoît Gannef89bbbe2021-03-04 14:31:03 +0100146endif()
147
148##############################################################################
Damjan Marion612dd6a2018-07-30 12:45:07 +0200149# install config
150##############################################################################
Nathan Moos2c919222021-01-15 13:28:34 -0800151option(VPP_SET_RPATH "Set rpath for resulting binaries and libraries." ON)
152if(VPP_SET_RPATH)
153 set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
154endif()
Damjan Marion612dd6a2018-07-30 12:45:07 +0200155set(CMAKE_INSTALL_MESSAGE NEVER)
156
Damjan Marion612dd6a2018-07-30 12:45:07 +0200157include_directories (
158 ${CMAKE_SOURCE_DIR}
Damjan Marion88b2e362021-04-29 18:47:25 +0200159 ${VPP_BINARY_DIR}
Damjan Marion612dd6a2018-07-30 12:45:07 +0200160)
161set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "vpp")
162
163set(THREADS_PREFER_PTHREAD_FLAG ON)
164find_package(Threads REQUIRED)
Damjan Marion612dd6a2018-07-30 12:45:07 +0200165
Damjan Marion1ee346a2019-03-18 17:06:51 +0100166include(cmake/syscall.cmake)
Damjan Mariond16004d2018-08-26 10:14:52 +0200167include(cmake/api.cmake)
Damjan Marion4553c952018-08-26 11:04:40 +0200168include(cmake/library.cmake)
169include(cmake/exec.cmake)
Damjan Mariond16004d2018-08-26 10:14:52 +0200170include(cmake/plugin.cmake)
Damjan Marion612dd6a2018-07-30 12:45:07 +0200171
172##############################################################################
Damjan Marion0abd4a22018-08-28 12:57:29 +0200173# subdirs - order matters
Damjan Marion612dd6a2018-07-30 12:45:07 +0200174##############################################################################
Damjan Marion599efc62020-05-07 16:49:45 +0200175option(VPP_HOST_TOOLS_ONLY "Build only host tools" OFF)
176if(VPP_HOST_TOOLS_ONLY)
177 set(SUBDIRS tools/vppapigen cmake)
178elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
Klement Sekeraefd4d702021-04-20 19:21:36 +0200179 find_package(OpenSSL)
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200180 set(SUBDIRS
Ole Troandf87f802020-11-18 19:17:48 +0100181 vppinfra svm vlib vlibmemory vlibapi vnet vpp vat vat2 vcl plugins
Damjan Marion5505ee82020-05-07 09:49:27 +0200182 vpp-api tools/vppapigen tools/g2 tools/perftool cmake pkg
Dave Barach0729f642019-03-24 16:25:03 -0400183 tools/appimage
Damjan Marion0fa900e2018-09-12 12:12:36 +0200184 )
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200185elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
186 set(SUBDIRS vppinfra)
187else()
188 message(FATAL_ERROR "Unsupported system: ${CMAKE_SYSTEM_NAME}")
189endif()
190
191foreach(DIR ${SUBDIRS})
Damjan Marion88b2e362021-04-29 18:47:25 +0200192 add_subdirectory(${DIR} ${VPP_BINARY_DIR}/${DIR})
Damjan Marion0abd4a22018-08-28 12:57:29 +0200193endforeach()
Damjan Marion612dd6a2018-07-30 12:45:07 +0200194
Damjan Marioneeadc142018-09-13 20:02:12 +0200195##############################################################################
Damjan Marionc6c02462018-08-31 17:38:57 +0200196# detect if we are inside git repo and add configure dependency
197##############################################################################
198execute_process(
199 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
200 COMMAND git rev-parse --show-toplevel
201 OUTPUT_VARIABLE VPP_GIT_TOPLEVEL_DIR
202 OUTPUT_STRIP_TRAILING_WHITESPACE
Damjan Marion6077c972019-01-28 17:55:59 +0100203 ERROR_QUIET
Damjan Marionc6c02462018-08-31 17:38:57 +0200204)
205
206if (VPP_GIT_TOPLEVEL_DIR)
207 set_property(
208 DIRECTORY APPEND PROPERTY
209 CMAKE_CONFIGURE_DEPENDS ${VPP_GIT_TOPLEVEL_DIR}/.git/index
210 )
211endif()
212
213##############################################################################
Damjan Marion88b2e362021-04-29 18:47:25 +0200214# custom targets
215##############################################################################
216
217add_custom_target(run
218 COMMAND ./${VPP_RUNTIME_DIR}/vpp -c startup.conf
219 COMMENT "Starting VPP..."
220 USES_TERMINAL
221)
222
223add_custom_target(debug
224 COMMAND gdb --args ./${VPP_RUNTIME_DIR}/vpp -c startup.conf
225 COMMENT "Starting VPP in the debugger..."
226 USES_TERMINAL
227)
228
Damjan Marionc8983242021-04-30 19:31:09 +0200229add_custom_target(config
Damjan Marion88b2e362021-04-29 18:47:25 +0200230 COMMAND ccmake ${CMAKE_BINARY_DIR}
231 COMMENT "Starting Configuration TUI..."
232 USES_TERMINAL
233)
234
Damjan Marion3a533cd2021-05-03 12:40:27 +0200235foreach(bt ${BUILD_TYPES})
236 add_custom_target(set-build-type-${bt}
237 COMMAND cmake -DCMAKE_BUILD_TYPE:STRING=${bt} .
238 COMMENT "Changing build type to ${bt}"
239 USES_TERMINAL
240 )
241endforeach()
242
Damjan Marion0d39cba2021-05-10 14:51:44 +0200243mark_as_advanced(CLEAR
244 CMAKE_C_FLAGS
245 CMAKE_C_COMPILER
246 CMAKE_EXPORT_COMPILE_COMMANDS
247 CMAKE_INSTALL_PREFIX
248 CMAKE_LINKER
249 CMAKE_SHARED_LINKER_FLAGS
250 CMAKE_VERBOSE_MAKEFILE
251)
252
Damjan Marion88b2e362021-04-29 18:47:25 +0200253##############################################################################
Damjan Marionc6c02462018-08-31 17:38:57 +0200254# print configuration
255##############################################################################
256message(STATUS "Configuration:")
257pr("VPP version" "${VPP_VERSION}")
258pr("VPP library version" "${VPP_LIB_VERSION}")
259pr("GIT toplevel dir" "${VPP_GIT_TOPLEVEL_DIR}")
Damjan Marion847d5282019-01-17 18:25:11 +0100260pr("Build type" "${CMAKE_BUILD_TYPE}")
261pr("C flags" "${CMAKE_C_FLAGS}${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UC}}")
Damjan Marion2baa1152019-11-07 11:32:16 +0100262pr("Linker flags (apps)" "${CMAKE_EXE_LINKER_FLAGS}${CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UC}}")
263pr("Linker flags (libs)" "${CMAKE_SHARED_LINKER_FLAGS}${CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UC}}")
Damjan Mariondd395292019-01-15 00:36:03 +0100264pr("Host processor" "${CMAKE_HOST_SYSTEM_PROCESSOR}")
Damjan Marionc6c02462018-08-31 17:38:57 +0200265pr("Target processor" "${CMAKE_SYSTEM_PROCESSOR}")
Damjan Marion4a6cb832018-09-18 18:41:38 +0200266pr("Prefix path" "${CMAKE_PREFIX_PATH}")
Damjan Marionc6c02462018-08-31 17:38:57 +0200267pr("Install prefix" "${CMAKE_INSTALL_PREFIX}")