blob: 5a7d2cdfef77403e1f1cb07f8d5dbe8345b76f12 [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)
25
26 add_library(${lib} SHARED)
27 target_sources(${lib} PRIVATE $<TARGET_OBJECTS:${lo}>)
28
Damjan Marion958192d2018-09-13 18:43:19 +020029 if(VPP_LIB_VERSION)
30 set_target_properties(${lib} PROPERTIES SOVERSION ${VPP_LIB_VERSION})
31 endif()
Damjan Marion4553c952018-08-26 11:04:40 +020032
33 # library deps
34 if(ARG_LINK_LIBRARIES)
35 target_link_libraries(${lib} ${ARG_LINK_LIBRARIES})
36 endif()
37 # install .so
Damjan Marion43b06062018-08-29 22:20:45 +020038 if(NOT ARG_COMPONENT)
39 set(ARG_COMPONENT vpp)
40 endif()
41 install(
42 TARGETS ${lib}
Damjan Marion599efc62020-05-07 16:49:45 +020043 DESTINATION ${VPP_LIBRARY_DIR}
Damjan Marion43b06062018-08-29 22:20:45 +020044 COMPONENT ${ARG_COMPONENT}
45 )
Damjan Marion4553c952018-08-26 11:04:40 +020046
Damjan Marionaf7892c2020-10-22 14:23:47 +020047 if (ARG_LTO AND VPP_USE_LTO)
Damjan Marion115012a2021-04-02 17:35:13 +020048 set_property(TARGET ${lo} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
Damjan Marionaf7892c2020-10-22 14:23:47 +020049 set_property(TARGET ${lib} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
50 target_compile_options (${lib} PRIVATE "-ffunction-sections")
51 target_compile_options (${lib} PRIVATE "-fdata-sections")
52 target_link_libraries (${lib} "-Wl,--gc-sections")
53 endif()
54
Damjan Marion4553c952018-08-26 11:04:40 +020055 if(ARG_MULTIARCH_SOURCES)
Ole Troan7d527a22020-12-02 14:19:49 +010056 vpp_library_set_multiarch_sources(${lib} DEPENDS ${ARG_DEPENDS} SOURCES ${ARG_MULTIARCH_SOURCES})
Damjan Marion4553c952018-08-26 11:04:40 +020057 endif()
58
59 if(ARG_API_FILES)
Oliver Gilesdc203712019-12-05 23:37:36 +020060 vpp_add_api_files(${lib} core vpp ${ARG_API_FILES})
Damjan Marion4553c952018-08-26 11:04:40 +020061 foreach(file ${ARG_API_FILES})
62 get_filename_component(dir ${file} DIRECTORY)
Damjan Marion43b06062018-08-29 22:20:45 +020063 install(
BenoƮt Ganne98438e42019-07-25 16:26:20 +020064 FILES ${file} ${CMAKE_CURRENT_BINARY_DIR}/${file}.h
Ole Troan2a1ca782019-09-19 01:08:30 +020065 ${CMAKE_CURRENT_BINARY_DIR}/${file}_enum.h
66 ${CMAKE_CURRENT_BINARY_DIR}/${file}_types.h
Ole Troandf87f802020-11-18 19:17:48 +010067 ${CMAKE_CURRENT_BINARY_DIR}/${file}_tojson.h
68 ${CMAKE_CURRENT_BINARY_DIR}/${file}_fromjson.h
Damjan Marion43b06062018-08-29 22:20:45 +020069 DESTINATION include/${lib}/${dir}
70 COMPONENT vpp-dev
71 )
Damjan Marion4553c952018-08-26 11:04:40 +020072 endforeach()
73 endif()
74
Ole Troan7d527a22020-12-02 14:19:49 +010075 if(NOT VPP_EXTERNAL_PROJECT)
Damjan Marion115012a2021-04-02 17:35:13 +020076 add_dependencies(${lo} api_headers)
Ole Troan7d527a22020-12-02 14:19:49 +010077 endif()
78
Damjan Marion4553c952018-08-26 11:04:40 +020079 if(ARG_DEPENDS)
Damjan Marion115012a2021-04-02 17:35:13 +020080 add_dependencies(${lo} ${ARG_DEPENDS})
Damjan Marion4553c952018-08-26 11:04:40 +020081 endif()
82
83 # install headers
84 if(ARG_INSTALL_HEADERS)
85 foreach(file ${ARG_INSTALL_HEADERS})
86 get_filename_component(dir ${file} DIRECTORY)
Damjan Marion43b06062018-08-29 22:20:45 +020087 install(
88 FILES ${file}
89 DESTINATION include/${lib}/${dir}
Damjan Marioneeadc142018-09-13 20:02:12 +020090 COMPONENT ${ARG_COMPONENT}-dev
Damjan Marion43b06062018-08-29 22:20:45 +020091 )
Damjan Marion4553c952018-08-26 11:04:40 +020092 endforeach()
93 endif()
94endmacro()
95
96##############################################################################
97# header files
98##############################################################################
99function (add_vpp_headers path)
100 foreach(file ${ARGN})
101 get_filename_component(dir ${file} DIRECTORY)
Damjan Marion833de8c2018-09-07 12:39:02 +0200102 install(
103 FILES ${file}
104 DESTINATION include/${path}/${dir}
105 COMPONENT vpp-dev
106 )
Damjan Marion4553c952018-08-26 11:04:40 +0200107 endforeach()
108endfunction()
Ole Troandf87f802020-11-18 19:17:48 +0100109
110macro(add_vpp_test_library lib)
111 cmake_parse_arguments(TEST
112 ""
113 ""
114 ${ARGN}
115 )
116
117 foreach(file ${ARGN})
118 get_filename_component(name ${file} NAME_WE)
119 set(test_lib ${lib}_${name}_plugin)
120 add_library(${test_lib} SHARED ${file}_test2.c)
121 if(NOT VPP_EXTERNAL_PROJECT)
122 add_dependencies(${test_lib} api_headers)
123 endif()
124 include_directories(${CMAKE_CURRENT_BINARY_DIR})
125 set_target_properties(${test_lib} PROPERTIES NO_SONAME 1)
126 set_target_properties(${test_lib} PROPERTIES
127 PREFIX ""
128 LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/vat2_plugins)
129
130 # Later: Install and package
131 # install .so
132 #install(
133 # TARGETS ${test_lib}
134 # DESTINATION ${VPP_LIBRARY_DIR}/vat2_plugins
135 # #COMPONENT ${ARG_COMPONENT}
136 # )
137 endforeach()
138endmacro()
Damjan Marion3648d932021-04-30 20:27:53 +0200139
140macro(vpp_find_library var)
141 find_library(${var} ${ARGN})
142 mark_as_advanced(${var})
143endmacro()
144macro(vpp_find_path var)
145 find_path(${var} ${ARGN})
146 mark_as_advanced(${var})
147endmacro()