# 7.2.使用 FLAVORS

要声明一个具有多个 flavor 的 Port，需要在其 **Makefile** 中添加 `FLAVORS`。`FLAVORS` 中的第一个 flavor 是默认的 flavor。

> **技巧**
>
> 同时定义 `FLAVOR` 为 `FLAVOR?= ${FLAVORS:[1]}` 有助于简化 **Makefile** 的逻辑。

> **重要**
>
> 为了将 flavor 与总是大写字母的选项区别开来，flavor 名称*只能*包含小写字母、数字和下划线 `_`。

**示例 1：基本的 Flavors 用法**

如果某个 Port 有一个“精简版”从属 Port，那么可以移除该从属 Port，并将该 Port 转换为 flavors，如下所示：

```sh
FLAVORS=	default lite
lite_PKGNAMESUFFIX=	-lite
[...]
.if ${FLAVOR:U} != lite
[启用非 lite 功能]
.endif
```

**示例 2：另一种基本的 Flavors 用法**

如果某个 Port 有一个 `-nox11` 从属 Port，那么可以移除该从属 Port，并将该 Port 转换为 flavors，如下所示：

```sh
FLAVORS=	x11 nox11
FLAVOR?=	${FLAVORS:[1]}
nox11_PKGNAMESUFFIX=	-nox11
[...]
.if ${FLAVOR} == x11
[启用 x11 功能]
.endif
```

**示例 3：更复杂的 Flavors 用法**

以下是经过略微修改的 [devel/libpeas](https://cgit.freebsd.org/ports/tree/devel/libpeas/) 的一段内容节选，这是一个使用了 [Python flavors](https://docs.freebsd.org/en/books/porters-handbook/flavors/#flavors-auto-python) 的 Port。由于默认的 Python 2 和 3 版本是 2.7 和 3.6，它会自动获得 `FLAVORS=py27 py36`

```makefile
USES=		gnome python
USE_PYTHON=	flavors

.if ${FLAVOR:Upy27:Mpy2*}
USE_GNOME=	pygobject3

CONFIGURE_ARGS+=	--enable-python2 --disable-python3

BUILD_WRKSRC=	${WRKSRC}/loaders/python
INSTALL_WRKSRC=	${WRKSRC}/loaders/python
.else # py3*
USE_GNOME+=	py3gobject3

CONFIGURE_ARGS+=	--disable-python2 --enable-python3 \
			ac_cv_path_PYTHON3_CONFIG=${LOCALBASE}/bin/python${PYTHON_VER}-config

BUILD_WRKSRC=	${WRKSRC}/loaders/python3
INSTALL_WRKSRC=	${WRKSRC}/loaders/python3
.endif

py34_PLIST=	${.CURDIR}/pkg-plist-py3
py35_PLIST=	${.CURDIR}/pkg-plist-py3
py36_PLIST=	${.CURDIR}/pkg-plist-py3
```

这个 Port 没有使用 `USE_PYTHON=distutils`，但仍然需要 Python flavors。为了防止 `FLAVOR` 为空（这会导致 [make(1)](https://man.freebsd.org/cgi/man.cgi?query=make\&sektion=1\&format=html) 出错），应在字符串比较中使用 `${FLAVOR:U}` 而不是 `${FLAVOR}`。Gnome 的 Python gobject3 绑定在 Python 2 和 Python 3 中名称不同，分别为 pygobject3 和 py3gobject3。虽然 `configure` 脚本必须在 **${WRKSRC}** 中运行，但我们只关心构建和安装 Python 2 或 Python 3 部分，因此需要相应地设置构建和安装的基础目录。同时需要提示正确的 Python 3 config 脚本路径名。使用 Python 3 构建时的打包清单是不同的，由于可能有三个 Python 3 版本，因此要使用 [辅助机制](https://docs.freebsd.org/en/books/porters-handbook/flavors/#flavors-using-helpers) 为这三个版本设置 `PLIST`。

## 7.2.1. Flavors 辅助机制

为了简化 **Makefile** 的编写，提供了一些 flavors 的辅助机制。

以下这些辅助机制会**设置**对应变量的值：

* `flavor_PKGNAMEPREFIX`
* `flavor_PKGNAMESUFFIX`
* `flavor_PLIST`
* `flavor_DESCR`

以下这些辅助机制会**追加**到对应变量中：

* `flavor_CONFLICTS`
* `flavor_CONFLICTS_BUILD`
* `flavor_CONFLICTS_INSTALL`
* `flavor_PKG_DEPENDS`
* `flavor_EXTRACT_DEPENDS`
* `flavor_PATCH_DEPENDS`
* `flavor_FETCH_DEPENDS`
* `flavor_BUILD_DEPENDS`
* `flavor_LIB_DEPENDS`
* `flavor_RUN_DEPENDS`
* `flavor_TEST_DEPENDS`

**示例 4：为特定 flavor 设置 `PKGNAME`**

由于所有的包必须具有不同的包名，因此 flavors 必须修改各自的包名，使用 `flavor_PKGNAMEPREFIX` 和 `flavor_PKGNAMESUFFIX` 可以轻松实现：

```makefile
FLAVORS=	normal lite
lite_PKGNAMESUFFIX=	-lite
```


---

# 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-7-zhang-flavors/7.2.-shi-yong-flavors.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.
