blob: 999bbf91be1f3f812697a635002e82093a410552 [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 Troan228e1952024-06-21 17:17:09 +020050# Define a variable for common apigen arguments
51set(COMMON_ARGS
52 OUTPUT ${OUTPUT_HEADERS}
53 COMMAND mkdir -p ${output_dir}
54 COMMAND ${PYENV} ${VPP_APIGEN}
55 ARGS ${includedir} --includedir ${CMAKE_SOURCE_DIR} --input ${CMAKE_CURRENT_SOURCE_DIR}/${file} --outputdir ${output_dir} --output ${output_name} -MF ${dependency_file}
56 DEPENDS ${VPP_APIGEN} ${CMAKE_CURRENT_SOURCE_DIR}/${file} ${VPPAPIGEN_SUBMODULES}
57 COMMENT "Generating API header ${output_name}"
58)
59
60if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.20")
Ole Troandf87f802020-11-18 19:17:48 +010061 add_custom_command (
Ole Troan228e1952024-06-21 17:17:09 +020062 ${COMMON_ARGS}
Ole Troaned61b202024-06-19 11:31:30 +020063 DEPFILE ${dependency_file}
Damjan Mariond16004d2018-08-26 10:14:52 +020064 )
Ole Troan228e1952024-06-21 17:17:09 +020065else()
66 message(WARNING "Your CMake version does not support DEPFILE. Consider upgrading to CMake 3.20 or later for improved dependency handling.")
67 add_custom_command (
68 ${COMMON_ARGS}
69 )
70endif()
Ole Troandf87f802020-11-18 19:17:48 +010071 set(t ${barename}_deps)
Ole Troaned61b202024-06-19 11:31:30 +020072
Ole Troandf87f802020-11-18 19:17:48 +010073 if (NOT TARGET ${t})
74 add_custom_target(${t} ALL DEPENDS ${OUTPUT_HEADERS})
75 add_dependencies(api_headers ${t})
76 endif()
Ole Troan7d527a22020-12-02 14:19:49 +010077
Damjan Mariond16004d2018-08-26 10:14:52 +020078endfunction()
79
Damjan Marion833de8c2018-09-07 12:39:02 +020080function(vpp_generate_api_json_header file dir component)
Damjan Mariond16004d2018-08-26 10:14:52 +020081 set (output_name ${CMAKE_CURRENT_BINARY_DIR}/${file}.json)
82 get_filename_component(output_dir ${output_name} DIRECTORY)
Damjan Marion0fa900e2018-09-12 12:12:36 +020083 if(NOT VPP_APIGEN)
84 set(VPP_APIGEN ${CMAKE_SOURCE_DIR}/tools/vppapigen/vppapigen)
85 endif()
Benoît Ganne6ceee452019-07-25 17:15:59 +020086 if (VPP_INCLUDE_DIR)
87 set(includedir "--includedir" ${VPP_INCLUDE_DIR})
88 endif()
Damjan Mariond16004d2018-08-26 10:14:52 +020089 add_custom_command (OUTPUT ${output_name}
90 COMMAND mkdir -p ${output_dir}
Damjan Marion7cf80af2021-05-25 19:28:21 +020091 COMMAND ${PYENV} ${VPP_APIGEN}
Ole Troanac0babd2024-01-23 18:56:23 +010092 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 +010093 DEPENDS ${VPP_APIGEN} ${CMAKE_CURRENT_SOURCE_DIR}/${file}
Damjan Mariond16004d2018-08-26 10:14:52 +020094 COMMENT "Generating API header ${output_name}"
95 )
Damjan Marion833de8c2018-09-07 12:39:02 +020096 install(
97 FILES ${output_name}
Wim de Withf521df72024-01-28 11:05:15 +010098 DESTINATION ${CMAKE_INSTALL_DATADIR}/vpp/api/${dir}/
Damjan Marion833de8c2018-09-07 12:39:02 +020099 COMPONENT ${component}
100 )
Damjan Mariond16004d2018-08-26 10:14:52 +0200101endfunction()
102
103##############################################################################
Oliver Gilesdc203712019-12-05 23:37:36 +0200104# VPP-API
105##############################################################################
106function(vpp_generate_vapi_c_header f)
107 get_filename_component(output ${f}.vapi.h NAME)
Damjan Marion88b2e362021-04-29 18:47:25 +0200108 set (output_name ${VPP_BINARY_DIR}/vpp-api/vapi/${output})
Oliver Gilesdc203712019-12-05 23:37:36 +0200109 if(NOT VPP_VAPI_C_GEN)
110 set(VPP_VAPI_C_GEN ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_c_gen.py)
111 set(VPP_VAPI_C_GEN_DEPENDS
112 ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_c_gen.py
113 ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_json_parser.py
114 )
115 endif()
116
117 # C VAPI Headers
118 set(input ${CMAKE_CURRENT_BINARY_DIR}/${f}.json)
119 add_custom_command(
120 OUTPUT ${output_name}
Damjan Marion88b2e362021-04-29 18:47:25 +0200121 WORKING_DIRECTORY ${VPP_BINARY_DIR}/vpp-api/vapi
Damjan Marion7cf80af2021-05-25 19:28:21 +0200122 COMMAND ${PYENV} ${VPP_VAPI_C_GEN}
Oliver Gilesdc203712019-12-05 23:37:36 +0200123 ARGS --remove-path ${input}
124 DEPENDS ${input} ${VPP_VAPI_C_GEN_DEPENDS}
125 COMMENT "Generating VAPI C header ${output_name}"
126 )
127 install(
128 FILES ${output_name}
Wim de Withf521df72024-01-28 11:05:15 +0100129 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/vapi
Oliver Gilesdc203712019-12-05 23:37:36 +0200130 COMPONENT vpp-dev
131 )
132endfunction ()
133
134function (vpp_generate_vapi_cpp_header f)
135 get_filename_component(output ${f}.vapi.hpp NAME)
Damjan Marion88b2e362021-04-29 18:47:25 +0200136 set (output_name ${VPP_BINARY_DIR}/vpp-api/vapi/${output})
Oliver Gilesdc203712019-12-05 23:37:36 +0200137 if(NOT VPP_VAPI_CPP_GEN)
138 set(VPP_VAPI_CPP_GEN ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_cpp_gen.py)
139 set(VPP_VAPI_CPP_GEN_DEPENDS
140 ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_cpp_gen.py
141 ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_json_parser.py
142 )
143 endif()
144 # C++ VAPI Headers
145 set(input ${CMAKE_CURRENT_BINARY_DIR}/${f}.json)
146 add_custom_command(
147 OUTPUT ${output_name}
Damjan Marion88b2e362021-04-29 18:47:25 +0200148 WORKING_DIRECTORY ${VPP_BINARY_DIR}/vpp-api/vapi
Damjan Marion7cf80af2021-05-25 19:28:21 +0200149 COMMAND ${PYENV} ${VPP_VAPI_CPP_GEN}
Oliver Gilesdc203712019-12-05 23:37:36 +0200150 ARGS --gen-h-prefix=vapi --remove-path ${input}
151 DEPENDS ${input} ${VPP_VAPI_CPP_GEN_DEPENDS}
152 COMMENT "Generating VAPI C++ header ${output_name}"
153 )
154 install(
155 FILES ${output_name}
Wim de Withf521df72024-01-28 11:05:15 +0100156 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/vapi
Oliver Gilesdc203712019-12-05 23:37:36 +0200157 COMPONENT vpp-dev
158 )
159endfunction ()
160
161
162##############################################################################
Damjan Mariond16004d2018-08-26 10:14:52 +0200163# generate the .h and .json files for a .api file
164# @param file - the name of the .api
165# @param dir - the install directory under ROOT/share/vpp/api to place the
166# generated .json file
167##############################################################################
Damjan Marion833de8c2018-09-07 12:39:02 +0200168function(vpp_generate_api_header file dir component)
Oliver Gilesdc203712019-12-05 23:37:36 +0200169 vpp_generate_api_c_header (${file})
170 vpp_generate_api_json_header (${file} ${dir} ${component})
171 vpp_generate_vapi_c_header (${file})
172 vpp_generate_vapi_cpp_header (${file})
Damjan Mariond16004d2018-08-26 10:14:52 +0200173endfunction()
174
Oliver Gilesdc203712019-12-05 23:37:36 +0200175function(vpp_add_api_files name dir component)
Damjan Mariond16004d2018-08-26 10:14:52 +0200176 unset(header_files)
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200177 set(target ${name}_api_headers)
178 file(RELATIVE_PATH rpath ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
Damjan Mariond16004d2018-08-26 10:14:52 +0200179 foreach(file ${ARGN})
Oliver Gilesdc203712019-12-05 23:37:36 +0200180 vpp_generate_api_header (${file} ${dir} ${component})
181 # Basic api headers get installed in a subdirectory according to
182 # their component name, but vapi is expected to be found directly under
183 # "vapi". Both by in-source components (e.g. vpp-api/vapi/vapi.c), and
184 # out-of-tree plugins use #include <vapi/component.api.vapi.h>.
185 # ${file} contains the subdirectory, so strip it here.
Ole Troaned61b202024-06-19 11:31:30 +0200186 file(MAKE_DIRECTORY ${VPP_BINARY_DIR}/vpp-api/vapi)
Oliver Gilesdc203712019-12-05 23:37:36 +0200187 get_filename_component(name ${file} NAME)
188 list(APPEND header_files
189 ${file}.h
Ole Troan7d527a22020-12-02 14:19:49 +0100190 ${file}_enum.h
191 ${file}_types.h
Oliver Gilesdc203712019-12-05 23:37:36 +0200192 ${file}.json
Damjan Marion88b2e362021-04-29 18:47:25 +0200193 ${VPP_BINARY_DIR}/vpp-api/vapi/${name}.vapi.h
194 ${VPP_BINARY_DIR}/vpp-api/vapi/${name}.vapi.hpp
Oliver Gilesdc203712019-12-05 23:37:36 +0200195 )
Damjan Mariond16004d2018-08-26 10:14:52 +0200196 endforeach()
197 add_custom_target(${target} DEPENDS ${header_files})
Ole Troan7d527a22020-12-02 14:19:49 +0100198 add_dependencies(api_headers ${target})
Damjan Mariond16004d2018-08-26 10:14:52 +0200199endfunction()
200
201add_custom_target(api_headers
Jakub Grajciar53f06a02020-03-30 08:12:57 +0200202 DEPENDS vlibmemory_api_headers vnet_api_headers vpp_api_headers vlib_api_headers
Damjan Mariond16004d2018-08-26 10:14:52 +0200203)
Ole Troaned61b202024-06-19 11:31:30 +0200204add_custom_target(vapi_headers
205)