================================================================================
                             Building rpms workflow
================================================================================

To build a RPM we need
- A .specfile: A file that describes the building process and the packages to
  build
- A source tarball: A .tar.gz compressed archive that will be used to build the
  package
- A buildtree: Directories placed by default under ~/rpmbuild/ that will be used
  during building

--------------------------------------------------------------------------------
                                 Pre-requisites
--------------------------------------------------------------------------------

Podman:
To target a build on Fedora, we need a Fedora ditribution. Although rpm is
available on Debian for instance, some environment variable may differ and the
RPM may not respect the target distribution philosophy. For instance the libdir
on Debian is resolved to /usr/lib while on Fedora it is /usr/lib64. Meaning that
RPMs built under Debian will not fully repect Fedora philosophy and may be a bit
odd.

To resolve that it is possible to install a Fedora distribution with containers
and build the rpms there.

Pulls a fedora-minimal image. fedora-minimal is resolved with
/etc/containers/registries.conf.d/shortnames.conf
$ podman pull fedora-minimal

Run a command and remove the filesytem right after
$ podman run --rm fedora-minimal:latest bash -c 'echo "Hello"'

Mount a shared volume
$ podman run --rm -v /build:/build fedora-minimal:latest bash -c 'echo "Hello"'
Here the -v option expect source_dir:target_dir where source is the sytem
running podman and target the filesystem in the container.

Do not use podman inside a schroot

--------------------------------------------------------------------------------
Filetree
--------------------------------------------------------------------------------

%{_topdir} defines the top directory for the filetree. It defaults to ~/rpmbuild

It can be overriden with macros:
On the command-line
$ rpmbuild --define "_topdir /build/rpmbuild" -ba <spec_file>
With a .rpmmacros
Define %_topdir to any path. See .rpmmacros.

To build the source tree
$ mkdir <topdir>/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

--------------------------------------------------------------------------------
Archiving
--------------------------------------------------------------------------------

It is possible to create a compressed archive from a commit in a git repository.
$ git archive --format=tar.gz --prefix=setup-1.0.2/ \
-o ~/rpmbuild/SOURCES/setup-1.0.2.tar.gz HEAD

--format: Format of the resulting archive, can be inferred from output filename
if option not given
--prefix: Prepends a prefix to all files in the archive. Useful here because
rpmbuild expects a certain filepath under builddir.
-o: Write to file given instead of stdout.
HEAD: Commit ID, since a tag is a commit ID. Can be used with v1.0.2 for
instance.

The archive includes the directory and all subdirectories only from where the
command is executed. It means that it should be executed at the root of the git
repo to include all files.

Git archive doesn't archive submodules

--------------------------------------------------------------------------------
Building
--------------------------------------------------------------------------------

To build rpms:
$ rpmbuild --define "_topdir /build/setup" -bb /build/setup/rpm/setup.spec

--define: Define a macro, here overrides the topdir macro
-bb: build only the binary packages

see rpmbuild docs for details.

--------------------------------------------------------------------------------
Putting all together
--------------------------------------------------------------------------------

To build the rpm package manually:

Installing packaging utilities (podman, git, ...)
$ sudo apt install setup-packaging

$ podman run --rm -it -v /tmp/setup:/package fedora-minimal:latest

$ cd /package
$ git clone --recurse-submodules https://git.zeromaximum.com/yestax/setup.git
$ cd setup
$ rpmbuild --build-in-place --define "_topdir /package" -bb /package/setup/rpm/setup.spec

Archiving the repo
$ mkdir /tmp/setup
$ git clone https://git.zeromaximum.com/yestax/setup.git
$ cd setup
$ cp rpm/setup.spec /tmp/setup
$ git archive --prefix=setup-1.0.2/ -o /tmp/setup/setup-1.0.2.tar.gz v1.0.2

To build it in one command:

$ podman run --rm -v /tmp/setup:/package fedora-minimal:latest \
bash -c 'dnf install -y setup-packaging &&
  cd /package &&
  git clone https://git.zeromaximum.com/yestax/setup.git &&
  cd setup &&
  rpmbuild --build-in-place --define "_topdir /build/setup" -bb /package/setup/rpm/setup.spec
'
