[TriLUG] how to build a "noarch" rpm

rpjday rpjday at mindspring.com
Sun Dec 30 09:23:02 EST 2001


  since i've noticed a number of people asking how to build a
simple "noarch" rpm, i decided (surprise, surprise) to write up
how to do it, and attached it.  feel free to ignore it if you
have no interest.

  given that i am only marginally clueful about this, i'm 
interested in comments and corrections.

rday

p.s.  for those who saw an early version of this on the rpm
mailing list, this is a revised version.
-------------- next part --------------
* Overview -- building a noarch RPM

  This document explains, with a concrete example, how to build
a non-architecture-specific ("noarch") RPM.  This can be useful
if you want to bundle up data files and directories, such as text 
files, image files, font files, or script programs like shell or 
Perl scripts, possibly along with some accompanying documentation 
files like a README.

  Obviously, this is a fairly restrictive thing to do, but enough
people have asked about it that I figured it was worth writing up.

  Before you go any further, a few assumptions or prerequisites:

  - I'm assuming you're already familiar with using "rpm" to 
    install, examine and remove RPMs.  If you're not, now would
    be a good time to learn.
  
  - In addition to the ubiquitous "rpm" RPM, you need to install
    the "rpm-build" RPM as well.

  - Assume, for the examples used in this document, that your username 
    is "fred".
* Building an RPM as a regular user

  It's generally considered a hanging offense to build new RPMs
as root, so you should first create your own RPM-building
environment.  Take a look at the directory structure under
/usr/src/redhat, and duplicate that structure under a directory
of some name (like "rpm" or "packages") in your home directory:

$ cd
$ mkdir -p ~/rpm/{BUILD,RPMS,SOURCES,SRPMS,SPECS,tmp}
$ mkdir ~/rpm/RPMS/{i386,i586,i686,noarch}

  To keep explanations simple, you can refer to ~/rpm as your
"RPM directory", and subdirectories ~/rpm/BUILD as your 
"build directory" and so on.  (Note that, since we're building
only noarch RPMs, this entire directory structure is more than
you really need, but you might as well create the whole thing
for the day when you really will build your own RPMs from
source.)
 
  Everything above should look familiar except for the directory
~/rpm/tmp, which you'll set up shortly as a temp directory for
future source-based builds.  Once again, you don't really need
it for "noarch" RPM construction, but put it in anyway.

  Finally, once you've done all of the above, create the following
file with the contents shown, which instructs "rpm" to use your
personal RPM directory for future builds:

~/.rpmmacros

  %_topdir	/home/fred/rpm
  %_tmppath	/home/fred/rpm/tmp
* The spec file

  Most of the work involved in creating your RPM involves writing
the "spec" file.  As an example, let's say we wanted to create an
RPM called "wonderpics" -- version 1.0, release 1 -- containing 
nothing but the graphics files under /tmp/wonderpics on your host, 
and install them in the same place on the target host, along with 
a README and ChangeLog file that will be installed under 
/usr/share/doc/wonderpics-1.0.

  Here's a sample spec file for the "wonderpics" RPM.  For the sake
of organization, you'd typically keep all your spec files together
in your SPECS directory but, technically speaking, they can be
anywhere.

----- file wonderpics.spec -----
%define	name	wonderpics
%define version	1.0
%define release	1

Summary: WonderPics, a collection of images
Name: %{name}
Version: %{version}
Release: %{release}
Copyright: GPL
Group: Amusements/Graphics
BuildArch: noarch
# BuildRoot: %{_builddir}/%{name}-root		# to be explained later
URL: http://www.wonderpicsrus.com/wonderpics
Distribution: whatever
Vendor: WonderPics, Inc.
Packager: fred at wonderpics.com
Provides: WonderPics
Requires: ee

%description
This package contains WonderPics, just random images as
a test for building a non-source RPM.

%prep
exit 0

%build
exit 0

%install
exit 0

%clean
exit 0

%files
%defattr(-,root,root)
/tmp/wonderpics
%attr(444,root,root) %doc /usr/share/doc/wonderpics-%{version}

----- end of wonderpics.spec -----

  Some comments about the above:

