blob: cbda4008a28696f1d10da4dc9bc82159946ce67f [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
Ole Troaned61b202024-06-19 11:31:30 +020014# Set the CMP0116 policy
15if(POLICY CMP0116)
16 cmake_policy(SET CMP0116 NEW)
17endif()
18
Damjan Mariond16004d2018-08-26 10:14:52 +020019##############################################################################
20# API
21##############################################################################
22function(vpp_generate_api_c_header file)
23 set (output_name ${CMAKE_CURRENT_BINARY_DIR}/${file}.h)
Ole Troaned61b202024-06-19 11:31:30 +020024 set (dependency_file ${CMAKE_CURRENT_BINARY_DIR}/${file}.d)
Damjan Mariond16004d2018-08-26 10:14:52 +020025 get_filename_component(output_dir ${output_name} DIRECTORY)
Damjan Marion0fa900e2018-09-12 12:12:36 +020026 if(NOT VPP_APIGEN)
Ole Troaned61b202024-06-19 11:31:30 +020027 set(VPP_APIGEN ${CMAKE_SOURCE_DIR}/tools/vppapigen/vppapigen)
28 set(VPPAPIGEN_SUBMODULES
29 ${CMAKE_SOURCE_DIR}/tools/vppapigen/vppapigen_c.py
30 ${CMAKE_SOURCE_DIR}/tools/vppapigen/vppapigen_json.py
31 )
Damjan Marion0fa900e2018-09-12 12:12:36 +020032 endif()
Benoît Ganne6ceee452019-07-25 17:15:59 +020033 if (VPP_INCLUDE_DIR)
34 set(includedir "--includedir" ${VPP_INCLUDE_DIR})
35 endif()
Ole Troandf87f802020-11-18 19:17:48 +010036
37 set(OUTPUT_HEADERS
38 "${CMAKE_CURRENT_BINARY_DIR}/${file}.h"
39 "${CMAKE_CURRENT_BINARY_DIR}/${file}_fromjson.h"
40 "${CMAKE_CURRENT_BINARY_DIR}/${file}_tojson.h"
41 "${CMAKE_CURRENT_BINARY_DIR}/${file}_enum.h"
42 "${CMAKE_CURRENT_BINARY_DIR}/${file}_types.h"
43 "${CMAKE_CURRENT_BINARY_DIR}/${file}.c"
44 "${CMAKE_CURRENT_BINARY_DIR}/${file}_test.c"
45 "${CMAKE_CURRENT_BINARY_DIR}/${file}_test2.c"
46 )
47
Ole Troaned61b202024-06-19 11:31:30 +020048 get_filename_component(barename ${file} NAME)
49
Ole Troandf87f802020-11-18 19:17:48 +010050 add_custom_command (
51 OUTPUT ${OUTPUT_HEADERS}
Damjan Mariond16004d2018-08-26 10:14:52 +020052 COMMAND mkdir -p ${output_dir}
Damjan Marion7cf80af2021-05-25 19:28:21 +020053 COMMAND ${PYENV} ${VPP_APIGEN}
Ole Troaned61b202024-06-19 11:31:30 +020054 ARGS ${includedir} --includedir ${CMAKE_SOURCE_DIR} --input ${CMAKE_CURRENT_SOURCE_DIR}/${file} --outputdir ${output_dir} --output ${output_name} -MF ${dependency_file}
55 DEPENDS ${VPP_APIGEN} ${CMAKE_CURRENT_SOURCE_DIR}/${file} ${VPPAPIGEN_SUBMODULES}
Damjan Mariond16004d2018-08-26 10:14:52 +020056 COMMENT "Generating API header ${output_name}"
Ole Troaned61b202024-06-19 11:31:30 +020057 DEPFILE ${dependency_file}
Damjan Mariond16004d2018-08-26 10:14:52 +020058 )
Ole Troandf87f802020-11-18 19:17:48 +010059 set(t ${barename}_deps)
Ole Troaned61b202024-06-19 11:31:30 +020060
Ole Troandf87f802020-11-18 19:17:48 +010061 if (NOT TARGET ${t})
62 add_custom_target(${t} ALL DEPENDS ${OUTPUT_HEADERS})
63 add_dependencies(api_headers ${t})
64 endif()
Ole Troan7d527a22020-12-02 14:19:49 +010065
Damjan Mariond16004d2018-08-26 10:14:52 +020066endfunction()
67
Damjan Marion833de8c2018-09-07 12:39:02 +020068function(vpp_generate_api_json_header file dir component)
Damjan Mariond16004d2018-08-26 10:14:52 +020069 set (output_name ${CMAKE_CURRENT_BINARY_DIR}/${file}.json)
70 get_filename_component(output_dir ${output_name} DIRECTORY)
Damjan Marion0fa900e2018-09-12 12:12:36 +020071 if(NOT VPP_APIGEN)
72 set(VPP_APIGEN ${CMAKE_SOURCE_DIR}/tools/vppapigen/vppapigen)
73 endif()
Benoît Ganne6ceee452019-07-25 17:15:59 +020074 if (VPP_INCLUDE_DIR)
75 set(includedir "--includedir" ${VPP_INCLUDE_DIR})
76 endif()
Damjan Mariond16004d2018-08-26 10:14:52 +020077 add_custom_command (OUTPUT ${output_name}
78 COMMAND mkdir -p ${output_dir}
Damjan Marion7cf80af2021-05-25 19:28:21 +020079 COMMAND ${PYENV} ${VPP_APIGEN}
Ole Troanac0babd2024-01-23 18:56:23 +010080 ARGS ${includedir} --includedir ${CMAKE_SOURCE_DIR} --input ${CMAKE_CURRENT_SOURCE_DIR}/${file} JSON --outputdir ${output_dir} --output ${output_name}
Ole Troane4f849c2018-11-29 20:14:33 +010081 DEPENDS ${VPP_APIGEN} ${CMAKE_CURRENT_SOURCE_DIR}/${file}
Damjan Mariond16004d2018-08-26 10:14:52 +020082 COMMENT "Generating API header ${output_name}"
83 )
Damjan Marion833de8c2018-09-07 12:39:02 +020084 install(
85 FILES ${output_name}
Wim de Withf521df72024-01-28 11:05:15 +010086 DESTINATION ${CMAKE_INSTALL_DATADIR}/vpp/api/${dir}/
Damjan Marion833de8c2018-09-07 12:39:02 +020087 COMPONENT ${component}
88 )
Damjan Mariond16004d2018-08-26 10:14:52 +020089endfunction()
90
91##############################################################################
Oliver Gilesdc203712019-12-05 23:37:36 +020092# VPP-API
93##############################################################################
94function(vpp_generate_vapi_c_header f)
95 get_filename_component(output ${f}.vapi.h NAME)
Damjan Marion88b2e362021-04-29 18:47:25 +020096 set (output_name ${VPP_BINARY_DIR}/vpp-api/vapi/${output})
Oliver Gilesdc203712019-12-05 23:37:36 +020097 if(NOT VPP_VAPI_C_GEN)
98 set(VPP_VAPI_C_GEN ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_c_gen.py)
99 set(VPP_VAPI_C_GEN_DEPENDS
100 ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_c_gen.py
101 ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_json_parser.py
102 )
103 endif()
104
105 # C VAPI Headers
106 set(input ${CMAKE_CURRENT_BINARY_DIR}/${f}.json)
107 add_custom_command(
108 OUTPUT ${output_name}
Damjan Marion88b2e362021-04-29 18:47:25 +0200109 WORKING_DIRECTORY ${VPP_BINARY_DIR}/vpp-api/vapi
Damjan Marion7cf80af2021-05-25 19:28:21 +0200110 COMMAND ${PYENV} ${VPP_VAPI_C_GEN}
Oliver Gilesdc203712019-12-05 23:37:36 +0200111 ARGS --remove-path ${input}
112 DEPENDS ${input} ${VPP_VAPI_C_GEN_DEPENDS}
113 COMMENT "Generating VAPI C header ${output_name}"
114 )
115 install(
116 FILES ${output_name}
Wim de Withf521df72024-01-28 11:05:15 +0100117 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/vapi
Oliver Gilesdc203712019-12-05 23:37:36 +0200118 COMPONENT vpp-dev
119 )
120endfunction ()
121
122function (vpp_generate_vapi_cpp_header f)
123 get_filename_component(output ${f}.vapi.hpp NAME)
Damjan Marion88b2e362021-04-29 18:47:25 +0200124 set (output_name ${VPP_BINARY_DIR}/vpp-api/vapi/${output})
Oliver Gilesdc203712019-12-05 23:37:36 +0200125 if(NOT VPP_VAPI_CPP_GEN)
126 set(VPP_VAPI_CPP_GEN ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_cpp_gen.py)
127 set(VPP_VAPI_CPP_GEN_DEPENDS
128 ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_cpp_gen.py
129 ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_json_parser.py
130 )
131 endif()
132 # C++ VAPI Headers
133 set(input ${CMAKE_CURRENT_BINARY_DIR}/${f}.json)
134 add_custom_command(
135 OUTPUT ${output_name}
Damjan Marion88b2e362021-04-29 18:47:25 +0200136 WORKING_DIRECTORY ${VPP_BINARY_DIR}/vpp-api/vapi
Damjan Marion7cf80af2021-05-25 19:28:21 +0200137 COMMAND ${PYENV} ${VPP_VAPI_CPP_GEN}
Oliver Gilesdc203712019-12-05 23:37:36 +0200138 ARGS --gen-h-prefix=vapi --remove-path ${input}
139 DEPENDS ${input} ${VPP_VAPI_CPP_GEN_DEPENDS}
140 COMMENT "Generating VAPI C++ header ${output_name}"
141 )
142 install(
143 FILES ${output_name}
Wim de Withf521df72024-01-28 11:05:15 +0100144 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/vapi
Oliver Gilesdc203712019-12-05 23:37:36 +0200145 COMPONENT vpp-dev
146 )
147endfunction ()
148
149
150##############################################################################
Damjan Mariond16004d2018-08-26 10:14:52 +0200151# generate the .h and .json files for a .api file
152# @param file - the name of the .api
153# @param dir - the install directory under ROOT/share/vpp/api to place the
154# generated .json file
155##############################################################################
Damjan Marion833de8c2018-09-07 12:39:02 +0200156function(vpp_generate_api_header file dir component)
Oliver Gilesdc203712019-12-05 23:37:36 +0200157 vpp_generate_api_c_header (${file})
158 vpp_generate_api_json_header (${file} ${dir} ${component})
159 vpp_generate_vapi_c_header (${file})
160 vpp_generate_vapi_cpp_header (${file})
Damjan Mariond16004d2018-08-26 10:14:52 +0200161endfunction()
162
Oliver Gilesdc203712019-12-05 23:37:36 +0200163function(vpp_add_api_files name dir component)
Damjan Mariond16004d2018-08-26 10:14:52 +0200164 unset(header_files)
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200165 set(target ${name}_api_headers)
166 file(RELATIVE_PATH rpath ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
Damjan Mariond16004d2018-08-26 10:14:52 +0200167 foreach(file ${ARGN})
Oliver Gilesdc203712019-12-05 23:37:36 +0200168 vpp_generate_api_header (${file} ${dir} ${component})
169 # Basic api headers get installed in a subdirectory according to
170 # their component name, but vapi is expected to be found directly under
171 # "vapi". Both by in-source components (e.g. vpp-api/vapi/vapi.c), and
172 # out-of-tree plugins use #include <vapi/component.api.vapi.h>.
173 # ${file} contains the subdirectory, so strip it here.
Ole Troaned61b202024-06-19 11:31:30 +0200174 file(MAKE_DIRECTORY ${VPP_BINARY_DIR}/vpp-api/vapi)
Oliver Gilesdc203712019-12-05 23:37:36 +0200175 get_filename_component(name ${file} NAME)
176 list(APPEND header_files
177 ${file}.h
Ole Troan7d527a22020-12-02 14:19:49 +0100178 ${file}_enum.h
179 ${file}_types.h
Oliver Gilesdc203712019-12-05 23:37:36 +0200180 ${file}.json
Damjan Marion88b2e362021-04-29 18:47:25 +0200181 ${VPP_BINARY_DIR}/vpp-api/vapi/${name}.vapi.h
182 ${VPP_BINARY_DIR}/vpp-api/vapi/${name}.vapi.hpp
Oliver Gilesdc203712019-12-05 23:37:36 +0200183 )
Damjan Mariond16004d2018-08-26 10:14:52 +0200184 endforeach()
185 add_custom_target(${target} DEPENDS ${header_files})
Ole Troan7d527a22020-12-02 14:19:49 +0100186 add_dependencies(api_headers ${target})
Damjan Mariond16004d2018-08-26 10:14:52 +0200187endfunction()
188
189add_custom_target(api_headers
Jakub Grajciar53f06a02020-03-30 08:12:57 +0200190 DEPENDS vlibmemory_api_headers vnet_api_headers vpp_api_headers vlib_api_headers
Damjan Mariond16004d2018-08-26 10:14:52 +0200191)
Ole Troaned61b202024-06-19 11:31:30 +0200192add_custom_target(vapi_headers
193)