---
url: /zh/posts/xmake-update-v3.1.3.md
---

Xmake v3.1.3 新增 QML AOT 与类型编译、moc 头文件自动检测、外部 C# 程序集引用，
以及默认测试选择等功能。

本次还改进了 C++ Modules 和 Verilator 的增量构建，修复包安装失败后卡死的问题，
并支持亚秒精度的文件时间戳。

## Qt/QML 支持

### QML 模块与 AOT 编译

新的 `qt_add_qml_module` 将 QML 文件和资源集成到 Qt Quick 应用中：

```lua
add_rules("mode.debug", "mode.release")
includes("@builtin/qt")

target("qmlapp")
    add_rules("qt.quickapp")
    add_files("main.cpp")
    qt_add_qml_module("*.qml", "*.js", "assets/*.svg", {
        uri = "MyApp",
        version = "1.0"
    })
```

默认使用 `qmlcachegen` 生成并编译缓存 C++ 源文件，需要 Qt >= 6.2 和对应工具。
例如 `MyApp` URI 对应 `/qt/qml/MyApp/` 资源前缀，应用可加载
`qrc:/qt/qml/MyApp/Main.qml`。

Qt >= 6.4 时可设置 `{type_compiler = true}` 开启 `qmltc`。Xmake 自动生成所需的 `qmldir`，
并处理生成的 C++ 源文件及 moc 步骤。Qt >= 6.8 且 SDK 支持时，可通过 `{aot_stats = true}`
要求生成 AOT 统计；不设置此选项则自动检测能力。