-  Obviously, you should select the appropriate copyright value
   for this RPM.

-  Standard groups and subgroups are listed in the doc file
   /usr/share/doc/rpm-4???/GROUPS, but nothing says you have
   to take one of these values. 

-  Note the BuildArch directive, which specifies that this will be
   a "noarch" RPM -- that is, not tied to a particular architecture.

-  Note also that, since this is a noarch RPM, the prep, build, 
   install and clean steps are null.
* The %files section of the spec file

  A quick refresher on the %files section in our spec file:

  %files
  %defattr(-,root,root)
  /tmp/wonderpics
  %doc %attr(444,root,root) /usr/share/doc/wonderpics-%{version}

  The %files section of the spec file lists the files and/or
directories to be bundled into the RPM and, simultaneously,
where they will be installed on the target host.  Note that,
by default, this means that you first have to put the files in
their target locations before creating the RPM -- a real pain
and even impossible if you're trying to build your RPMs as a
regular user.  We'll fix this shortly so, ignoring that little
irritation, some features of the %files section:

  - Entries in the %files section can be directory names, 
    individual file names or any combination, with multiple
    entries per line if you want.   If you list a directory,
    the directory and its contents are added to the RPM.
    You can also use standard wildcard metacharacters here.

  - You can use the %defattr directive just once to define the
    default permissions, owner and group of the files and
    directories.  In this example, %defattr(-,root,root) means
    owner and group of root, and whatever permissions the files
    and directories had when they RPM was created.

  - Each line in the %files section can have its own overriding
    attribute specifier, as in %attr(444,root,root).

  - Each line in the %files section can also have a %doc qualifier,
    which identifies the entries on this line as documentation.
    This gives the installer the option of not installing anything
    labelled as documentation via the "--excludedocs" installation
    option.  [WARNING:  Even though the RPM documentation describes
    the %doc tag like this, it doesn't seem to work for me.  Can
    anyone clarify this?]

  At this point, we're almost ready to build our noarch RPM.
* Defining a "build root" for your new RPM

  Recall that, by default, all of the files and directories
listed in the %files section must be at those locations already
on the build host to build your RPM.  Since this could be difficult,
if not impossible, to set up first, you can (and should) instead
define a "build root" for your RPMs, which would contain a copy
of the entire directory structure for that RPM from the root
directory on down.

  While you can set up a build root anywhere you want, here's a
typical structure.  Once you define the "_topdir" macro in the
file ~/.rpmmacros as /home/fred/rpm, the macro "_builddir" will
automatically have the value /home/fred/rpm/BUILD -- call this
your "general" build directory.

  Your spec file could then have the directive:

  BuildRoot: %{_builddir}/${name}-root

which would identify an RPM-specific build root for each of your
RPMs under the general build directory.  For your wonderpics RPM,
you would then create the following directory sub-structure:

  /home/fred/rpm/BUILD				general build directory
  /home/fred/rpm/BUILD/wonderpics-root		wonderpics build root
  /home/fred/rpm/BUILD/wonderpics-root/tmp/wonderpics/<files>
  /home/fred/rpm/BUILD/wonderpics-root/usr/share/doc/<etc etc>

You get the idea -- each noarch RPM you want to build should
have its own build root and corresponding files under your general
build directory.  Again, you don't need to name each build root
<name>-root -- call it whatever you want, then populate it with
the contents of the future RPM.
* Building the noarch RPM

  Once you've done all of the above, go to your SPECS directory
and build the wonderpics RPM with:

  $ rpm -bb wonderpics.spec

then cd to your RPMS/noarch directory, where you should find a
file with the name "wonderpics-1.0-1.noarch.rpm".  Check its
attributes and contents with:

  $ rpm -qpi wonderpics-1.0-1.noarch.rpm
  $ rpm -qplv wonderpics-1.0-1.noarch.rpm

Note well that the file listing shows that the files will be
installed under /tmp/wonderpics -- all of the BuildRoot information
was stripped off when you created the RPM.
* Copyright

  This doc copyright Robert P. J. Day, 2001.  All rights reserved.
To be placed under the GPL for documentation once it's finished.


More information about the TriLUG mailing list