使用rpmbuild构建rpm包流程和问题记录
rpmbuild的基础阶段包括:prep(预处理)、build(编译)、install(安装)、check(测试)、clean(清理)。
1、安装工具、生成目录结构
yum install -y git rpm-build rpmdevtools rpmlint dnf-plugins-core
rpmdev-setuptree
2、下载源码、放在对应目录
cd && git clone https://gitee.com/src-oepkgs-fedora-rv/autossh.git
cp /root/autossh/autossh.spec /root/rpmbuild/SPECS/
cp /root/autossh/* /root/rpmbuild/SOURCES/
spectool -g -R /root/rpmbuild/SPECS/autossh.spec
3、编译、质量检查、安装验证
cat /etc/rpm/macros.dist # 该文件用于定义宏变量
rpmbuild -ba /root/rpmbuild/SPECS/autossh.spec
rpmlint /root/rpmbuild/RPMS/x86_64/autossh-*.rpm
rpm -ivh /root/rpmbuild/RPMS/x86_64/autossh-*.rpm
4、RPM开发常用命令
rpmdev-extract nginx-1.24.0-1.el7.x86_64.rpm # 解压RPM包到当前目录
rpmdev-setuptree # 创建RPM构建目录结构
rpmdev-newspec # 创建一个新的spec文件模板
rpmspec -P nginx.spec # 解析spec文件到标准输出(替换宏)
rpmspec -q nginx.spec # 查看spec生成的二进制rpm包
rpmspec -q --buildrequires nginx.spec # 查看spec文件的build依赖
spectool -l nginx.spec # 列出spec文件中定义的Source和Patch文件
spectool -g nginx.spec # 下载spec文件中定义的Source和Patch文件到当前目录
spectool -g -R nginx.spec # 下载Source和Patch文件到%{_sourcedir}目录
yum-builddep -y nginx.spec # 为spec文件安装构建依赖
yum-builddep --downloadonly --downloaddir=/tmp nginx.spec # 仅下载spec文件构建依赖
yumdownloader nginx # 下载example的rpm包(不包括依赖)
yumdownloader --resolve nginx # 下载example的rpm及其依赖包
yum install -y nginx --downloadonly --downloaddir=/tmp # 与上一条命令等价
rpm --setperms -a # 重置rpm软件包回到最初安装时的默认权限状态
rpm --setperms --setugids -a # 重置权限、所有者和组
5、RPM开发错误记录
5.1 /var/tmp/rpm-tmp.xxxxxx: line xx: fg: no job control
line xx: fg: no job control意味着没有定义这个宏(宏定义位于/usr/lib/rpm/macros和/usr/lib/rpm/macros.d/macros.*),需安装相关的依赖包(如%pom_remove_plugin宏由包javapackages-local提供)。当然也可修改spec文件将引用的宏自行实现。
如果是%cmake_build或者%cmake_install宏未定义(由cmake-rpm-macros包提供),可以改为传统的make或make install,如下:
%build
#%cmake_build
make %{?_smp_mflags}
%install
#%cmake_install
make install DESTDIR=%{buildroot}
5.2 error: Architecture is not included: x86_64
架构不匹配导致,在rpmbuild命令中指定目标架构即可:
rpmbuild --target=aarch64 -ba /root/rpmbuild/SPECS/xxx.spec
5.3 configure: error: C compiler cannot create executables
checking whether the C compiler works... no
configure: error: in `/home/abuild/rpmbuild/BUILD/xorg-server-1.20.8':
configure: error: C compiler cannot create executables
See `config.log' for more details
error: Bad exit status from /var/tmp/rpm-tmp.AmtX1u (%build)
编译和链接选项的环境变量导致,需要检查路径是否正确。如检查generic-hardened-ld(由redhat-rpm-config/centos-rpm-config等提供)的路径是否正确,然后修改spec文件。
export LDFLAGS="$RPM_LD_FLAGS -specs=/usr/lib/rpm/generic-hardened-ld"
export CXXFLAGS="$RPM_OPT_FLAGS -specs=/usr/lib/rpm/generic-hardened-cc1"
export CFLAGS="$RPM_OPT_FLAGS -specs=/usr/lib/rpm/generic-hardened-cc1"
5.4 缺少依赖
No matching package to install: 'libcurl-devel >= 7.62.0'
nothing provides pkgconfig(gssdp-1.0) need by gupnp-devel
nothing provides libcrypto.so.10()(64bit) need by mysql-community-server-8
# yum provides支持多种查询语法
yum provides 'libcurl-devel >= 7.62.0'
yum provides 'pkgconfig(gssdp-1.0)'
yum provides 'libcrypto.so.10()(64bit)'
5.5 git lfs install 命令提示:git:'lfs' 不是一个 git 命令。参见 'git --help'。
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash # Debian/Ubuntu
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.rpm.sh | sudo bash # RedHat/CentOS
sudo yum install git-lfs # RedHat/CentOS
sudo apt-get install git-lfs # Debian/Ubuntu
# 手动指定发行版(适用国产发行版)
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.rpm.sh | sudo os=el dist=8 bash
yum install git-lfs
git lfs install 命令用于初始化 Git Large File Storage (LFS) 的配置,主要完成以下操作:
(1)设置全局 Git 钩子:在本地 Git 配置中添加必要的钩子脚本,用于在提交、推送或拉取时自动处理大文件。
(2)优化存储空间:通过将大型文件转换为指针文件(仅存储文件变更部分),减少仓库体积膨胀问题。
(3)激活 LFS 功能:确保 Git 操作(如 push、pull)能正确识别并处理 LFS 跟踪的文件。
5.6 WARNING: 'automake-1.16' is missing on your system.
automake版本不匹配,修改spec文件,将Makefile相关文件的automake版本号改为当前系统的automake版本号
find . -type f -exec grep -q "aclocal-1.16" {} \; -exec sed -i 's/aclocal-1.16/aclocal-1.17/g' {} +
find . -type f -exec grep -q "automake-1.16" {} \; -exec sed -i 's/automake-1.16/automake-1.17/g' {} +
sed -i "s/am__api_version='1.16'/am__api_version='1.17'/g" configure
sed -i "s/am__api_version='1.16'/am__api_version='1.17'/g" aclocal.m4
sed -i "s/\[1.16.5\]/\[1.17\]/g" aclocal.m4
# 直接sed命令修改spec文件
sed -i '67i\find . -type f -exec grep -q "aclocal-1.16" {} \\; -exec sed -i '\''s/aclocal-1.16\/aclocal-1.17/g'\'' {} +' xz.spec
sed -i '68i\find . -type f -exec grep -q "automake-1.16" {} \\; -exec sed -i '\''s/automake-1.16\/automake-1.17/g'\'' {} +' xz.spec
sed -i '69i\sed -i "s/am__api_version='\''1.16'\''/am__api_version='\''1.17'\''/g" configure' xz.spec
sed -i '70i\sed -i "s/am__api_version='\''1.16'\''/am__api_version='\''1.17'\''/g" aclocal.m4' xz.spec
sed -i '71i\sed -i "s/\\[1.16.5\\]/\\[1.17\\]/g" aclocal.m4' xz.spec
5.7 CMake Error at CMakeLists.txt:373 (message): In-source builds are not allowed.
核心问题:构建目录配置不当,CMake 禁止在源代码目录内直接生成构建文件(“In-source build”),要求使用独立的构建目录。
检查spec文件是否有类似%define __cmake_in_source_build的定义,修复构建逻辑。
5.8 error: Empty %files file /root/rpmbuild/BUILD/xxx/debugfiles.list
解决方案:禁止生成调试信息包(debug package),在spec文件开头增加如下代码:
%global _enable_debug_package 0
%global debug_package %{nil}

浙公网安备 33010602011771号