这些选项分别控制不同环节：`cachegen = false` 关闭缓存生成，`only_bytecode = true` 请求只生成字节码，
AOT 报告则需要启用原生 AOT 编译。选项及可运行工程代码见
[Qt 示例](/zh/examples/cpp/graphics/qt#qml-modules-and-aot-compilation)。
相关改动：[#7885](https://github.com/xmake-io/xmake/pull/7885)、
[#7900](https://github.com/xmake-io/xmake/pull/7900)、[#7903](https://github.com/xmake-io/xmake/pull/7903)。

### 自动检测 moc 头文件

启用 automoc 后，不必再为每个 Qt 头文件单独填写 `add_files`：

```lua
target("widgets")
    add_rules("qt.widgetapp")
    set_policy("build.qt.automoc", true)
    add_files("src/*.cpp")
    add_headerfiles("src/*.h")
    add_includedirs("src")
```

Xmake 先识别 Qt 元对象宏，再匹配同名源文件，并为剩余候选跟踪本地传递包含关系。
扫描会处理循环引用和不同目录的同名头文件，不会递归进入包的头文件树。
该策略默认关闭，原有显式 moc 配置保持有效。
参阅 [#7899](https://github.com/xmake-io/xmake/pull/7899)、
[#7924](https://github.com/xmake-io/xmake/pull/7924) 和
[策略参考](/zh/api/description/builtin-policies#build-qt-automoc)。

## 外部 C# 程序集引用

对于预编译的托管 DLL，现在可以使用熟悉的链接配置接口：

```lua
target("app")
    set_kind("binary")
    add_files("src/*.cs")
    add_linkdirs("lib")
    add_links("MyLib")
```

存在 `lib/MyLib.dll` 时，Xmake 生成带 `HintPath` 的程序集引用，将 DLL 加入构建依赖，
并向 vsxmake 导出按配置区分的引用。如果没有在指定目录中找到 DLL，则由 MSBuild 尝试框架/GAC 解析。
其他 C# 目标仍使用 `add_deps`，NuGet 包沿用原有集成方式。

参阅 [#7725](https://github.com/xmake-io/xmake/pull/7725)、
[#7923](https://github.com/xmake-io/xmake/pull/7923) 和
[外部程序集示例](/zh/examples/other-languages/csharp#external-assemblies)。

## 默认测试选择

启用 `test.respect_default` 后，直接执行 `xmake test` 时会排除非默认目标的测试。
该策略默认关闭：

```lua
set_policy("test.respect_default", true)

target("slow_tests")
    set_kind("binary")
    set_default(false)
    add_files("tests/slow.cpp")
    add_tests("default")
```

需要运行所有测试时，使用 `xmake test -a` 或 `xmake test --all`。
显式测试名称和组筛选也能选择非默认测试，测试级的 `default` 显式配置具有更高优先级。
参阅 [#7910](https://github.com/xmake-io/xmake/pull/7910) 和
[测试指南](/zh/guide/basic-commands/run-tests#default-test-selection)。

## 增量构建改进

生成 `compile_commands.json` 不应导致后续构建重编未变化的 GCC 模块。
现在 mapper 文件只有在内容发生变化时才写入；编译数据库也会包含非默认目标，重新配置时能正确清理失效模块缓存。
其他修复涉及 GCC 子目录头文件单元、clang-cl 模块复用与实现单元调度，以及 Clang 回退扫描使用的语言标准参数。
参阅 [#7921](https://github.com/xmake-io/xmake/pull/7921)、
[#7871](https://github.com/xmake-io/xmake/pull/7871)、[#7893](https://github.com/xmake-io/xmake/pull/7893)、
[#7882](https://github.com/xmake-io/xmake/pull/7882)、[#7902](https://github.com/xmake-io/xmake/pull/7902)、
[#7872](https://github.com/xmake-io/xmake/pull/7872)。

文件时间 API 会在平台支持时保留小数秒，缓存序列化也会保留浮点数精度。
这样，同一秒内发生的源文件修改也能参与依赖判断，实际分辨率仍取决于文件系统。
参阅 [#7879](https://github.com/xmake-io/xmake/pull/7879) 和
[os.mtime](/zh/api/scripts/builtin-modules/os#os-mtime)。

## 其他改动

* Verilator 支持 `.vlt` 控制文件，并修复反复触发增量重建的问题。
  参阅 [#7904](https://github.com/xmake-io/xmake/pull/7904)、[#7901](https://github.com/xmake-io/xmake/pull/7901)。
* Wasm release 模式默认优先优化体积，目标显式配置的优化等级仍优先。
  参阅 [#7887](https://github.com/xmake-io/xmake/pull/7887)。
* vsxmake 支持文件级 C/C++ 语言标准，并修复命名空间目标的工程引用。
  参阅 [#7912](https://github.com/xmake-io/xmake/pull/7912)、[#7926](https://github.com/xmake-io/xmake/pull/7926)。
* 修复 MSVC ARM64 链接参数检测、Windows SDK 主机工具选择、Qt Linux 打包，
  以及目标级 `install.strip_packagelibs` 配置。
  参阅 [#7859](https://github.com/xmake-io/xmake/pull/7859)、[#7881](https://github.com/xmake-io/xmake/pull/7881)、
  [#7892](https://github.com/xmake-io/xmake/pull/7892)、[#7907](https://github.com/xmake-io/xmake/pull/7907)。
* 后台主机探测不再在工程目录中留下 wget 日志文件。
  参阅 [#7876](https://github.com/xmake-io/xmake/pull/7876)。

## 更新内容

### 新特性

* [#7885](https://github.com/xmake-io/xmake/pull/7885): 支持通过 `qmlcachegen` 进行 QML AOT 编译
* [#7900](https://github.com/xmake-io/xmake/pull/7900): 支持 QML 类型编译器 `qmltc`
* [#7903](https://github.com/xmake-io/xmake/pull/7903): 支持通过 `qmlaotstats` 生成 QML AOT 统计
* [#7899](https://github.com/xmake-io/xmake/pull/7899): 添加 `build.qt.automoc` 策略，支持自动检测 Qt moc 头文件
* [#7725](https://github.com/xmake-io/xmake/pull/7725), [#7923](https://github.com/xmake-io/xmake/pull/7923): 支持通过 `add_links` 和 `add_linkdirs` 引用外部 C# 程序集
* [#7904](https://github.com/xmake-io/xmake/pull/7904): 支持 Verilator 的 `.vlt` 控制文件
* [#7910](https://github.com/xmake-io/xmake/pull/7910): 添加 `test.respect_default` 策略和 `xmake test --all`
* [#7912](https://github.com/xmake-io/xmake/pull/7912): 支持在 vsxmake 工程中设置文件级 C/C++ 语言标准

### 改进

* [#7879](https://github.com/xmake-io/xmake/pull/7879): 保留文件时间戳的亚秒精度及浮点数序列化精度
* [#7887](https://github.com/xmake-io/xmake/pull/7887): WebAssembly 的 release 模式默认优先优化体积
* [#7867](https://github.com/xmake-io/xmake/pull/7867): 为 Emscripten 复用 Clang 编译参数处理
* [#7863](https://github.com/xmake-io/xmake/pull/7863): 扩展 C/C++ 编译器名称的兼容映射
* [#7866](https://github.com/xmake-io/xmake/pull/7866): 避免自动探测不可用的 Ascend C 工具链时输出警告
* [#7890](https://github.com/xmake-io/xmake/pull/7890): 添加 QML 模块示例和回归测试
* [#7874](https://github.com/xmake-io/xmake/pull/7874): 修正构建目录的 Git 忽略规则
* [#7913](https://github.com/xmake-io/xmake/pull/7913): 修复 README 中的包管理演示图片

### Bug 修复

* [#7870](https://github.com/xmake-io/xmake/pull/7870): 修复 GCC 模块 mapper 文件中的根目录配置
* [#7882](https://github.com/xmake-io/xmake/pull/7882): 修复 GCC 子目录头文件单元及相对包含路径
* [#7850](https://github.com/xmake-io/xmake/pull/7850), [#7902](https://github.com/xmake-io/xmake/pull/7902): 修复 clang-cl 模块复用及实现单元的构建调度
* [#7889](https://github.com/xmake-io/xmake/pull/7889): 修复 clang-cl 的 reduced BMI 参数传递
* [#7893](https://github.com/xmake-io/xmake/pull/7893): 重新配置时清理失效的 C++ 模块缓存
* [#7895](https://github.com/xmake-io/xmake/pull/7895): 修复 C++ 模块对 Emscripten 编译器的识别
* [#7871](https://github.com/xmake-io/xmake/pull/7871): 生成编译数据库时包含非默认构建目标
* [#7921](https://github.com/xmake-io/xmake/pull/7921): 避免生成编译数据库后无效重编 GCC 模块
* [#7872](https://github.com/xmake-io/xmake/pull/7872): 保留 Clang 模块回退扫描所需的语言标准
* [#7872](https://github.com/xmake-io/xmake/pull/7872): 修复 Linux 下链接 libc++ 时缺失 ABI 符号的问题
* [#7872](https://github.com/xmake-io/xmake/pull/7872): 更新内置 tbox，修复 Unix 子进程轮询
* [#7881](https://github.com/xmake-io/xmake/pull/7881): 按主机架构选择 Windows SDK 工具
* [#7859](https://github.com/xmake-io/xmake/pull/7859): 修复 MSVC 交叉编译到 ARM64 时的链接参数检测
* [#7898](https://github.com/xmake-io/xmake/pull/7898): 修复依赖包或串行包安装失败后卡死的问题
* [#7876](https://github.com/xmake-io/xmake/pull/7876): 避免后台主机探测生成 wget 日志文件
* [#7883](https://github.com/xmake-io/xmake/pull/7883), [#7901](https://github.com/xmake-io/xmake/pull/7901): 修复 Verilator 增量编译时反复重建的问题
* [#7892](https://github.com/xmake-io/xmake/pull/7892): 修复 Linux 下 xpack 打包遗漏 Qt 可执行文件的问题
* [#7907](https://github.com/xmake-io/xmake/pull/7907): 正确应用目标级 `install.strip_packagelibs` 配置
* [#7909](https://github.com/xmake-io/xmake/pull/7909): 修复格式化插件的文件收集、去重和 Qt 规则匹配
* [#7924](https://github.com/xmake-io/xmake/pull/7924): 修复 Qt automoc 对间接包含头文件的检测
* [#7926](https://github.com/xmake-io/xmake/pull/7926): 修复 vsxmake 对命名空间目标的工程引用
* [#7920](https://github.com/xmake-io/xmake/pull/7920): 更新内置 PDCurses，修复新版 MSVC 下的编译错误
