improve: simplify xmake.lua

This commit is contained in:
Jelly_Candy committed 2026-11-23 17:46:05 +08:00
1 parent 4502296915
commit 06237c103a
4 files changed
+217 -563

No files matched your search

+8 -231
View File
@@ -1,17 +1,18 @@
if not make_ros_config then
includes("../../xmake/ros2.lua")
end
add_requires("pkgconfig::eigen3", {system = true}) add_requires("pkgconfig::eigen3", {system = true})
add_cxxflags("-Wall", "-Wextra") add_cxxflags("-Wall", "-Wextra")
if is_mode("release") then if is_mode("release") then
set_optimize("faster") set_optimize("faster")
end end
-- ============================================================================ -- ============================================================================
-- Field Localization -- Field Localization
-- ============================================================================ -- ============================================================================
-- ============================================================================ local ros_common = make_ros_config({
-- ROS 2 dependency configuration
-- ============================================================================
local ros_direct_packages = {
"rclcpp", "rclcpp",
"std_srvs", "std_srvs",
"tf2", "tf2",
@@ -19,9 +20,7 @@ local ros_direct_packages = {
"nav_msgs", "nav_msgs",
"geometry_msgs", "geometry_msgs",
"tf2_ros", "tf2_ros",
} }, {
local message_packages = {
"statistics_msgs", "statistics_msgs",
"rosgraph_msgs", "rosgraph_msgs",
"rcl_interfaces", "rcl_interfaces",
@@ -34,229 +33,7 @@ local message_packages = {
"std_msgs", "std_msgs",
"tf2_msgs", "tf2_msgs",
"builtin_interfaces", "builtin_interfaces",
} })
local ros_config_cache = nil
local function ros_common()
on_load(function(target)
local function get_ros_config()
if ros_config_cache then
return ros_config_cache
end
-- Discover ROS installation.
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
else
local ament_prefix_path = os.getenv("AMENT_PREFIX_PATH")
ROS = nil
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
ROS = prefix
break
end
end
end
if not ROS then
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
ROS = candidate
end
end
end
if not ROS then
raise(
"ROS installation not found. "
.. "Source ROS setup or set ROS_PREFIX / ROS_DISTRO."
)
end
end
-- Resolve ROS include directories.
local script = path.join(
target:scriptdir(),
"../../tools/_internal/resolve_ros_includes.py"
)
if not os.isfile(script) then
raise("ROS include resolver not found: " .. script)
end
local output_file = path.join(
os.tmpdir(),
"field_localization_ros_include_dirs.txt"
)
local error_file = path.join(
os.tmpdir(),
"field_localization_ros_include_errors.txt"
)
os.tryrm(output_file)
os.tryrm(error_file)
os.execv("python3", {
script,
"--prefix", ROS,
"--roots", table.concat(ros_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()
if output == "" then
raise("ROS dependency resolver returned empty output.")
end
-- Collect and deduplicate include directories.
local project_include = path.join(
target:scriptdir(),
"include"
)
local includes = {project_include}
local include_seen = {
[project_include] = true
}
for line in output:gmatch("[^\r\n]+") do
line = line:gsub("^%s+", "")
line = line:gsub("%s+$", "")
if line ~= "" and not include_seen[line] then
table.insert(includes, line)
include_seen[line] = true
end
end
-- Collect message typesupport libraries.
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
-- ROS runtime libraries.
local runtime_libraries = {
"rclcpp",
"rcl",
"rcl_action",
"tf2",
"tf2_ros",
"rosidl_typesupport_cpp",
"rosidl_typesupport_c",
"rosidl_runtime_c",
"rmw",
"rmw_implementation",
"rmw_dds_common",
"rcutils",
"rcpputils",
"tracetools",
"rcl_logging_interface",
"rcl_yaml_param_parser",
"libstatistics_collector",
}
for _, name in ipairs(runtime_libraries) do
if os.isfile(ROS .. "/lib/lib" .. name .. ".so") then
table.insert(libs, name)
end
end
ros_config_cache = {
prefix = ROS,
includes = includes,
libs = libs,
}
return ros_config_cache
end
local config = get_ros_config()
local project_include = path.join(target:scriptdir(), "include")
for _, dir in ipairs(config.includes) do
if dir ~= project_include then target:add("sysincludedirs", dir) end
end
target:add("includedirs", project_include)
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
-- ============================================================================ -- ============================================================================
-- Core library: independent of ROS 2 -- Core library: independent of ROS 2
+7 -332
View File
@@ -1,29 +1,26 @@
if not make_ros_config then
includes("../../xmake/ros2.lua")
end
-- ============================================================================ -- ============================================================================
-- ROS 2 dependency configuration -- Sentry Navigation
-- ============================================================================ -- ============================================================================
-- PCL dependency, discovered through pkg-config. -- PCL dependency, discovered through pkg-config.
add_requires("pkgconfig::pcl_common", {system = true}) add_requires("pkgconfig::pcl_common", {system = true})
-- 1. ROS packages directly included by project source files. local ros_common = make_ros_config({
-- Transitive dependencies are resolved through ament package metadata.
local ros_direct_packages = {
"rclcpp", "rclcpp",
"rclcpp_action", "rclcpp_action",
"nav2_msgs", "nav2_msgs",
"nav_msgs", "nav_msgs",
"geometry_msgs", "geometry_msgs",
"sensor_msgs", "sensor_msgs",
"std_msgs",
"tf2_ros", "tf2_ros",
"tf2_sensor_msgs", "tf2_sensor_msgs",
"pcl_conversions", "pcl_conversions",
"action_msgs", "action_msgs",
} }, {
-- 2. ROS message typesupport libraries.
local message_packages = {
"nav2_msgs", "nav2_msgs",
"sensor_msgs", "sensor_msgs",
"nav_msgs", "nav_msgs",
@@ -38,313 +35,7 @@ local message_packages = {
"service_msgs", "service_msgs",
"type_description_interfaces", "type_description_interfaces",
"statistics_msgs", "statistics_msgs",
} })
-- 3. Shared ROS configuration cache.
-- All ROS targets share the same dependency resolution result.
local ros_config_cache = nil
-- ============================================================================
-- Common ROS target configuration
-- ============================================================================
local function ros_common()
on_load(function(target)
-- --------------------------------------------------------------------
-- Resolve ROS configuration.
--
-- IMPORTANT:
-- This function is intentionally defined inside on_load().
-- This ensures that os/path/io APIs are resolved in the script domain.
-- --------------------------------------------------------------------
local function get_ros_config()
if ros_config_cache then
return ros_config_cache
end
-- Discover ROS installation 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
else
local ament_prefix_path = os.getenv("AMENT_PREFIX_PATH")
ROS = nil
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
ROS = prefix
break
end
end
end
-- 再退一步:按发行版名找 /opt/ros/<发行版>。
-- 这样换 ROS 版本不用改构建文件,而且**不写死任何发行版名字**
-- ——ROS_DISTRO 是 ROS 自己的 setup.bash 导出的。
if not ROS then
local distro = os.getenv("ROS_DISTRO")
if distro and distro ~= "" then
local cand = "/opt/ros/" .. distro
if os.isdir(cand .. "/include")
and os.isdir(cand .. "/lib") then
ROS = cand
end
end
end
if not ROS then
raise(
"ROS installation not found.\n"
.. " 办法一:source 你的 ROS 环境再 xmake"
.. "(会读 AMENT_PREFIX_PATH)\n"
.. " 办法二:ROS_PREFIX=<ROS 安装前缀> xmake\n"
.. " 办法三:ROS_DISTRO=<发行版名> xmake"
.. "(会去 /opt/ros/<名字> 找)"
)
end
end
-- ----------------------------------------------------------------
-- Resolve include directories through ament metadata.
-- ----------------------------------------------------------------
local script = path.join(
target:scriptdir(),
"../../tools/_internal/resolve_ros_includes.py"
)
if not os.isfile(script) then
raise(
"ROS include resolver not found: " .. script
)
end
local output_file = path.join(
os.tmpdir(),
"sentry_nav_ros_include_dirs.txt"
)
local error_file = path.join(
os.tmpdir(),
"sentry_nav_ros_include_errors.txt"
)
-- Remove stale output before executing the resolver.
os.tryrm(output_file)
os.tryrm(error_file)
-- Execute Python dependency resolver.
os.execv("python3", {
script,
"--prefix", ROS,
"--roots", table.concat(ros_direct_packages, ","),
}, {
stdout = output_file,
stderr = error_file,
})
-- Read generated include directories.
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 read ROS dependency resolver output:\n"
.. error_message
)
end
local output = file:read("*a")
file:close()
-- Read resolver diagnostics.
local errors = ""
local error_handle = io.open(error_file, "r")
if error_handle then
errors = error_handle:read("*a")
error_handle:close()
end
if output == "" then
raise(
"ROS dependency resolver returned empty output:\n"
.. errors
)
end
-- Parse and deduplicate include directories.
-- Use an absolute path to avoid Xmake resolving it from the project root.
local project_include = path.join(
target:scriptdir(),
"include"
)
local ros_includes = {project_include}
local include_seen = {
[project_include] = true
}
for line in output:gmatch("[^\r\n]+") do
line = line:gsub("^%s+", "")
line = line:gsub("%s+$", "")
if line ~= "" and not include_seen[line] then
table.insert(ros_includes, line)
include_seen[line] = true
end
end
if #ros_includes <= 1 then
raise(
"ROS dependency resolver returned no include directories."
)
end
-- ----------------------------------------------------------------
-- Collect ROS message typesupport libraries.
-- ----------------------------------------------------------------
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
-- ----------------------------------------------------------------
-- Collect ROS runtime libraries.
-- ----------------------------------------------------------------
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(runtime_libraries) do
if os.isfile(
ROS .. "/lib/lib" .. name .. ".so"
) then
table.insert(libs, name)
end
end
-- Cache resolved configuration.
ros_config_cache = {
prefix = ROS,
includes = ros_includes,
libs = libs,
}
return ros_config_cache
end
-- --------------------------------------------------------------------
-- Apply ROS configuration to current target.
-- --------------------------------------------------------------------
local config = get_ros_config()
local project_include = path.join(target:scriptdir(), "include")
for _, dir in ipairs(config.includes) do
if dir ~= project_include then target:add("sysincludedirs", dir) end
end
target:add("includedirs", project_include)
target:add(
"defines",
"DEFAULT_RMW_IMPLEMENTATION=rmw_fastrtps_cpp"
)
target:add(
"linkdirs",
config.prefix .. "/lib"
)
target:add(
"rpathdirs",
config.prefix .. "/lib"
)
target:add(
"linkgroups",
config.libs,
{group = true}
)
end)
end
-- ============================================================================ -- ============================================================================
-- Targets -- Targets
@@ -353,22 +44,6 @@ end
-- --------------------------------------------------------------------------- -- ---------------------------------------------------------------------------
-- 统一产物目录:build/bin/ -- 统一产物目录:build/bin/
-- --------------------------------------------------------------------------- -- ---------------------------------------------------------------------------
-- 为什么不用 xmake 默认的 build/<平台>/<架构>/<模式>/:
-- 那一串平台三元组会被抄进文档、脚本、报错提示里,一旦换架构 / 换 OS /
-- 换构建模式(release↔debug)就全部失效 —— 本工程已经因为这件事
-- 反复出现「文档里的路径跑不通」。
-- 固定成 build/bin/ 之后,文档里可以永远写 build/bin/nav_client。
--
-- 取舍:release 和 debug 会落在同一层,覆盖同一个文件。
-- 要同时保留两种,用 xmake 的 -o 换构建目录,或者临时改这里。
--
-- 实现上**每个 target 里各写一行 `set_targetdir("$(builddir)/bin")`**。
-- 试过包成 `nav_target()` helper,但 xmake 的 `target()` 不返回目标实例
-- (返回 nil),那种写法会报 `attempt to index a nil value`。
--
-- ⚠️ **新增目标时必须自己补这一行**,否则它会掉回默认的
-- `build/<平台>/<架构>/<模式>/`,和其它的分家。
-- ---- ROS-independent algorithm core ---- -- ---- ROS-independent algorithm core ----
+3
View File
@@ -41,6 +41,9 @@ add_cxxflags("-Wall", "-Wextra")
-- 详见 .vscode/README.md。 -- 详见 .vscode/README.md。
add_rules("plugin.compile_commands.autoupdate", { outputdir = ".", lsp = "clangd" }) add_rules("plugin.compile_commands.autoupdate", { outputdir = ".", lsp = "clangd" })
-- ROS 2 构建依赖公共解析模块
includes("xmake/ros2.lua")
-- 真正的目标都在子目录里(与源码放在一起,加文件时不用来回跳)。 -- 真正的目标都在子目录里(与源码放在一起,加文件时不用来回跳)。
includes("src/sentry_nav") includes("src/sentry_nav")
includes("src/field_localization") includes("src/field_localization")
+199
View File
@@ -0,0 +1,199 @@
-- ============================================================================
-- 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