You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Meson跨平台将资源文件夹复制到构建目录?

跨平台复制资源目录到构建目录的Meson实现

方案一:使用Meson内置的fs.copy函数(推荐,要求Meson ≥ 0.60.0)

Meson 0.60.0及以上版本提供了跨平台的fs.copy函数,支持递归复制目录,无需依赖系统命令:

# 将源码目录下的demo/res完整复制到当前构建目录的res文件夹
fs.copy(meson.source_root() / 'demo/res', meson.current_build_dir() / 'res', recursive: true)

该方法由Meson内部处理不同系统的复制逻辑,完全跨平台,写法简洁可靠。

方案二:兼容旧版本Meson的跨平台自定义目标

如果项目需要兼容0.60.0以下的Meson版本,可以通过判断宿主系统选择对应命令:

# 根据系统选择复制命令
if host_system == 'windows'
    # Windows使用xcopy,/E递归复制子目录,/Y覆盖文件不提示
    copy_cmd = ['cmd.exe', '/c', 'xcopy', '/E', '/Y', meson.source_root() + '/demo/res', meson.current_build_dir() + '/res\\']
else
    # 类Unix系统使用cp -r
    copy_cmd = ['cp', '-r', meson.source_root() + '/demo/res', meson.current_build_dir()]
endif

# 创建自定义目标触发复制
demo_res = custom_target('demo-res',
    input: meson.source_root() / 'demo/res',
    output: 'res',
    command: copy_cmd,
    install: false,
    build_by_default: true,
    depends: files('demo/res'), # 监听目录内容变化,确保更新时重新构建
)

注意:Windows下xcopy的目标路径末尾必须加反斜杠\,否则会弹出询问是文件还是目录的提示。

内容的提问来源于stack exchange,提问作者Kagetsuki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 19:57:51