Comprehensive Guide to RPM Spec File Macros: %setup, %buildroot and Core Directives


3 views

For RPM spec file macros like %setup and %buildroot that aren't defined in /usr/lib/rpm/macros, the primary reference is the rpm-build man page. You can access it using:

man rpm-build

These core macros are documented in the "MACROS" section of the man page. The rpm.org website also maintains documentation, but the man page remains the most authoritative source.

Here are the key macros you'll encounter in RPM spec files:

%setup Macro

The %setup macro handles source extraction and directory preparation. Basic usage:

%setup -q -n %{name}-%{version}

Common options:

-q    Quiet mode
-n    Specify directory name
-c    Create directory first
-D    Don't delete directory before unpacking
-T    Disable automatic unpacking

%buildroot Macro

Defines the installation root for package building:

BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root

Here's a complete spec file section demonstrating these macros:

%prep
%setup -q -c -n %{name}-%{version}

%build
%configure \
    --prefix=%{_prefix} \
    --sysconfdir=%{_sysconfdir} \
    --localstatedir=%{_localstatedir}
make %{?_smp_mflags}

%install
rm -rf %{buildroot}
make install DESTDIR=%{buildroot}

For complex scenarios, you can combine macros with conditional logic:

%if 0%{?rhel} >= 7
%setup -q -n custom-dir
%else
%setup -q
%endif

You can define your own macros in the spec file:

%define _build_cflags %{optflags} -DSPECIAL_FEATURE
  • Use rpm --eval '%{macro}' to test macro expansion
  • For debugging, add --verbose to %setup
  • Check /usr/lib/rpm/macros for system-wide definitions

Remember that RPM macro behavior can vary slightly between distributions, so always test your spec files in your target environment.


While many RPM macros are defined in /usr/lib/rpm/macros, the core spec file directives like %setup and %buildroot are actually documented in the RPM source code itself. The most authoritative reference is the rpm man page (section "MACROS"), though it requires careful reading.

Here are the most frequently used macros in RPM spec files with examples:

%setup -q -n %{name}-%{version}  # Standard unpack with quiet mode
%setup -T -D -b 1                # Skip deletion, disable patch application
%setup -c -n newdir              # Create directory before unpacking

The %setup macro handles source unpacking with numerous switches:

  • -n: Specify alternate directory name
  • -q: Quiet mode (suppress output)
  • -c: Create directory before unpacking
  • -D: Disable deleting directory before unpacking
  • -T: Disable default patch application

The %buildroot macro defines where files are staged before packaging:

%install
rm -rf %{buildroot}
mkdir -p %{buildroot}%{_bindir}
install -m 755 myapp %{buildroot}%{_bindir}/myapp

For complete macro documentation:

  1. Check man rpm (MACROS section)
  2. Examine /usr/lib/rpm/macros on your system
  3. Review RPM source headers (particularly macros.in)
  4. Consult the unofficial but excellent "Maximum RPM" book

To see macro expansions during build:

rpmbuild --eval '%{_bindir}'
rpmbuild -bp --debug specfile.spec