quentin.dufour.io/_posts/2018-05-20-build-a-rpm-package-and-publish-it-on-copr.md

215 lines
6.4 KiB
Markdown
Raw Permalink Normal View History

2018-05-20 11:49:15 +00:00
---
layout: post
slug: build-a-rpm-package-and-publish-it-on-copr
status: published
sitemap: true
2021-07-14 15:13:17 +00:00
title: Publish on Copr
2018-05-20 11:49:15 +00:00
description: They said it was easy
2021-07-14 15:13:17 +00:00
category: developpement
2018-05-20 11:49:15 +00:00
tags:
- fedora
- copr
- rpm
2021-07-14 15:13:17 +00:00
- en
2018-05-20 11:49:15 +00:00
---
2018-05-20 12:00:19 +00:00
*Disclaimer: I'm not a linux packaging expert, some parts could be considered as bad practises. Moreover, there are billions different ways of building packages. I am proposing only one in this article.*
2018-05-20 11:49:15 +00:00
2021-07-14 15:13:17 +00:00
## Setup
2018-05-20 11:49:15 +00:00
2018-05-20 12:00:19 +00:00
First of all, we will need some tools to build packages:
2018-05-20 11:49:15 +00:00
```bash
sudo dnf install git rpmdevtools rpm-build
```
2018-05-20 12:00:19 +00:00
RPM is a bit special, as it needs its own folder hierarchy (stored by default in `~/rpmbuild`) and can't build without this hierarchy. It's a bit annoying when you manage your packages in different repositories. We will see one way to get around this problem.
2018-05-20 11:49:15 +00:00
First, create a git repository for your RPMs:
```bash
mkdir my-rpms && cd my-rpms
git init
```
We will create one folder for each RPM and initialize with the essence of a RPM: a spec file. For now, let's start with only one, `chez-scheme` which is a real example:
```bash
mkdir chez-scheme && cd chez-scheme
rpmdev-newspec --macros chez-scheme.spec
```
You should have generated the following file:
2018-05-20 12:00:19 +00:00
```
2018-05-20 11:49:15 +00:00
Name: chez-scheme
Version:
Release: 1%{?dist}
Summary:
License:
URL:
Source0:
BuildRequires:
Requires:
%description
%prep
%autosetup
%build
%configure
%make_build
%install
rm -rf %{buildroot}
%make_install
%files
%license add-license-file-here
%doc add-docs-here
%changelog
* Sun May 20 2018 Quentin Dufour <quentin@dufour.io>
-
```
So, now we must fill this file. You can use the Fedora Packaging Guidelines guide to find help for the different macro (lines starting with a percent). You can also take some inspiration from existing `.spec` files like [meshlab.spec](https://src.fedoraproject.org/rpms/meshlab/blob/master/f/meshlab.spec) or [chez-scheme.spec](https://github.com/superboum/rpm/blob/master/chez-scheme/chez-scheme.spec).
2021-07-14 15:13:17 +00:00
## Sources and patches
2018-05-20 11:49:15 +00:00
We will consider the case where a source tarball is provided, but you need to patch it.
You can easily retrieve a tarball from any github or gitlab project, for a given release, like:
```
Source0: https://github.com/cisco/ChezScheme/archive/v%{version}.tar.gz
```
By templating the link, it will be easier to upgrade the package.
To create the patches, you have different options. You can either clone the repository, do the modification in a specific branch or commit, then generate a patch:
```bash
git checkout -b v9.5-patches
git commit -m "A first patch"
git commit -m "A second patch"
git format-patch v9.5
```
Another way is to download and extract the tarball, create a copy and use diff:
```bash
tar xf v9.5.tar.gz
cp -r v9.5 v9.5-src
# modify v9.5
diff -Naur v9.5-src v9.5 > some-modification.patch
```
We will put these patches in our git repository, next to our `.spec` file:
```
rpm
└── chez-scheme
├── chez-scheme.spec
├── chez-scheme-symlink.patch
└── chez-scheme-xlocale.patch
```
2018-05-20 12:00:19 +00:00
Applying patches and extracting tarball can lead to some problems.
First, patches can have different "roots". We will use `patch -p0` to apply git patches as they don't include the main folder and `patch -p1` for diff patches as they include the main folder.
If you use the `autosetup` macro in your `.spec` file, you will put (for diff patches):
```
%autosetup -p1
```
Second, once downloaded, your tarball doesn't necessarily extract in a folder of the name `<package>-<version>`. If it's not the case, you must precise it too:
```
%autosetup -p1 -n ChezScheme-%{version}
```
Now, if we want to build the RPM, we will have to type the following commands:
2018-05-20 11:49:15 +00:00
```bash
spectool -g -R ./chez-scheme.spec # Download sources
cp *.patch ~/rpmbuild/SOURCES # Copy patches
rpmbuild -ba ./chez-scheme.spec # Build RPM
```
2018-05-20 12:00:19 +00:00
There are some tools like rpkg, fedpkg or tito that can do most of the steps for you.
Unfortunately, they are not very flexible. rpkg and fedpkg never download the source directly, but from a mirror, and fails with a 404 as they don't find the file. If I want to push my source to this mirror, it fails with an unauthorized error. These packages seem to be aimed at distribution maintainers. tito, for its part, doesn't handle correctly patches and sources. It consider that everything should be embedded in a single git repository.
2018-05-20 11:49:15 +00:00
So, we will need an home made solution.
2021-07-14 15:13:17 +00:00
## make srpm
2018-05-20 11:49:15 +00:00
2018-05-20 12:00:19 +00:00
Fortunately, copr propose different solutions. One is to build your `.srpm` locally and send it to copr, but we can even avoid this step and only give a git repository to copr: we will build our own builder with a Makefile.
2018-05-20 11:49:15 +00:00
Copr will call a Makefile located in `<git_root>/.copr` from the folder of our package (`<git_root>/chez-scheme`) and call:
```bash
make -f <git_root>/.copr/Makefile srpm outdir="<outdir>" spec="<spec_path>"
```
Copr proposes a simple Makefile in its documentation. I'm proposing a bit more complex one that use only `rpmbuild`:
```makefile
rpmbuild_src = $(shell rpmbuild --eval '%{_sourcedir}')
.PHONY: prepare srpm rpm
prepare:
dnf -y install rpmdevtools rpm-build
cp *.patch $(rpmbuild_src)
spectool -g -R $(spec)
srpm: prepare
rpmbuild -bs --define "_srcrpmdir $(outdir)" $(spec)
rpm: prepare
rpmbuild -bb --define "_rpmdir $(outdir)" $(spec)
```
The `rpm` target will not be used by Copr but it can be convenient to locally test your package build before sending it to Copr.
2018-05-20 12:00:19 +00:00
Your folder structure should now look like this:
```
rpm
├── chez-scheme
│   ├── chez-scheme.spec
│   ├── chez-scheme-symlink.patch
│  └── chez-scheme-xlocale.patch
└─── .copr
   └── Makefile
```
You really have to put the Makefile here, otherwise copr will not find it.
2021-07-14 15:13:17 +00:00
## Use Copr
2018-05-20 11:49:15 +00:00
Now you just need to commit + push your repository and use the [Copr web interface](http://copr.fedorainfracloud.org/) to create a new project, add a package and trigger a build !
After that, your users can just enable the copr repository on their system and install your package:
```
sudo dnf copr enable superboum/chez-scheme
sudo dnf install chez-scheme
```
You can configure some webhooks to rebuild your packages for each git commit.
And that's all!
2021-07-14 15:13:17 +00:00
## Sources
2018-05-20 11:49:15 +00:00
* [Fedora Packaging Guidelines](https://fedoraproject.org/wiki/Packaging:Guidelines)
* [Spectool](https://pagure.io/spectool)
* [Copr documentation](https://docs.pagure.org/copr.copr/user_documentation.html)