# 26.9.在非 FreeBSD 主机上进行构建

以往，构建 FreeBSD 需要一台 FreeBSD 主机。如今，可以在 Linux 和 macOS 上构建 FreeBSD。

为了在非 FreeBSD 主机上构建，推荐使用 `tools/build/make.py` 脚本。该脚本充当 FreeBSD 使用的 `bmake`（make 实现）的包装器。它确保必要的工具链（包括 FreeBSD 的 [make(1)](https://man.freebsd.org/cgi/man.cgi?query=make\&sektion=1\&format=html)）被引导，并且构建环境被正确配置。特别地，它设置了外部工具链变量，如 `XCC`、`XLD` 等。此外，该脚本还可以将任何额外的命令参数传递给 `bmake`，例如并行构建的 `-j 4` 或特定的 make 目标。

> **注意**
>
> 也可以使用一个较新的 `bmake` 版本来代替 `tools/build/make.py` 脚本。不过，在这种情况下，必须手动设置所需的环境变量（获取环境变量列表的最简单方法是运行 `tools/build/make.py --debug`）。

否则，构建 FreeBSD 的先决条件相当简单。实际上，它只需要安装一些依赖项。

在 macOS 上，唯一的依赖项是 LLVM。可以通过包管理器（例如 [Homebrew](https://brew.sh/)）安装所需的依赖项：

```sh
brew install llvm
```

在 Linux 发行版上，安装 [Clang](https://clang.llvm.org/) 10.0 或更高版本，以及 libarchive 和 libbz2 的头文件（通常作为 libarchive-dev 和 libbz2-dev 打包）。

安装依赖项后，主机应该能够构建 FreeBSD。

例如，以下 `tools/build/make.py` 调用将构建世界：

```sh
MAKEOBJDIRPREFIX=/tmp/obj tools/build/make.py -j 8 TARGET=arm64 TARGET_ARCH=aarch64 buildworld
```

它将在 8 个 CPU 上为目标 `aarch64:arm64` 构建世界，并将对象文件存储在 **/tmp/obj** 目录下。请注意，在非 FreeBSD 主机上构建时，变量 `MAKEOBJDIRPREFIX`、`TARGET` 和 `TARGET_ARCH` 是必需的。此外，确保创建由 `MAKEOBJDIRPREFIX` 环境变量指向的对象目录。

有关更多详细信息，请参阅 [arch(7)](https://man.freebsd.org/cgi/man.cgi?query=arch\&sektion=7\&format=html) 和 [build(7)](https://man.freebsd.org/cgi/man.cgi?query=build\&sektion=7\&format=html)。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.bsdcn.org/hanbook/di-26-zhang-freebsd-geng-xin-yu-sheng-ji/26.9.-zai-fei-freebsd-zhu-ji-shang-jin-xing-gou-jian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
