blob: ad4adfcabc4f034ab8f8f7584d5b68632ef5b9ad [file] [log] [blame]
Damjan Marion4553c952018-08-26 11:04:40 +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
14macro(add_vpp_library lib)
15 cmake_parse_arguments(ARG
Damjan Marionaf7892c2020-10-22 14:23:47 +020016 "LTO"
Damjan Marion43b06062018-08-29 22:20:45 +020017 "COMPONENT"
Damjan Marion4553c952018-08-26 11:04:40 +020018 "SOURCES;MULTIARCH_SOURCES;API_FILES;LINK_LIBRARIES;INSTALL_HEADERS;DEPENDS"
19 ${ARGN}
20 )
21
Damjan Marion115012a2021-04-02 17:35:13 +020022 set (lo ${lib}_objs)
23 add_library(${lo} OBJECT ${ARG_SOURCES})
24 set_target_properties(${lo} PROPERTIES POSITION_INDEPENDENT_CODE ON)
Damjan Marion0d39cba2021-05-10 14:51:44 +020025 target_compile_options(${lo} PUBLIC ${VPP_DEFAULT_MARCH_FLAGS})
Damjan Marion115012a2021-04-02 17:35:13 +020026
27 add_library(${lib} SHARED)
28 target_sources(${lib} PRIVATE $<TARGET_OBJECTS:${lo}>)
29
Damjan Marion958192d2018-09-13 18:43:19 +020030 if(VPP_LIB_VERSION)
31 set_target_properties(${lib} PROPERTIES SOVERSION ${VPP_LIB_VERSION})
32 endif()
Damjan Marion4553c952018-08-26 11:04:40 +020033
34 # library deps
35 if(ARG_LINK_LIBRARIES)
36 target_link_libraries(${lib} ${ARG_LINK_LIBRARIES})
37 endif()
38 # install .so
Damjan Marion43b06062018-08-29 22:20:45 +020039 if(NOT ARG_COMPONENT)
40 set(ARG_COMPONENT vpp)
41 endif()
42 install(
43 TARGETS ${lib}
Damjan Marion599efc62020-05-07 16:49:45 +020044 DESTINATION ${VPP_LIBRARY_DIR}
Damjan Marion43b06062018-08-29 22:20:45 +020045 COMPONENT ${ARG_COMPONENT}
46 )
Damjan Marion4553c952018-08-26 11:04:40 +020047
Damjan Marionaf7892c2020-10-22 14:23:47 +020048 if (ARG_LTO AND VPP_USE_LTO)
Damjan Marion115012a2021-04-02 17:35:13 +020049 set_property(TARGET ${lo} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
Damjan Marionaf7892c2020-10-22 14:23:47 +020050 set_property(TARGET ${lib} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
51 target_compile_options (${lib} PRIVATE "-ffunction-sections")
52 target_compile_options (${lib} PRIVATE "-fdata-sections")
53 target_link_libraries (${lib} "-Wl,--gc-sections")
Damjan Marion1e267242021-11-06 17:09:37 +010054 if(compiler_flag_no_stringop_overflow)
55 target_link_libraries (${lib} "-Wno-stringop-overflow")
56 endif()
Damjan Marionaf7892c2020-10-22 14:23:47 +020057 endif()
58
Damjan Marion4553c952018-08-26 11:04:40 +020059 if(ARG_MULTIARCH_SOURCES)
Ole Troan7d527a22020-12-02 14:19:49 +010060 vpp_library_set_multiarch_sources(${lib} DEPENDS ${ARG_DEPENDS} SOURCES ${ARG_MULTIARCH_SOURCES})
Damjan Marion4553c952018-08-26 11:04:40 +020061 endif()
62
63 if(ARG_API_FILES)
Oliver Gilesdc203712019-12-05 23:37:36 +020064 vpp_add_api_files(${lib} core vpp ${ARG_API_FILES})
Damjan Marion4553c952018-08-26 11:04:40 +020065 foreach(file ${ARG_API_FILES})
66 get_filename_component(dir ${file} DIRECTORY)
Damjan Marion43b06062018-08-29 22:20:45 +020067 install(
BenoƮt Ganne98438e42019-07-25 16:26:20 +020068 FILES ${file} ${CMAKE_CURRENT_BINARY_DIR}/${file}.h
Ole Troan2a1ca782019-09-19 01:08:30 +020069 ${CMAKE_CURRENT_BINARY_DIR}/${file}_enum.h
70 ${CMAKE_CURRENT_BINARY_DIR}/${file}_types.h
Ole Troandf87f802020-11-18 19:17:48 +010071 ${CMAKE_CURRENT_BINARY_DIR}/${file}_tojson.h
72 ${CMAKE_CURRENT_BINARY_DIR}/${file}_fromjson.h
Damjan Marion43b06062018-08-29 22:20:45 +020073 DESTINATION include/${lib}/${dir}
74 COMPONENT vpp-dev
75 )
Damjan Marion4553c952018-08-26 11:04:40 +020076 endforeach()
77 endif()
78
Ole Troan7d527a22020-12-02 14:19:49 +010079 if(NOT VPP_EXTERNAL_PROJECT)
Damjan Marion115012a2021-04-02 17:35:13 +020080 add_dependencies(${lo} api_headers)
Ole Troan7d527a22020-12-02 14:19:49 +010081 endif()
82
Guillaume Solignac08bd44b2021-09-28 15:01:26 +020083 if(VPP_EXTERNAL_PROJECT AND ARG_API_FILES)
84 add_dependencies(${lo} ${lib}_api_headers)
85 endif()
86
Damjan Marion4553c952018-08-26 11:04:40 +020087 if(ARG_DEPENDS)
Damjan Marion115012a2021-04-02 17:35:13 +020088 add_dependencies(${lo} ${ARG_DEPENDS})
Damjan Marion4553c952018-08-26 11:04:40 +020089 endif()
90
91 # install headers
92 if(ARG_INSTALL_HEADERS)
93 foreach(file ${ARG_INSTALL_HEADERS})
94 get_filename_component(dir ${file} DIRECTORY)
Damjan Marion43b06062018-08-29 22:20:45 +020095 install(
96 FILES ${file}
97 DESTINATION include/${lib}/${dir}
Damjan Marioneeadc142018-09-13 20:02:12 +020098 COMPONENT ${ARG_COMPONENT}-dev
Damjan Marion43b06062018-08-29 22:20:45 +020099 )
Damjan Marion4553c952018-08-26 11:04:40 +0200100 endforeach()
101 endif()
102endmacro()
103
104##############################################################################
105# header files
106##############################################################################
107function (add_vpp_headers path)
108 foreach(file ${ARGN})
109 get_filename_component(dir ${file} DIRECTORY)
Damjan Marion833de8c2018-09-07 12:39:02 +0200110 install(
111 FILES ${file}
112 DESTINATION include/${path}/${dir}
113 COMPONENT vpp-dev
114 )
Damjan Marion4553c952018-08-26 11:04:40 +0200115 endforeach()
116endfunction()
Ole Troandf87f802020-11-18 19:17:48 +0100117
Florin Coras248210c2021-09-14 18:54:45 -0700118macro(add_vat_test_library lib)
119 cmake_parse_arguments(TEST
120 ""
121 ""
122 ${ARGN}
123 )
124
125 foreach(file ${ARGN})
126 get_filename_component(name ${file} NAME_WE)
127 set(test_lib ${lib}_${name}_plugin)
128 add_library(${test_lib} SHARED ${file})
129 target_compile_options(${test_lib} PUBLIC ${VPP_DEFAULT_MARCH_FLAGS})
130 if(NOT VPP_EXTERNAL_PROJECT)
131 add_dependencies(${test_lib} api_headers)
132 endif()
133 include_directories(${CMAKE_CURRENT_BINARY_DIR})
134 set_target_properties(${test_lib} PROPERTIES NO_SONAME 1)
135 set_target_properties(${test_lib} PROPERTIES
136 PREFIX ""
137 LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/vpp_api_test_plugins)
138
139 # install .so
140 install(
141 TARGETS ${test_lib}
142 DESTINATION ${VPP_LIBRARY_DIR}/vpp_api_test_plugins
143 COMPONENT ${ARG_COMPONENT}
144 )
145 endforeach()
146endmacro()
147
Ole Troandf87f802020-11-18 19:17:48 +0100148macro(add_vpp_test_library lib)
149 cmake_parse_arguments(TEST
150 ""
151 ""
152 ${ARGN}
153 )
154
155 foreach(file ${ARGN})
156 get_filename_component(name ${file} NAME_WE)
157 set(test_lib ${lib}_${name}_plugin)
158 add_library(${test_lib} SHARED ${file}_test2.c)
Damjan Marion0d39cba2021-05-10 14:51:44 +0200159 target_compile_options(${test_lib} PUBLIC ${VPP_DEFAULT_MARCH_FLAGS})
Ole Troandf87f802020-11-18 19:17:48 +0100160 if(NOT VPP_EXTERNAL_PROJECT)
161 add_dependencies(${test_lib} api_headers)
162 endif()
163 include_directories(${CMAKE_CURRENT_BINARY_DIR})
164 set_target_properties(${test_lib} PROPERTIES NO_SONAME 1)
165 set_target_properties(${test_lib} PROPERTIES
166 PREFIX ""
167 LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/vat2_plugins)
168
Ole Troandf87f802020-11-18 19:17:48 +0100169 # install .so
Ole Troan91144fb2021-08-17 12:57:00 +0200170 install(
171 TARGETS ${test_lib}
172 DESTINATION ${VPP_LIBRARY_DIR}/vat2_plugins
173 COMPONENT ${ARG_COMPONENT}
174 )
Ole Troandf87f802020-11-18 19:17:48 +0100175 endforeach()
176endmacro()
Damjan Marion3648d932021-04-30 20:27:53 +0200177
178macro(vpp_find_library var)
179 find_library(${var} ${ARGN})
180 mark_as_advanced(${var})
181endmacro()
182macro(vpp_find_path var)
183 find_path(${var} ${ARGN})
184 mark_as_advanced(${var})
185endmacro()