|
| 1 | +# Copyright 2016-2017 Esteve Fernandez <esteve@apache.org> |
| 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 | +if(NOT WIN32) |
| 16 | + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| 17 | + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined") |
| 18 | + elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
| 19 | + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-undefined,error") |
| 20 | + endif() |
| 21 | +endif() |
| 22 | + |
| 23 | +set(_output_path |
| 24 | + "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_rs/${PROJECT_NAME}") |
| 25 | +set(_generated_common_rs_files "") |
| 26 | + |
| 27 | +set(_generated_msg_rs_files "") |
| 28 | +set(_generated_srv_rs_files "") |
| 29 | +set(_generated_action_rs_files "") |
| 30 | + |
| 31 | +set(_has_msg FALSE) |
| 32 | +set(_has_srv FALSE) |
| 33 | +set(_has_action FALSE) |
| 34 | + |
| 35 | +foreach(_idl_file ${rosidl_generate_interfaces_ABS_IDL_FILES}) |
| 36 | + get_filename_component(_parent_folder "${_idl_file}" DIRECTORY) |
| 37 | + get_filename_component(_parent_folder "${_parent_folder}" NAME) |
| 38 | + get_filename_component(_module_name "${_idl_file}" NAME_WE) |
| 39 | + |
| 40 | + if(_parent_folder STREQUAL "msg") |
| 41 | + set(_has_msg TRUE) |
| 42 | + set(_idl_files ${_idl_files} ${_idl_file}) |
| 43 | + elseif(_parent_folder STREQUAL "srv") |
| 44 | + set(_has_srv TRUE) |
| 45 | + set(_idl_files ${_idl_files} ${_idl_file}) |
| 46 | + elseif(_parent_folder STREQUAL "action") |
| 47 | + set(_has_action TRUE) |
| 48 | + set(_idl_files ${_idl_files} ${_idl_file}) |
| 49 | + else() |
| 50 | + message(FATAL_ERROR "Interface file with unknown parent folder: ${_idl_file}") |
| 51 | + endif() |
| 52 | +endforeach() |
| 53 | + |
| 54 | +list(APPEND _generated_common_rs_files |
| 55 | + "${_output_path}/rust/src/lib.rs" |
| 56 | + "${_output_path}/rust/build.rs" |
| 57 | + "${_output_path}/rust/Cargo.toml" |
| 58 | +) |
| 59 | + |
| 60 | +if(${_has_msg}) |
| 61 | + list(APPEND _generated_msg_rs_files |
| 62 | + "${_output_path}/rust/src/msg.rs" |
| 63 | + ) |
| 64 | +endif() |
| 65 | + |
| 66 | +if(${_has_srv}) |
| 67 | + list(APPEND _generated_srv_rs_files |
| 68 | + "${_output_path}/rust/src/srv.rs" |
| 69 | + ) |
| 70 | +endif() |
| 71 | + |
| 72 | +if(${_has_action}) |
| 73 | + list(APPEND _generated_action_rs_files |
| 74 | + "${_output_path}/rust/src/action.rs" |
| 75 | + ) |
| 76 | +endif() |
| 77 | + |
| 78 | +set(_dependency_files "") |
| 79 | +set(_dependencies "") |
| 80 | +foreach(_pkg_name ${rosidl_generate_interfaces_DEPENDENCY_PACKAGE_NAMES}) |
| 81 | + foreach(_idl_file ${${_pkg_name}_IDL_FILES}) |
| 82 | + set(_abs_idl_file "${${_pkg_name}_DIR}/../${_idl_file}") |
| 83 | + normalize_path(_abs_idl_file "${_abs_idl_file}") |
| 84 | + list(APPEND _dependency_files "${_abs_idl_file}") |
| 85 | + list(APPEND _dependencies "${_pkg_name}:${_abs_idl_file}") |
| 86 | + endforeach() |
| 87 | +endforeach() |
| 88 | + |
| 89 | +set(target_dependencies |
| 90 | + "${rosidl_generator_rs_BIN}" |
| 91 | + ${rosidl_generator_rs_GENERATOR_FILES} |
| 92 | + "${rosidl_generator_rs_TEMPLATE_DIR}/action.rs.em" |
| 93 | + "${rosidl_generator_rs_TEMPLATE_DIR}/msg_idiomatic.rs.em" |
| 94 | + "${rosidl_generator_rs_TEMPLATE_DIR}/msg_rmw.rs.em" |
| 95 | + "${rosidl_generator_rs_TEMPLATE_DIR}/msg.rs.em" |
| 96 | + "${rosidl_generator_rs_TEMPLATE_DIR}/srv_idiomatic.rs.em" |
| 97 | + "${rosidl_generator_rs_TEMPLATE_DIR}/srv_rmw.rs.em" |
| 98 | + "${rosidl_generator_rs_TEMPLATE_DIR}/srv.rs.em" |
| 99 | + ${rosidl_generate_interfaces_ABS_IDL_FILES} |
| 100 | + ${_idl_files} |
| 101 | + ${_dependency_files}) |
| 102 | +foreach(dep ${target_dependencies}) |
| 103 | + if(NOT EXISTS "${dep}") |
| 104 | + message(FATAL_ERROR "Target dependency '${dep}' does not exist") |
| 105 | + endif() |
| 106 | +endforeach() |
| 107 | + |
| 108 | +set(generator_arguments_file "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_rs__arguments.json") |
| 109 | +rosidl_write_generator_arguments( |
| 110 | + "${generator_arguments_file}" |
| 111 | + PACKAGE_NAME "${PROJECT_NAME}" |
| 112 | + IDL_TUPLES "${rosidl_generate_interfaces_IDL_TUPLES}" |
| 113 | + ROS_INTERFACE_FILES "${_idl_files}" |
| 114 | + ROS_INTERFACE_DEPENDENCIES "${_dependencies}" |
| 115 | + OUTPUT_DIR "${_output_path}" |
| 116 | + TEMPLATE_DIR "${rosidl_generator_rs_TEMPLATE_DIR}" |
| 117 | + TARGET_DEPENDENCIES ${target_dependencies} |
| 118 | +) |
| 119 | + |
| 120 | +file(READ ${generator_arguments_file} contents) |
| 121 | +string(REPLACE "\n}" |
| 122 | + ",\n \"package_version\": \"${${PROJECT_NAME}_VERSION}\"\n}" contents ${contents}) |
| 123 | +file(WRITE ${generator_arguments_file} ${contents}) |
| 124 | + |
| 125 | +file(MAKE_DIRECTORY "${_output_path}") |
| 126 | + |
| 127 | +set(_target_suffix "__rs") |
| 128 | + |
| 129 | +# needed to avoid multiple calls to the Rust generator trick copied from |
| 130 | +# https://github.com/ros2/rosidl/blob/master/rosidl_generator_py/cmake/rosidl_generator_py_generate_interfaces.cmake |
| 131 | +set(_subdir "${CMAKE_CURRENT_BINARY_DIR}/${rosidl_generate_interfaces_TARGET}${_target_suffix}") |
| 132 | +file(MAKE_DIRECTORY "${_subdir}") |
| 133 | +file(READ "${rosidl_generator_rs_DIR}/custom_command.cmake" _custom_command) |
| 134 | +file(WRITE "${_subdir}/CMakeLists.txt" "${_custom_command}") |
| 135 | +add_subdirectory("${_subdir}" ${rosidl_generate_interfaces_TARGET}${_target_suffix}) |
| 136 | + |
| 137 | +add_dependencies(${rosidl_generate_interfaces_TARGET} ${rosidl_generate_interfaces_TARGET}${_target_suffix}) |
| 138 | + |
| 139 | +set_property( |
| 140 | + SOURCE |
| 141 | + ${_generated_common_rs_files} |
| 142 | + ${_generated_msg_rs_files} |
| 143 | + ${_generated_srv_rs_files} |
| 144 | + ${_generated_action_rs_files} |
| 145 | + PROPERTY GENERATED 1) |
| 146 | + |
| 147 | +set(_rsext_suffix "__rsext") |
| 148 | +if(NOT rosidl_generate_interfaces_SKIP_INSTALL) |
| 149 | + ament_index_register_resource("rust_packages") |
| 150 | + install( |
| 151 | + DIRECTORY "${_output_path}/rust" |
| 152 | + DESTINATION "share/${PROJECT_NAME}" |
| 153 | + ) |
| 154 | +endif() |
| 155 | + |
| 156 | +if(BUILD_TESTING AND rosidl_generate_interfaces_ADD_LINTER_TESTS) |
| 157 | + if( |
| 158 | + NOT _generated_msg_rs_files STREQUAL "" OR |
| 159 | + NOT _generated_srv_rs_files STREQUAL "" OR |
| 160 | + NOT _generated_action_rs_files STREQUAL "" |
| 161 | + ) |
| 162 | + # TODO(esteve): add linters for Rust files |
| 163 | + endif() |
| 164 | +endif() |
0 commit comments