The %autosetup and %setup macros

Use the %autosetup macro to unpack the source archives and apply patches automatically instead of manually specifying each patch by using the %patch directive.

Important
%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.

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

The %autosetup macro options

Use the following %autosetup macro options to control its behavior. %autosetup also accepts all %setup macro options.

Note
You can combine the %autosetup macro options.

For more information, see the patch(1) man page on your system.

Table 1. The %autosetup macro options
Macro option Description

-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.

The %autopatch macro options

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

Note
You can combine the %autopatch macro options.

For more information, see the patch(1) man page on your system.

Table 2. The %autopatch macro options
Macro option Description

-v

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

-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.

The %setup macro options

Use the following %setup macro options for controlling the unpacking of source archives.

Note
You can combine the %setup macro options.
Table 3. The %setup macro options
Macro option Description

-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