Friday, 4 October 2013

Building your first RPM

Building wget manually

The wget utility, like many other open source applications, can be built manually. Understanding that process is the starting point for bundling wget in a package. Per the general convention, building wget requires four steps:

  1. Download and unpack the source.
  2. Configure the build.
  3. Build the code.
  4. Install the software


tar -xvf wget-1.12.tar
cd wget-1.12
./configure
make
make install
 


./configure queries the system and sets compilation options suitable for the hardware and software detected. make compiles the code, and sudo make install installs the code in system directories.
 By default, the directories are rooted at /usr/local,

To convert this process to RPM, you place the source in a repository and write a configuration file to dictate where to find the source to be compiled and how to build and install the code. 


The configuration file, called a spec file, is the input to a utility called rpmbuild. The spec file and the binaries are packaged by rpmbuild into an RPM. 

When another user downloads your RPM, the rpm utility reads the spec file and installs the package per your pre-written instructions.

Before you continue, one word of caution. In the past, packages were built by root, the superuser, because root was the only user able to access the system source code repository. However, this approach was potentially hazardous. Because root can alter any file on the system, it was easy to inadvertently alter a running system by adding extraneous files or removing important files during interim builds of an RPM. More recently, the RPM system changed to allow any user to build RPMs in a home directory. Building an RPM without the privileges of root prevents changes to core system files. Here, the more modern approach is shown.

To build an RPM, you must:

   * Set up a directory hierarchy per the rpmbuild specifications.


   * Place your source code and supplemental files in the 

      proper locations in the hierarchy.

   * Create your spec file.


   * Build the RPM. You can optionally build a source RPM 

     to share your source code with others.

To begin, build the hierarchy. In a directory in your home directory—say, $HOME/mywget—create five sub-directories:

   * BUILD: BUILD is used as scratch space to 

      actually compile the software.

   * RPMS. RPMS contains the binary RPM that rpmbuild builds.


   * SOURCES. SOURCES is for source code.


   * SPECS. SPECS contains your spec file or files—one 

     spec file per RPM you want to build.

   * SRPMS. SRPMS contains the source RPM built during the process.

At a minimum, you need source code in SOURCES and a spec file in SPECS.

Copy your source, ideally bundled as a tarball, into the SOURCES directory, as shown in Listing 2. If necessary, rename the tarball to include the version number of the application to differentiate it from others. The naming convention is package-version.tar.gz. In the case of wget, you would use:


1.create a new user

# useradd rpm
# passwd rpm
# su - rpm

1.1 Copying your source
# mkdir mywget

# cd mywget

# mkdir BUILD RPMS SOURCES SPECS SRPMS

# cd SOURCES

# cp wget-latest.tar.gz .

# mv wget-latest.tar.gz wget-1.12.tar.gz

# cd ..

Next, create the spec file. A spec file is nothing more than a text file with a special syntax. Listing 3 shows an example of a spec file.

1.2 Sample spec file
# ls /home/rpm/mywget# wget.spec vim wget.spec
# This is a sample spec file for wget
%define _topdir         /home/rpm/mywget
%define name            wget
%define release         1
%define version         1.12
%define buildroot %{_topdir}/%{name}-%{version}-root

BuildRoot:      %{buildroot}
Summary:        GNU wget
License:        GPL
Name:           %{name}
Version:        %{version}
Release:        %{release}
Source:         %{name}-%{version}.tar.gz
Prefix:         /usr
Group:          Development/Tools

%description
The GNU wget program downloads files from the Internet using the command-line.

%prep
%setup -q

%build
./configure
make

%install
make install prefix=$RPM_BUILD_ROOT/usr

%files
%defattr(-,root,root)
/usr/bin/wget
/usr/etc/wgetrc
/usr/share/info/wget.info.gz
/usr/share/locale/be/LC_MESSAGES/wget.mo
/usr/share/locale/bg/LC_MESSAGES/wget.mo
/usr/share/locale/ca/LC_MESSAGES/wget.mo
/usr/share/locale/cs/LC_MESSAGES/wget.mo
/usr/share/locale/da/LC_MESSAGES/wget.mo
/usr/share/locale/de/LC_MESSAGES/wget.mo
/usr/share/locale/el/LC_MESSAGES/wget.mo
/usr/share/locale/en@boldquot/LC_MESSAGES/wget.mo
/usr/share/locale/en@quot/LC_MESSAGES/wget.mo
/usr/share/locale/en_GB/LC_MESSAGES/wget.mo
/usr/share/locale/en_US/LC_MESSAGES/wget.mo
/usr/share/locale/eo/LC_MESSAGES/wget.mo
/usr/share/locale/es/LC_MESSAGES/wget.mo
/usr/share/locale/et/LC_MESSAGES/wget.mo
/usr/share/locale/eu/LC_MESSAGES/wget.mo
/usr/share/locale/fi/LC_MESSAGES/wget.mo
/usr/share/locale/fr/LC_MESSAGES/wget.mo
/usr/share/locale/ga/LC_MESSAGES/wget.mo
/usr/share/locale/gl/LC_MESSAGES/wget.mo
/usr/share/locale/he/LC_MESSAGES/wget.mo
/usr/share/locale/hr/LC_MESSAGES/wget.mo
/usr/share/locale/hu/LC_MESSAGES/wget.mo
/usr/share/locale/id/LC_MESSAGES/wget.mo
/usr/share/locale/it/LC_MESSAGES/wget.mo
/usr/share/locale/ja/LC_MESSAGES/wget.mo
/usr/share/locale/lt/LC_MESSAGES/wget.mo
/usr/share/locale/nb/LC_MESSAGES/wget.mo
/usr/share/locale/nl/LC_MESSAGES/wget.mo
/usr/share/locale/pl/LC_MESSAGES/wget.mo
/usr/share/locale/pt/LC_MESSAGES/wget.mo
/usr/share/locale/pt_BR/LC_MESSAGES/wget.mo
/usr/share/locale/ro/LC_MESSAGES/wget.mo
/usr/share/locale/ru/LC_MESSAGES/wget.mo
/usr/share/locale/sk/LC_MESSAGES/wget.mo
/usr/share/locale/sl/LC_MESSAGES/wget.mo
/usr/share/locale/sr/LC_MESSAGES/wget.mo
/usr/share/locale/sv/LC_MESSAGES/wget.mo
/usr/share/locale/tr/LC_MESSAGES/wget.mo
/usr/share/locale/uk/LC_MESSAGES/wget.mo
/usr/share/locale/vi/LC_MESSAGES/wget.mo
/usr/share/locale/zh_CN/LC_MESSAGES/wget.mo
/usr/share/locale/zh_TW/LC_MESSAGES/wget.mo
/usr/share/man/man1/wget.1.gz

#/usr/bin/wget

%doc %attr(0444,root,root) /usr/share/man/man1/wget.1.gz

1.3 Revving the RPM
# yum groupinstall development-tools# yum install rpm-build rpmdevtools rpmlint mock

# yum install rpm-build
# mv wget.spec SPECS/# rpmbuild -v -bb --clean SPECS/wget.spec

rpmbuild is the command to build the package
rpmdevtools is a collection of handy utils


No comments:

Post a Comment