Creating Spec File For RPM

Home » CentOS » Creating Spec File For RPM
CentOS 5 Comments

Im trying to learn how to create RPMs but am a little confused by the spec file.

Im trying to package up some Python scripts, rather than binary files, and I want them to be installed in a subdirectory under /usr/local which I shall refer to as /usr/local/X

Ive put the scripts in a tarball under /home/me/X/RPM/SOURCES

but Im getting terribly confused over what to put in the  %build
%install and %files sections

If I set $RPM_BUILD_ROOT to /home/me/X/RPM/temp then put

%prep

%setup -q

that will unpack the tarball, yes?, but where to, /home/me/X/RPM/temp ?

how would I then tell it to move the files I want from there into
/usr/local/X and set appropriate permissions on them? The examples online all assume you have a makefile with a make install target, could I simply use the cp command under the %install section? But where am I
copying from and to?

My brain is tied in knots!

Many thanks

Andrew

5 thoughts on - Creating Spec File For RPM

  • OK.

    I don’t see a reply that answer your questions directly, so here I go… Is this /home/me/RPM your rpm topdir? On my setup, sources should go in
    $HOME/rpmbuild/SOURCES (see below.)
    Build is building – compiling if you are using a “compiled language”.

    Install should put everything on the directory where you want the final rpm to install it – but prefixed by the “build root” (i.e. your
    $RPM_BUILD_ROOT discussed below.)

    Files is a list of files to include in the package. It should match what you install to the build root. If you list a directory, all its files are recursively included, unless you put “%dir” in front of it. How so you set this up? In the environment?

    Usually, you include something like BuildRoot: /home/me/X/RPM/temp in the spec file, and “rpmbuild” sets up the variable for you during build. You can also use %{buildroot}. See https://www.redhat.com/archives/rpm-list/2002-July/msg00110.html. These days you generally get a usable build root without specifying anything.

    “%{…}” represents the expansion of a so-called rpm macro – it’s the rpm macro equivalent to the shell’s “${…}”. rpmbuild defines a number of macros for you automatically and/or reads in definitions from config files; more on that below. No. It will unpack to a directory specified by the rpm configuration. On my system (CentOS 6), the default is $HOME/rpmbuild. The configuration takes the form of an rpm macro, which is defined in /usr/lib/rpm/macros or /etc/rpm/macros. Look for _builddir and also _topdir in these files.
    (There may also be a user-specific location where you can configure this. I forget, as I’m generally OK with the defaults.)

    You should probably replace “setup -q” with just “setup”, as the unpack command will then tell you more. Yes. You can put any shell command there. You should just copy the files to your install location “under the build root”, i.e. for instance
    %{buildroot}/usr/local/X a.k.a. ${RPM_BUILD_ROOT}/usr/local/X.

    There are also macros that contain various standard commands, so you can for instance write “%{__cp}” instead of “cp”. This is supposed to make the spec file more portable. Have a look /usr/lib/rpm/macros*.

    I wouldn’t install on /usr/local, by the way (like some others who’ve responded.) Personally, I reckon /usr should be OK when using the
    “native” package manager, but some people argue that /opt is more proper. I’d use macros for this, too, anyhow. See _prefix, _bindir,
    _libdir etc. in the above mentioned files.

    You copy from the current directory, where you’ll find your tarball contents – rpmbuild automatically changes to the appropriate location. For the “to” part, see above.

    You should probably make sure the tarball has a toplevel directory of the form , by the way. But see http://ftp.rpm.org/max-rpm/s1-rpm-specref-macros.html. Better?

    – Toralf

  • Very helpful thanks, Im working through this and the other replies and trying to put the jigsaw together.