打包前的准备&需要了解的信息
系统环境:openEuler 20.03 LTS SP3
依赖工具:rpmbuild rpmdevtools
yum install rpmbuild rpmdevtools -y  
软件源码(这里拿GNU的helloworld程序作为示例):http://ftp.gnu.org/gnu/hello/hello-2.12.tar.gz
SPEC阶段和目录的对应关系(RPM 包的构建 - 实例 - Michael翔 - 博客园 (cnblogs.com))
| 阶段 | 读取的目录 | 写入的目录 | 具体动作 | 
|---|---|---|---|
| %prep | %\_sourcedir | %\_builddir | 读取位于 %\_sourcedir 目录的源代码和 patch 。之后,解压源代码至 %\_builddir 的子目录并应用所有 patch。 | 
| %build | %\_builddir | %\_builddir | 编译位于 %\_builddir 构建目录下的文件。通过执行类似 ./configure && make 的命令实现。 | 
| %install | %\_builddir | %\_buildrootdir | 读取位于 %\_builddir 构建目录下的文件并将其安装至 %\_buildrootdir 目录。这些文件就是用户安装 RPM 后,最终得到的文件。注意一个奇怪的地方: 最终安装目录 不是 构建目录。通过执行类似 make install 的命令实现。 | 
| %check | %\_builddir | %\_builddir | 检查软件是否正常运行。通过执行类似 make test 的命令实现。很多软件包都不需要此步。 | 
| bin | %\_buildrootdir | %\_rpmdir | 读取位于 %\_buildrootdir 最终安装目录下的文件,以便最终在 %\_rpmdir 目录下创建 RPM 包。在该目录下,不同架构的 RPM 包会分别保存至不同子目录, noarch 目录保存适用于所有架构的 RPM 包。这些 RPM 文件就是用户最终安装的 RPM 包。 | 
| src | %\_sourcedir | %\_srcrpmdir | 创建源码 RPM 包(简称 SRPM,以.src.rpm 作为后缀名),并保存至 %\_srcrpmdir 目录。SRPM 包通常用于审核和升级软件包。 | 
打包工具的使用
通常,我们不使用root用户进行这一系列的操作,所以我们可以在系统中添加一个rpmbuilder用户用来进行打包相关操作,具体命令如下:
useradd rpmbuilder
passwd rpmbuilder #set your pwd
su - rpmbuilder
创建工作目录
如果提示command not found  
执行yum install tree nano vim -y即可。
[rpmbuilder@VM ~]# rpmdev-setuptree
[rpmbuilder@VM ~]# tree rpmbuild
rpmbuild
├── BUILD
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS
此命令会在$HOME里创建工作目录。
软件编译构建
我们需要先了解该软件的构建方式,获取源码解压后我们可以看到以下文件列表:
可见,存在configure文件,可以用 ./configure; make; make install 方式编译安装。
构建时,需要使源码文件位于$HOME/rpmbuild/SOURCES/文件夹内。
spec文件的格式与编写
nano是Linux下一个优秀的文本编辑器,极易上手,部分操作如下:  
打开文件:
nano filename  
Ctrl+X->Y->Enter 保存三步骤  
Ctrl+W 查找相关字符串
spec文件一般以name-version.spec命名,其部分内容如下(spec文件内不应有注释):
[rpmbuilder@VM ~]# rpmdev-newspec -o hello-2.12.spec #利用工具新建spec文件
[rpmbuilder@VM ~]# nano hello-2.12.spec
[rpmbuilder@VM ~]# cat hello-2.12.spec
Name:           hello
Version:        2.12
Release:        1%{?dist}
Summary:        Hello World!
License:        GPL v3+
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_build
%install
make install DESTDIR=%{buildroot} #install到$HOME/rpmbuild/BUILDROOT/%{name}-%{version}.%{arch}目录
%find_lang %{name} #本地化的一些操作
rm -f %{buildroot}/%{_infodir}/dir #执行简单的清理
%clean #编译后执行清理
%pre #安装前
%post #安装后
/sbin/install-info %{_infodir}/%{name}.info %{_infodir}/dir || :
%preun #卸载执行前
if [ $1 = 0 ] ; then
/sbin/install-info --delete %{_infodir}/%{name}.info %{_infodir}/dir || :
fi
%postun #卸载执行后
%files -f %{name}.lang
%doc AUTHORS ChangeLog NEWS README THANKS TODO #将上述文件复制至doc文件夹里
%license COPYING #将上述文件复制至相应license文件夹里
%{_mandir}/man1/hello.1.* #主要文件 %{_mandir}为宏定义 位于%{buildroot}目录下/usr/share/man/
%{_infodir}/hello.info.* #/usr/share/info/
%{_bindir}/hello #/usr/bin/
%changelog
* Wed Apr 20 2022 root
- 
类似的 还有 %{\\_includedir} %{\\_libdir}
%file标记可以将%{buildroot}目录下的文件安装至任意目录。
重要: 不要%{\_bindir}直接写到%file标记下,这样安装时没问题,卸载软件包时会将/usr/bin/里的文件全部删除。
打包相关操作
编写好spec文件后,可以开始进行打包操作:
[rpmbuilder@VM ~]# rpmbuild -bb hello-2.12.spec #只生成源码格式的rpm包  
命令执行完成后 可以安装测试效果
[rpmbuilder@VM ~]# sudo rpm -ivh $HOME/rpmbuild/RPMS/aarch64/hello-2.12-1.aarch64.rpm
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:hello-2.12-1                     ################################# [100%]
如果提示rpmbuilder用户不在sudoer file里,root添加里添加一下即可或是切换至root用户安装。
安装完成后测试各个功能是否正常:
[rpmbuilder@VM ~]# hello
Hello, world!
[rpmbuilder@VM ~]# hello -t
hello, world
[rpmbuilder@VM ~]# hello --traditional
hello, world
[rpmbuilder@VM ~]# hello -g
hello: option requires an argument -- 'g'
Try 'hello --help' for more information.
[rpmbuilder@VM ~]# hello -g "Hello,openEuler!"
Hello,openEuler!
[rpmbuilder@VM ~]# hello --version
hello (GNU Hello) 2.12.1-6fe9
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Karl Berry, Sami Kerola, Jim Meyering,
and Reuben Thomas.
[rpmbuilder@VM ~]# rpm -qi hello
Name        : hello
Version     : 2.12
Release     : 1
Architecture: aarch64
Install Date: Wed 20 Apr 2022 04:43:20 PM CST
Group       : Unspecified
Size        : 210280
License     : GPL v3+
Signature   : (none)
Source RPM  : hello-2.12-1.src.rpm
Build Date  : Wed 20 Apr 2022 04:36:34 PM CST
Build Host  : ecs-20ed
URL         : http://ftp.gnu.org/gnu/hello
Summary     : Hello World!
Description :
The "Hello World" program, done with all bells and whistles of a proper FOSS
project, including configuration, build, internationalization, help files, etc.

6 条评论
华纳东方明珠客服电话是多少?(??155--8729--1507?《?薇-STS5099】【?扣6011643?】
华纳东方明珠开户专线联系方式?(??155--8729--1507?《?薇-STS5099】【?扣6011643?】
华纳东方明珠客服电话是多少?(▲18288362750?《?微信STS5099? 】
如何联系华纳东方明珠客服?(▲18288362750?《?微信STS5099? 】
华纳东方明珠官方客服联系方式?(▲18288362750?《?微信STS5099?
华纳东方明珠客服热线?(▲18288362750?《?微信STS5099?
华纳东方明珠24小时客服电话?(▲18288362750?《?微信STS5099? 】
华纳东方明珠官方客服在线咨询?(▲18288362750?《?微信STS5099?
如何申请华纳公司账户?
华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
新车首发,新的一年,只带想赚米的人coinsrore.com
新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
新车上路,只带前10个人coinsrore.com
新盘首开 新盘首开 征召客户!!!coinsrore.com
新项目准备上线,寻找志同道合的合作伙伴coinsrore.com
新车即将上线 真正的项目,期待你的参与coinsrore.com
新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com
新车上路,只带前10个人coinsrore.com