Source code package build (build) binary package process

background

Sometimes when we install a software package on the Linux system, we hope to install it quickly and simply, instead of compiling it with the source package every time. If we use the rpm report of the general Linux environment, we will use the latest rpm packaging tool rpmbuild here.

Command description

The rpmbuild command is used to create rpm binary packages and source code packages. This command comes from the rpm-build package. If the system does not have it, you can install it: yum install -y rpm-build; you can also install rpmdevtools. This tool department contains some other tools:

yum install -y rpmdevtools

Syntax
rpmbuild (options)

eg1: rpmbuild -ba'spec file path' //rpm is divided into binary rpm and source rpm, the spec file is the decompressed file of the rpm source code

Option
-initdb: Initialize the RPM database;

--Rebuilddb: Rebuild the RPM database in the direction from the installed header files;

-ba: create binary and source code packages;

-bb: create a binary code package;

-bs: Create a source code package.

When the local source package is successfully compiled and installed, we have generated the spec file (the file should end with .spec), then we can establish a packaging environment, that is, the establishment of a directory tree, which is generally established in the ~/rpmbuild directory 5 directories. as follows:

BUILD: The directory is used to store the source files in the packaging process, which is derived from SOURCE.
SOURCE: Used to store the source files and patches used for packaging, mainly some tar packages.
SPEC: Used to store the spec files.
SRPM: Store packaged and generated Rpm format source file
RPM: binary file

The rpmbuild command uses a standardized "workspace" to generate the %_topdir working directory ~/rpmbuild and the configuration file ~/.rpmmacros

The command rpmdev-setuptree is brought by installing rpmdevtools. You can see that after running this command, there is an additional folder called rpmbuild in the $HOME home directory, and the BUILD, RPMS, SOURCES, SPECS, and SRPMS directories are automatically created in the current user's home/rpmbuild directory. If rpmdevtools is not installed, use the mkdir command to create these folders:

mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

【limit】

1) The version of rpm<=4.4.x, the default working path of the rpmbuid tool is /usr/src/redhat. Due to permissions issues, ordinary users cannot make rpm packages. You must switch to root when making rpm packages.

2) rpm Starting from version 4.5.x, move the default working path of rpmbuid to the rpmbuild directory under the user's home directory , namely $HOME/rpmbuild , and it is recommended that users try not to operate as root when making rpm packages.

Operation process

1) Generate SPEC file
SPEC file, the naming format is generally in the form of "software name-version.spec", copy it to the SPECS directory.
If the system has the rpmdevtools tool, you can use the rpmdev-newspec -o name.spec command to generate a template of the SPEC file, and then modify it. If rpmdevtools is not installed, you can also create a spec file manually. The spec file is as follows:

Name:           myapp
Version:
Release:        1%{
    
    ?dist}
Summary:

License:
URL:
Source0:

BuildRequires:
Requires:

%description


%prep
%setup -q


%build
%configure
make %{
    
    ?_smp_mflags}


%install
rm -rf $RPM_BUILD_ROOT
%make_install


%files
%doc

2) Execute package compilation in the rpmbuild/SPECS directory, switch to this directory to execute the package compilation command:

rpmbuild -bb software name-version.spec //Only generate rpm packages in binary format, and the generated files will exist in the RPM directory just created

rpmbuild -bs software name-version.spec //only generate rpm package in src format rpmbuild -bp software name-version.spec
//only need to generate the complete source file, that is, unpack the tar package and merge all the patch files And generate a complete source file with the latest functions
rpmbuild -ba software name-version.spec //After the software package is made, you can use the rpm command to query to see the effect. If you are not satisfied, you can modify the package description file again and rerun the above command to generate a new RPM package.

3) Example:
Copy all the source codes, shell scripts, and configuration files used to generate the rpm package to the SOURCES directory. Note that the source code is usually compressed in *.tar.gz format.

cd ~/rpmbuild/SOURCES
wget http://ftp.gnu.org/gnu/hello/hello-2.10.tar.gz

Edit the SPEC file:
cd ~/rpmbuild/SPECS
vim hello.spec

Name:     hello
Version:  2.10
Release:  1%{
    
    ?dist}
Summary:  The "Hello World" program from GNU
Summary(zh_CN):  GNU "Hello World" 程序
License:  GPLv3+
URL:      http://ftp.gnu.org/gnu/hello
Source0:  http://ftp.gnu.org/gnu/hello/%{
    
    name}-%{
    
    version}.tar.gz

BuildRequires:  gettext
Requires(post): info
Requires(preun): info

%description
The "Hello World" program, done with all bells and whistles of a proper FOSS
project, including configuration, build, internationalization, help files, etc.

%description -l zh_CN
"Hello World" 程序, 包含 FOSS 项目所需的所有部分, 包括配置, 构建, 国际化, 帮助文件等.

%prep
%setup -q

%build
%configure
make %{
    
    ?_smp_mflags}

%install
make install DESTDIR=%{
    
    buildroot}
%find_lang %{
    
    name}
rm -f %{
    
    buildroot}/%{
    
    _infodir}/dir

%post
/sbin/install-info %{
    
    _infodir}/%{
    
    name}.info %{
    
    _infodir}/dir || :

%preun
if [ $1 = 0 ] ; then
/sbin/install-info --delete %{
    
    _infodir}/%{
    
    name}.info %{
    
    _infodir}/dir || :
fi

%files -f %{
    
    name}.lang
%doc AUTHORS ChangeLog NEWS README THANKS TODO
%license COPYING
%{
    
    _mandir}/man1/hello.1.*
%{
    
    _infodir}/hello.info.*
%{
    
    _bindir}/hello

%changelog
* Sun Dec 4 2016 Your Name <[email protected]> - 2.10-1
- Update to 2.10
* Sat Dec 3 2016 Your Name <[email protected]> - 2.9-1
- Update to 2.9

Build the RPM package: rpmbuild -ba hello.spec

Appendix: The difference between configure, make, and make install commands

1) configure it is a shell script, ./configure then runs this shell script! ./configure is used to detect the target characteristics of your installation platform. For example, it will detect whether you have CC or GCC, but does not need CC or GCC. This step is generally used to generate Makefile to prepare for the next compilation. The installation can be controlled by adding parameters after configure. For example, ./configure --prefix=/usr. It means that if the software is installed under /usr, the executable file will be installed in /usr/bin (if not specified, the default path is /usr/local/bin), and the resource file will be installed in /usr/share (and Not the default /usr/local/share). You can view the detailed help through ./configure --help. gcc includes many compilers (C, C++, Objective-C, Ada, Fortran, and Java). GCC can be used to compile programs in C/C++, FORTRAN, JAVA, OBJC, ADA and other languages. You can choose to install and support programs according to your needs. Language.
2) make is used to compile, it reads instructions from Makefile, And then compile. If there is an error during the make process, you have to write down the error code (not only the last line), and then you can submit a bugreport to the developer (usually there is a submission address in INSTALL), or your system is less dependent Libraries, etc., you need to carefully study the error code yourself. make is a control program for automatic compilation in the Linux development kit. It uses the compilation specifications written in the Makefile. Automatically call gcc, ld, and run some required programs to compile programs. Makefile is used for automatic compilation and linking. A project consists of many files. Each file change will cause the project to be relinked. Makefile can record file information and decide which files need to be recompiled when linking.
3) make install is to put the compiled binary files, libraries, configuration files, etc. in the corresponding directory
4) make uninstal is to uninstall, without parameters, the source code is compiled by default.
5) make clean clear the compilation results

Guess you like

Origin blog.csdn.net/ximenjianxue/article/details/113883536