Linux工具篇--rpmbuild打包代码

383 阅读3分钟

简介

rpmbuild可以将代码打包为linux可识别格式,通过rpm管理代码的升级,安装,降级,删除等操作。实现对代码的发布和管理的可视化,增加对代码的可控性。

1. 信息定义阶段

### 自定义宏,不是必须,自定义一个宏名字为nginx_user值为nginx,%{nginx_user}引用 
%define nginx_user nginx

###  软件名字,要与spec的文件名一致 openresty
Name:           ngx_openresty

### 软件主版本号,参考下载的源码包
Version:        1.9.7.1

###  发行编号,每打包一次值递增,主版本号发布新版后需重置该值
Release:        4%{?dist}

###  一行简短的软件简介,结尾不要加标点
Summary:        OpenResty  is a full-fledged web platform

###  安装后所属的组, 通过/usr/share/doc/rpm-4.8.0/GROUPS选择,部分发行版已经废除此标签
Group:          System Environment/Daemons

### 软件许可
License:        GPLv2

###  软件项目主页
URL:           https://openresty.org/

###  放置在SOUIRCES目录的软件源码包名,可以指定多个:source1、source2等
Source0:        %{name}-%{version}.tar.gz

###  补丁名,也可以写多个patch1、patch2等
#patch0:            0.patch

### 在 install阶段的测试安装目录,方便写files
#buildroot:     %_topdir/BUILDROOT

### 编译过程所需的软件
BuildRequires:  gcc,make

### 安装软件包时所需的依赖包列表,可以指定版本如 bash >= 1.1.1
Requires:       readline-devel,pcre-devel,openssl-devel

### 程序的详细多行描述,每行必须小于等于 80 个字符,空行表示开始新段
%description 
OpenResty is a full-fledged web platform by integrating the standard Nginx core,
LuaJIT, many carefully written Lua libraries, lots of high quality 
3rd-party Nginx modules, and most of their external dependencies. 
It is designed to help developers easily build scalable 
web applications, web services, and dynamic web gateways.
this rpm created by 52os.net

2.准备阶段

%prep    
### 静默模式解压并进入解压后的目录,也常用:%autosetup -n %{name}                                            
#%autosetup -n %{name}-%{version}.tar.gz
%setup -q        
### 需要打补丁,在这里写打补丁的命令                         
#%patch0 -p1

3. 编译阶段

%build 
### 编译参数
./configure \
    --with-luajit \
    --without-http_redis2_module \
    --without-http_xss_module \
    --without-http_memc_module \
    --user=nginx \
    --group=nginx \

#使用多核处理器并行编译 
make %{?_smp_mflags}

4.安装阶段

%install 

### 删除之前的残留文件
rm -rf %{buildroot}   

### 指定安装目录为虚拟目录    
#gmake install DESTDIR=%{buildroot} 
gmake install DESTDIR=$RPM_BUILD_ROOT
#如果makefile不支持make install DESTDIR=$RPM_BUILD_ROOT,可以手写安装流程,即先建好目标目录,在复制文件

### rpm安装前制行的脚本 
%pre  

### $1==1 代表的是第一次安装,2代表是升级,0代表是卸载 
if [ $1 == 1 ];then     
     /usr/sbin/useradd -r %{nginx_user} 2> /dev/null 
fi 

### 安装后执行的脚本
%post        

###卸载前执行的脚本
%preun       
if [ $1 == 0 ];then 
     /usr/sbin/userdel -r %{nginx_user} 2> /dev/null 
fi 

### 卸载后执行的脚本
%postun

5.清理阶段

%clean 
### 删除buildroot目录
rm -rf %{buildroot}

6.文件设置阶段

%files  
### 设定默认权限,如果下面没有指定权限,则继承默认
%defattr (-,root,root,0755)    

###要打包的文件和目录,在执行完rpmbuild -bi后,参考%{rootbuild}下生成的文件和目录
/usr/

7. 变更日志

格式固定,生成请用命令:

rpmdev-bumpspec --comment=COMMENT --userstring=NAME+EMAIL_STRING SPECFILES
%changelog
* Thu Dec 31 2015 will <will@52os.net> - 1.9.7.1-4
- openresty init

8. 获取环境变量


rpm --eval "%{_sysconfdir}"
rpm --showrc
宏定义配置文件
/usr/lib/rpm/macros、/usr/lib/rpm/macros.d、/usr/lib/rpm/redhat/macros、/etc/rpm/、~/.rpmmacros
优先级:
用户自定义相关:~/.rpmmacros > 系统相关的配置:/etc/rpm/ > 全局扩展配置:/usr/lib/rpm/macros.d/* > 全局的配置:/usr/lib/rpm/macros
宏定义解释
%{nil} 空
%__os_install_post 对安装的所有文件进行strip操作,去除文件的一些调试信息,并将这些调试信息放到debuginfo包中
%__elf_requires    可执行可链接文件格式依赖,设置为空则取消所有的依赖

9. 关键字定义


%if 0%(?rhel) 
# 在spec文件中0为假,非0为真;如果%{rhel}被定义了,则 %{?rhel} 返回%{rhel},否则%{?rhel}视为未定义。当%{rhel}被定义时, 0%{?rhel}就等于0%{rhel},则执行;反之,0%{?rhel}等于0,不执行。

%bcond_with
# %bcond_with defaults value to 0
# %bcond_without defaults value to 1
%if %{with python2} 判断python2是否为1;%if %{without python2},判断python2是否为0。

%{?macro:expression}: 假如宏(macro)存在,展开expression,如果不存在,什么也不会输出,直接为空。

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 3 天,点击查看活动详情