# 10.6.PREFIX 和 DESTDIR

`PREFIX` 决定了 Port 的安装位置。默认情况下，它是 **/usr/local**，但用户可以设置为自定义路径，如 **/opt**。Port 必须遵守该变量的值。

`DESTDIR` 如果由用户设置，决定了完整的替代环境，通常是 jail 或者是挂载在 **/** 以外的安装系统。Port 实际上会安装到 **DESTDIR/PREFIX**，并在 **DESTDIR/var/db/pkg** 中注册到包数据库。`DESTDIR` 由 Ports 基础设施通过 [chroot(8)](https://man.freebsd.org/cgi/man.cgi?query=chroot\&sektion=8\&format=html) 自动处理。无需修改或额外注意编写 `DESTDIR` 合规的 Ports。

`PREFIX` 的值将被设置为 `LOCALBASE`（默认值为 **/usr/local**）。如果设置了 `USE_LINUX_PREFIX`，`PREFIX` 将是 `LINUXBASE`（默认值为 **/compat/linux**）。

避免在源代码中硬编码 **/usr/local** 路径，可以使 Port 更加灵活，能够满足其他站点的需求。通常可以通过将 Port 中各种 **Makefile** 中的 **/usr/local** 替换为 `${PREFIX}` 来实现这一点。这个变量会自动传递到构建和安装过程的每个阶段。

确保应用程序没有将文件安装到 **/usr/local** 而不是 `PREFIX`。一种快速检查硬编码路径的方法是：

```sh
% make clean; make package PREFIX=/var/tmp/`make -V PORTNAME`
```

如果安装了任何不在 `PREFIX` 中的文件，包创建过程将提示找不到这些文件。

此外，还值得检查使用阶段目录支持（见 [Staging](https://docs.freebsd.org/en/books/porters-handbook/special/#staging)）的情况：

```sh
% make stage && make check-plist && make stage-qa && make package
```

* `check-plist` 检查 plist 中缺少的文件以及 Port 未安装但出现在 plist 中的文件。
* `stage-qa` 检查常见问题，如错误的 shebang、指向阶段目录外的符号链接、setuid 文件以及未剥离的库文件等。

这些测试不会发现 Port 文件中硬编码的路径，也不会验证 `LOCALBASE` 是否被正确使用以引用其他 Port 中的文件。必须测试临时安装在 **/var/tmp/**`make -V PORTNAME` 中的 Port，以确保路径没有问题。

`PREFIX` 不得在 Port 的 **Makefile** 中显式设置。安装 Port 的用户可能已将 `PREFIX` 设置为自定义位置，Port 必须遵守该设置。

通过上述变量引用其他 Port 的程序和文件，而不是使用显式的路径。例如，如果 Port 需要宏 `PAGER` 来获取 `less` 的完整路径名，不能使用 **/usr/local/bin/less** 的字面路径。应改为使用 `${LOCALBASE}`：

```sh
-DPAGER=\"${LOCALBASE}/bin/less\"
```

使用 `LOCALBASE` 的路径在管理员将整个 **/usr/local** 树移动到其他地方时，更有可能仍然有效。

> **技巧**
>
> 所有这些测试都在运行 `poudriere testport` 或 `poudriere bulk -t` 时自动完成。强烈建议每个 Port 贡献者安装并用其测试自己的 Port。有关更多信息，请参见 [poudriere](https://docs.freebsd.org/en/books/porters-handbook/testing/#testing-poudriere)。


---

# 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://porters-handbook.bsdcn.org/di-10-zhang-ce-shi-port/10.6.prefix-he-destdir.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.
