> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/handbook/di-27-zhang-freebsd-geng-xin-yu-sheng-ji/27.10.-zai-fei-freebsd-zhu-ji-shang-jin-xing-gou-jian.md).

# 27.10.在非 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://book.bsdcn.org/handbook/di-27-zhang-freebsd-geng-xin-yu-sheng-ji/27.10.-zai-fei-freebsd-zhu-ji-shang-jin-xing-gou-jian.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
