- 論壇徽章:
- 0
|
本帖最后由 duanjigang 于 2012-12-22 14:49 編輯
Macros (宏)
這里是根據(jù)自己的理解寫點,如果有錯誤之處請指出。
RPM 不提供那種像C語言一樣格式的對過程的宏定義,我們在spec 文件中能看到的都是單個變量的定義,比如:但是卻很少或者說基本看不到類似于:
- #define TR_HLEN (sizeof(struct trh_hdr)+sizeof(struct trllc))
復(fù)制代碼 這樣定義過程代碼的寫法。
不過在RPM的 build 過程中有兩個動作對于大多數(shù)包來說基本上都是一樣的操作,因此 RPM 為它們設(shè)置了兩個宏定義:源代碼的解壓縮 和 代碼的 patch 過程
- The %setup macro, which is used to unpack the original sources.
- The %patch macro, which is used to apply patches to the original sources.
復(fù)制代碼 %setup 宏在使用時大多數(shù)情況都是不加任何選項的,比如:
- Source: ftp://ftp.gnomovision.com/pub/cdplayer/cdplayer-1.0.tgz
- %prep
- %setup
復(fù)制代碼 這個過程,解釋成執(zhí)行腳本,內(nèi)容如下:
- cd /usr/src/redhat/BUILD
- rm -rf cdplayer-1.0
- gzip -dc /usr/src/redhat/SOURCES/cdplayer-1.0.tgz | tar -xvvf -
- if [ $? -ne 0 ]; then
- exit $?
- fi
- cd cdplayer-1.0
- cd /usr/src/redhat/BUILD/cdplayer-1.0
- chown -R root.root .
- chmod -R a+rX,g-w,o-w .
復(fù)制代碼 我們能看到,就是目錄切換,代碼解壓縮,檢測解壓縮結(jié)果,進入目錄,修改屬主/組和mode.
在這里需要注意幾點,在 %setup 不加任何參數(shù)時,rpm 命令執(zhí)行時默認了幾個咚咚:
第一個:build 目錄是,cdplayer-1.0,并且路徑是 /usr/src/redhat/BUILD/cdplayer-1.0
第二:刪除了原來的build 目錄
第三:對源代碼進行了解壓縮。
第四:修改屬性時需要進入cdplayer-1.0目錄。
看到這里你可能會感覺到比較迷惑,難道這些不應(yīng)該嗎?呵呵。是的,不過 rpm 在 %setup 的宏定義中提供了幾個執(zhí)行選項,可以修改默認執(zhí)行動作中的操作流程。
下來我們一一看下.
-n <name> : 設(shè)置build 目錄的名字。
因為默認的build 目錄名字是 <name>-<version>的方式,但是你也可以通過 -n name 來指定,比如:執(zhí)行的腳本過程如下:
- cd /usr/src/redhat/BUILD
- rm -rf cd-player
- gzip -dc /usr/src/redhat/SOURCES/cdplayer-1.0.tgz | tar -xvvf -
- if [ $? -ne 0 ]; then
- exit $?
- fi
- cd cd-player
- cd /usr/src/redhat/BUILD/cd-player
- chown -R root.root .
- chmod -R a+rX,g-w,o-w .
復(fù)制代碼 我們能看到,默認的build 名稱,是被 -n 選項修改了。
-c 創(chuàng)建build目錄,并且在解壓縮前進入該目錄
這一點,個人很有感觸,有時候在制作rpm前壓制源碼文件時,經(jīng)常會
- tar -czf mycode.tar.gz mycode
復(fù)制代碼 把整個目錄打包成一個壓縮文件,而有時候則會直接把源代碼壓縮,而不包括目錄
- cd mycode
- tar -czf mycode.tar.gz *
復(fù)制代碼 ,這時,-c 選項就能幫上忙了,替你建立目錄,然后在目錄里面解壓縮文件。
在該例中,加上 -c 后,解釋執(zhí)行的腳本過程如下:
- cd /usr/src/redhat/BUILD
- rm -rf cdplayer-1.0
- mkdir -p cdplayer-1.0
- cd cdplayer-1.0
- gzip -dc /usr/src/redhat/SOURCES/cdplayer-1.0.tgz | tar -xvvf -
- if [ $? -ne 0 ]; then
- exit $?
- fi
- cd /usr/src/redhat/BUILD/cdplayer-1.0
- chown -R root.root .
- chmod -R a+rX,g-w,o-w .
復(fù)制代碼 如果你的壓縮包解壓縮后就是白花花的代碼,那就應(yīng)該用上這個選項。
-D: 在解壓縮前不刪除目錄
這種情況的應(yīng)用場景就是你新解壓縮的代碼是要被加入到新的目錄樹中,因此以前的目錄不能刪除。
對應(yīng)的解釋腳本執(zhí)行過程如下:
- cd /usr/src/redhat/BUILD
- gzip -dc /usr/src/redhat/SOURCES/cdplayer-1.0.tgz | tar -xvvf -
- if [ $? -ne 0 ]; then
- exit $?
- fi
- cd cdplayer-1.0
- cd /usr/src/redhat/BUILD/cdplayer-1.0
- chown -R root.root .
- chmod -R a+rX,g-w,o-w .
復(fù)制代碼 -T: 不進行默認壓縮文件的解壓縮
有人可能會問:不解壓縮代碼,那還搞毛?先不要著急,我們會接后著后面的 option 一起來看這個 選項的應(yīng)用場景.
首先看下加上 -T 時 %setup 執(zhí)行的動作:
- cd /usr/src/redhat/BUILD
- rm -rf cdplayer-1.0
- cd cdplayer-1.0
- cd /usr/src/redhat/BUILD/cdplayer-1.0
- chown -R root.root .
- chmod -R a+rX,g-w,o-w
復(fù)制代碼 -b <n>: 在進入目錄前解壓縮第 n 個源碼包
看下 openssl 中的source 定義片段:
- Source: openssl-fips-%{version}-usa.tar.bz2
- Source1: hobble-openssl
- Source2: Makefile.certificate
復(fù)制代碼 RPM 對 Source 的解釋是 編號是0的Source,也就是 Source0,然后我們看下在spec 中,如下寫的執(zhí)行結(jié)果:根據(jù) -b 的解釋,它的執(zhí)行過程如下:
- cd /usr/src/redhat/BUILD
- rm -rf cdplayer-1.0
- gzip -dc /usr/src/redhat/SOURCES/cdplayer-1.0.tgz | tar -xvvf -
- if [ $? -ne 0 ]; then
- exit $?
- fi
- gzip -dc /usr/src/redhat/SOURCES/cdplayer-1.0.tgz | tar -xvvf -
- if [ $? -ne 0 ]; then
- exit $?
- fi
- cd cdplayer-1.0
- cd /usr/src/redhat/BUILD/cdplayer-1.0
- chown -R root.root .
- chmod -R a+rX,g-w,o-w .
復(fù)制代碼 可以看到對 Source0 的解壓縮進行了兩次,重復(fù)了。
我想這下你應(yīng)該理解了 -T 的作用了,是為了避免不必要的冗余解壓縮,當然這個場景是你直接想用Source0 以后的源碼,而不用第一個Source.
然后看下 %setup -T -b 0 結(jié)合使用的效果:
- cd /usr/src/redhat/BUILD
- rm -rf cdplayer-1.0
- gzip -dc /usr/src/redhat/SOURCES/cdplayer-1.0.tgz | tar -xvvf -
- if [ $? -ne 0 ]; then
- exit $?
- fi
- cd cdplayer-1.0
- cd /usr/src/redhat/BUILD/cdplayer-1.0
- chown -R root.root .
- chmod -R a+rX,g-w,o-w
復(fù)制代碼 這樣就對了,當然重點是 -b 后面的參數(shù)你可以自己設(shè)置。
-a n: 在進入目錄后解壓縮第 n 個源碼
這個跟 -b 有些像,不過 -b 是在進入目錄前解壓縮。反正他們都是跟目錄相關(guān)的,具體場景自己應(yīng)該可以去想了,我們只看下執(zhí)行的動作:
%setup -T -a 0
- cd /usr/src/redhat/BUILD
- rm -rf cdplayer-1.0
- cd cdplayer-1.0
- gzip -dc /usr/src/redhat/SOURCES/cdplayer-1.0.tgz | tar -xvvf -
- if [ $? -ne 0 ]; then
- exit $?
- fi
- cd /usr/src/redhat/BUILD/cdplayer-1.0
- chown -R root.root .
- chmod -R a+rX,g-w,o-w .
復(fù)制代碼 可以看到?jīng)]有創(chuàng)建目錄直接就 cd 了,因此,它大多與 -c 一起結(jié)合使用:
%setup -c -T -a 0
- cd /usr/src/redhat/BUILD
- rm -rf cdplayer-1.0
- mkdir -p cdplayer-1.0
- cd cdplayer-1.0
- gzip -dc /usr/src/redhat/SOURCES/cdplayer-1.0.tgz | tar -xvvf -
- if [ $? -ne 0 ]; then
- exit $?
- fi
- cd /usr/src/redhat/BUILD/cdplayer-1.0
- chown -R root.root .
- chmod -R a+rX,g-w,o-w .
復(fù)制代碼 然后把這些結(jié)合起來,看下 %setup 處理多個source 源碼包的一個例子:
- %setup
- %setup -T -D -a 1
- mkdir database
- cd database
- gzip -dc /usr/src/redhat/SOURCES/source-two.tar.gz | tar -xvvf -
復(fù)制代碼 按照上面的規(guī)范,它解釋后的動作是:
- cd /usr/src/redhat/BUILD
- rm -rf cdplayer-1.0
- gzip -dc /usr/src/redhat/SOURCES/source-zero.tar.gz | tar -xvvf -
- if [ $? -ne 0 ]; then
- exit $?
- fi
- cd cdplayer-1.0
- cd /usr/src/redhat/BUILD/cdplayer-1.0
- chown -R root.root .
- chmod -R a+rX,g-w,o-w .
- cd /usr/src/redhat/BUILD
- cd cdplayer-1.0
- gzip -dc /usr/src/redhat/SOURCES/source-one.tar.gz | tar -xvvf -
- if [ $? -ne 0 ]; then
- exit $?
- fi
- mkdir database
- cd database
- gzip -dc /usr/src/redhat/SOURCES/source-two.tar.gz | tar -xvvf -
復(fù)制代碼 我們能夠看到是把 0 代碼和 1代碼解壓縮到同一個目錄下,然后在build 目錄下建立 database 目錄,把 2 代碼解壓縮到 database 目錄下。
上面所有參數(shù)結(jié)合起來使用使得這個過程以最簡潔的方式執(zhí)行了。
得瑟點個人的感覺:spec 中這樣去增加%setup 的多個選項來控制腳本執(zhí)行的過程有些得不償失,開發(fā)人員有記住這些選項的時間,自己會去研究原始的腳本怎么樣寫更合適。
不過也算是一種機制,其目的就在于你在使用它時,為用戶提供能用實現(xiàn)功能的接口,至于接口的好壞,可能并沒做太多考慮,有些爛,呵呵。
|
|