blob: 8f057e98f3ab53d24855b0eff922c3c67f0df86e [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
64check_function_exists(memfd_create HAVE_MEMFD_CREATE)
65if(${HAVE_MEMFD_CREATE})
66 add_definitions(-DHAVE_MEMFD_CREATE)
67endif()
68
69include_directories(src)
msardara8f554b72018-12-11 18:36:55 +010070
msardara8f554b72018-12-11 18:36:55 +010071add_subdirectory(src)
72add_subdirectory(examples)
73
Daniel Béreš98533422023-01-19 10:19:27 +010074enable_testing()
75include(CTest)
76add_subdirectory(test)
Damjan Marion599efc62020-05-07 16:49:45 +020077##############################################################################
78# Packaging
79##############################################################################
80
81# parse /etc/os-release
82file(READ "/etc/os-release" os_version)
83string(REPLACE "\n" ";" os_version ${os_version})
84foreach(_ver ${os_version})
85 string(REPLACE "=" ";" _ver ${_ver})
86 list(GET _ver 0 _name)
87 list(GET _ver 1 _value)
88 set(OS_${_name} ${_value})
89endforeach()
90
91 # extract version from git
92execute_process(
93 COMMAND git describe --long --match v*
94 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
95 OUTPUT_VARIABLE VER
96 OUTPUT_STRIP_TRAILING_WHITESPACE
msardara8f554b72018-12-11 18:36:55 +010097)
Damjan Marion599efc62020-05-07 16:49:45 +020098string(REGEX REPLACE "v(.*)-([0-9]+)-(g[0-9a-f]+)" "\\1;\\2;\\3" VER ${VER})
99list(GET VER 0 tag)
100list(GET VER 1 commit_num)
101list(GET VER 2 commit_name)
102
103#define DEB and RPM version numbers
104if(${commit_num} EQUAL 0)
105 set(deb_ver "${tag}")
106 set(rpm_ver "${tag}")
107else()
108 if (DEFINED ENV{BUILD_NUMBER})
109 set(deb_ver "${tag}~${commit_num}-${commit_name}~b$ENV{BUILD_NUMBER}")
110 set(rpm_ver "${tag}~${commit_num}_${commit_name}~b$ENV{BUILD_NUMBER}")
111 else()
112 set(deb_ver "${tag}~${commit_num}-${commit_name}")
113 set(rpm_ver "${tag}~${commit_num}_${commit_name}")
114 endif()
115endif()
116
117set(CPACK_PACKAGE_NAME "memif")
118set(CPACK_STRIP_FILES OFF)
119set(CPACK_PACKAGE_VENDOR "fd.io")
120set(CPACK_COMPONENTS_IGNORE_GROUPS 1)
121set(CPACK_${CPACK_GENERATOR}_COMPONENT_INSTALL ON)
122set(CPACK_${type}_PACKAGE_DESCRIPTION "memif Shared Memory Interface")
123set(CPACK_${type}_PACKAGE_RELEASE 1)
124
125if(OS_ID_LIKE MATCHES "debian")
126 set(CPACK_GENERATOR "DEB")
127 set(type "DEBIAN")
128 set(CPACK_PACKAGE_VERSION "${deb_ver}")
129 set(CPACK_DEBIAN_PACKAGE_MAINTAINER "VPP Team")
130 execute_process(
131 COMMAND dpkg --print-architecture
132 OUTPUT_VARIABLE CPACK_DEBIAN_PACKAGE_ARCHITECTURE
133 OUTPUT_STRIP_TRAILING_WHITESPACE
134 )
135 set(CPACK_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}_${CPACK_PACKAGE_VERSION}_${CPACK_DEBIAN_PACKAGE_ARCHITECTURE})
136elseif(OS_ID_LIKE MATCHES "rhel")
137 set(CPACK_GENERATOR "RPM")
138 set(type "RPM")
139 set(CPACK_PACKAGE_VERSION "${rpm_ver}")
140 execute_process(
141 COMMAND uname -m
142 OUTPUT_VARIABLE CPACK_RPM_PACKAGE_ARCHITECTURE
143 OUTPUT_STRIP_TRAILING_WHITESPACE
144 )
145 set(CPACK_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}.${CPACK_RPM_PACKAGE_ARCHITECTURE})
146endif()
147
148if(CPACK_GENERATOR)
149 include(CPack)
150else()
151 message(ERROR "CPACK_GENERATOR must be set")
152endif()
153