|
| 1 | +# Copyright 2021 Tier IV, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +macro(autoware_api_generate_library target) |
| 16 | + |
| 17 | + # init path settings |
| 18 | + normalize_path(_build_path "${CMAKE_CURRENT_BINARY_DIR}/autoware_api_generator") |
| 19 | + normalize_path(_share_path "${autoware_api_generator_DIR}/..") |
| 20 | + |
| 21 | + # parse and check |
| 22 | + cmake_parse_arguments(_ARG "USE_GROUP_PATH" "REMAP;EXTRA" "SPECS" ${ARGN}) |
| 23 | + if(NOT _ARG_REMAP) |
| 24 | + set(_remap_file "${_share_path}/config/remap.yaml") |
| 25 | + else() |
| 26 | + set(_remap_file "${CMAKE_CURRENT_SOURCE_DIR}/${_ARG_REMAP}") |
| 27 | + endif() |
| 28 | + if(NOT _ARG_EXTRA) |
| 29 | + set(_extra_file "${_share_path}/config/extra.yaml") |
| 30 | + else() |
| 31 | + set(_extra_file "${CMAKE_CURRENT_SOURCE_DIR}/${_ARG_EXTRA}") |
| 32 | + endif() |
| 33 | + |
| 34 | + # create specification file list |
| 35 | + set(_spec_files "") |
| 36 | + list(APPEND _spec_files "${_share_path}/resource/external.yaml") |
| 37 | + list(APPEND _spec_files "${_share_path}/resource/internal.yaml") |
| 38 | + |
| 39 | + # create generating file list |
| 40 | + set(_target_api_names "") |
| 41 | + set(_target_cpp_files "") |
| 42 | + set(_target_py_files "") |
| 43 | + set(_target_cmake_files "") |
| 44 | + foreach(_api ${_ARG_UNPARSED_ARGUMENTS}) |
| 45 | + string(REGEX REPLACE "^/+" "" _api "${_api}") |
| 46 | + if(NOT ${_ARG_USE_GROUP_PATH}) |
| 47 | + string(REGEX REPLACE "^[^/]+" "" _api "${_api}") |
| 48 | + string(REGEX REPLACE "^/+" "" _api "${_api}") |
| 49 | + endif() |
| 50 | + list(APPEND _target_api_names "${_api}") |
| 51 | + list(APPEND _target_cpp_files "${_build_path}/cpp/${_api}.hpp") |
| 52 | + list(APPEND _target_py_files "${_build_path}/py/${_api}.py") |
| 53 | + list(APPEND _target_cmake_files "${_build_path}/cmake/${_api}.cmake") |
| 54 | + endforeach() |
| 55 | + |
| 56 | + # set dependencies for main target |
| 57 | + set(autoware_api_generator_TARGET "${target}") |
| 58 | + add_custom_target(${autoware_api_generator_TARGET} ALL SOURCES ${_spec_files} ${_remap_file}) |
| 59 | + add_custom_target(${autoware_api_generator_TARGET}__cmake SOURCES ${_target_cmake_files}) |
| 60 | + |
| 61 | + # set dependencies for cpp target |
| 62 | + add_custom_target(${autoware_api_generator_TARGET}__cpp SOURCES ${_target_cpp_files}) |
| 63 | + add_dependencies(${autoware_api_generator_TARGET} ${autoware_api_generator_TARGET}__cpp) |
| 64 | + |
| 65 | + # set dependencies for py target |
| 66 | + add_custom_target(${autoware_api_generator_TARGET}__py SOURCES ${_target_py_files}) |
| 67 | + add_dependencies(${autoware_api_generator_TARGET} ${autoware_api_generator_TARGET}__py) |
| 68 | + |
| 69 | + # use a temporary file to avoid long command |
| 70 | + file(WRITE "${_build_path}/list.txt" "${_target_api_names}") |
| 71 | + |
| 72 | + # generate configure_file scripts |
| 73 | + add_custom_command( |
| 74 | + OUTPUT ${_target_cmake_files} |
| 75 | + COMMAND python3 ${_share_path}/script/configure.py |
| 76 | + "--package" ${PROJECT_NAME} |
| 77 | + "--build" ${_build_path}/cmake |
| 78 | + "--names" ${_build_path}/list.txt |
| 79 | + "--remap" ${_remap_file} |
| 80 | + "--extra" ${_extra_file} |
| 81 | + "--specs" ${_spec_files} |
| 82 | + DEPENDS |
| 83 | + ${_share_path}/script/configure.py |
| 84 | + ${_build_path}/list.txt |
| 85 | + ${_remap_file} |
| 86 | + ${_spec_files} |
| 87 | + ) |
| 88 | + |
| 89 | + # generate cpp file from configure_file script |
| 90 | + foreach(_api ${_target_api_names}) |
| 91 | + set(_api_output "${_build_path}/cpp/${_api}.hpp") |
| 92 | + set(_api_script "${_build_path}/cmake/${_api}.cmake") |
| 93 | + add_custom_command( |
| 94 | + OUTPUT ${_api_output} |
| 95 | + COMMAND ${CMAKE_COMMAND} |
| 96 | + -D INPUT=${_share_path}/template/cpp |
| 97 | + -D OUTPUT=${_api_output} |
| 98 | + -P ${_api_script} |
| 99 | + DEPENDS |
| 100 | + ${_share_path}/template/cpp/service.in |
| 101 | + ${_share_path}/template/cpp/topic.in |
| 102 | + ${autoware_api_generator_TARGET}__cmake |
| 103 | + ) |
| 104 | + endforeach() |
| 105 | + |
| 106 | + # generate py file from configure_file script |
| 107 | + foreach(_api ${_target_api_names}) |
| 108 | + set(_api_output "${_build_path}/py/${_api}.py") |
| 109 | + set(_api_script "${_build_path}/cmake/${_api}.cmake") |
| 110 | + add_custom_command( |
| 111 | + OUTPUT ${_api_output} |
| 112 | + COMMAND ${CMAKE_COMMAND} |
| 113 | + -D INPUT=${_share_path}/template/py |
| 114 | + -D OUTPUT=${_api_output} |
| 115 | + -P ${_api_script} |
| 116 | + DEPENDS |
| 117 | + ${_share_path}/template/py/service.in |
| 118 | + ${_share_path}/template/py/topic.in |
| 119 | + ${autoware_api_generator_TARGET}__cmake |
| 120 | + ) |
| 121 | + endforeach() |
| 122 | + |
| 123 | + # install generated cpp files |
| 124 | + if(EXISTS ${_build_path}) |
| 125 | + install( |
| 126 | + DIRECTORY ${_build_path}/cpp/ |
| 127 | + DESTINATION include/${PROJECT_NAME} |
| 128 | + PATTERN *.hpp |
| 129 | + ) |
| 130 | + ament_export_include_directories(include) |
| 131 | + endif() |
| 132 | + |
| 133 | + # install generated py files |
| 134 | + file(WRITE ${_build_path}/py/__init__.py "") |
| 135 | + ament_python_install_package(${PROJECT_NAME} PACKAGE_DIR "${_build_path}/py") |
| 136 | + |
| 137 | +endmacro(autoware_api_generate_library) |
0 commit comments