指定新的工作区_topdir
默认的工作区为/usr/src/redhat
;通过配置文件rpmmacros
指定:
cat>>~/.rpmmacros<<end
> %_topdir /root/helloworld
> end
这个配置也可以用命令行
来指定:
rpmbuild --define "_topdir /root/helloworld"
在工作区创建工作目录
mkdir -p /root/helloworld/{RPMS,SRPMS,BUILD,SOURCES,SPECS}
工作目录解释:
- helloworld/BUILD: 命令rpmbuild将在这个目录下解压源文件,并在这个目录下编译程序
- helloworld/SPECS: 存放spec文件spec files
- helloworld/SOURCES: 存放源码包source files
- helloworld/SRPMS: 存放包含源码的rpm文件
- helloworld/RPMS: 存放包含二进制的rpm包文件
创建SPEC文件
helloworld/SPECS/hello.spec
:
Summary: hello world rpm package
Name: hello
Version: 0.1
Release: 1
Source: %{name}-%{version}.tar.gz
License: GPL
Packager: amoblin
Group: Application/System
BuildRoot: %{_topdir}/root/
%description
This is a software for making your life more beautiful!
%prep
rm -rf $RPM_BUILD_DIR/%{name}-%{version}
zcat $RPM_SOURCE_DIR/%{name}-%{version}.tar.gz | tar -xv
%build
cd %{name}-%{version}
gcc -o hello hello.c
%install
rm -rf %{buildroot}
mkdir -p %{buildroot}/usr/local/bin/
cd %{name}-%{version}
cp hello %{buildroot}/usr/local/bin/hello
%files
/usr/local/bin/hello
建立源文件
hello.c
:
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
把源文件打包放到SOURCES
目录下
mkdir -p hello-0.1
mv hello.c hello-0.1
tar czvf hello-0.1.tar.gz hello-0.1
mv hello-0.1.tar.gz /root/helloworld/SOURCES/
编译RPM包
#编译rpm
rpmbuild -vv -ba helloworld/SPECS/hello.spec
#或者通过命令行指定topdir编译
rpmbuild --define "_topdir /root/helloworld" -vv -ba helloworld/SPECS/hello.spec
#验证SPEC
rpmbuild --define "_topdir /root/helloworld" -vv -bl helloworld/SPECS/hello.spec
查看生成的RPM包类容:
[root@localhost ~]# rpm -qpil helloworld/RPMS/i386/hello-0.1-1.i386.rpm
安装和卸载生成的RPM包
[root@localhost ~]# rpm -iv helloworld/RPMS/i386/hello-0.1-1.i386.rpm
Preparing packages for installation...
hello-0.1-1
[root@localhost ~]# hello
Hello, World!
[root@localhost ~]# rpm -ev hello-0.1-1
再次查看一下到底都装了些什么东西
[root@localhost ~]# rpm -ql hello
/usr/local/bin/hello
SPEC文件中的一些重要字段:
- Group: 需要需先定义[
less /usr/share/doc/rpm-*/GROUPS
] - BuildRoot: 安装阶段
install
的根目录(在%build
阶段之后). 默认的根目录是%{_topdir}/BUILDROOT/
- %prep:
prepare
准备阶段调用的shell脚本 - %build:
build
编译阶段调用的shell脚本 - %install:
install
安装阶段候调用的shell脚本
注意: 执行%prep, %build, %install 的shell脚本的pwd
目录为/root/helloworld/BUILD - %files: 指定生成的二进制RPM包中包含哪些文件,这些文件必须在
BuildRoot
目录下存在