api: vat2 and json autogeneration for api messages
[vpp.git] / src / cmake / api.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 ##############################################################################
15 # API
16 ##############################################################################
17 function(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)
20   if(NOT VPP_APIGEN)
21      set(VPP_APIGEN ${CMAKE_SOURCE_DIR}/tools/vppapigen/vppapigen)
22   endif()
23   if (VPP_INCLUDE_DIR)
24     set(includedir "--includedir" ${VPP_INCLUDE_DIR})
25   endif()
26
27   #set(VPP_APIGEN_DEPS ${CMAKE_SOURCE_DIR}/tools/vppapigen/mkdep)
28   #execute_process(
29   #  COMMAND ${VPP_APIGEN_DEPS} ${CMAKE_CURRENT_SOURCE_DIR}/${file}
30   #  OUTPUT_VARIABLE DEPENDENCIES
31   # RESULT_VARIABLE RETURN_VALUE
32   #  )
33   #if (NOT RETURN_VALUE EQUAL 0)
34   #  message(FATAL_ERROR "Failed to get the dependencies")
35   #endif()
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
48   add_custom_command (
49     OUTPUT ${OUTPUT_HEADERS}
50     COMMAND mkdir -p ${output_dir}
51     COMMAND ${VPP_APIGEN}
52     ARGS ${includedir} --includedir ${CMAKE_SOURCE_DIR} --input ${CMAKE_CURRENT_SOURCE_DIR}/${file} --outputdir ${output_dir} --output ${output_name}
53     DEPENDS ${VPP_APIGEN} ${CMAKE_CURRENT_SOURCE_DIR}/${file} # ${DEPENDENCIES}
54     COMMENT "Generating API header ${output_name}"
55   )
56   get_filename_component(barename ${file} NAME)
57   set(t ${barename}_deps)
58   if (NOT TARGET ${t})
59     add_custom_target(${t} ALL DEPENDS ${OUTPUT_HEADERS})
60     add_dependencies(api_headers ${t})
61   endif()
62 endfunction()
63
64 function(vpp_generate_api_json_header file dir component)
65   set (output_name ${CMAKE_CURRENT_BINARY_DIR}/${file}.json)
66   get_filename_component(output_dir ${output_name} DIRECTORY)
67   if(NOT VPP_APIGEN)
68      set(VPP_APIGEN ${CMAKE_SOURCE_DIR}/tools/vppapigen/vppapigen)
69   endif()
70   if (VPP_INCLUDE_DIR)
71     set(includedir "--includedir" ${VPP_INCLUDE_DIR})
72   endif()
73   add_custom_command (OUTPUT ${output_name}
74     COMMAND mkdir -p ${output_dir}
75     COMMAND ${VPP_APIGEN}
76     ARGS ${includedir} --includedir ${CMAKE_SOURCE_DIR} --input ${CMAKE_CURRENT_SOURCE_DIR}/${file} JSON --output ${output_name}
77     DEPENDS ${VPP_APIGEN} ${CMAKE_CURRENT_SOURCE_DIR}/${file}
78     COMMENT "Generating API header ${output_name}"
79   )
80   install(
81     FILES ${output_name}
82     DESTINATION share/vpp/api/${dir}/
83     COMPONENT ${component}
84   )
85 endfunction()
86
87 ##############################################################################
88 # VPP-API
89 ##############################################################################
90 function(vpp_generate_vapi_c_header f)
91   get_filename_component(output ${f}.vapi.h NAME)
92   set (output_name ${CMAKE_BINARY_DIR}/vpp-api/vapi/${output})
93   if(NOT VPP_VAPI_C_GEN)
94     set(VPP_VAPI_C_GEN ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_c_gen.py)
95     set(VPP_VAPI_C_GEN_DEPENDS
96         ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_c_gen.py
97         ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_json_parser.py
98     )
99   endif()
100
101   # C VAPI Headers
102   set(input ${CMAKE_CURRENT_BINARY_DIR}/${f}.json)
103   add_custom_command(
104     OUTPUT ${output_name}
105     WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/vpp-api/vapi
106     COMMAND ${VPP_VAPI_C_GEN}
107     ARGS --remove-path ${input}
108     DEPENDS ${input} ${VPP_VAPI_C_GEN_DEPENDS}
109     COMMENT "Generating VAPI C header ${output_name}"
110   )
111   install(
112     FILES ${output_name}
113     DESTINATION include/vapi
114     COMPONENT vpp-dev
115   )
116 endfunction ()
117
118 function (vpp_generate_vapi_cpp_header f)
119   get_filename_component(output ${f}.vapi.hpp NAME)
120   set (output_name ${CMAKE_BINARY_DIR}/vpp-api/vapi/${output})
121   if(NOT VPP_VAPI_CPP_GEN)
122     set(VPP_VAPI_CPP_GEN ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_cpp_gen.py)
123     set(VPP_VAPI_CPP_GEN_DEPENDS
124         ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_cpp_gen.py
125         ${CMAKE_SOURCE_DIR}/vpp-api/vapi/vapi_json_parser.py
126     )
127   endif()
128   # C++ VAPI Headers
129   set(input ${CMAKE_CURRENT_BINARY_DIR}/${f}.json)
130   add_custom_command(
131     OUTPUT ${output_name}
132     WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/vpp-api/vapi
133     COMMAND ${VPP_VAPI_CPP_GEN}
134     ARGS --gen-h-prefix=vapi --remove-path ${input}
135     DEPENDS ${input} ${VPP_VAPI_CPP_GEN_DEPENDS}
136     COMMENT "Generating VAPI C++ header ${output_name}"
137   )
138   install(
139     FILES ${output_name}
140     DESTINATION include/vapi
141     COMPONENT vpp-dev
142   )
143 endfunction ()
144
145
146 ##############################################################################
147 # generate the .h and .json files for a .api file
148 #  @param file - the name of the .api
149 #  @param dir  - the install directory under ROOT/share/vpp/api to place the
150 #                generated .json file
151 ##############################################################################
152 function(vpp_generate_api_header file dir component)
153   vpp_generate_api_c_header (${file})
154   vpp_generate_api_json_header (${file} ${dir} ${component})
155   vpp_generate_vapi_c_header (${file})
156   vpp_generate_vapi_cpp_header (${file})
157 endfunction()
158
159 function(vpp_add_api_files name dir component)
160   unset(header_files)
161   set(target ${name}_api_headers)
162   file(RELATIVE_PATH rpath ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
163   foreach(file ${ARGN})
164     vpp_generate_api_header (${file} ${dir} ${component})
165     # Basic api headers get installed in a subdirectory according to
166     # their component name, but vapi is expected to be found directly under
167     # "vapi". Both by in-source components (e.g. vpp-api/vapi/vapi.c), and
168     # out-of-tree plugins use #include <vapi/component.api.vapi.h>.
169     # ${file} contains the subdirectory, so strip it here.
170     get_filename_component(name ${file} NAME)
171     list(APPEND header_files
172       ${file}.h
173       ${file}.json
174       ${CMAKE_BINARY_DIR}/vpp-api/vapi/${name}.vapi.h
175       ${CMAKE_BINARY_DIR}/vpp-api/vapi/${name}.vapi.hpp
176     )
177   endforeach()
178   add_custom_target(${target} DEPENDS ${header_files})
179 endfunction()
180
181 add_custom_target(api_headers
182   DEPENDS vlibmemory_api_headers vnet_api_headers vpp_api_headers vlib_api_headers
183 )