If you have been using the Linux operating system for a long time and even know a little about programming, sooner or later you may need to compile the program from the source code. Maybe the necessary program will not appear in the distribution repositories. Or the old version program in these repositories, and you need the latest one. Or maybe you created a new program and want to share it with other users.
Of course, you can install the program according to the instructions that came with the source code. But then you will need to manually manage the program files and monitor its dependencies. It is much better to build a package with the program and use the application manager built into the operating system.
I hope this article helps you quickly figure out how to build an RPM package for Rosa Linux or for another distribution that uses the RPM package manager (Mandriva, RedHat). Or at least tell me where to look for information.
Here I will show how to create RPM packages on my local computer, using a simple example. We will build the xkb-switch program in the Rosa Linux operating system. We will take the source code of the program from GitHub .
Rosa Linux is a distribution created and supported by Russian developers . It is based on Mandriva , but with some features. I am using the 32-bit version of Rosa Fresh R8 , which was released in 2014. Now the R8 version is no longer supported by developers (repositories for it are not updated), but it works well on my laptop, so I do not want to install a new version.
Rosa Fresh R8 Uses the KDE4 desktop. All versions of the distribution package use rpm
and urpm
managers to manage packages with applications, and the corresponding commands are rpm
, urpmi
, urpme
, urpmq
, urpmf
.
The principles for building software packages are usually rarely changed within the same Linux distribution family. Therefore, what is described in this article should work in all versions of Rosa distributions.
Building packages on Rosa Linux is a bit simpler than on some other RPM- based distributions. Some points are automated and do not need to be configured.
I liked the idea of using the plugin for the Vim text editor - xkbswitch , about which there is an article on Habré . It automatically switches the keyboard layout to English when you exit the input mode, and vice versa - to the one on which the text was entered last time - if you enter the input mode again. For this plugin to work, you need the xkb-switch program, which allows you to switch the keyboard layout from the command line and from plugins for the Vim editor. This program was not in the Rosa Linux R8 repositories (and most likely no one will add it, because the distribution is already old, and the xkb-switch program is not very popular).
Often you need to configure the compilation of the program to work correctly in a particular distribution or even for a specific system. It happens that a program needs to be built with other options or with the support of some special driver. Or maybe you want to fix errors in the program code or just check if it works. To do this, it is better to copy the source code to your computer before building the RPM package, try to compile it, and check how the compiled program works.
If you are sure that you will not need to modify the project files, then you can use the original source code from the project site and immediately proceed to build the package. But if you need to change the compilation settings, it is usually much easier to change the file in the source folder (the so-called makefile ) than to then deal with the compilation command options and the macro's internal device for the spec file.
If you decide to work with the source code or project compilation settings, but you don’t have your own profile on GitHub or don’t want to use it, you can simply download and unzip the source code. In our case, it might look like this (I use the ~/Projects/cpp
folder for C ++ source code):
cd ~/Projects/cpp git clone https://github.com/ierton/xkb-switch.git
This will create the ~/Projects/cpp/xkb-switch
folder containing the source code. Of course, instead of using git
you can download the archive with the program and unzip it.
From the README.md
file in the project folder, it is easy to guess that the cmake
utility is used to build the program. Therefore, if you need to change the compilation settings, in our case they are in the CMakeLists.txt
file. And we just try to compile the program and check if it works. What commands you need to use for this is written in the same README.md
file in the root of the project.
cd xkb-switch mkdir build && cd build cmake .. make ./xkb-switch # ./xkb-switch -l # ./xkb-switch -s ru # ( )
After that, in order to use the changed project, you will have to clear or delete the build
folder from the project and pack the source code into the archive. It might look like this:
cd ~/Projects/cpp/xkb-switch rm -rf ./build cd ~/Projects/cpp zip -r xkb-switch.zip xkb-switch
The xkb-switch.zip
file xkb-switch.zip
then need to be used to build the package.
I assume that anyone who reads this section is at least a little familiar with working with git and has already set up a profile on GitHub . I think this method is the best, so the rest of the article will imply that it is used, unless otherwise stated.
First you need to fork the original project into your GitHub . After that, as in the previous method, we clone the project to our computer, but from our repository (in my case, the username is alexandersolovyov ):
cd ~/Projects/cpp git clone https://github.com/alexandersolovyov/xkb-switch.git
To add any changes, it is better to use a new branch. Then, if desired, it will be possible to use different versions of the program. And it will also be possible to propose changes to the project author using pull-requests. For example, if you decide to fix the CMakeLists.txt
file a little, before that you need to create a new branch with:
git branch cmake_corrections
After the changes are made, checked and added to the branch (using git commit -am " "
), you can add a new branch to your GitHub:
git push origin cmake_corrections
After that, you can make a pull-request, if necessary.
You can make a link to the original repository:
git remote add ierton https://github.com/ierton/xkb-switch.git
And then update the main branch of your repository so that it matches the original:
git pull ierton master
The program code corresponding to the corrected branch can be used when building the RPM. To do this, you will need to specify the address to the archive in the form in the spec file:
Source0: https://github.com/-/-/archive/---.zip
First you need to create a directory structure. All assembly takes place in the rpmbuild
folder in the rpmbuild
's home folder. Create the initial directory structure and go to the folder for spec files:
mkdir -p ~/rpmbuild/SPECS && cd ~/rpmbuild/SPECS
For Rosa Linux, this is enough: the remaining folders will be created automatically.
Another distribution may use a different file location to build the package. And maybe you’ll have to create the entire folder hierarchy manually. Look for information for your distribution if you are not using Rosa Linux. For example, this might look like this in Red Hat .
If the package was built with errors, then all files created by the rpmbuild
command rpmbuild
not deleted - the same as with a successful build. We will try to collect the package many times, and the files remaining after the last time will interfere. Therefore, it is better to make a simple script that will help to remove them quickly. Create a cleanup.sh file and place it in the ~/rpmbuild/SPECS
. Here are its contents:
!#/bin/sh rm -rf ~/rpmbuild/BUILD/* rm -rf ~/rpmbuild/BUILDROOT/* rm -rf ~/rpmbuild/RPMS/* rm -rf ~/rpmbuild/SRPMS/*
Of course, it’s better to add execution rights for it using the chmod u+x cleanup.sh
.
If you want to collect a package from files that are on the local computer — if you are not using GitHub and made changes to the project files, or if you want to create a package from your own program that is stored only on your computer — you need to pack the project into an archive ( e.g. .zip
, .tar
or .tar.gz
) and put it in the SOURCES
folder. For example, like this:
cd ~/Projects/cpp/ zip -r xkb-switch.zip xkb-switch mkdir -p ~/rpmbuild/SOURCES cp xkb-switch.zip ~/rpmbuild/SOURCES/
The basis for building an RPM package is the so-called speck file . This file contains the instructions for the rpmbuild
program (or rather rpm
) necessary for building the package.
Create the xkb-switch.spec
file in the ~/rpmbuild/SPECS/
. The easiest way to start is with a template that can be found on the Template Spec Files website.
From the README
on the xkb-switch project page, it is known that the program is compiled using the cmake
utility. Therefore, we will select the Spec file for a program built using CMake and copy the entire template to our spec file. Of course, in order to properly assemble our RPM package, this template needs to be changed a lot, which we will do.
Name: xkb-switch
%
%description
%{_}
Name: xkb-switch
%define
In the spec file, the header is always written first. This is a list of options that apply to the main RPM package. When the package is ready, almost all of this information will be displayed when viewing its description. Here is what the individual lines indicate:
README.md
Query and change XKB layout state
xkb-switch
CMakeLists.txt
MAJOR_VERSION
MINOR_VERSION
RELEASE_VERSION
1.6.0
rpmbuild/RPMS
rpmbuild/SRPMS
README.md
README.md
GPLv3+
Development\X11
https://github.com/ierton/xkb-switch
~/rpmbuild/SOURCES/
rpmbuild
~/rpmbuild/SOURCES/
https://github.com/alexandersolovyov/xkb-switch/archive/master.zip
Source1
Source2
cmake
CMakeLists.txt
cmake >= 2.6
BuildRequires:
README
CMakeLists.txt
FIND_PACKAGE
TARGET_LINK_LIBRARY
FIND_PROGRAM
urpmq -a __
CMakeLists.txt
# CMake 2.6 BuildRequires: cmake >= 2.6 # C/C++ BuildRequires: gcc # libxkbfile BuildRequires: libxkbfile-devel # X11 libxkbfile, # libx11-devel . # man: BuildRequires: xz
Provides
Requires
rpm
urpm
Provides
Requires
>=
=
<=
Requires
Provides
BuildRequires:
%description xkb-switch is a C++ program that allows to query and change the keyboard layout state for X11 Window System (XKB) via command line. It provides a command 'xkb-switch', and bindings for API of the Vim text editor via 'libxkbswitch.so'. It is mainly used by a plugin for Vim text editor, vim-xkbswitch. Originally ruby-based code written by J.Broomley.
Lines from the template starting with %files
and %find_lang
are needed only if you build an application with support for several languages, so delete them.
Further, after the dividing line-comment, followed by commands and macros that must be completed before packing the files. All these commands are divided into blocks defined using special keywords.
The first is the %prep
block, which indicates the commands to prepare for compilation. In it is the macro command %setup
. She runs a script that does the following:
SourceX:
Source0:
~/rpmbuild/SOURCES/
~/rpmbuild/BUILD/
Its -q
option specifies that less information be displayed at runtime.
You can immediately try to build the package, stopping after executing the %prep
block:
rpmbuild -bp ~/rpmbuild/SPECS/xkb-switch.spec
To build, use the rpmbuild
command. If your system does not have this command, then you can use rpm
with the same options rpmbuild
: rpmbuild
is its limited synonym, which is intended only for building packages. All build commands must be executed as a regular user (not as root
and without the sudo
). A description of all rpmbuild
options can be seen by running man rpmbuild
. You can also see the list of folders in which macros and other files used by this command are located, in case you want to understand in more detail the order of the rpmbuild
.
As a result, we get the error message: xkb-switch-1.6.0: No such file or directory
. The command output shows that after unpacking the archive, the script tries to go to the ~/rpmbuild/BUILD/xkb-switch-1.6.0
folder, which is not there. Running the command
ls ~/rpmbuild/BUILD/
We see that the xkb-switch-master
folder is located there. It must be specified with the %setup
command using the -n
option. As a result, the %prep
block in the spec file should look like this:
%prep %setup -q -n xkb-switch-master
Run our ~/rpmbuild/SPECS/cleanup.sh
to clear the BUILD
folder and try building the package again, ending with the %prep
block. Now we see that the last message displays exit 0
. This means that the block is executed without errors. You can move on.
Compilation of the source code is performed by the %build
block. The project’s README
says that compilation is done by cmake ..
and make
, but our spec file uses the same name macros. This is correct: this way the commands will be executed with all the transitions to the folders and the options that are needed for proper compilation. Leave it as it is, clear the folders and build to the %build
point inclusive:
~/rpmbuild/SPECS/cleanup.sh rpmbuild -bc ~/rpmbuild/SPECS/xkb-switch.spec
At the end of the output we see exit 0
: everything was done without errors.
In general, it is not necessary to use only macros in command blocks. You can add any shell commands (that is, bash or zsh, or whatever you have there). The rpmbuild
command moves through folders during build, so you need to add the transition to the correct working folder before your command. In these shell commands, you can use the spec file constants - as elsewhere in this file. ( .)
- , , , %build
shell .
, , . cmake
, , , %cmake
- , , . ( — CMakeLists.txt
). , .
, . %install
, , , ~/rpmbuild/BUILDROOT/__-buildroot/
.
__
, Name:
-, ( Version:
-), ( Release:
-), , .
README
, make install
, build
. - - %makeinstall_std -C build
, . ( , ):
~/rpmbuild/SPECS/cleanup.sh rpmbuild -bi ~/rpmbuild/SPECS/xkb-switch.spec
, RPM. - , .
, - . , ~/rpmbild/BUILDROOT/
. ( , , tree
, - Linux.) , , .debug
. , , : .
, , %files
-. , , . .
%files
-. , . Rosa Linux %prep
( - ). , , , , — . - (, , ).
%files
, RPM. , :
%files %{_bindir}/%{name} %{_libdir}/libxkbswitch.so %{_libdir}/libxkbswitch.so.1 %{_libdir}/libxkbswitch.so.%{version} %{_mandir}/man1/%{name}.1.xz
-. , , /usr/lib/rpm/macros
. , %{_bindir}
, %{_libdir}
— , %{_mandir}
— man. %{name}
%{version}
Name:
Version:
-.
:
~/rpmbuild/SPECS/cleanup.sh rpmbuild -ba ~/rpmbuild/SPECS/xkb-switch.spec
… 2 1 . rpmlint
, . , , Rpmlint Errors , :
-devel
All clear? I don’t think so. Let's take a closer look at what's happening here.
Rosa Linux rpmbuild
rpmlint
, . rpmlint
, , . , Rosa Linux ( , rpmlint
) .
, , . ~/rpmbuild/RPMS/_/
rpmlint -i __
.
By the way, if you downloaded the ready-made RPM package on the website of a project, then before installing such a package, you can check how correctly it is built for Rosa Linux using the same command rpmlint -i __
.
Packages for Rosa Linux are divided into 6 types. In the ideal case, each assembled package should correspond to only one of these types.
/bin/
/usr/bin
libxkbfile
libxkbfile.so.1.0.2
libxkbfile.so.1
lib
libxkbfile
libxkbfile1
libxkbfile.so
.h
-devel
libxkbfile-devel
libxkbfile-devel
libxkbfile1
-src
-source
apache-source
rpmbuild
~/rpmbuild/SRPMS/
rpmbuild
-debuginfo
doc
libx11-doc
java-1.7.0-openjdk-javadoc
file:///usr/share/doc/
Now everything has become clearer.
xkb-switch
libxkbswitch.so
xkb-switch.i586: W: devel-file-in-non-devel-package /usr/lib/libxkbswitch.so
/usr/lib/libxkbswitch.so.1
/usr/lib/libxkbswitch.so.1.6.0
xkb-switch.i586: E: executable-in-library-package (Badness: 1) /usr/bin/xkb-switch
xkb-switch.i586: E: incoherent-version-in-name (Badness: 50) 1
, rpmlint
, . . xkb-switch , libxkbswitch.so.1.6.0
, Vim . xkb-switch , C C++ . RPM- .
-:
Summary: Query and change XKB layout state Name: xkb-switch Version: 1.6.0 Release: 1 License: GPLv3+ Group: Development/X11 URL: https://github.com/ierton/xkb-switch Source0: https://github.com/alexandersolovyov/xkb-switch/archive/master.zip BuildRequires: cmake >= 2.6 BuildRequires: gcc BuildRequires: libxkbfile-devel BuildRequires: xz %description xkb-switch is a C++ program that allows to query and change the keyboard layout state for X11 Window System (XKB) via command line. It provides a command 'xkb-switch', and bindings for API of the Vim text editor via 'libxkbswitch.so'. It is mainly used by a plugin for Vim text editor, vim-xkbswitch. Originally ruby-based code written by J.Broomley. %files %{_bindir}/%{name} %{_libdir}/libxkbswitch.so %{_libdir}/libxkbswitch.so.1 %{_libdir}/libxkbswitch.so.%{version} %{_mandir}/man1/%{name}.1.xz #------------------------------------------------------------------ %prep %setup -q -n xkb-switch-master %build %cmake %make %install %makeinstall_std -C build
— , . , , xkb-switch
3 : , . :
/usr/bin/xkb-switch
/usr/share/man/man1/xkb-switch.1.xz
/usr/lib/libxkbswitch.so.1
/usr/lib/libxkbswitch.so.1.6.0
/usr/lib/libxkbswitch.so
-. , ,
- Maximum RPM . - libxkbfile Rosa Linux .
.
— - libxkbfile . - :
%define major 1 %define libname %mklibname xkbswitch %{major} %define develname %mklibname -d xkbswitch
%define
. , ( ) — , . , %{major}
1
, .
%mklibname
«lib», , — ( ). %{libname}
«lib» + «xkbswitch» + ( %{major}) = libxkbswitch1
— .
-d
%mklibname
-devel
. %{develname}
libxkbswitch-devel
— .
Version:
Version: %{major}.6.0
, -.
- . . :
Name: xkb-switch Version: %{major}.6.0 Release: 1 Summary: Query and change XKB layout state License: GPLv3+ Group: Development/X11 URL: https://github.com/alexandersolovyov/xkb-switch Source0: https://github.com/alexandersolovyov/xkb-switch/archive/master.zip BuildRequires: cmake >= 2.6 BuildRequires: gcc BuildRequires: libxkbfile-devel BuildRequires: xz %description xkb-switch is a C++ program that allows to query and change the keyboard layout state for X11 Window System (XKB) via command line. It provides a command 'xkb-switch', and bindings for API of the Vim text editor, a library 'libxkbswitch'. It is mainly used for some plugins for Vim text editor, such as vim-xkbswitch. Originally ruby-based code written by J.Broomley.
- . - , - ( rpm ). %package
. -. , -. Version
, Summary
, Group
. Provides
Requires
, . Name
: , %package
.
. %description
— , %package
.
Rosa Linux - %description
-. libxkbswitch1
:
%package -n %{libname} Version: %{version} Summary: A library for xkb-switch tool, provides API bindings for Vim text editor Group: Development/X11 %description -n %{libname} libxkbswitch library, required by xkb-switch tool. It provides bindings for API of the Vim text editor, which can be used via 'libxkbswitch.so.1'.
-n
%package
%description
, . - , xkb-switch-libxkbswitch1
. libxkbswitch1
. .
:
%package -n %{develname} Version: %{version} Summary: Development library libxkbswitch, provides API bindings for Vim text editor Group: Development/X11 %description -n %{develname} Development files for libxkbswitch. Provides bindings for API of the Vim text editor via 'libxkbswitch.so'.
, . %files
.
, , %package
, %description
%files
. , %description
- . , , — xkb-switch
.
- Rosa Linux %files
, %prep
. :
%files %{_bindir}/%{name} %{_mandir}/%{name}.1.xz %files -n %{libname} %{_libdir}/libxkbswitch.so.%{major} %{_libdir}/libxkbswitch.so.%{version} %files -n %{develname} %{_libdir}/libxkbswitch.so
, :
xkb-switch
~/usr/bin/xkb-switch
~/usr/share/man/man1/xkb-switch.1
libxkbswitch1
~/usr/lib/libxkbswitch.so.1
~/usr/lib/libxkbswitch.so.1.6.0
libxkbswitch-devel
~/usr/lib/libxkbswitch.so
cleanup.sh
rpmbuild -ba ~/rpmbuild/SPECS/xkb-switch.spec
. 3 :
libxkbswitch1.i586: W: no-documentation libxkbswitch-devel.i586: W: no-documentation libxkbswitch-devel.i586: W: no-dependency-on libxkbswitch/libxkbswitch-libs/liblibxkbswitch
, . , . . Let's try to figure it out.
, , , README.md
. %files
— %doc
:
%doc README.md
. %doc
— . ~/rpmbuild/BUILD/xkb-switdh-master/README.md
.
: libxkbswitch-devel.i586: W: no-dependency-on libxkbswitch/libxkbswitch-libs/liblibxkbswitch
. , libxkbswitch-devel
libxkbswitch
.
rpm -qp --
. :
[user@pc ~] $ cd ~/rpmbuild/RPMS/i586/ [user@pc ~/rpmbuild/RPMS/i586] $ ls libxkbswitch1-1.6.0-1-rosa2014.1.i586.rpm xkb-switch-1.6.0-1-rosa2014.1.i586.rpm libxkbswitch-devel-1.6.0-1-rosa2014.1.i586.rpm xkb-switch-debuginfo-1.6.0-1-rosa2014.1.i586.rpm [user@pc ~/rpmbuild/RPMS/i586] $ rpm -qp --provides libxkbswitch1-1.6.0-1-rosa2014.1.i586.rpm libxkbswitch.so.1 libxkbswitch1 = 1.6.0-1:2014.1 [user@pc ~/rpmbuild/RPMS/i586] $ rpm -qp --provides libxkbswitch-devel-1.6.0-1-rosa2014.1.i586.rpm devel(libxkbswitch) libxkbswitch-devel = 1.6.0-1:2014.1 [user@pc ~/rpmbuild/RPMS/i586] $ rpm -qp --requires libxkbswitch-devel-1.6.0-1-rosa2014.1.i586.rpm devel(libX11) devel(libgcc_s) devel(libstdc++) devel(libxkbfile) rpmlib(PayloadIsXz) <= 5.2-1 [user@pc ~/rpmbuild/RPMS/i586] $ rpm -qp --requires xkb-switch-1.6.0-1-rosa2014.1.i586.rpm libc.so.6 libc.so.6(GLIBC_2.0) libc.so.6(GLIBC_2.1.3) libgcc_s.so.1 libgcc_s.so.1(GCC_3.0) libstdc++.so.6 libstdc++.so.6(CXXABI_1.3) libstdc++.so.6(GLIBCXX_3.4) libstdc++.so.6(GLIBCXX_3.4.11) libstdc++.so.6(GLIBCXX_3.4.20) libstdc++.so.6(GLIBCXX_3.4.9) libxkbswitch.so.1 rpmlib(PayloadIsXz) <= 5.2-1
, libxkbswitch1
libxkbswitch.so.1
libxkbswitch1
. xkb-switch
libxkbswitch.so.1
, libxkbswitch-devel
libxkbswitch1
. , %package
libxkbswitch-devel
. :
%package -n %{develname} Version: %{version} Summary: Development library libxkbswitch, provides API bindings for Vim text editor Group: Development/X11 Requires: %{libname} >= %{version}
, … . libxkbswitch-devel
, , . , , rpmbuild
.
-, ( README.md
), — :
%define major 1 %define libname %mklibname xkbswitch %{major} %define develname %mklibname -d xkbswitch # Main package. Automaticaly requires libxkbswitch and libxkbswitch-devel Name: xkb-switch Version: %{major}.6.0 Release: 1 Summary: Query and change XKB layout state License: GPLv3+ Group: Development/X11 URL: https://github.com/alexandersolovyov/xkb-switch Source0: https://github.com/alexandersolovyov/xkb-switch/archive/master.zip BuildRequires: cmake >= 2.6 BuildRequires: gcc BuildRequires: libxkbfile-devel BuildRequires: xz %description xkb-switch is a C++ program that allows to query and change the keyboard layout state for X11 Window System (XKB) via command line. It provides a command 'xkb-switch', and bindings for API of the Vim text editor, a library 'libxkbswitch'. It is mainly used for some plugins for Vim text editor, such as vim-xkbswitch. Originally ruby-based code written by J.Broomley. # libxkbswitch %package -n %{libname} Version: %{version} Summary: A library for xkb-switch tool, provides API bindings for Vim text editor Group: Development/X11 %description -n %{libname} libxkbswitch library, required by xkb-switch tool. It provides bindings for API of the Vim text editor, which can be used via 'libxkbswitch.so.1'. # libxkbswitch-devel %package -n %{develname} Version: %{version} Summary: Development library libxkbswitch, provides API bindings for Vim text editor Group: Development/X11 Requires: %{libname} >= %{version} %description -n %{develname} Development files for libxkbswitch. Provides bindings for API of the Vim text editor via 'libxkbswitch.so'. # xkb-switch %files %{_bindir}/%{name} %{_mandir}/man1/%{name}.1.xz # libxkbswitch1 %files -n %{libname} %{_libdir}/libxkbswitch.so.%{major} %{_libdir}/libxkbswitch.so.%{version} %doc README.md # libxkbswitch-devel %files -n %{develname} %{_libdir}/libxkbswitch.so %doc README.md #------------------------------------------------------------------ %prep %setup -q -n xkb-switch-master %build %cmake %make %install %makeinstall_std -C build
, RPM Rosa Linux ( ). , -. , — , , rpmrc , ABF , — .
— , -, - , — .
, p
. , Rosa Linux, . urpmq
, rpm -q
. , urpmq -l _
, urpmq --requires _
.
rpm
.spec
.spec
rpmlint