blob: f3af68748d62092f3dc2d121420cbd550d89ea63 [file] [log] [blame]
Damjan Mariond16004d2018-08-26 10:14:52 +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
14##############################################################################
15# API
16##############################################################################
17function(vpp_generate_api_c_header file)
18 set (output_name ${CMAKE_CURRENT_BINARY_DIR}/${file}.h)
19 get_filename_component(output_dir ${output_name} DIRECTORY)
Damjan Marion0fa900e2018-09-12 12:12:36 +020020 if(NOT VPP_APIGEN)
21 set(VPP_APIGEN ${CMAKE_SOURCE_DIR}/tools/vppapigen/vppapigen)
22 endif()
Benoît Ganne6ceee452019-07-25 17:15:59 +020023 if (VPP_INCLUDE_DIR)
24 set(includedir "--includedir" ${VPP_INCLUDE_DIR})
25 endif()
Ole Troandf87f802020-11-18 19:17:48 +010026
27 set(OUTPUT_HEADERS
28 "${CMAKE_CURRENT_BINARY_DIR}/${file}.h"
29 "${CMAKE_CURRENT_BINARY_DIR}/${file}_fromjson.h"
30 "${CMAKE_CURRENT_BINARY_DIR}/${file}_tojson.h"
31 "${CMAKE_CURRENT_BINARY_DIR}/${file}_enum.h"
32 "${CMAKE_CURRENT_BINARY_DIR}/${file}_types.h"
33 "${CMAKE_CURRENT_BINARY_DIR}/${file}.c"
34 "${CMAKE_CURRENT_BINARY_DIR}/${file}_test.c"
35 "${CMAKE_CURRENT_BINARY_DIR}/${file}_test2.c"
36 )
37
38 add_custom_command (
39 OUTPUT ${OUTPUT_HEADERS}
Damjan Mariond16004d2018-08-26 10:14:52 +020040 COMMAND mkdir -p ${output_dir}
Damjan Marion0fa900e2018-09-12 12:12:36 +020041 COMMAND ${VPP_APIGEN}
Ole Troan2a1ca782019-09-19 01:08:30 +020042 ARGS ${includedir} --includedir ${CMAKE_SOURCE_DIR} --input ${CMAKE_CURRENT_SOURCE_DIR}/${file} --outputdir ${output_dir} --output ${output_name}
Ole Troane4f849c2018-11-29 20:14:33 +010043 DEPENDS ${VPP_APIGEN} ${CMAKE_CURRENT_SOURCE_DIR}/${file}
Damjan Mariond16004d2018-08-26 10:14:52 +020044 COMMENT "Generating API header ${output_name}"
45 )
Ole Troandf87f802020-11-18 19:17:48 +010046 get_filename_component(barename ${file} NAME)
47 set(t ${barename}_deps)
48 if (NOT TARGET ${t})
49 add_custom_target(${t} ALL DEPENDS ${OUTPUT_HEADERS})
50 add_dependencies(api_headers ${t})
51 endif()
Damjan Mariond16004d2018-08-26 10:14:52 +020052endfunction()
53
Damjan Marion833de8c2018-09-07 12:39:02 +020054function(vpp_generate_api_json_header file dir component)
Damjan Mariond16004d2018-08-26 10:14:52 +020055 set (output_name ${CMAKE_CURRENT_BINARY_DIR}/${file}.json)
56 get_filename_component(output_dir ${output_name} DIRECTORY)
Damjan Marion0fa900e2018-09-12 12:12:36 +020057 if(NOT VPP_APIGEN)
58 set(VPP_APIGEN ${CMAKE_SOURCE_DIR}/tools/vppapigen/vppapigen)
59 endif()
Benoît Ganne6ceee452019-07-25 17:15:59 +020060 if (VPP_INCLUDE_DIR)
61 set(includedir "--includedir" ${VPP_INCLUDE_DIR})
62 endif()
Damjan Mariond16004d2018-08-26 10:14:52 +020063 add_custom_command (OUTPUT ${output_name}
64 COMMAND mkdir -p ${output_dir}
Damjan Marion0fa900e2018-09-12 12:12:36 +020065 COMMAND ${VPP_APIGEN}
Benoît Ganne6ceee452019-07-25 17:15:59 +020066 ARGS ${includedir} --includedir ${CMAKE_SOURCE_DIR} --input ${CMAKE_CURRENT_SOURCE_DIR}/${file} JSON --output ${output_name}
Ole Troane4f849c2018-11-29 20:14:33 +010067 DEPENDS ${VPP_APIGEN} ${CMAKE_CURRENT_SOURCE_DIR}/${file}
Damjan Mariond16004d2018-08-26 10:14:52 +020068 COMMENT "Generating API header ${output_name}"
69 )
Damjan Marion833de8c2018-09-07 12:39:02 +020070 install(
71 FILES ${output_name}
72 DESTINATION share/vpp/api/${dir}/
73 COMPONENT ${component}
74 )
Damjan Mariond16004d2018-08-26 10:14:52 +020075endfunction()
76
77##############################################################################
Oliver Gilesdc203712019-12-05 23:37:36 +020078# VPP-API
79##############################################################################
80function(vpp_generate_vapi_c_header f)
81 get_filename_component(output ${f}.vapi.h NAME)
82 set (output_name ${CMAKE_BINARY_DIR}/vpp-api/vapi/${output})
83 if(NOT VPP_VAPI_C_GEN)
84 set(VPP_VAPI_C_GEN ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_c_gen.py)
85 set(VPP_VAPI_C_GEN_DEPENDS
86 ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_c_gen.py
87 ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_json_parser.py
88 )
89 endif()
90
91 # C VAPI Headers
92 set(input ${CMAKE_CURRENT_BINARY_DIR}/${f}.json)
93 add_custom_command(
94 OUTPUT ${output_name}
95 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/vpp-api/vapi
96 COMMAND ${VPP_VAPI_C_GEN}
97 ARGS --remove-path ${input}
98 DEPENDS ${input} ${VPP_VAPI_C_GEN_DEPENDS}
99 COMMENT "Generating VAPI C header ${output_name}"
100 )
101 install(
102 FILES ${output_name}
103 DESTINATION include/vapi
104 COMPONENT vpp-dev
105 )
106endfunction ()
107
108function (vpp_generate_vapi_cpp_header f)
109 get_filename_component(output ${f}.vapi.hpp NAME)
110 set (output_name ${CMAKE_BINARY_DIR}/vpp-api/vapi/${output})
111 if(NOT VPP_VAPI_CPP_GEN)
112 set(VPP_VAPI_CPP_GEN ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_cpp_gen.py)
113 set(VPP_VAPI_CPP_GEN_DEPENDS
114 ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_cpp_gen.py
115 ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_json_parser.py
116 )
117 endif()
118 # C++ VAPI Headers
119 set(input ${CMAKE_CURRENT_BINARY_DIR}/${f}.json)
120 add_custom_command(
121 OUTPUT ${output_name}
122 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/vpp-api/vapi
123 COMMAND ${VPP_VAPI_CPP_GEN}
124 ARGS --gen-h-prefix=vapi --remove-path ${input}
125 DEPENDS ${input} ${VPP_VAPI_CPP_GEN_DEPENDS}
126 COMMENT "Generating VAPI C++ header ${output_name}"
127 )
128 install(
129 FILES ${output_name}
130 DESTINATION include/vapi
131 COMPONENT vpp-dev
132 )
133endfunction ()
134
135
136##############################################################################
Damjan Mariond16004d2018-08-26 10:14:52 +0200137# generate the .h and .json files for a .api file
138# @param file - the name of the .api
139# @param dir - the install directory under ROOT/share/vpp/api to place the
140# generated .json file
141##############################################################################
Damjan Marion833de8c2018-09-07 12:39:02 +0200142function(vpp_generate_api_header file dir component)
Oliver Gilesdc203712019-12-05 23:37:36 +0200143 vpp_generate_api_c_header (${file})
144 vpp_generate_api_json_header (${file} ${dir} ${component})
145 vpp_generate_vapi_c_header (${file})
146 vpp_generate_vapi_cpp_header (${file})
Damjan Mariond16004d2018-08-26 10:14:52 +0200147endfunction()
148
Oliver Gilesdc203712019-12-05 23:37:36 +0200149function(vpp_add_api_files name dir component)
Damjan Mariond16004d2018-08-26 10:14:52 +0200150 unset(header_files)
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200151 set(target ${name}_api_headers)
152 file(RELATIVE_PATH rpath ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
Damjan Mariond16004d2018-08-26 10:14:52 +0200153 foreach(file ${ARGN})
Oliver Gilesdc203712019-12-05 23:37:36 +0200154 vpp_generate_api_header (${file} ${dir} ${component})
155 # Basic api headers get installed in a subdirectory according to
156 # their component name, but vapi is expected to be found directly under
157 # "vapi". Both by in-source components (e.g. vpp-api/vapi/vapi.c), and
158 # out-of-tree plugins use #include <vapi/component.api.vapi.h>.
159 # ${file} contains the subdirectory, so strip it here.
160 get_filename_component(name ${file} NAME)
161 list(APPEND header_files
162 ${file}.h
163 ${file}.json
164 ${CMAKE_BINARY_DIR}/vpp-api/vapi/${name}.vapi.h
165 ${CMAKE_BINARY_DIR}/vpp-api/vapi/${name}.vapi.hpp
166 )
Damjan Mariond16004d2018-08-26 10:14:52 +0200167 endforeach()
168 add_custom_target(${target} DEPENDS ${header_files})
169endfunction()
170
171add_custom_target(api_headers
Jakub Grajciar53f06a02020-03-30 08:12:57 +0200172 DEPENDS vlibmemory_api_headers vnet_api_headers vpp_api_headers vlib_api_headers
Damjan Mariond16004d2018-08-26 10:14:52 +0200173)