Hello Readers,

With this tutorial, I intend to provide more insight to developers who want to package their own RPM and are starting from scratch. I will provide an overview to .spec file which is the key essential file on generating RPM's so that you can get started really quickly. Just to mention, I did this on CentOS 6.2 virtual machine.

Requirements:

In-order to package RPM's and create an environment to work on this, we will need the following

sudo yum install -y rpm-build  
sudo yum install -y redhat-rpm-config  
sudo yum install -y rpmdevtools rpmlint  
#Post Installation
rpmdev-setuptree  

Process:

This will create the directory rpmbuild in your home path. Inside that directory, we can see multiple folders such as :BUILD, RPMS, SOURCES, SPECS, SRPMS , BUILDROOT*, tmp

In short descriptions, I will explain what each directory contains. BUILD will contain files when your packaging process is in the preparation state. RPMS folder contains the resultant rpm files. SOURCES will contain your tar source file to be used for RPM. There can be multiple source files. SRPMS similarly contains Source RPMS which can be re-packaged to generate different RPMS. I copied my curl-7.50.0.tar.gz
The script which generates the package is the SPEC file and resides in the SPECS directory. Everything is scripted into this file. The various major sections of a spec file are header, installation preparation, build, install, files, post install and clean-up. There are many more, but we would concentrate on these. The spec file is as follows and these 2 files are all that you need to generate your rpm.

[palash@localhost SPECS]$ cat curl.spec 
%define name owncurl
%define tarball curl
%define version 7.50.0
%define release 1

%define curlroot %{_builddir}/%{tarball}-%{version}
%define curlprefix /opt/owncurl

Summary: get a file from an FTP or HTTP server.  
Name: %{name}  
Version: %{version}  
License: MIT  
Release: %{release}  
Vendor: Daniel Stenberg <Daniel.Stenberg@haxx.se>  
Packager: Troy Engel <tengel@sonic.net>  
Group: Utilities/Console  
Source: %{tarball}-%{version}.tar.gz  
URL: http://curl.haxx.se/  
BuildRoot: %{_tmppath}/%{tarball}-%{version}-root  
Requires: libssh2  
AutoReqProv: no


%description
curl is a client to get documents/files from servers, using any of the  
supported protocols. The command is designed to work without user  
interaction or any kind of interactivity.

curl offers a busload of useful tricks like proxy support, user  
authentication, ftp upload, HTTP post, file transfer resume and more.

%package    devel
Summary:    The includes, libs, and man pages to develop with libcurl  
Group:        Development/Libraries  
Provides:    curl-devel  
AutoReqProv: no


%description devel
libcurl is the core engine of curl; this packages contains all the libs,  
headers, and manual pages to develop applications using libcurl.

%prep

%setup -q -n %{tarball}-%{version}

%build
cd %{curlroot} && (if [ -f configure.in ]; then mv -f configure.in configure.in.rpm; fi)

%define CONFIG_FLAGS --prefix=%{curlprefix} --with-ssl=/opt/alt-openssl/ --enable-http --enable-ftp
%define LDFLAGS -L/opt/alt-openssl/lib
%define CPP_FLAGS -I/opt/alt-openssl/include
%define SSL_LDFLAGS -L/opt/alt-openssl/lib
%define SSL_CPPFLAGS -I/opt/alt-openssl/include

./configure  %{CONFIG_FLAGS}

cd %{curlroot} && (if [ -f configure.in.rpm ]; then mv -f configure.in.rpm configure.in; fi)  
make

%install
[ "%{buildroot}" != "/" ] && rm -rf %{buildroot}
make DESTDIR=%{buildroot} install-strip

%clean
[ "%{buildroot}" != "/" ] && rm -rf %{buildroot}
[ "%{curlroot}" != "/" ] && rm -rf %{curlroot}

%post
/sbin/ldconfig

%postun
/sbin/ldconfig

%files
%defattr(-,root,root)
%attr(0755,root,root) %{curlprefix}/bin/curl
%attr(0644,root,root) %{curlprefix}/share/man/man1/curl.1*
%{curlprefix}/lib/libcurl.so*
%{curlprefix}/lib/pkgconfig/libcurl.pc*
%{curlprefix}/lib/libcurl.so*
%{curlprefix}/share/aclocal/libcurl.m4
#%{_datadir}/curl/curl-ca-bundle.crt
%doc CHANGES COPYING README docs/BUGS docs/SSLCERTS
%doc docs/CONTRIBUTE docs/FAQ docs/FEATURES docs/HISTORY docs/INSTALL
%doc docs/KNOWN_BUGS docs/MANUAL docs/RESOURCES docs/THANKS
%doc docs/TODO docs/VERSIONS docs/TheArtOfHttpScripting tests

%files devel
%defattr(-,root,root)
%attr(0755,root,root) %{curlprefix}/bin/curl-config
%attr(0644,root,root) %{curlprefix}/share/man/man1/curl-config.1*
%attr(0644,root,root) %{curlprefix}/share/man/man3/*
%attr(0644,root,root) %{curlprefix}/include/curl/*
%{curlprefix}/lib/libcurl.a
%{curlprefix}/lib/libcurl.la
%doc docs/BINDINGS docs/INTERNALS docs/examples/* 
[palash@localhost ~]$ 

Generate the RPM:

To generate the RPM, all you have to do is run and it will generate the rpm package

rpmbuild -ba

The SPEC file, explained:

The %prep section will un-tar the archive file in the BUILD directory. I have named the package Owncurl so that it does not conflict with the native curl on the system. The %setup will switch the working directory for %build to the directory where the files are extracted. If the folder is of a different name, than your package, then you need to pass the -n parameter. In the %build portion of the spec file, we have configured curl to pick up the native installation of openssl. The %install code decides the directories which this code will be installed into post building. And similarly %files section lists all the files to be packaged in the RPM. Make sure that you push in all the files in your buildroot to the %files  section to avoid errors.
Thanks for reading! For any doubts or clarifications, feel free to drop me an email.