blob: a291c4d022d59447cddad3e21014d3f6f742fc22 [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
28include(CheckCCompilerFlag)
Damjan Marionaf7892c2020-10-22 14:23:47 +020029include(CheckIPOSupported)
Damjan Marionff426932019-01-26 14:12:25 +010030include(cmake/misc.cmake)
Damjan Mariond16004d2018-08-26 10:14:52 +020031include(cmake/cpu.cmake)
32include(cmake/ccache.cmake)
Damjan Marion612dd6a2018-07-30 12:45:07 +020033
34##############################################################################
Damjan Marionc6c02462018-08-31 17:38:57 +020035# VPP Version
36##############################################################################
37execute_process(
38 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
39 COMMAND scripts/version
40 OUTPUT_VARIABLE VPP_VERSION
41 OUTPUT_STRIP_TRAILING_WHITESPACE
42)
43string(REPLACE "-" ";" VPP_LIB_VERSION ${VPP_VERSION})
44list(GET VPP_LIB_VERSION 0 VPP_LIB_VERSION)
45
46##############################################################################
Damjan Mariondd395292019-01-15 00:36:03 +010047# cross compiling
48##############################################################################
49if(CMAKE_CROSSCOMPILING)
50 set(CMAKE_IGNORE_PATH
51 /usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/
52 /usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/lib/
53 )
54endif()
Damjan Marion64911362019-01-18 17:09:44 +010055set(CMAKE_C_COMPILER_TARGET ${CMAKE_SYSTEM_PROCESSOR}-linux-gnu)
Damjan Mariondd395292019-01-15 00:36:03 +010056
57##############################################################################
Damjan Marion612dd6a2018-07-30 12:45:07 +020058# build config
59##############################################################################
Damjan Marion847d5282019-01-17 18:25:11 +010060check_c_compiler_flag("-Wno-address-of-packed-member"
61 compiler_flag_no_address_of_packed_member)
Damjan Marion599efc62020-05-07 16:49:45 +020062set(VPP_RUNTIME_DIR "bin" CACHE STRING "Relative runtime directory path")
63set(VPP_LIBRARY_DIR "lib" CACHE STRING "Relative library directory path")
Damjan Marion847d5282019-01-17 18:25:11 +010064
Damjan Marion599efc62020-05-07 16:49:45 +020065set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${VPP_RUNTIME_DIR})
66set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${VPP_LIBRARY_DIR})
Damjan Marion88b2e362021-04-29 18:47:25 +020067set(VPP_BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeFiles)
Damjan Marion612dd6a2018-07-30 12:45:07 +020068
Damjan Marion847d5282019-01-17 18:25:11 +010069if (CMAKE_BUILD_TYPE)
Damjan Marion2baa1152019-11-07 11:32:16 +010070 set(CMAKE_C_FLAGS "-g -fPIC -Werror -Wall ${CMAKE_C_FLAGS}")
Damjan Marion33ed3e42018-08-27 15:59:30 +020071endif()
72
Damjan Marion847d5282019-01-17 18:25:11 +010073if (compiler_flag_no_address_of_packed_member)
74 set(CMAKE_C_FLAGS "-Wno-address-of-packed-member ${CMAKE_C_FLAGS}")
75endif()
76
Damjan Marion2baa1152019-11-07 11:32:16 +010077# release
Damjan Marion952a7b82019-11-19 17:58:36 +010078list(APPEND BUILD_TYPES "release")
Damjan Marion2baa1152019-11-07 11:32:16 +010079string(CONCAT CMAKE_C_FLAGS_RELEASE
Damjan Marion1bb67ab2021-04-30 11:11:08 +020080 "-O3 "
Damjan Marion2baa1152019-11-07 11:32:16 +010081 "-fstack-protector "
Neale Ranns2a81d9a2021-01-20 09:03:30 +000082 "-D_FORTIFY_SOURCE=2 "
Damjan Marion2baa1152019-11-07 11:32:16 +010083 "-fno-common "
84)
85
86string(CONCAT CMAKE_EXE_LINKER_FLAGS_RELEASE "-pie")
87
88# debug
Damjan Marion952a7b82019-11-19 17:58:36 +010089list(APPEND BUILD_TYPES "debug")
Damjan Marion2baa1152019-11-07 11:32:16 +010090string(CONCAT CMAKE_C_FLAGS_DEBUG
91 "-O0 "
92 "-DCLIB_DEBUG "
93 "-fstack-protector "
Damjan Marion2baa1152019-11-07 11:32:16 +010094 "-fno-common "
95)
96
97# coverity
Damjan Marion952a7b82019-11-19 17:58:36 +010098list(APPEND BUILD_TYPES "coverity")
Damjan Marion2baa1152019-11-07 11:32:16 +010099string(CONCAT CMAKE_C_FLAGS_COVERITY "-O2 -D__COVERITY__")
100
101# gcov
Damjan Marion952a7b82019-11-19 17:58:36 +0100102list(APPEND BUILD_TYPES "gcov")
Damjan Marion2baa1152019-11-07 11:32:16 +0100103string(CONCAT CMAKE_C_FLAGS_GCOV
104 "-O0 "
105 "-DCLIB_DEBUG "
106 "-DCLIB_GCOV "
107 "-fprofile-arcs "
108 "-ftest-coverage ")
109
Damjan Marion847d5282019-01-17 18:25:11 +0100110string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UC)
111
Damjan Marion952a7b82019-11-19 17:58:36 +0100112
113string(REPLACE ";" " " BUILD_TYPES "${BUILD_TYPES}")
114set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
115 HELPSTRING "Build type - valid options are: ${BUILD_TYPES}")
116
Damjan Marion612dd6a2018-07-30 12:45:07 +0200117##############################################################################
Damjan Marionaf7892c2020-10-22 14:23:47 +0200118# link time optimizations
119##############################################################################
120if (CMAKE_BUILD_TYPE_UC STREQUAL "RELEASE")
121 check_ipo_supported(RESULT _result)
122 if (_result)
123 option(VPP_USE_LTO "Link time optimization of release binaries" ON)
124 endif()
125endif()
126
127##############################################################################
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200128# sanitizers
129##############################################################################
130
Damjan Marion599efc62020-05-07 16:49:45 +0200131option(VPP_ENABLE_SANITIZE_ADDR "Enable Address Sanitizer" OFF)
Benoît Ganne173484f2020-06-16 12:05:07 +0200132set(VPP_SANITIZE_ADDR_OPTIONS
Damjan Marionec3a3f12020-05-21 19:09:13 +0200133 "unmap_shadow_on_exit=1:disable_coredump=0:abort_on_error=1:detect_leaks=0"
134 CACHE
135 STRING "Address sanitizer arguments"
136)
137
Damjan Marion599efc62020-05-07 16:49:45 +0200138if (VPP_ENABLE_SANITIZE_ADDR)
Damjan Marioncea46522020-05-21 16:47:05 +0200139 set(CMAKE_C_FLAGS "-fsanitize=address -DCLIB_SANITIZE_ADDR ${CMAKE_C_FLAGS}")
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200140 set(CMAKE_EXE_LINKER_FLAGS "-fsanitize=address ${CMAKE_EXE_LINKER_FLAGS}")
141 set(CMAKE_SHARED_LINKER_FLAGS "-fsanitize=address ${CMAKE_SHARED_LINKER_FLAGS}")
Damjan Marion599efc62020-05-07 16:49:45 +0200142endif (VPP_ENABLE_SANITIZE_ADDR)
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200143
144##############################################################################
Benoît Gannef89bbbe2021-03-04 14:31:03 +0100145# trajectory trace
146##############################################################################
147
148option(VPP_ENABLE_TRAJECTORY_TRACE "Build vpp with trajectory tracing enabled" OFF)
149if(VPP_ENABLE_TRAJECTORY_TRACE)
150 set(CMAKE_C_FLAGS "-DVLIB_BUFFER_TRACE_TRAJECTORY=1 ${CMAKE_C_FLAGS}")
151endif()
152
153##############################################################################
Damjan Marion612dd6a2018-07-30 12:45:07 +0200154# install config
155##############################################################################
Nathan Moos2c919222021-01-15 13:28:34 -0800156option(VPP_SET_RPATH "Set rpath for resulting binaries and libraries." ON)
157if(VPP_SET_RPATH)
158 set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
159endif()
Damjan Marion612dd6a2018-07-30 12:45:07 +0200160set(CMAKE_INSTALL_MESSAGE NEVER)
161
Damjan Marion612dd6a2018-07-30 12:45:07 +0200162include_directories (
163 ${CMAKE_SOURCE_DIR}
Damjan Marion88b2e362021-04-29 18:47:25 +0200164 ${VPP_BINARY_DIR}
Damjan Marion612dd6a2018-07-30 12:45:07 +0200165)
166set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "vpp")
167
168set(THREADS_PREFER_PTHREAD_FLAG ON)
169find_package(Threads REQUIRED)
Damjan Marion612dd6a2018-07-30 12:45:07 +0200170
Damjan Marion1ee346a2019-03-18 17:06:51 +0100171include(cmake/syscall.cmake)
Damjan Mariond16004d2018-08-26 10:14:52 +0200172include(cmake/api.cmake)
Damjan Marion4553c952018-08-26 11:04:40 +0200173include(cmake/library.cmake)
174include(cmake/exec.cmake)
Damjan Mariond16004d2018-08-26 10:14:52 +0200175include(cmake/plugin.cmake)
Damjan Marion612dd6a2018-07-30 12:45:07 +0200176
177##############################################################################
Damjan Marion0abd4a22018-08-28 12:57:29 +0200178# subdirs - order matters
Damjan Marion612dd6a2018-07-30 12:45:07 +0200179##############################################################################
Damjan Marion599efc62020-05-07 16:49:45 +0200180option(VPP_HOST_TOOLS_ONLY "Build only host tools" OFF)
181if(VPP_HOST_TOOLS_ONLY)
182 set(SUBDIRS tools/vppapigen cmake)
183elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200184 find_package(OpenSSL REQUIRED)
185 set(SUBDIRS
Ole Troandf87f802020-11-18 19:17:48 +0100186 vppinfra svm vlib vlibmemory vlibapi vnet vpp vat vat2 vcl plugins
Damjan Marion5505ee82020-05-07 09:49:27 +0200187 vpp-api tools/vppapigen tools/g2 tools/perftool cmake pkg
Dave Barach0729f642019-03-24 16:25:03 -0400188 tools/appimage
Damjan Marion0fa900e2018-09-12 12:12:36 +0200189 )
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200190elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
191 set(SUBDIRS vppinfra)
192else()
193 message(FATAL_ERROR "Unsupported system: ${CMAKE_SYSTEM_NAME}")
194endif()
195
196foreach(DIR ${SUBDIRS})
Damjan Marion88b2e362021-04-29 18:47:25 +0200197 add_subdirectory(${DIR} ${VPP_BINARY_DIR}/${DIR})
Damjan Marion0abd4a22018-08-28 12:57:29 +0200198endforeach()
Damjan Marion612dd6a2018-07-30 12:45:07 +0200199
Damjan Marioneeadc142018-09-13 20:02:12 +0200200##############################################################################
Damjan Marionc6c02462018-08-31 17:38:57 +0200201# detect if we are inside git repo and add configure dependency
202##############################################################################
203execute_process(
204 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
205 COMMAND git rev-parse --show-toplevel
206 OUTPUT_VARIABLE VPP_GIT_TOPLEVEL_DIR
207 OUTPUT_STRIP_TRAILING_WHITESPACE
Damjan Marion6077c972019-01-28 17:55:59 +0100208 ERROR_QUIET
Damjan Marionc6c02462018-08-31 17:38:57 +0200209)
210
211if (VPP_GIT_TOPLEVEL_DIR)
212 set_property(
213 DIRECTORY APPEND PROPERTY
214 CMAKE_CONFIGURE_DEPENDS ${VPP_GIT_TOPLEVEL_DIR}/.git/index
215 )
216endif()
217
218##############################################################################
Damjan Marion88b2e362021-04-29 18:47:25 +0200219# custom targets
220##############################################################################
221
222add_custom_target(run
223 COMMAND ./${VPP_RUNTIME_DIR}/vpp -c startup.conf
224 COMMENT "Starting VPP..."
225 USES_TERMINAL
226)
227
228add_custom_target(debug
229 COMMAND gdb --args ./${VPP_RUNTIME_DIR}/vpp -c startup.conf
230 COMMENT "Starting VPP in the debugger..."
231 USES_TERMINAL
232)
233
Damjan Marionc8983242021-04-30 19:31:09 +0200234add_custom_target(config
Damjan Marion88b2e362021-04-29 18:47:25 +0200235 COMMAND ccmake ${CMAKE_BINARY_DIR}
236 COMMENT "Starting Configuration TUI..."
237 USES_TERMINAL
238)
239
240add_custom_target(compdb
241 COMMAND ninja -C ${CMAKE_BINARY_DIR} -t compdb |
242 ${CMAKE_SOURCE_DIR}/scripts/compdb_cleanup.py >
243 ${CMAKE_BINARY_DIR}/compile_commands.json
244 COMMENT "Generating compile_commands.json"
245 USES_TERMINAL
246)
247
248##############################################################################
Damjan Marionc6c02462018-08-31 17:38:57 +0200249# print configuration
250##############################################################################
251message(STATUS "Configuration:")
252pr("VPP version" "${VPP_VERSION}")
253pr("VPP library version" "${VPP_LIB_VERSION}")
254pr("GIT toplevel dir" "${VPP_GIT_TOPLEVEL_DIR}")
Damjan Marion847d5282019-01-17 18:25:11 +0100255pr("Build type" "${CMAKE_BUILD_TYPE}")
256pr("C flags" "${CMAKE_C_FLAGS}${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UC}}")
Damjan Marion2baa1152019-11-07 11:32:16 +0100257pr("Linker flags (apps)" "${CMAKE_EXE_LINKER_FLAGS}${CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UC}}")
258pr("Linker flags (libs)" "${CMAKE_SHARED_LINKER_FLAGS}${CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UC}}")
Damjan Mariondd395292019-01-15 00:36:03 +0100259pr("Host processor" "${CMAKE_HOST_SYSTEM_PROCESSOR}")
Damjan Marionc6c02462018-08-31 17:38:57 +0200260pr("Target processor" "${CMAKE_SYSTEM_PROCESSOR}")
Damjan Marion4a6cb832018-09-18 18:41:38 +0200261pr("Prefix path" "${CMAKE_PREFIX_PATH}")
Damjan Marionc6c02462018-08-31 17:38:57 +0200262pr("Install prefix" "${CMAKE_INSTALL_PREFIX}")