api: split api generated files
[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   add_custom_command (OUTPUT ${output_name}
27     COMMAND mkdir -p ${output_dir}
28     COMMAND ${VPP_APIGEN}
29     ARGS ${includedir} --includedir ${CMAKE_SOURCE_DIR} --input ${CMAKE_CURRENT_SOURCE_DIR}/${file} --outputdir ${output_dir} --output ${output_name}
30     DEPENDS ${VPP_APIGEN} ${CMAKE_CURRENT_SOURCE_DIR}/${file}
31     COMMENT "Generating API header ${output_name}"
32   )
33 endfunction()
34
35 function(vpp_generate_api_json_header file dir component)
36   set (output_name ${CMAKE_CURRENT_BINARY_DIR}/${file}.json)
37   get_filename_component(output_dir ${output_name} DIRECTORY)
38   if(NOT VPP_APIGEN)
39      set(VPP_APIGEN ${CMAKE_SOURCE_DIR}/tools/vppapigen/vppapigen)
40   endif()
41   if (VPP_INCLUDE_DIR)
42     set(includedir "--includedir" ${VPP_INCLUDE_DIR})
43   endif()
44   add_custom_command (OUTPUT ${output_name}
45     COMMAND mkdir -p ${output_dir}
46     COMMAND ${VPP_APIGEN}
47     ARGS ${includedir} --includedir ${CMAKE_SOURCE_DIR} --input ${CMAKE_CURRENT_SOURCE_DIR}/${file} JSON --output ${output_name}
48     DEPENDS ${VPP_APIGEN} ${CMAKE_CURRENT_SOURCE_DIR}/${file}
49     COMMENT "Generating API header ${output_name}"
50   )
51   install(
52     FILES ${output_name}
53     DESTINATION share/vpp/api/${dir}/
54     COMPONENT ${component}
55   )
56 endfunction()
57
58 ##############################################################################
59 # generate the .h and .json files for a .api file
60 #  @param file - the name of the .api
61 #  @param dir  - the install directory under ROOT/share/vpp/api to place the
62 #                generated .json file
63 ##############################################################################
64 function(vpp_generate_api_header file dir component)
65     vpp_generate_api_c_header (${file})
66     vpp_generate_api_json_header (${file} ${dir} ${component})
67 endfunction()
68
69 function(vpp_add_api_files name)
70   unset(header_files)
71   set(target ${name}_api_headers)
72   file(RELATIVE_PATH rpath ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
73   foreach(file ${ARGN})
74     vpp_generate_api_header (${file} core vpp)
75     list(APPEND header_files ${file}.h ${file}.json)
76     set_property(GLOBAL APPEND PROPERTY VPP_API_FILES ${rpath}/${file})
77   endforeach()
78   add_custom_target(${target} DEPENDS ${header_files})
79 endfunction()
80
81 add_custom_target(api_headers
82   DEPENDS vlibmemory_api_headers vnet_api_headers vpp_api_headers
83 )
84