Macros de RPM

RPM proporciona un amplio conjunto de macros para simplificar y homogeneizar el mantenimiento de los paquetes. Por ejemplo, incluye una lista de definiciones de rutas predeterminadas que utilizan las macros del sistema de compilación, así como definiciones para los directorios específicos de compilación de paquetes RPM. Generalmente, se recomienda usar estas definiciones en lugar de directorios codificados. También proporciona el conjunto predeterminado de opciones de compilación como macros, que deben usarse al compilar manualmente y no depender de un sistema de compilación.

Obtener y configurar Macros en la línea de instrucción

Es posible dejar la evaluación de RPM de cadenas arbitrarias conteniendo macros en la línea de comando ejecutando rpm --eval en la línea de comando:

$ rpm --eval "some text printed on %{_arch}"
some text printed on x86_64

Additionally, values for macros can be temporarily provided (and overridden) by providing command line options to rpm and rpmbuild:

$ rpm --define "test Hello, World!" --eval "%{test}"
Hello, World!

Macros for paths set and used by build systems

The macros for build system invocations (for example, %configure, %cmake, or %meson) use the values defined by RPM to set installation paths for packages. So, it’s usually preferable to not hard-code these paths in spec files either, but use the same macros for consistency.

The values for these macros can be inspected by looking at /usr/lib/rpm/platform/*/macros for the respective platform.

The following table lists macros which are widely used in fedora .spec files.

macro definition comment

%{_sysconfdir}

/etc

%{_prefix}

/usr

can be defined to /app for flatpak builds

%{_exec_prefix}

%{_prefix}

default: /usr

%{_includedir}

%{_prefix}/include

default: /usr/include

%{_bindir}

%{_exec_prefix}/bin

default: /usr/bin

%{_libdir}

%{_exec_prefix}/%{_lib}

default: /usr/%{_lib}

%{_libexecdir}

%{_exec_prefix}/libexec

default: /usr/libexec

%{_datadir}

%{_datarootdir}

default: /usr/share

%{_infodir}

%{_datarootdir}/info

default: /usr/share/info

%{_mandir}

%{_datarootdir}/man

default: /usr/share/man

%{_docdir}

%{_datadir}/doc

default: /usr/share/doc

%{_rundir}

/run

%{_localstatedir}

/var

%{_sharedstatedir}

/var/lib

%{_lib}

lib64

lib on 32bit platforms

Some seldomly used macros are listed below for completeness. Old .spec files might still use them, and there might be cases where they are still needed.

macro definition comment

%{_datarootdir}

%{_prefix}/share

default: /usr/share

%{_var}

/var

%{_sbindir}

same as %{_bindir}

historically /usr/sbin, now /usr/bin, provided for compatibility

%{_tmppath}

%{_var}/tmp

default: /var/tmp

%{_usr}

/usr

%{_usrsrc}

%{_usr}/src

default: /usr/src

%{_initddir}

%{_sysconfdir}/rc.d/init.d

default: /etc/rc.d/init.d

%{_initrddir}

%{_initddir}

old misspelling, provided for compatiblity

Macros set for the RPM (and SRPM) build process

RPM also exposes the locations of several directories that are relevant to the package build process via macros.

The only macro that’s widely used in .spec files is %{buildroot}, which points to the root of the installation target directory. It is used for setting DESTDIR in the package’s %install step.

The other macros are usually only used outside .spec files. For example, they are set by fedpkg to override the default directories.

macro definition comment

%{buildroot}

%{_buildrootdir}/%{name}-%{version}-%{release}.%{_arch}

same as $BUILDROOT

%{_topdir}

%{getenv:HOME}/rpmbuild

%{_builddir}

%{_topdir}/BUILD

%{_rpmdir}

%{_topdir}/RPMS

%{_sourcedir}

%{_topdir}/SOURCES

%{_specdir}

%{_topdir}/SPECS

%{_srcrpmdir}

%{_topdir}/SRPMS

%{_buildrootdir}

%{_topdir}/BUILDROOT

Macros proporcionen indicadores de compilador y enlazados

The default build flags for binaries on fedora are available via macros. They are used by the build system macros to setup the build environment, so it is usually not necessary to use them directly — except, for example, when doing bare bones compilation with gcc directly.

The set of flags listed below reflects the current state of fedora 42 on a x86_64 machine, as defined in the file /usr/lib/rpm/redhat/macros.

The %{optflags} macro contains flags that determine CFLAGS, CXXFLAGS, FFLAGS, etc.

Las definiciones actual de estos valores pueden ser encontrados dentro del paquete redhat-rpm-config, en el build flags documentation.

$ rpm --eval "%{optflags}"
-O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Wno-complain-wrong-lang -Werror=format-security -Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=3 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1  -m64 -march=x86-64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -mtls-dialect=gnu2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer

El valor de la variable del entorno LDFLAGS fijado por sistemas de compilación está determinado por la macro %{build_ldflags}:

$ rpm -E "%{build_ldflags}"
-Wl,-z,relro -Wl,--as-needed  -Wl,-z,pack-relative-relocs -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1  -Wl,--build-id=sha1