|
| 1 | +# Copyright 2023 Open Source Robotics Foundation, 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 | +# |
| 16 | +# Generate a JSON / YAML file containing additional context data for expanding |
| 17 | +# IDL templates |
| 18 | +# |
| 19 | +# |
| 20 | +# @public |
| 21 | +# |
| 22 | +function(rosidl_write_additional_context output_file) |
| 23 | + |
| 24 | + set(OPTIONAL_ONE_VALUE_KEYWORDS |
| 25 | + "DISABLE_DESCRIPTION_CODEGEN") |
| 26 | + set(OPTIONAL_MULTI_VALUE_KEYWORDS |
| 27 | + "TYPE_SUPPORTS") |
| 28 | + |
| 29 | + cmake_parse_arguments( |
| 30 | + ARG |
| 31 | + "" |
| 32 | + "${OPTIONAL_ONE_VALUE_KEYWORDS}" |
| 33 | + "${OPTIONAL_MULTI_VALUE_KEYWORDS}" |
| 34 | + ${ARGN}) |
| 35 | + if(ARG_UNPARSED_ARGUMENTS) |
| 36 | + message(FATAL_ERROR "rosidl_write_additional_context() called with unused " |
| 37 | + "arguments: ${ARG_UNPARSED_ARGUMENTS}") |
| 38 | + endif() |
| 39 | + |
| 40 | + # create folder |
| 41 | + get_filename_component(output_path "${output_file}" PATH) |
| 42 | + file(MAKE_DIRECTORY "${output_path}") |
| 43 | + |
| 44 | + # open object |
| 45 | + file(WRITE "${output_file}" |
| 46 | + "{") |
| 47 | + |
| 48 | + set(first_element TRUE) |
| 49 | + |
| 50 | + # write string values |
| 51 | + foreach(one_value_argument ${OPTIONAL_ONE_VALUE_KEYWORDS}) |
| 52 | + if(DEFINED ARG_${one_value_argument}) |
| 53 | + # write conditional comma and mandatory newline |
| 54 | + if(NOT first_element) |
| 55 | + file(APPEND "${output_file}" ",") |
| 56 | + else() |
| 57 | + set(first_element FALSE) |
| 58 | + endif() |
| 59 | + file(APPEND "${output_file}" "\n") |
| 60 | + |
| 61 | + string(TOLOWER "${one_value_argument}" key) |
| 62 | + string(REPLACE "\\" "\\\\" value "${ARG_${one_value_argument}}") |
| 63 | + file(APPEND "${output_file}" |
| 64 | + " \"${key}\": \"${value}\"") |
| 65 | + endif() |
| 66 | + endforeach() |
| 67 | + |
| 68 | + # write array values |
| 69 | + foreach(multi_value_argument ${OPTIONAL_MULTI_VALUE_KEYWORDS}) |
| 70 | + if(ARG_${multi_value_argument}) |
| 71 | + # write conditional comma and mandatory newline and indentation |
| 72 | + if(NOT first_element) |
| 73 | + file(APPEND "${output_file}" ",") |
| 74 | + else() |
| 75 | + set(first_element FALSE) |
| 76 | + endif() |
| 77 | + file(APPEND "${output_file}" "\n") |
| 78 | + |
| 79 | + # write key, open array |
| 80 | + string(TOLOWER "${multi_value_argument}" key) |
| 81 | + file(APPEND "${output_file}" |
| 82 | + " \"${key}\": [\n") |
| 83 | + |
| 84 | + # write array values, last without trailing colon |
| 85 | + list(GET ARG_${multi_value_argument} -1 last_value) |
| 86 | + list(REMOVE_AT ARG_${multi_value_argument} -1) |
| 87 | + foreach(value ${ARG_${multi_value_argument}}) |
| 88 | + string(REPLACE "\\" "\\\\" value "${value}") |
| 89 | + file(APPEND "${output_file}" |
| 90 | + " \"${value}\",\n") |
| 91 | + endforeach() |
| 92 | + string(REPLACE "\\" "\\\\" last_value "${last_value}") |
| 93 | + file(APPEND "${output_file}" |
| 94 | + " \"${last_value}\"\n") |
| 95 | + |
| 96 | + # close array |
| 97 | + file(APPEND "${output_file}" |
| 98 | + " ]") |
| 99 | + endif() |
| 100 | + endforeach() |
| 101 | + |
| 102 | + # close object |
| 103 | + file(APPEND "${output_file}" |
| 104 | + "\n}\n") |
| 105 | +endfunction() |
0 commit comments