blob: 59c7512a044e84662aefbf8f924a76b21136bbcc [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
14cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
15
Damjan Marion162330f2020-04-29 21:28:15 +020016set(CMAKE_C_COMPILER_NAMES clang-10 clang-9 gcc-9 cc)
Damjan Marion4ba16a42020-04-28 13:29:37 +020017
Damjan Marion612dd6a2018-07-30 12:45:07 +020018project(vpp C)
19
20include(CheckCCompilerFlag)
Damjan Marionff426932019-01-26 14:12:25 +010021include(cmake/misc.cmake)
Damjan Mariond16004d2018-08-26 10:14:52 +020022include(cmake/cpu.cmake)
23include(cmake/ccache.cmake)
Damjan Marion612dd6a2018-07-30 12:45:07 +020024
25##############################################################################
Damjan Marionc6c02462018-08-31 17:38:57 +020026# VPP Version
27##############################################################################
28execute_process(
29 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
30 COMMAND scripts/version
31 OUTPUT_VARIABLE VPP_VERSION
32 OUTPUT_STRIP_TRAILING_WHITESPACE
33)
34string(REPLACE "-" ";" VPP_LIB_VERSION ${VPP_VERSION})
35list(GET VPP_LIB_VERSION 0 VPP_LIB_VERSION)
36
37##############################################################################
Damjan Mariondd395292019-01-15 00:36:03 +010038# cross compiling
39##############################################################################
40if(CMAKE_CROSSCOMPILING)
41 set(CMAKE_IGNORE_PATH
42 /usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/
43 /usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/lib/
44 )
45endif()
Damjan Marion64911362019-01-18 17:09:44 +010046set(CMAKE_C_COMPILER_TARGET ${CMAKE_SYSTEM_PROCESSOR}-linux-gnu)
Damjan Mariondd395292019-01-15 00:36:03 +010047
48##############################################################################
Damjan Marion612dd6a2018-07-30 12:45:07 +020049# build config
50##############################################################################
Damjan Marion847d5282019-01-17 18:25:11 +010051check_c_compiler_flag("-Wno-address-of-packed-member"
52 compiler_flag_no_address_of_packed_member)
Damjan Marion599efc62020-05-07 16:49:45 +020053set(VPP_RUNTIME_DIR "bin" CACHE STRING "Relative runtime directory path")
54set(VPP_LIBRARY_DIR "lib" CACHE STRING "Relative library directory path")
Damjan Marion847d5282019-01-17 18:25:11 +010055
Damjan Marion599efc62020-05-07 16:49:45 +020056set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${VPP_RUNTIME_DIR})
57set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${VPP_LIBRARY_DIR})
Damjan Marion612dd6a2018-07-30 12:45:07 +020058
Damjan Marion847d5282019-01-17 18:25:11 +010059if (CMAKE_BUILD_TYPE)
Damjan Marion2baa1152019-11-07 11:32:16 +010060 set(CMAKE_C_FLAGS "-g -fPIC -Werror -Wall ${CMAKE_C_FLAGS}")
Damjan Marion33ed3e42018-08-27 15:59:30 +020061endif()
62
Damjan Marion847d5282019-01-17 18:25:11 +010063if (compiler_flag_no_address_of_packed_member)
64 set(CMAKE_C_FLAGS "-Wno-address-of-packed-member ${CMAKE_C_FLAGS}")
65endif()
66
Damjan Marion2baa1152019-11-07 11:32:16 +010067# release
Damjan Marion952a7b82019-11-19 17:58:36 +010068list(APPEND BUILD_TYPES "release")
Damjan Marion2baa1152019-11-07 11:32:16 +010069string(CONCAT CMAKE_C_FLAGS_RELEASE
70 "-O2 "
71 "-fstack-protector "
72 "-DFORTIFY_SOURCE=2 "
73 "-fno-common "
74)
75
76string(CONCAT CMAKE_EXE_LINKER_FLAGS_RELEASE "-pie")
77
78# debug
Damjan Marion952a7b82019-11-19 17:58:36 +010079list(APPEND BUILD_TYPES "debug")
Damjan Marion2baa1152019-11-07 11:32:16 +010080string(CONCAT CMAKE_C_FLAGS_DEBUG
81 "-O0 "
82 "-DCLIB_DEBUG "
83 "-fstack-protector "
84 "-DFORTIFY_SOURCE=2 "
85 "-fno-common "
86)
87
88# coverity
Damjan Marion952a7b82019-11-19 17:58:36 +010089list(APPEND BUILD_TYPES "coverity")
Damjan Marion2baa1152019-11-07 11:32:16 +010090string(CONCAT CMAKE_C_FLAGS_COVERITY "-O2 -D__COVERITY__")
91
92# gcov
Damjan Marion952a7b82019-11-19 17:58:36 +010093list(APPEND BUILD_TYPES "gcov")
Damjan Marion2baa1152019-11-07 11:32:16 +010094string(CONCAT CMAKE_C_FLAGS_GCOV
95 "-O0 "
96 "-DCLIB_DEBUG "
97 "-DCLIB_GCOV "
98 "-fprofile-arcs "
99 "-ftest-coverage ")
100
Damjan Marion847d5282019-01-17 18:25:11 +0100101string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UC)
102
Damjan Marion952a7b82019-11-19 17:58:36 +0100103
104string(REPLACE ";" " " BUILD_TYPES "${BUILD_TYPES}")
105set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
106 HELPSTRING "Build type - valid options are: ${BUILD_TYPES}")
107
Damjan Marion612dd6a2018-07-30 12:45:07 +0200108##############################################################################
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200109# sanitizers
110##############################################################################
111
Damjan Marion599efc62020-05-07 16:49:45 +0200112option(VPP_ENABLE_SANITIZE_ADDR "Enable Address Sanitizer" OFF)
113if (VPP_ENABLE_SANITIZE_ADDR)
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200114 set(CMAKE_C_FLAGS "-fsanitize=address --param asan-stack=0 -DCLIB_SANITIZE_ADDR ${CMAKE_C_FLAGS}")
115 set(CMAKE_EXE_LINKER_FLAGS "-fsanitize=address ${CMAKE_EXE_LINKER_FLAGS}")
116 set(CMAKE_SHARED_LINKER_FLAGS "-fsanitize=address ${CMAKE_SHARED_LINKER_FLAGS}")
Damjan Marion599efc62020-05-07 16:49:45 +0200117endif (VPP_ENABLE_SANITIZE_ADDR)
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200118
119##############################################################################
Damjan Marion612dd6a2018-07-30 12:45:07 +0200120# install config
121##############################################################################
Damjan Marion79dcbc72018-09-12 14:01:10 +0200122set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
Damjan Marion612dd6a2018-07-30 12:45:07 +0200123set(CMAKE_INSTALL_MESSAGE NEVER)
124
Damjan Marion612dd6a2018-07-30 12:45:07 +0200125include_directories (
126 ${CMAKE_SOURCE_DIR}
127 ${CMAKE_BINARY_DIR}
128 ${CMAKE_BINARY_DIR}/include
129)
130set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "vpp")
131
132set(THREADS_PREFER_PTHREAD_FLAG ON)
133find_package(Threads REQUIRED)
Damjan Marion612dd6a2018-07-30 12:45:07 +0200134
Damjan Marion1ee346a2019-03-18 17:06:51 +0100135include(cmake/syscall.cmake)
Damjan Mariond16004d2018-08-26 10:14:52 +0200136include(cmake/api.cmake)
Damjan Marion4553c952018-08-26 11:04:40 +0200137include(cmake/library.cmake)
138include(cmake/exec.cmake)
Damjan Mariond16004d2018-08-26 10:14:52 +0200139include(cmake/plugin.cmake)
Damjan Marion612dd6a2018-07-30 12:45:07 +0200140
141##############################################################################
Damjan Marion0abd4a22018-08-28 12:57:29 +0200142# subdirs - order matters
Damjan Marion612dd6a2018-07-30 12:45:07 +0200143##############################################################################
Damjan Marion599efc62020-05-07 16:49:45 +0200144option(VPP_HOST_TOOLS_ONLY "Build only host tools" OFF)
145if(VPP_HOST_TOOLS_ONLY)
146 set(SUBDIRS tools/vppapigen cmake)
147elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200148 find_package(OpenSSL REQUIRED)
149 set(SUBDIRS
150 vppinfra svm vlib vlibmemory vlibapi vnet vpp vat vcl plugins
Damjan Marion5505ee82020-05-07 09:49:27 +0200151 vpp-api tools/vppapigen tools/g2 tools/perftool cmake pkg
Dave Barach0729f642019-03-24 16:25:03 -0400152 tools/appimage
Damjan Marion0fa900e2018-09-12 12:12:36 +0200153 )
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200154elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
155 set(SUBDIRS vppinfra)
156else()
157 message(FATAL_ERROR "Unsupported system: ${CMAKE_SYSTEM_NAME}")
158endif()
159
160foreach(DIR ${SUBDIRS})
Damjan Marion612dd6a2018-07-30 12:45:07 +0200161 add_subdirectory(${DIR})
Damjan Marion0abd4a22018-08-28 12:57:29 +0200162endforeach()
Damjan Marion612dd6a2018-07-30 12:45:07 +0200163
Damjan Marioneeadc142018-09-13 20:02:12 +0200164##############################################################################
Damjan Marionc6c02462018-08-31 17:38:57 +0200165# detect if we are inside git repo and add configure dependency
166##############################################################################
167execute_process(
168 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
169 COMMAND git rev-parse --show-toplevel
170 OUTPUT_VARIABLE VPP_GIT_TOPLEVEL_DIR
171 OUTPUT_STRIP_TRAILING_WHITESPACE
Damjan Marion6077c972019-01-28 17:55:59 +0100172 ERROR_QUIET
Damjan Marionc6c02462018-08-31 17:38:57 +0200173)
174
175if (VPP_GIT_TOPLEVEL_DIR)
176 set_property(
177 DIRECTORY APPEND PROPERTY
178 CMAKE_CONFIGURE_DEPENDS ${VPP_GIT_TOPLEVEL_DIR}/.git/index
179 )
180endif()
181
182##############################################################################
183# print configuration
184##############################################################################
185message(STATUS "Configuration:")
186pr("VPP version" "${VPP_VERSION}")
187pr("VPP library version" "${VPP_LIB_VERSION}")
188pr("GIT toplevel dir" "${VPP_GIT_TOPLEVEL_DIR}")
Damjan Marion847d5282019-01-17 18:25:11 +0100189pr("Build type" "${CMAKE_BUILD_TYPE}")
190pr("C flags" "${CMAKE_C_FLAGS}${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UC}}")
Damjan Marion2baa1152019-11-07 11:32:16 +0100191pr("Linker flags (apps)" "${CMAKE_EXE_LINKER_FLAGS}${CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UC}}")
192pr("Linker flags (libs)" "${CMAKE_SHARED_LINKER_FLAGS}${CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UC}}")
Damjan Mariondd395292019-01-15 00:36:03 +0100193pr("Host processor" "${CMAKE_HOST_SYSTEM_PROCESSOR}")
Damjan Marionc6c02462018-08-31 17:38:57 +0200194pr("Target processor" "${CMAKE_SYSTEM_PROCESSOR}")
Damjan Marion4a6cb832018-09-18 18:41:38 +0200195pr("Prefix path" "${CMAKE_PREFIX_PATH}")
Damjan Marionc6c02462018-08-31 17:38:57 +0200196pr("Install prefix" "${CMAKE_INSTALL_PREFIX}")