blob: 1526abdce3a8106d23cfd4984e823afa6203bb01 [file] [log] [blame]
msardara8f554b72018-12-11 18:36:55 +01001# Copyright (c) 2017 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
16project(memif)
17set(CMAKE_C_STANDARD 11)
18
19include(CheckCCompilerFlag)
20include(CheckFunctionExists)
Daniel Béreš98533422023-01-19 10:19:27 +010021find_package(Git REQUIRED)
22
23include(ExternalProject)
24set(UNITY unity_project)
25
26ExternalProject_Add(
27 unity_project
28 GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git
29 GIT_TAG cf949f45ca6d172a177b00da21310607b97bc7a7
30 PREFIX ${PROJECT_BINARY_DIR}/external/${UNITY}
31 INSTALL_COMMAND cmake --install . --prefix ${PROJECT_BINARY_DIR}
32
33)
34set_source_files_properties(
35 ${PROJECT_BINARY_DIR}/external/${UNITY}/src/${UNITY}/src/unity.c
36 ${PROJECT_BINARY_DIR}/external/${UNITY}/src/${UNITY}/extras/fixture/src/unity_fixture.c
37 ${PROJECT_BINARY_DIR}/external/${UNITY}/src/${UNITY}/extras/memory/src/unity_memory.c
38 PROPERTIES GENERATED TRUE)
39add_library(unity STATIC
40 ${PROJECT_BINARY_DIR}/external/${UNITY}/src/${UNITY}/src/unity.c
41 ${PROJECT_BINARY_DIR}/external/${UNITY}/src/${UNITY}/extras/fixture/src/unity_fixture.c
42 ${PROJECT_BINARY_DIR}/external/${UNITY}/src/${UNITY}/extras/memory/src/unity_memory.c
43)
44target_include_directories(unity PUBLIC
45 ${PROJECT_BINARY_DIR}/external/${UNITY}/src/${UNITY}/src/
46 ${PROJECT_BINARY_DIR}/external/${UNITY}/src/${UNITY}/extras/fixture/src/
47 ${PROJECT_BINARY_DIR}/external/${UNITY}/src/${UNITY}/extras/memory/src/
48)
49add_dependencies(unity unity_project)
msardara8f554b72018-12-11 18:36:55 +010050
msardara8f554b72018-12-11 18:36:55 +010051if (NOT CMAKE_BUILD_TYPE)
52 message(STATUS "No build type selected, default to Release")
53 set(CMAKE_BUILD_TYPE "Release")
54endif ()
55
56set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -DMEMIF_DBG -DICMP_DBG")
57set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
58set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib)
59set(CMAKE_INSTALL_MESSAGE NEVER)
60
61find_package(Threads REQUIRED)
62include_directories(${CMAKE_THREADS_INCLUDE_DIRS})
63
Mohsin Kazmic60266d2024-01-17 12:00:16 +000064if(DEFINED LIBMEMIF_CACHELINE_SIZE)
65 # Cache line size assigned via cmake args
66elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)")
67 set(LIBMEMIF_CACHELINE_SIZE 128)
68else()
69 set(LIBMEMIF_CACHELINE_SIZE 64)
70endif()
71
72message(STATUS "System Architecture: ${CMAKE_SYSTEM_PROCESSOR}")
73message(STATUS "Libmemif Cacheline Size: ${LIBMEMIF_CACHELINE_SIZE}")
74
msardara8f554b72018-12-11 18:36:55 +010075check_function_exists(memfd_create HAVE_MEMFD_CREATE)
76if(${HAVE_MEMFD_CREATE})
77 add_definitions(-DHAVE_MEMFD_CREATE)
78endif()
79
80include_directories(src)
msardara8f554b72018-12-11 18:36:55 +010081
msardara8f554b72018-12-11 18:36:55 +010082add_subdirectory(src)
83add_subdirectory(examples)
84
Daniel Béreš98533422023-01-19 10:19:27 +010085enable_testing()
86include(CTest)
87add_subdirectory(test)
Damjan Marion599efc62020-05-07 16:49:45 +020088##############################################################################
89# Packaging
90##############################################################################
91
92# parse /etc/os-release
93file(READ "/etc/os-release" os_version)
94string(REPLACE "\n" ";" os_version ${os_version})
95foreach(_ver ${os_version})
96 string(REPLACE "=" ";" _ver ${_ver})
97 list(GET _ver 0 _name)
98 list(GET _ver 1 _value)
99 set(OS_${_name} ${_value})
100endforeach()
101
102 # extract version from git
103execute_process(
104 COMMAND git describe --long --match v*
105 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
106 OUTPUT_VARIABLE VER
107 OUTPUT_STRIP_TRAILING_WHITESPACE
msardara8f554b72018-12-11 18:36:55 +0100108)
Damjan Marion599efc62020-05-07 16:49:45 +0200109string(REGEX REPLACE "v(.*)-([0-9]+)-(g[0-9a-f]+)" "\\1;\\2;\\3" VER ${VER})
110list(GET VER 0 tag)
111list(GET VER 1 commit_num)
112list(GET VER 2 commit_name)
113
114#define DEB and RPM version numbers
115if(${commit_num} EQUAL 0)
116 set(deb_ver "${tag}")
117 set(rpm_ver "${tag}")
118else()
119 if (DEFINED ENV{BUILD_NUMBER})
120 set(deb_ver "${tag}~${commit_num}-${commit_name}~b$ENV{BUILD_NUMBER}")
121 set(rpm_ver "${tag}~${commit_num}_${commit_name}~b$ENV{BUILD_NUMBER}")
122 else()
123 set(deb_ver "${tag}~${commit_num}-${commit_name}")
124 set(rpm_ver "${tag}~${commit_num}_${commit_name}")
125 endif()
126endif()
127
128set(CPACK_PACKAGE_NAME "memif")
129set(CPACK_STRIP_FILES OFF)
130set(CPACK_PACKAGE_VENDOR "fd.io")
131set(CPACK_COMPONENTS_IGNORE_GROUPS 1)
132set(CPACK_${CPACK_GENERATOR}_COMPONENT_INSTALL ON)
133set(CPACK_${type}_PACKAGE_DESCRIPTION "memif Shared Memory Interface")
134set(CPACK_${type}_PACKAGE_RELEASE 1)
135
136if(OS_ID_LIKE MATCHES "debian")
137 set(CPACK_GENERATOR "DEB")
138 set(type "DEBIAN")
139 set(CPACK_PACKAGE_VERSION "${deb_ver}")
140 set(CPACK_DEBIAN_PACKAGE_MAINTAINER "VPP Team")
141 execute_process(
142 COMMAND dpkg --print-architecture
143 OUTPUT_VARIABLE CPACK_DEBIAN_PACKAGE_ARCHITECTURE
144 OUTPUT_STRIP_TRAILING_WHITESPACE
145 )
146 set(CPACK_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}_${CPACK_PACKAGE_VERSION}_${CPACK_DEBIAN_PACKAGE_ARCHITECTURE})
147elseif(OS_ID_LIKE MATCHES "rhel")
148 set(CPACK_GENERATOR "RPM")
149 set(type "RPM")
150 set(CPACK_PACKAGE_VERSION "${rpm_ver}")
151 execute_process(
152 COMMAND uname -m
153 OUTPUT_VARIABLE CPACK_RPM_PACKAGE_ARCHITECTURE
154 OUTPUT_STRIP_TRAILING_WHITESPACE
155 )
156 set(CPACK_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}.${CPACK_RPM_PACKAGE_ARCHITECTURE})
157endif()
158
159if(CPACK_GENERATOR)
160 include(CPack)
161else()
162 message(ERROR "CPACK_GENERATOR must be set")
163endif()
164