CentOS 7 and the incorrect dist RPM macro

When creating RPM packages recently, I had the effect that package names on CentOS 7 were set incorrectly. For example, a package had the name pinkepank-0.6-1.el7.centos.x86_64.rpm but should have set pinkepank-0.6-1.el7.x86_64.rpm instead. As Enterprise Linux derivates (CentOS, Scientific Linux,...) offer binary compatibility to Red Hat Enterprise Linux, I prefer omitting distribution-specific tags in package names.

In the RPM spec file package name, version and release are set like this:

1Name: interceptty
2Version: 0.6
3Release: 1%{?dist}

The variable dist is replaced by the major release on Enterprise Linux derivates - e.g. el7 for Enterprise Linux 7. So, no centos is set here. After a long time, I discovered that the macro dist is defined in the /etc/rpm/macros.dist file - e.g. under EL6:

1el6$ grep dist /etc/rpm/macros.dist
2# dist macros.
3%dist .el6

Under CentOS 7 the file looks like this:

1el7$ grep dist /etc/rpm/macros.dist
2# dist macros.
3%dist .el7.centos

On a Red Hat Enterprise Linux 7 system the affected line looks liks this:

1%dist .el7

I changed the line and rebuild some packages inside a virtual machine - after changing the line, package names were correct again.

As I'm using Docker containers for creating RPM packages, I added the following line to the appropriate Dockerfile:

1# fix broken RPM macro
2RUN sed -i "s/.el7.centos/.el7/g" /etc/rpm/macros.dist

When creating the image, the file mentioned above is changed. This can be checked by executing the following command:

1$ docker run rpmdevel-centos7:latest cat /etc/rpm/macros.dist | grep dist
2# dist macros.
3%dist .el7

Unfortunately, the issue wasn't resolved - package names were still incorrect. Adding the following lines to the RPM spec file did the trick:

1%if 0%{?rhel} == 7
2 %define dist .el7
3%endif

These lines manually set the dist variable to the value .el7 if an Enterprise Linux 7 environment was detected. It is important the these lines are pasted before the release definition - otherwise this override won't work:

1%if 0%{?rhel} == 7
2 %define dist .el7
3%endif
4
5Name:    pinkepank
6Version: 0.6
7Release: 1%{?dist}

The following example is incorrect:

1Name:    pinkepank
2Version: 0.6
3Release: 1%{?dist}
4
5%if 0%{?rhel} == 7
6 %define dist .el7
7%endif

In this case, the package name would still be pinkepank-0.6-1.el7.centos.x86_64.rpm instead of pinkepank-0.6-1.el7.x86_64.rpm.

Translations: