aca90ab7fb606c74d1acdd123a8616f73ff3c35d
[vpp.git] / src / cmake / plugin.cmake
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 macro(add_vpp_plugin name)
15   cmake_parse_arguments(PLUGIN
16     ""
17     "LINK_FLAGS;COMPONENT;DEV_COMPONENT"
18     "SOURCES;API_FILES;MULTIARCH_SOURCES;LINK_LIBRARIES;INSTALL_HEADERS;API_TEST_SOURCES;"
19     ${ARGN}
20   )
21   set(plugin_name ${name}_plugin)
22   set(api_includes)
23   if(NOT PLUGIN_COMPONENT)
24     set(PLUGIN_COMPONENT vpp-plugin-core)
25   endif()
26   if(NOT PLUGIN_DEV_COMPONENT)
27     if(NOT VPP_EXTERNAL_PROJECT)
28       set(PLUGIN_DEV_COMPONENT vpp-dev)
29     else()
30       set(PLUGIN_DEV_COMPONENT ${PLUGIN_COMPONENT}-dev)
31     endif()
32   endif()
33
34   vpp_add_api_files(${plugin_name} plugins ${PLUGIN_COMPONENT} ${PLUGIN_API_FILES})
35   foreach(f ${PLUGIN_API_FILES})
36     get_filename_component(dir ${f} DIRECTORY)
37     list(APPEND api_includes ${f}.h ${f}.json)
38     install(
39       FILES ${CMAKE_CURRENT_BINARY_DIR}/${f}.h
40       ${CMAKE_CURRENT_BINARY_DIR}/${f}_enum.h
41       ${CMAKE_CURRENT_BINARY_DIR}/${f}_types.h
42       DESTINATION include/vpp_plugins/${name}/${dir}
43       COMPONENT ${PLUGIN_DEV_COMPONENT}
44     )
45   endforeach()
46   add_library(${plugin_name} SHARED ${PLUGIN_SOURCES} ${api_includes})
47   set_target_properties(${plugin_name} PROPERTIES NO_SONAME 1)
48   target_compile_options(${plugin_name} PRIVATE "-fvisibility=hidden")
49   target_compile_options (${plugin_name} PRIVATE "-ffunction-sections")
50   target_compile_options (${plugin_name} PRIVATE "-fdata-sections")
51   target_link_libraries (${plugin_name} "-Wl,--gc-sections")
52   set(deps "")
53   if(PLUGIN_API_FILES)
54     list(APPEND deps ${plugin_name}_api_headers)
55   endif()
56   if(NOT VPP_EXTERNAL_PROJECT)
57     list(APPEND deps vpp_version_h api_headers)
58   endif()
59   if(deps)
60     add_dependencies(${plugin_name} ${deps})
61   endif()
62   set_target_properties(${plugin_name} PROPERTIES
63     PREFIX ""
64     LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/vpp_plugins)
65   if(PLUGIN_MULTIARCH_SOURCES)
66     vpp_library_set_multiarch_sources(${plugin_name} "${deps}" ${PLUGIN_MULTIARCH_SOURCES})
67   endif()
68   if(PLUGIN_LINK_LIBRARIES)
69     target_link_libraries(${plugin_name} ${PLUGIN_LINK_LIBRARIES})
70   endif()
71   if(PLUGIN_LINK_FLAGS)
72     set_target_properties(${plugin_name} PROPERTIES LINK_FLAGS "${PLUGIN_LINK_FLAGS}")
73   endif()
74   if(PLUGIN_INSTALL_HEADERS)
75     foreach(file ${PLUGIN_INSTALL_HEADERS})
76       get_filename_component(dir ${file} DIRECTORY)
77       install(
78         FILES ${file}
79         DESTINATION include/vpp_plugins/${name}/${dir}
80         COMPONENT vpp-dev
81       )
82     endforeach()
83   endif()
84   if(PLUGIN_API_TEST_SOURCES)
85     set(test_plugin_name ${name}_test_plugin)
86     add_library(${test_plugin_name} SHARED ${PLUGIN_API_TEST_SOURCES}
87                 ${api_includes})
88     set_target_properties(${test_plugin_name} PROPERTIES NO_SONAME 1)
89     if(PLUGIN_API_FILES)
90       add_dependencies(${test_plugin_name} ${plugin_name}_api_headers)
91     endif()
92     if(NOT VPP_EXTERNAL_PROJECT)
93       add_dependencies(${test_plugin_name} api_headers)
94     endif()
95     set_target_properties(${test_plugin_name} PROPERTIES
96       PREFIX ""
97       LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/vpp_api_test_plugins)
98     install(
99       TARGETS ${test_plugin_name}
100       DESTINATION ${VPP_LIBRARY_DIR}/vpp_api_test_plugins
101       COMPONENT ${PLUGIN_COMPONENT}
102     )
103   endif()
104   if (PLUGIN_API_FILES)
105     add_vpp_test_library(${name}_test_plugin ${PLUGIN_API_FILES})
106   endif()
107
108   install(
109     TARGETS ${plugin_name}
110     DESTINATION ${VPP_LIBRARY_DIR}/vpp_plugins
111     COMPONENT ${PLUGIN_COMPONENT}
112   )
113 endmacro()
114
115 macro(vpp_plugin_find_library plugin var name)
116   find_library(${var} NAMES ${name} ${ARGN})
117 if (NOT ${var})
118   message(WARNING "-- ${name} library not found - ${plugin} plugin disabled")
119   return()
120 endif()
121     message(STATUS "${plugin} plugin needs ${name} library - found at ${${var}}")
122 endmacro()