Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 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 | |
Ole Troan | ed61b20 | 2024-06-19 11:31:30 +0200 | [diff] [blame] | 14 | # Set the CMP0116 policy |
| 15 | if(POLICY CMP0116) |
| 16 | cmake_policy(SET CMP0116 NEW) |
| 17 | endif() |
| 18 | |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 19 | ############################################################################## |
| 20 | # API |
| 21 | ############################################################################## |
| 22 | function(vpp_generate_api_c_header file) |
| 23 | set (output_name ${CMAKE_CURRENT_BINARY_DIR}/${file}.h) |
Ole Troan | ed61b20 | 2024-06-19 11:31:30 +0200 | [diff] [blame] | 24 | set (dependency_file ${CMAKE_CURRENT_BINARY_DIR}/${file}.d) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 25 | get_filename_component(output_dir ${output_name} DIRECTORY) |
Damjan Marion | 0fa900e | 2018-09-12 12:12:36 +0200 | [diff] [blame] | 26 | if(NOT VPP_APIGEN) |
Ole Troan | ed61b20 | 2024-06-19 11:31:30 +0200 | [diff] [blame] | 27 | 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 Marion | 0fa900e | 2018-09-12 12:12:36 +0200 | [diff] [blame] | 32 | endif() |
Benoît Ganne | 6ceee45 | 2019-07-25 17:15:59 +0200 | [diff] [blame] | 33 | if (VPP_INCLUDE_DIR) |
| 34 | set(includedir "--includedir" ${VPP_INCLUDE_DIR}) |
| 35 | endif() |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 36 | |
| 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 Troan | ed61b20 | 2024-06-19 11:31:30 +0200 | [diff] [blame] | 48 | get_filename_component(barename ${file} NAME) |
| 49 | |
Ole Troan | 228e195 | 2024-06-21 17:17:09 +0200 | [diff] [blame^] | 50 | # Define a variable for common apigen arguments |
| 51 | set(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 | |
| 60 | if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.20") |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 61 | add_custom_command ( |
Ole Troan | 228e195 | 2024-06-21 17:17:09 +0200 | [diff] [blame^] | 62 | ${COMMON_ARGS} |
Ole Troan | ed61b20 | 2024-06-19 11:31:30 +0200 | [diff] [blame] | 63 | DEPFILE ${dependency_file} |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 64 | ) |
Ole Troan | 228e195 | 2024-06-21 17:17:09 +0200 | [diff] [blame^] | 65 | else() |
| 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 | ) |
| 70 | endif() |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 71 | set(t ${barename}_deps) |
Ole Troan | ed61b20 | 2024-06-19 11:31:30 +0200 | [diff] [blame] | 72 | |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 73 | if (NOT TARGET ${t}) |
| 74 | add_custom_target(${t} ALL DEPENDS ${OUTPUT_HEADERS}) |
| 75 | add_dependencies(api_headers ${t}) |
| 76 | endif() |
Ole Troan | 7d527a2 | 2020-12-02 14:19:49 +0100 | [diff] [blame] | 77 | |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 78 | endfunction() |
| 79 | |
Damjan Marion | 833de8c | 2018-09-07 12:39:02 +0200 | [diff] [blame] | 80 | function(vpp_generate_api_json_header file dir component) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 81 | set (output_name ${CMAKE_CURRENT_BINARY_DIR}/${file}.json) |
| 82 | get_filename_component(output_dir ${output_name} DIRECTORY) |
Damjan Marion | 0fa900e | 2018-09-12 12:12:36 +0200 | [diff] [blame] | 83 | if(NOT VPP_APIGEN) |
| 84 | set(VPP_APIGEN ${CMAKE_SOURCE_DIR}/tools/vppapigen/vppapigen) |
| 85 | endif() |
Benoît Ganne | 6ceee45 | 2019-07-25 17:15:59 +0200 | [diff] [blame] | 86 | if (VPP_INCLUDE_DIR) |
| 87 | set(includedir "--includedir" ${VPP_INCLUDE_DIR}) |
| 88 | endif() |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 89 | add_custom_command (OUTPUT ${output_name} |
| 90 | COMMAND mkdir -p ${output_dir} |
Damjan Marion | 7cf80af | 2021-05-25 19:28:21 +0200 | [diff] [blame] | 91 | COMMAND ${PYENV} ${VPP_APIGEN} |
Ole Troan | ac0babd | 2024-01-23 18:56:23 +0100 | [diff] [blame] | 92 | ARGS ${includedir} --includedir ${CMAKE_SOURCE_DIR} --input ${CMAKE_CURRENT_SOURCE_DIR}/${file} JSON --outputdir ${output_dir} --output ${output_name} |
Ole Troan | e4f849c | 2018-11-29 20:14:33 +0100 | [diff] [blame] | 93 | DEPENDS ${VPP_APIGEN} ${CMAKE_CURRENT_SOURCE_DIR}/${file} |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 94 | COMMENT "Generating API header ${output_name}" |
| 95 | ) |
Damjan Marion | 833de8c | 2018-09-07 12:39:02 +0200 | [diff] [blame] | 96 | install( |
| 97 | FILES ${output_name} |
Wim de With | f521df7 | 2024-01-28 11:05:15 +0100 | [diff] [blame] | 98 | DESTINATION ${CMAKE_INSTALL_DATADIR}/vpp/api/${dir}/ |
Damjan Marion | 833de8c | 2018-09-07 12:39:02 +0200 | [diff] [blame] | 99 | COMPONENT ${component} |
| 100 | ) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 101 | endfunction() |
| 102 | |
| 103 | ############################################################################## |
Oliver Giles | dc20371 | 2019-12-05 23:37:36 +0200 | [diff] [blame] | 104 | # VPP-API |
| 105 | ############################################################################## |
| 106 | function(vpp_generate_vapi_c_header f) |
| 107 | get_filename_component(output ${f}.vapi.h NAME) |
Damjan Marion | 88b2e36 | 2021-04-29 18:47:25 +0200 | [diff] [blame] | 108 | set (output_name ${VPP_BINARY_DIR}/vpp-api/vapi/${output}) |
Oliver Giles | dc20371 | 2019-12-05 23:37:36 +0200 | [diff] [blame] | 109 | 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 Marion | 88b2e36 | 2021-04-29 18:47:25 +0200 | [diff] [blame] | 121 | WORKING_DIRECTORY ${VPP_BINARY_DIR}/vpp-api/vapi |
Damjan Marion | 7cf80af | 2021-05-25 19:28:21 +0200 | [diff] [blame] | 122 | COMMAND ${PYENV} ${VPP_VAPI_C_GEN} |
Oliver Giles | dc20371 | 2019-12-05 23:37:36 +0200 | [diff] [blame] | 123 | 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 With | f521df7 | 2024-01-28 11:05:15 +0100 | [diff] [blame] | 129 | DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/vapi |
Oliver Giles | dc20371 | 2019-12-05 23:37:36 +0200 | [diff] [blame] | 130 | COMPONENT vpp-dev |
| 131 | ) |
| 132 | endfunction () |
| 133 | |
| 134 | function (vpp_generate_vapi_cpp_header f) |
| 135 | get_filename_component(output ${f}.vapi.hpp NAME) |
Damjan Marion | 88b2e36 | 2021-04-29 18:47:25 +0200 | [diff] [blame] | 136 | set (output_name ${VPP_BINARY_DIR}/vpp-api/vapi/${output}) |
Oliver Giles | dc20371 | 2019-12-05 23:37:36 +0200 | [diff] [blame] | 137 | 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 Marion | 88b2e36 | 2021-04-29 18:47:25 +0200 | [diff] [blame] | 148 | WORKING_DIRECTORY ${VPP_BINARY_DIR}/vpp-api/vapi |
Damjan Marion | 7cf80af | 2021-05-25 19:28:21 +0200 | [diff] [blame] | 149 | COMMAND ${PYENV} ${VPP_VAPI_CPP_GEN} |
Oliver Giles | dc20371 | 2019-12-05 23:37:36 +0200 | [diff] [blame] | 150 | 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 With | f521df7 | 2024-01-28 11:05:15 +0100 | [diff] [blame] | 156 | DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/vapi |
Oliver Giles | dc20371 | 2019-12-05 23:37:36 +0200 | [diff] [blame] | 157 | COMPONENT vpp-dev |
| 158 | ) |
| 159 | endfunction () |
| 160 | |
| 161 | |
| 162 | ############################################################################## |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 163 | # 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 Marion | 833de8c | 2018-09-07 12:39:02 +0200 | [diff] [blame] | 168 | function(vpp_generate_api_header file dir component) |
Oliver Giles | dc20371 | 2019-12-05 23:37:36 +0200 | [diff] [blame] | 169 | 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 Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 173 | endfunction() |
| 174 | |
Oliver Giles | dc20371 | 2019-12-05 23:37:36 +0200 | [diff] [blame] | 175 | function(vpp_add_api_files name dir component) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 176 | unset(header_files) |
Damjan Marion | 4c64b6e | 2018-08-26 18:14:46 +0200 | [diff] [blame] | 177 | set(target ${name}_api_headers) |
| 178 | file(RELATIVE_PATH rpath ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 179 | foreach(file ${ARGN}) |
Oliver Giles | dc20371 | 2019-12-05 23:37:36 +0200 | [diff] [blame] | 180 | 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 Troan | ed61b20 | 2024-06-19 11:31:30 +0200 | [diff] [blame] | 186 | file(MAKE_DIRECTORY ${VPP_BINARY_DIR}/vpp-api/vapi) |
Oliver Giles | dc20371 | 2019-12-05 23:37:36 +0200 | [diff] [blame] | 187 | get_filename_component(name ${file} NAME) |
| 188 | list(APPEND header_files |
| 189 | ${file}.h |
Ole Troan | 7d527a2 | 2020-12-02 14:19:49 +0100 | [diff] [blame] | 190 | ${file}_enum.h |
| 191 | ${file}_types.h |
Oliver Giles | dc20371 | 2019-12-05 23:37:36 +0200 | [diff] [blame] | 192 | ${file}.json |
Damjan Marion | 88b2e36 | 2021-04-29 18:47:25 +0200 | [diff] [blame] | 193 | ${VPP_BINARY_DIR}/vpp-api/vapi/${name}.vapi.h |
| 194 | ${VPP_BINARY_DIR}/vpp-api/vapi/${name}.vapi.hpp |
Oliver Giles | dc20371 | 2019-12-05 23:37:36 +0200 | [diff] [blame] | 195 | ) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 196 | endforeach() |
| 197 | add_custom_target(${target} DEPENDS ${header_files}) |
Ole Troan | 7d527a2 | 2020-12-02 14:19:49 +0100 | [diff] [blame] | 198 | add_dependencies(api_headers ${target}) |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 199 | endfunction() |
| 200 | |
| 201 | add_custom_target(api_headers |
Jakub Grajciar | 53f06a0 | 2020-03-30 08:12:57 +0200 | [diff] [blame] | 202 | DEPENDS vlibmemory_api_headers vnet_api_headers vpp_api_headers vlib_api_headers |
Damjan Marion | d16004d | 2018-08-26 10:14:52 +0200 | [diff] [blame] | 203 | ) |
Ole Troan | ed61b20 | 2024-06-19 11:31:30 +0200 | [diff] [blame] | 204 | add_custom_target(vapi_headers |
| 205 | ) |