6.4. Working with spec files
To package new software, you must create a spec file. You can create the spec file either of the following ways:
-
Write the new
specfile manually from scratch. -
Use the
rpmdev-newspecutility. This utility creates an unpopulatedspecfile, where you fill the necessary directives and fields.
Some programmer-focused text editors pre-populate a new spec file with their own spec template. The rpmdev-newspec utility provides an editor-agnostic method.
6.4.1. Creating a new spec file for sample Bash, Python, and C programs リンクのコピーリンクがクリップボードにコピーされました!
You can create a spec file for each of the three implementations of the Hello World! program by using the rpmdev-newspec utility.
Prerequisites
The following
Hello World!program implementations were placed into the~/rpmbuild/SOURCESdirectory:
Procedure
Navigate to the
~/rpmbuild/SPECSdirectory:$ cd ~/rpmbuild/SPECSCreate a
specfile for each of the three implementations of theHello World!program:$ rpmdev-newspec bello bello.spec created; type minimal, rpm version >= 4.11. $ rpmdev-newspec cello cello.spec created; type minimal, rpm version >= 4.11. $ rpmdev-newspec pello pello.spec created; type minimal, rpm version >= 4.11.The
~/rpmbuild/SPECS/directory now contains threespecfiles namedbello.spec,cello.spec, andpello.spec.Examine the created files.
The directives in the files represent those described in About spec files. In the following sections, you will populate a particular section in the output files of
rpmdev-newspec.
6.4.2. Modifying an original spec file リンクのコピーリンクがクリップボードにコピーされました!
The original output spec file generated by the rpmdev-newspec utility represents a template that you must modify to provide necessary instructions for the rpmbuild utility. rpmbuild then uses these instructions to build an RPM package.
Prerequisites
-
The unpopulated
~/rpmbuild/SPECS/<name>.specspecfile was created by using therpmdev-newspecutility. For more information, see Creating a new spec file for sample Bash, Python, and C programs.
Procedure
-
Open the
~/rpmbuild/SPECS/<name>.specfile provided by therpmdev-newspecutility. Populate the following directives of the
specfile Preamble section:Name-
Namewas already specified as an argument torpmdev-newspec. Version-
Set
Versionto match the upstream release version of the source code. Release-
Releaseis automatically set to1%{?dist}, which is initially1. Summary- Enter a one-line explanation of the package.
License- Enter the software license associated with the source code.
URL-
Enter the URL to the upstream software website. For consistency, utilize the
%{name}RPM macro variable and use thehttps://example.com/%{name}format. SourceEnter the URL to the upstream software source code. Link directly to the software version being packaged.
注記The example URLs in this documentation include hard-coded values that could possibly change in the future. Similarly, the release version can change as well. To simplify these potential future changes, use the
%{name}and%{version}macros. By using these macros, you need to update only one field in thespecfile.BuildRequires- Specify build-time dependencies for the package.
Requires- Specify run-time dependencies for the package.
BuildArch- Specify the software architecture.
Populate the following directives of the
specfile Body section. You can think of these directives as section headings, because these directives can define multi-line, multi-instruction, or scripted tasks to occur.%description- Enter the full description of the software.
%prep- Enter a command or series of commands to prepare software for building.
%conf- Enter a command or series of commands to configure software for building.
%build- Enter a command or series of commands for building software.
%install-
Enter a command or series of commands that instruct the
rpmbuildcommand on how to install the software into theBUILDROOTdirectory. %files- Specify the list of files, provided by the RPM package, to be installed on your system.
%changelogEnter the list of datestamped entries for each
Version-Releaseof the package.Start the first line of the
%changelogsection with an asterisk (*) character followed byDay-of-Week Month Day Year Name Surname <email> - Version-Release.For the actual change entry, follow these rules:
- Each change entry can contain multiple items, one for each change.
- Each item starts on a new line.
-
Each item begins with a hyphen (
-) character.
You have now written an entire spec file for the required program.
6.4.3. An example spec file for a sample Bash program リンクのコピーリンクがクリップボードにコピーされました!
You can use the following example spec file for the bello program written in bash for your reference.
An example spec file for the bello program written in bash
Name: bello
Version: 0.1
Release: 1%{?dist}
Summary: Hello World example implemented in bash script
License: GPLv3+
URL: https://www.example.com/%{name}
Source0: https://www.example.com/%{name}/releases/%{name}-%{version}.tar.gz
Requires: bash
BuildArch: noarch
%description
The long-tail description for our Hello World Example implemented in
bash script.
%prep
%setup -q
%build
%install
mkdir -p %{buildroot}/%{_bindir}
install -m 0755 %{name} %{buildroot}/%{_bindir}/%{name}
%files
%license LICENSE
%{_bindir}/%{name}
%changelog
* Tue May 31 2016 Adam Miller <maxamillion@fedoraproject.org> - 0.1-1
- First bello package
- Example second item in the changelog for version-release 0.1-1
-
The
BuildRequiresdirective, which specifies build-time dependencies for the package, was deleted because there is no building step forbello. Bash is a raw interpreted programming language, and the files are just installed to their location on the system. -
The
Requiresdirective, which specifies run-time dependencies for the package, includes onlybash, because thebelloscript requires only thebashshell environment to execute. -
The
%buildsection, which specifies how to build the software, is blank, because thebashscript does not need to be built.
To install bello, you must create the destination directory and install the executable bash script file there. Therefore, you can use the install command in the %install section. You can use RPM macros to do this without hardcoding paths.
6.4.4. An example spec file for a sample Python program リンクのコピーリンクがクリップボードにコピーされました!
You can use the following example spec file for the pello program written in the Python programming language for your reference.
An example spec file for the pello program written in Python
Name: pello
Version: 0.1.1
Release: 1%{?dist}
Summary: Hello World example implemented in Python
License: GPLv3+
URL: https://www.example.com/%{name}
Source0: https://www.example.com/%{name}/releases/%{name}-%{version}.tar.gz
BuildRequires: python
Requires: python
Requires: bash
BuildArch: noarch
%description
The long-tail description for our Hello World Example implemented in Python.
%prep
%setup -q
%build
python -m compileall %{name}.py
%install
mkdir -p %{buildroot}/%{_bindir}
mkdir -p %{buildroot}/usr/lib/%{name}
cat > %{buildroot}/%{_bindir}/%{name} <<EOF
#!/bin/bash
/usr/bin/python /usr/lib/%{name}/%{name}.pyc
EOF
chmod 0755 %{buildroot}/%{_bindir}/%{name}
install -m 0644 %{name}.py* %{buildroot}/usr/lib/%{name}/
%files
%license LICENSE
%dir /usr/lib/%{name}/
%{_bindir}/%{name}
/usr/lib/%{name}/%{name}.py*
%changelog
* Tue May 31 2016 Adam Miller <maxamillion@fedoraproject.org> - 0.1.1-1
- First pello package
The
Requiresdirective, which specifies run-time dependencies for the package, includes two packages:-
The
pythonpackage required to execute the byte-compiled code at runtime. -
The
bashpackage required to execute the small entry-point script.
-
The
-
The
BuildRequiresdirective, which specifies build-time dependencies for the package, includes only thepythonpackage. Thepelloprogram requirespythonto perform the byte-compile build process. -
The
%buildsection, which specifies how to build the software, creates a byte-compiled version of the script. Note that in real-world packaging, it is usually done automatically, depending on the distribution used. -
The
%installsection corresponds to the fact that you must install the byte-compiled file into a library directory on the system so that it can be accessed.
This example of creating a wrapper script in-line in the spec file shows that the spec file itself is scriptable. This wrapper script executes the Python byte-compiled code by using the here document.
6.4.5. An example spec file for a sample C program リンクのコピーリンクがクリップボードにコピーされました!
You can use the following example spec file for the cello program that was written in the C programming language for your reference.
An example spec file for the cello program written in C
Name: cello
Version: 1.0
Release: 1%{?dist}
Summary: Hello World example implemented in C
License: GPLv3+
URL: https://www.example.com/%{name}
Source0: https://www.example.com/%{name}/releases/%{name}-%{version}.tar.gz
Patch0: cello-output-first-patch.patch
BuildRequires: gcc
BuildRequires: make
%description
The long-tail description for our Hello World Example implemented in
C.
%prep
%setup -q
%patch0
%build
make %{?_smp_mflags}
%install
%make_install
%files
%license LICENSE
%{_bindir}/%{name}
%changelog
* Tue May 31 2016 Adam Miller <maxamillion@fedoraproject.org> - 1.0-1
- First cello package
The
BuildRequiresdirective, which specifies build-time dependencies for the package, includes the following packages required to perform the compilation build process:-
gcc -
make
-
-
The
Requiresdirective, which specifies run-time dependencies for the package, is omitted in this example. All runtime requirements are handled byrpmbuild, and thecelloprogram does not require anything outside of the core C standard libraries. -
The
%buildsection reflects the fact that in this example theMakefilefile for the cello program was written. Therefore, you can use the GNU make command. However, you must remove the call to%configurebecause you did not provide a configure script.
You can install the cello program by using the %make_install macro. This is possible because the Makefile file for the cello program is available.