-- ============================================================================ -- ROS 2 Target Configuration Helper for Xmake -- ============================================================================ -- -- This module discovers the ROS 2 installation (via ROS_PREFIX, AMENT_PREFIX_PATH, -- or ROS_DISTRO) and provides helper functions to configure ROS 2 include -- directories, defines, library directories, and runtime / typesupport libraries. -- -- Usage: -- 1. In root xmake.lua: -- includes("xmake/ros2.lua") -- -- 2. In package xmake.lua: -- local ros_common = make_ros_config(direct_packages, message_packages, extra_runtime_libs) -- -- target("my_node") -- ... -- ros_common() -- ============================================================================ local ros_resolution_cache = {} -- Discover ROS 2 installation prefix local function get_ros_prefix() local ROS = os.getenv("ROS_PREFIX") if ROS and ROS ~= "" then if not os.isdir(ROS .. "/include") or not os.isdir(ROS .. "/lib") then raise("Invalid ROS_PREFIX: " .. ROS) end return ROS end local ament_prefix_path = os.getenv("AMENT_PREFIX_PATH") if ament_prefix_path then for prefix in string.gmatch(ament_prefix_path, "[^:]+") do if os.isdir(prefix .. "/include") and os.isdir(prefix .. "/lib") then return prefix end end end local distro = os.getenv("ROS_DISTRO") if distro and distro ~= "" then local candidate = "/opt/ros/" .. distro if os.isdir(candidate .. "/include") and os.isdir(candidate .. "/lib") then return candidate end end raise("ROS installation not found. Source ROS setup or set ROS_PREFIX / ROS_DISTRO.") end -- Create a reusable ros_common target configuration closure function make_ros_config(direct_packages, message_packages, extra_runtime_libs) direct_packages = direct_packages or {} message_packages = message_packages or {} extra_runtime_libs = extra_runtime_libs or {} local cache_key = table.concat(direct_packages, ",") .. "|" .. table.concat(message_packages, ",") .. "|" .. table.concat(extra_runtime_libs, ",") return function() on_load(function(target) local config = ros_resolution_cache[cache_key] if not config then local ROS = get_ros_prefix() local script = path.join(os.projectdir(), "tools/_internal/resolve_ros_includes.py") if not os.isfile(script) then script = path.join(os.projectdir(), "tools/resolve_ros_includes.py") end if not os.isfile(script) then raise("ROS include resolver not found: " .. script) end local output_file = os.tmpfile() local error_file = os.tmpfile() os.tryrm(output_file) os.tryrm(error_file) os.execv("python3", { script, "--prefix", ROS, "--roots", table.concat(direct_packages, ","), }, { stdout = output_file, stderr = error_file, }) local file = io.open(output_file, "r") if not file then local error_message = "" local error_handle = io.open(error_file, "r") if error_handle then error_message = error_handle:read("*a") error_handle:close() end raise("Failed to resolve ROS includes:\n" .. error_message) end local output = file:read("*a") file:close() os.tryrm(output_file) os.tryrm(error_file) if output == "" then raise("ROS dependency resolver returned empty output.") end local ros_includes = {} local include_seen = {} for line in output:gmatch("[^\r\n]+") do line = line:gsub("^%s+", ""):gsub("%s+$", "") if line ~= "" and not include_seen[line] then table.insert(ros_includes, line) include_seen[line] = true end end local libs = {} for _, pkg in ipairs(message_packages) do for _, suffix in ipairs({ "__rosidl_typesupport_cpp", "__rosidl_typesupport_introspection_cpp", }) do local name = pkg .. suffix if os.isfile(ROS .. "/lib/lib" .. name .. ".so") then table.insert(libs, name) end end end local runtime_libraries = { "rclcpp", "rclcpp_action", "rcl_action", "rcl", "tf2", "tf2_ros", "tf2_sensor_msgs", "tf2_geometry_msgs", "message_filters", "pcl_conversions", "rosidl_typesupport_cpp", "rosidl_typesupport_c", "rosidl_runtime_c", "rosidl_dynamic_typesupport", "rmw", "rmw_implementation", "rmw_dds_common", "rcutils", "rcpputils", "tracetools", "rcl_logging_interface", "rcl_yaml_param_parser", "libstatistics_collector", } for _, name in ipairs(extra_runtime_libs) do table.insert(runtime_libraries, name) end for _, name in ipairs(runtime_libraries) do if os.isfile(ROS .. "/lib/lib" .. name .. ".so") then table.insert(libs, name) end end config = { prefix = ROS, ros_includes = ros_includes, libs = libs, } ros_resolution_cache[cache_key] = config end -- Prioritize project local include local project_include = path.join(target:scriptdir(), "include") if os.isdir(project_include) then target:add("includedirs", project_include) end for _, dir in ipairs(config.ros_includes) do if dir ~= project_include then target:add("sysincludedirs", dir) end end target:add("defines", "DEFAULT_RMW_IMPLEMENTATION=rmw_fastrtps_cpp") target:add("linkdirs", config.prefix .. "/lib") target:add("rpathdirs", config.prefix .. "/lib") target:add("runenvs", "LD_LIBRARY_PATH", config.prefix .. "/lib") target:add("linkgroups", config.libs, {group = true}) end) end end