@@ -96,11 +96,12 @@ def download_dev_environment(self):
9696
9797 def build_dev_environment (self ):
9898 print ("Building micro-ROS dev dependencies" )
99+ self .patch_dev_sources ()
99100
100- # Fix build: Ignore rmw_test_fixture_implementation in rolling
101+ # Fix build: Ignore rmw_test_fixture_implementation in rolling, kilted, lyrical
101102 touch_command = ''
102- if self .distro in ('rolling' , 'kilted' ):
103- touch_command = 'touch src/ament_cmake_ros/rmw_test_fixture_implementation/COLCON_IGNORE && '
103+ if self .distro in ('rolling' , 'kilted' , 'lyrical' ):
104+ touch_command = 'touch src/ament_cmake_ros/rmw_test_fixture_implementation/COLCON_IGNORE src/ament_cmake_ros/rmw_test_fixture/COLCON_IGNORE && '
104105
105106 command = "cd {} && {} . {} && colcon build --cmake-args -DBUILD_TESTING=OFF -DPython3_EXECUTABLE=`which python`" .format (self .dev_folder , touch_command , self .python_env )
106107 result = run_cmd (command , env = self .env )
@@ -173,6 +174,7 @@ def get_repositories_from_yaml(self, yaml_file):
173174
174175 def build_mcu_environment (self , meta_file , toolchain_file , user_meta = "" ):
175176 print ("Building micro-ROS library" )
177+ self .patch_mcu_sources ()
176178
177179 common_meta_path = self .library_folder + '/metas/common.meta'
178180 colcon_command = '. {} && colcon build --merge-install --packages-ignore-regex=.*_cpp --metas {} {} {} --cmake-args -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=OFF -DTHIRDPARTY=ON -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE={} -DPython3_EXECUTABLE=`which python`' .format (self .python_env , common_meta_path , meta_file , user_meta , toolchain_file )
@@ -213,6 +215,18 @@ def package_mcu_library(self):
213215 # Copy includes
214216 shutil .copytree (self .build_folder + "/mcu/install/include" , self .includes )
215217
218+ timer_h = os .path .join (self .includes , "rclc" , "timer.h" )
219+ if os .path .exists (timer_h ):
220+ with open (timer_h , "r" ) as f_t :
221+ ct = f_t .read ()
222+ if "rclc_timer_init_default2" not in ct :
223+ ct = ct .replace (
224+ "#if __cplusplus\n }\n #endif" ,
225+ "static inline rcl_ret_t rclc_timer_init_default2(rcl_timer_t * timer, rclc_support_t * support, const uint64_t timeout_ns, const rcl_timer_callback_t callback, bool autostart) { (void)autostart; return rclc_timer_init_default(timer, support, timeout_ns, callback); }\n \n #if __cplusplus\n }\n #endif"
226+ )
227+ with open (timer_h , "w" ) as f_t :
228+ f_t .write (ct )
229+
216230 # Fix include paths
217231 include_folders = os .listdir (self .includes )
218232
@@ -235,3 +249,158 @@ def resolve_binutils_path(self):
235249 sys .exit (1 )
236250
237251 return ""
252+
253+ def patch_dev_sources (self ):
254+ all_cmake = os .path .join (self .dev_src_folder , "ament_cmake" , "ament_cmake_core" , "cmake" , "core" , "all.cmake" )
255+ if os .path .exists (all_cmake ):
256+ with open (all_cmake , "r" ) as f :
257+ ac = f .read ()
258+ if "macro(ament_target_dependencies" not in ac :
259+ macro_def = """
260+
261+ # Compatibility ament_target_dependencies macro for micro-ROS
262+ macro(ament_target_dependencies target)
263+ cmake_parse_arguments(_ARG "SYSTEM;INTERFACE;PUBLIC;PRIVATE" "" "" ${ARGN})
264+ set(_dependencies ${_ARG_UNPARSED_ARGUMENTS})
265+ foreach(_dep ${_dependencies})
266+ find_package(${_dep} QUIET)
267+ if(TARGET ${_dep})
268+ if(_ARG_INTERFACE)
269+ target_link_libraries(${target} INTERFACE ${_dep})
270+ elseif(_ARG_PUBLIC)
271+ target_link_libraries(${target} PUBLIC ${_dep})
272+ else()
273+ target_link_libraries(${target} PRIVATE ${_dep})
274+ endif()
275+ elseif(TARGET ${_dep}::${_dep})
276+ if(_ARG_INTERFACE)
277+ target_link_libraries(${target} INTERFACE ${_dep}::${_dep})
278+ elseif(_ARG_PUBLIC)
279+ target_link_libraries(${target} PUBLIC ${_dep}::${_dep})
280+ else()
281+ target_link_libraries(${target} PRIVATE ${_dep}::${_dep})
282+ endif()
283+ endif()
284+ if(${_dep}_INCLUDE_DIRS)
285+ if(_ARG_INTERFACE)
286+ target_include_directories(${target} INTERFACE ${${_dep}_INCLUDE_DIRS})
287+ elseif(_ARG_PUBLIC)
288+ target_include_directories(${target} PUBLIC ${${_dep}_INCLUDE_DIRS})
289+ else()
290+ target_include_directories(${target} PRIVATE ${${_dep}_INCLUDE_DIRS})
291+ endif()
292+ endif()
293+ if(${_dep}_LIBRARIES)
294+ if(_ARG_INTERFACE)
295+ target_link_libraries(${target} INTERFACE ${${_dep}_LIBRARIES})
296+ elseif(_ARG_PUBLIC)
297+ target_link_libraries(${target} PUBLIC ${${_dep}_LIBRARIES})
298+ else()
299+ target_link_libraries(${target} PRIVATE ${${_dep}_LIBRARIES})
300+ endif()
301+ endif()
302+ endforeach()
303+ endmacro()
304+ """
305+ with open (all_cmake , "a" ) as f :
306+ f .write (macro_def )
307+
308+ def patch_mcu_sources (self ):
309+ # 1. Patch rcutils base64.c for no thread support
310+ base64_c = os .path .join (self .mcu_src_folder , "rcutils" , "src" , "base64.c" )
311+ if os .path .exists (base64_c ):
312+ with open (base64_c , "r" ) as f :
313+ bc = f .read ()
314+ if "RCUTILS_NO_THREAD_SUPPORT" not in bc :
315+ target1 = "#ifdef _WIN32\n static INIT_ONCE base64_map_initialization_once = INIT_ONCE_STATIC_INIT;"
316+ rep1 = "#if defined(RCUTILS_NO_THREAD_SUPPORT)\n static bool base64_map_initialized = false;\n #elif defined(_WIN32)\n static INIT_ONCE base64_map_initialization_once = INIT_ONCE_STATIC_INIT;"
317+ target2 = "#else\n pthread_once(&base64_map_initialization_once, initialize_base64_map);\n #endif"
318+ rep2 = "#elif defined(RCUTILS_NO_THREAD_SUPPORT)\n if (!base64_map_initialized) { initialize_base64_map(); base64_map_initialized = true; }\n #else\n pthread_once(&base64_map_initialization_once, initialize_base64_map);\n #endif"
319+ bc = bc .replace (target1 , rep1 ).replace (target2 , rep2 )
320+ with open (base64_c , "w" ) as f :
321+ f .write (bc )
322+
323+ # 2. Patch rosidl_buffer buffer.hpp for -fno-exceptions
324+ buffer_hpp = os .path .join (self .mcu_src_folder , "rosidl" , "rosidl_buffer" , "include" , "rosidl_buffer" , "buffer.hpp" )
325+ if os .path .exists (buffer_hpp ):
326+ with open (buffer_hpp , "r" ) as f :
327+ bh = f .read ()
328+ if "__EXCEPTIONS" not in bh :
329+ orig_throw1 = ' throw std::invalid_argument("Buffer implementation must not be null");'
330+ patch_throw1 = '#if __EXCEPTIONS || defined(__cpp_exceptions)\n throw std::invalid_argument("Buffer implementation must not be null");\n #endif'
331+ bh = bh .replace (orig_throw1 , patch_throw1 )
332+
333+ orig_fn = """ void throw_if_not_cpu_backend() const
334+ {
335+ if (!cpu_impl_) {
336+ throw std::runtime_error(
337+ "Operation requires CPU backend. Current backend: " +
338+ impl_->get_backend_type() +
339+ ". Use to_vector() for explicit conversion to CPU.");
340+ }
341+ }"""
342+ patch_fn = """ void throw_if_not_cpu_backend() const
343+ {
344+ #if __EXCEPTIONS || defined(__cpp_exceptions)
345+ if (!cpu_impl_) {
346+ throw std::runtime_error(
347+ "Operation requires CPU backend. Current backend: " +
348+ impl_->get_backend_type() +
349+ ". Use to_vector() for explicit conversion to CPU.");
350+ }
351+ #endif
352+ }"""
353+ bh = bh .replace (orig_fn , patch_fn )
354+ with open (buffer_hpp , "w" ) as f :
355+ f .write (bh )
356+
357+ # 3. Patch rclc timer.c for rcl_timer_init2
358+ timer_c = os .path .join (self .mcu_src_folder , "rclc" , "rclc" , "src" , "rclc" , "timer.c" )
359+ if os .path .exists (timer_c ):
360+ with open (timer_c , "r" ) as f :
361+ tc = f .read ()
362+ if "rcl_timer_init2" not in tc :
363+ target_timer = """ rcl_ret_t rc = rcl_timer_init(
364+ timer,
365+ &support->clock,
366+ &support->context,
367+ timeout_ns,
368+ callback,
369+ (*support->allocator));"""
370+ rep_timer = """ rcl_ret_t rc = rcl_timer_init2(
371+ timer,
372+ &support->clock,
373+ &support->context,
374+ timeout_ns,
375+ callback,
376+ (*support->allocator),
377+ true);"""
378+ tc = tc .replace (target_timer , rep_timer )
379+ with open (timer_c , "w" ) as f :
380+ f .write (tc )
381+
382+ # 4. Patch rclc_lifecycle for rcl_lifecycle_state_machine_init clock parameter
383+ lc_c = os .path .join (self .mcu_src_folder , "rclc" , "rclc_lifecycle" , "src" , "rclc_lifecycle" , "rclc_lifecycle.c" )
384+ if os .path .exists (lc_c ):
385+ with open (lc_c , "r" ) as f :
386+ lcc = f .read ()
387+ if "lifecycle_clock" not in lcc :
388+ target_lc = """ rcl_ret_t rcl_ret = rcl_lifecycle_state_machine_init(
389+ state_machine,
390+ node,
391+ ROSIDL_GET_MSG_TYPE_SUPPORT(lifecycle_msgs, msg, TransitionEvent),"""
392+ rep_lc = """ static rcl_clock_t lifecycle_clock;
393+ static bool lifecycle_clock_initialized = false;
394+ if (!lifecycle_clock_initialized) {
395+ rcl_ros_clock_init(&lifecycle_clock, allocator);
396+ lifecycle_clock_initialized = true;
397+ }
398+
399+ rcl_ret_t rcl_ret = rcl_lifecycle_state_machine_init(
400+ state_machine,
401+ node,
402+ &lifecycle_clock,
403+ ROSIDL_GET_MSG_TYPE_SUPPORT(lifecycle_msgs, msg, TransitionEvent),"""
404+ lcc = "#include <rcl/time.h>\n " + lcc .replace (target_lc , rep_lc )
405+ with open (lc_c , "w" ) as f :
406+ f .write (lcc )
0 commit comments