CentOS7 RPM构建

yuanheci 2025年06月02日 18次浏览

参考教程

https://www.cnblogs.com/yipianchuyun/p/15442896.html

按照教程成功构建:
image-1748833573895

image-1748833588923


SPEC文件解析

关键:RPM预定义了很多宏

RPM SPEC 文件详解:以 hello 程序为例

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 <youremail@xxx.xxx> - 2.10-1
- Update to 2.10
* Sat Dec 3 2016 Your Name <youremail@xxx.xxx> - 2.9-1
- Update to 2.9

这是一个完整的 RPM SPEC 文件,用于构建 GNU hello 程序的 RPM 包。这个文件定义了从源码到 RPM 包的完整流程,下面我将逐段解释其结构和功能。

  1. 头部元数据(Header)
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

关键字段说明:

  • Name/Version/Release:
    定义包名、版本号和发布号。%{?dist} 是一个宏,会被自动替换为发行版标识(如 .el7),用于区分不同发行版的包。
  • Summary:
    包的简短描述,支持多语言(通过 (zh_CN) 后缀指定中文描述)。
  • Source0:
    源代码的下载地址。%{name} 和 %{version} 是宏,会被替换为实际的包名和版本号。
  1. 构建依赖(BuildRequires)
BuildRequires:  gettext
Requires(post): info
Requires(preun): info

功能:

  • BuildRequires:
    声明构建过程所需的依赖包(如 gettext 用于国际化支持)。
  • Requires(post) 和 Requires(preun):
    声明安装后(post)和卸载前(preun)脚本所需的依赖(如 info 包提供 install-info 工具)。
  1. 描述段落(% description)
%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 项目所需的所有部分, 包括配置, 构建, 国际化, 帮助文件等.

功能:
提供软件的详细描述,支持多语言(通过 -l zh_CN 指定中文描述)。

  1. 准备阶段(% prep)
%prep
%setup -q

功能:
解压源代码包并进入源码目录。
%setup 是一个宏,等价于 tar xzf … + cd 操作。
-q 表示静默模式,减少输出信息。

  1. 编译阶段(% build)
%build
%configure
make %{?_smp_mflags}

功能:

  • %configure:执行 ./configure 脚本,检测系统环境并生成 Makefile。
  • make %{?_smp_mflags}:编译源代码。%{?_smp_mflags} 会根据系统 CPU 核心数自动启用并行编译(如 -j4)。
  1. 安装阶段(% install)
%install
make install DESTDIR=%{buildroot}
%find_lang %{name}
rm -f %{buildroot}/%{_infodir}/dir

功能:

  • make install DESTDIR=%{buildroot}:
    将编译后的文件安装到 %{buildroot} 目录(这是一个临时目录,而非系统实际路径)。
  • %find_lang %{name}:
    自动检测并生成语言支持文件(用于国际化)。
  • rm -f %{buildroot}/%{_infodir}/dir:
    删除 Info 目录中的 dir 文件,避免与其他包冲突。
  1. 安装后脚本(% post)
%post
/sbin/install-info %{_infodir}/%{name}.info %{_infodir}/dir || :

功能:
在包安装后,将 hello.info 文件添加到系统 Info 索引中。

  • || : 表示忽略命令执行失败(确保脚本不会因错误而终止)。
  1. 卸载前脚本(% preun)
%preun
if [ $1 = 0 ] ; then
/sbin/install-info --delete %{_infodir}/%{name}.info %{_infodir}/dir || :
fi

功能:
在完全卸载包(而非升级)时,从系统 Info 索引中删除 hello.info 文件。
$1 是 RPM 传递给脚本的参数(0 表示完全卸载,1 表示升级)。

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

功能:

  • -f %{name}.lang:
    包含由 %find_lang 生成的语言文件列表。
  • %doc/%license:
    标记文档和许可证文件(这些文件会被特殊处理,如自动安装到 /usr/share/doc)。
  • %{_mandir}/%{_infodir}/%{_bindir}:
    指定二进制文件、手册页和 Info 文件的安装路径。
  1. 变更日志(% changelog)
%changelog
* Sun Dec 4 2016 Your Name <youremail@xxx.xxx> - 2.10-1
- Update to 2.10
* Sat Dec 3 2016 Your Name <youremail@xxx.xxx> - 2.9-1
- Update to 2.9

功能:
记录包的变更历史,格式为:

* 日期 维护者 版本号-发布号
- 变更描述

总结:SPEC 文件的核心作用
这个 SPEC 文件定义了:

  • 软件元数据(名称、版本、许可证等)
  • 构建依赖(如 gettext)
  • 编译和安装流程(从源码到二进制的转换)
  • 安装 / 卸载脚本(管理 Info 索引)
  • 打包规则(哪些文件被包含在 RPM 包中)
    通过这个文件,RPM 构建系统可以自动完成从下载、编译到打包的全过程,确保软件在不同环境中一致安装。