# 5.11.从属 port 和 MASTERDIR

如果 port 需要通过使变量（例如分辨率或纸张大小）取不同的值来构建略有不同版本的包，则为每个包创建一个子目录，以便用户更容易理解该怎么做，但尽量使尽可能多的文件在 ports 之间共享。通常，通过巧妙使用变量，除了一个目录外，其他目录只需要非常简短的 **Makefile**。在唯一的 **Makefile** 中，使用 `MASTERDIR` 来指定其他文件所在的目录。同时，使用变量作为 [`PKGNAMESUFFIX`](https://docs.freebsd.org/en/books/porters-handbook/makefiles/#porting-pkgname) 的一部分，以便使包具有不同的名称。

通过一个例子可以更好地展示这一点。以下是 **print/pkfonts300/Makefile** 的一部分：

```makefile
PORTNAME=	pkfonts${RESOLUTION}
PORTVERSION=	1.0
DISTFILES=	pk${RESOLUTION}.tar.gz

PLIST=		${PKGDIR}/pkg-plist.${RESOLUTION}

.if !defined(RESOLUTION)
RESOLUTION=	300
.else
.if ${RESOLUTION} != 118 && ${RESOLUTION} != 240 && \
	${RESOLUTION} != 300 && ${RESOLUTION} != 360 && \
	${RESOLUTION} != 400 && ${RESOLUTION} != 600
.BEGIN:
	@${ECHO_MSG} "Error: invalid value for RESOLUTION: \"${RESOLUTION}\""
	@${ECHO_MSG} "Possible values are: 118, 240, 300, 360, 400 and 600."
	@${FALSE}
.endif
.endif
```

[print/pkfonts300](https://cgit.freebsd.org/ports/tree/print/pkfonts300/) 还包含所有常规的补丁、包文件等。在该目录中运行 `make`，它将采用分辨率的默认值（300）并正常构建 port。

至于其他分辨率，以下是整个 **print/pkfonts360/Makefile**：

```makefile
RESOLUTION=	360
MASTERDIR=	${.CURDIR}/../pkfonts300

.include	"${MASTERDIR}/Makefile"
```

(**print/pkfonts118/Makefile**、**print/pkfonts600/Makefile**，以及所有其他类似的文件)。`MASTERDIR` 定义告诉 **bsd.port.mk**，像 `FILESDIR` 和 `SCRIPTDIR` 这样的常规子目录位于 **pkfonts300** 下。`RESOLUTION=360` 这一行将覆盖 **pkfonts300/Makefile** 中的 `RESOLUTION=300`，并且该 port 将以分辨率设置为 360 来构建。


---

# 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-5-zhang-pei-zhi-makefile/5.11.-cong-shu-port-he-masterdir.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.
