Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 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: |
| 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 | |
| 14 | cmake_minimum_required(VERSION 3.5 FATAL_ERROR) |
| 15 | |
| 16 | project(vpp C) |
| 17 | |
| 18 | include(CheckCCompilerFlag) |
Damjan Marion | ff42693 | 2019-01-26 14:12:25 +0100 | [diff] [blame] | 19 | include(cmake/misc.cmake) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 20 | include(cmake/cpu.cmake) |
| 21 | include(cmake/ccache.cmake) |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 22 | |
| 23 | ############################################################################## |
Damjan Marion | c6c0246 | 2018-08-31 17:38:57 +0200 | [diff] [blame] | 24 | # VPP Version |
| 25 | ############################################################################## |
| 26 | execute_process( |
| 27 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 28 | COMMAND scripts/version |
| 29 | OUTPUT_VARIABLE VPP_VERSION |
| 30 | OUTPUT_STRIP_TRAILING_WHITESPACE |
| 31 | ) |
| 32 | string(REPLACE "-" ";" VPP_LIB_VERSION ${VPP_VERSION}) |
| 33 | list(GET VPP_LIB_VERSION 0 VPP_LIB_VERSION) |
| 34 | |
| 35 | ############################################################################## |
Damjan Marion | dd39529 | 2019-01-15 00:36:03 +0100 | [diff] [blame] | 36 | # cross compiling |
| 37 | ############################################################################## |
| 38 | if(CMAKE_CROSSCOMPILING) |
| 39 | set(CMAKE_IGNORE_PATH |
| 40 | /usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/ |
| 41 | /usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/lib/ |
| 42 | ) |
| 43 | endif() |
Damjan Marion | 6491136 | 2019-01-18 17:09:44 +0100 | [diff] [blame] | 44 | set(CMAKE_C_COMPILER_TARGET ${CMAKE_SYSTEM_PROCESSOR}-linux-gnu) |
Damjan Marion | dd39529 | 2019-01-15 00:36:03 +0100 | [diff] [blame] | 45 | |
| 46 | ############################################################################## |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 47 | # build config |
| 48 | ############################################################################## |
Damjan Marion | 847d528 | 2019-01-17 18:25:11 +0100 | [diff] [blame] | 49 | check_c_compiler_flag("-Wno-address-of-packed-member" |
| 50 | compiler_flag_no_address_of_packed_member) |
| 51 | |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 52 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) |
| 53 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 54 | |
Damjan Marion | 847d528 | 2019-01-17 18:25:11 +0100 | [diff] [blame] | 55 | if (CMAKE_BUILD_TYPE) |
Damjan Marion | 2baa115 | 2019-11-07 11:32:16 +0100 | [diff] [blame] | 56 | set(CMAKE_C_FLAGS "-g -fPIC -Werror -Wall ${CMAKE_C_FLAGS}") |
Damjan Marion | 33ed3e4 | 2018-08-27 15:59:30 +0200 | [diff] [blame] | 57 | endif() |
| 58 | |
Damjan Marion | 847d528 | 2019-01-17 18:25:11 +0100 | [diff] [blame] | 59 | if (compiler_flag_no_address_of_packed_member) |
| 60 | set(CMAKE_C_FLAGS "-Wno-address-of-packed-member ${CMAKE_C_FLAGS}") |
| 61 | endif() |
| 62 | |
Damjan Marion | 2baa115 | 2019-11-07 11:32:16 +0100 | [diff] [blame] | 63 | # release |
Damjan Marion | 952a7b8 | 2019-11-19 17:58:36 +0100 | [diff] [blame] | 64 | list(APPEND BUILD_TYPES "release") |
Damjan Marion | 2baa115 | 2019-11-07 11:32:16 +0100 | [diff] [blame] | 65 | string(CONCAT CMAKE_C_FLAGS_RELEASE |
| 66 | "-O2 " |
| 67 | "-fstack-protector " |
| 68 | "-DFORTIFY_SOURCE=2 " |
| 69 | "-fno-common " |
| 70 | ) |
| 71 | |
| 72 | string(CONCAT CMAKE_EXE_LINKER_FLAGS_RELEASE "-pie") |
| 73 | |
| 74 | # debug |
Damjan Marion | 952a7b8 | 2019-11-19 17:58:36 +0100 | [diff] [blame] | 75 | list(APPEND BUILD_TYPES "debug") |
Damjan Marion | 2baa115 | 2019-11-07 11:32:16 +0100 | [diff] [blame] | 76 | string(CONCAT CMAKE_C_FLAGS_DEBUG |
| 77 | "-O0 " |
| 78 | "-DCLIB_DEBUG " |
| 79 | "-fstack-protector " |
| 80 | "-DFORTIFY_SOURCE=2 " |
| 81 | "-fno-common " |
| 82 | ) |
| 83 | |
| 84 | # coverity |
Damjan Marion | 952a7b8 | 2019-11-19 17:58:36 +0100 | [diff] [blame] | 85 | list(APPEND BUILD_TYPES "coverity") |
Damjan Marion | 2baa115 | 2019-11-07 11:32:16 +0100 | [diff] [blame] | 86 | string(CONCAT CMAKE_C_FLAGS_COVERITY "-O2 -D__COVERITY__") |
| 87 | |
| 88 | # gcov |
Damjan Marion | 952a7b8 | 2019-11-19 17:58:36 +0100 | [diff] [blame] | 89 | list(APPEND BUILD_TYPES "gcov") |
Damjan Marion | 2baa115 | 2019-11-07 11:32:16 +0100 | [diff] [blame] | 90 | string(CONCAT CMAKE_C_FLAGS_GCOV |
| 91 | "-O0 " |
| 92 | "-DCLIB_DEBUG " |
| 93 | "-DCLIB_GCOV " |
| 94 | "-fprofile-arcs " |
| 95 | "-ftest-coverage ") |
| 96 | |
Damjan Marion | 847d528 | 2019-01-17 18:25:11 +0100 | [diff] [blame] | 97 | string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UC) |
| 98 | |
Damjan Marion | 952a7b8 | 2019-11-19 17:58:36 +0100 | [diff] [blame] | 99 | |
| 100 | string(REPLACE ";" " " BUILD_TYPES "${BUILD_TYPES}") |
| 101 | set_property(CACHE CMAKE_BUILD_TYPE PROPERTY |
| 102 | HELPSTRING "Build type - valid options are: ${BUILD_TYPES}") |
| 103 | |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 104 | ############################################################################## |
BenoƮt Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame^] | 105 | # sanitizers |
| 106 | ############################################################################## |
| 107 | |
| 108 | option(ENABLE_SANITIZE_ADDR "Enable Address Sanitizer" OFF) |
| 109 | if (ENABLE_SANITIZE_ADDR) |
| 110 | set(CMAKE_C_FLAGS "-fsanitize=address --param asan-stack=0 -DCLIB_SANITIZE_ADDR ${CMAKE_C_FLAGS}") |
| 111 | set(CMAKE_EXE_LINKER_FLAGS "-fsanitize=address ${CMAKE_EXE_LINKER_FLAGS}") |
| 112 | set(CMAKE_SHARED_LINKER_FLAGS "-fsanitize=address ${CMAKE_SHARED_LINKER_FLAGS}") |
| 113 | endif (ENABLE_SANITIZE_ADDR) |
| 114 | |
| 115 | ############################################################################## |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 116 | # install config |
| 117 | ############################################################################## |
Damjan Marion | 79dcbc7 | 2018-09-12 14:01:10 +0200 | [diff] [blame] | 118 | set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 119 | set(CMAKE_INSTALL_MESSAGE NEVER) |
| 120 | |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 121 | include_directories ( |
| 122 | ${CMAKE_SOURCE_DIR} |
| 123 | ${CMAKE_BINARY_DIR} |
| 124 | ${CMAKE_BINARY_DIR}/include |
| 125 | ) |
| 126 | set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "vpp") |
| 127 | |
| 128 | set(THREADS_PREFER_PTHREAD_FLAG ON) |
| 129 | find_package(Threads REQUIRED) |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 130 | |
Damjan Marion | 1ee346a | 2019-03-18 17:06:51 +0100 | [diff] [blame] | 131 | include(cmake/syscall.cmake) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 132 | include(cmake/api.cmake) |
Damjan Marion | 4553c95 | 2018-08-26 11:04:40 +0200 | [diff] [blame] | 133 | include(cmake/library.cmake) |
| 134 | include(cmake/exec.cmake) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 135 | include(cmake/plugin.cmake) |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 136 | |
| 137 | ############################################################################## |
Damjan Marion | 0abd4a2 | 2018-08-28 12:57:29 +0200 | [diff] [blame] | 138 | # subdirs - order matters |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 139 | ############################################################################## |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 140 | if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") |
| 141 | find_package(OpenSSL REQUIRED) |
| 142 | set(SUBDIRS |
| 143 | vppinfra svm vlib vlibmemory vlibapi vnet vpp vat vcl plugins |
Damjan Marion | 4d2f86a | 2019-01-18 13:28:22 +0100 | [diff] [blame] | 144 | vpp-api tools/vppapigen tools/g2 tools/elftool tools/perftool cmake pkg |
Dave Barach | 0729f64 | 2019-03-24 16:25:03 -0400 | [diff] [blame] | 145 | tools/appimage |
Damjan Marion | 0fa900e | 2018-09-12 12:12:36 +0200 | [diff] [blame] | 146 | ) |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 147 | elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin") |
| 148 | set(SUBDIRS vppinfra) |
| 149 | else() |
| 150 | message(FATAL_ERROR "Unsupported system: ${CMAKE_SYSTEM_NAME}") |
| 151 | endif() |
| 152 | |
| 153 | foreach(DIR ${SUBDIRS}) |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 154 | add_subdirectory(${DIR}) |
Damjan Marion | 0abd4a2 | 2018-08-28 12:57:29 +0200 | [diff] [blame] | 155 | endforeach() |
Damjan Marion | 612dd6a | 2018-07-30 12:45:07 +0200 | [diff] [blame] | 156 | |
Damjan Marion | eeadc14 | 2018-09-13 20:02:12 +0200 | [diff] [blame] | 157 | ############################################################################## |
Damjan Marion | c6c0246 | 2018-08-31 17:38:57 +0200 | [diff] [blame] | 158 | # detect if we are inside git repo and add configure dependency |
| 159 | ############################################################################## |
| 160 | execute_process( |
| 161 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 162 | COMMAND git rev-parse --show-toplevel |
| 163 | OUTPUT_VARIABLE VPP_GIT_TOPLEVEL_DIR |
| 164 | OUTPUT_STRIP_TRAILING_WHITESPACE |
Damjan Marion | 6077c97 | 2019-01-28 17:55:59 +0100 | [diff] [blame] | 165 | ERROR_QUIET |
Damjan Marion | c6c0246 | 2018-08-31 17:38:57 +0200 | [diff] [blame] | 166 | ) |
| 167 | |
| 168 | if (VPP_GIT_TOPLEVEL_DIR) |
| 169 | set_property( |
| 170 | DIRECTORY APPEND PROPERTY |
| 171 | CMAKE_CONFIGURE_DEPENDS ${VPP_GIT_TOPLEVEL_DIR}/.git/index |
| 172 | ) |
| 173 | endif() |
| 174 | |
| 175 | ############################################################################## |
| 176 | # print configuration |
| 177 | ############################################################################## |
| 178 | message(STATUS "Configuration:") |
| 179 | pr("VPP version" "${VPP_VERSION}") |
| 180 | pr("VPP library version" "${VPP_LIB_VERSION}") |
| 181 | pr("GIT toplevel dir" "${VPP_GIT_TOPLEVEL_DIR}") |
Damjan Marion | 847d528 | 2019-01-17 18:25:11 +0100 | [diff] [blame] | 182 | pr("Build type" "${CMAKE_BUILD_TYPE}") |
| 183 | pr("C flags" "${CMAKE_C_FLAGS}${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UC}}") |
Damjan Marion | 2baa115 | 2019-11-07 11:32:16 +0100 | [diff] [blame] | 184 | pr("Linker flags (apps)" "${CMAKE_EXE_LINKER_FLAGS}${CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UC}}") |
| 185 | pr("Linker flags (libs)" "${CMAKE_SHARED_LINKER_FLAGS}${CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UC}}") |
Damjan Marion | dd39529 | 2019-01-15 00:36:03 +0100 | [diff] [blame] | 186 | pr("Host processor" "${CMAKE_HOST_SYSTEM_PROCESSOR}") |
Damjan Marion | c6c0246 | 2018-08-31 17:38:57 +0200 | [diff] [blame] | 187 | pr("Target processor" "${CMAKE_SYSTEM_PROCESSOR}") |
Damjan Marion | 4a6cb83 | 2018-09-18 18:41:38 +0200 | [diff] [blame] | 188 | pr("Prefix path" "${CMAKE_PREFIX_PATH}") |
Damjan Marion | c6c0246 | 2018-08-31 17:38:57 +0200 | [diff] [blame] | 189 | pr("Install prefix" "${CMAKE_INSTALL_PREFIX}") |