3.6. The %autosetup and %setup macros


You can use the %autosetup macro to unpack the source archives and apply patches automatically without manually specifying each patch by using the %patch directive.

重要

%autosetup applies patches in the order they appear in the spec file. Always consider keeping sources and patches sorted by their number in spec to avoid unexpected actions. Alternatively, avoid the numbering entirely by using %patchlist or non-numbered Patch entries.

You can also use the %setup macro to unpack the source archives for building RPM packages.

重要

Use the %autosetup macro whenever possible, instead of the %setup macro.

3.6.1. The %autosetup macro options

You can use the following %autosetup macro options to control its behavior. %autosetup also accepts all %setup macro options.

注意

You can combine the %autosetup macro options.

Expand
表 3.2. The %autosetup macro options
Macro optionDescription

-v

Use the -v option to enable the verbosity of the %autosetup macro.

-N

Use the -N option to disable automatic patch application.

-S<vcs_name>

Use the -S<vcs_name> option to specify the version control system (VCS) to use, for example, git_am, git, patch, or gendiff.

-p

Use the -p option to control patch prefix stripping. For more information, see the patch(1) man page on your system.

3.6.2. The %autopatch macro options

The %autopatch macro applies all patches in the order stated in a spec file. You can use the following %autopatch macro options to control its behavior.

注意

You can combine the %autopatch macro options.

Expand
表 3.3. The %autopatch macro options
Macro optionDescription

-v

Use the -v option to enable the verbosity of the %autopatch macro.

-q

Use the -q option to turn off warnings if no matching patches are detected.

-p

Use the -p option to control patch prefix stripping. For more information, see the patch(1) man page on your system.

-m<patch_number>

Use the -m<patch_number> option to apply a range of patches starting from the specified patch number.

-M<patch_number>

Use the -M<patch_number> to apply a range of patches up to the specified patch number.

3.6.3. The %setup macro options

You can use the following %setup macro options for controlling the unpacking of source archives.

注意

You can combine the %setup macro options.

Expand
表 3.4. The %setup macro options
Macro optionDescription

-q

Use the -q option to limit the verbosity of the %setup macro. Pass this option as the first option to the %setup macro.

-n

Use the -n option to specify the name of the directory from an expanded source code archive.

For example, you can use this option when the directory from the expanded source code archive has a different name from what is expected (%{<name>}-%{<version>}), which can lead to an error of the `%setup macro. For example, if the package name is cello, but the source code archive name is hello-1.0.tgz, and it contains the hello/ directory, the spec file must have the following contents:

Name: cello
Source0: https://example.com/%{name}/release/hello-%{version}.tar.gz
...
%prep
%setup -n hello

-c

Use the -c option to create a directory and then change to this directory before unpacking any sources, for example:

/usr/bin/mkdir -p cello-1.0
cd 'cello-1.0'

The directory is not changed after archive expansion.

-D

Use the -D option to disable deletion of the source code directory. You can use this option, for example, if the %setup macro is used several times. With the -D option, the following lines are not used:

rm -rf 'cello-1.0'

-T

Use the -T option to skip the default action of the %setup macro, which includes unpacking of the first source archive. This option removes the following line from the script:

/usr/bin/gzip -dc '/builddir/build/SOURCES/cello-1.0.tar.gz' | /usr/bin/tar -xvvof -

-b

Use the -b (before) option to expand specific sources before entering the working directory.

For example, examples are provided in a separate cello-1.0-examples.tar.gz archive, which expands into cello-1.0/examples. In this case, use -b 1 to expand Source1 before entering the working directory:

Source0: https://example.com/%{name}/release/%{name}-%{version}.tar.gz
Source1: %{name}-%{version}-examples.tar.gz
...
%prep
%setup -b 1

-a

Use the -a (after) to expand specific sources after entering the working directory. The argument is a source number from the spec file preamble section.

For example, the cello-1.0.tar.gz archive contains an empty examples directory. The examples are shipped in a separate examples.tar.gz archive, and they expand into the directory with the same name. In this case, use -a 1 to expand Source1 after entering the working directory:

Source0: https://example.com/%{name}/release/%{name}-%{version}.tar.gz
Source1: examples.tar.gz
...
%prep
%setup -a 1
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部