用automake打包軟件

1. 用 autoscan 產生一個 configure.in 的雛型,執行 autoscan 後會產生一個configure.scan 的檔案,我們可以用它做爲configure.in檔的藍本。 #autoscan #ls autoscan.log configure.scan drcom.c 2. 編輯 configure.scan 檔,如下所示,並且把它的檔名改成configure.in # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ(2.59) AC_INIT(Dr.com Client, 1.0, [email protected]) AM_INIT_AUTOMAKE(drcom_client_CLI, 1.0) AC_CONFIG_HEADER([config.h]) AC_CONFIG_SRCDIR([drcom.c]) AC_CONFIG_FILES([Makefile]) # Checks for programs. AC_PROG_CC # Checks for libraries. # Checks for header files. AC_HEADER_STDC AC_CHECK_HEADERS([stdlib.h string.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics. AC_C_CONST # Checks for library functions. AC_OUTPUT 3. 執行 aclocal 和 autoconf ,分別會產生 aclocal.m4 及 configure 兩個檔案 #aclocal #ls aclocal.m4 autom4te.cache autoscan.log configure.in configure.in~ drcom.c #autoconf #ls aclocal.m4 autoscan.log configure.in drcom.c autom4te.cache configure 4. 編輯 Makefile.am 檔,內容如下 AUTOMAKE_OPTIONS= foreign bin_PROGRAMS= drcom drcom_SOURCES= drcom.c 5.autoheader 接着使用autoheader命令,它負責生成config.h.in文件。該工具通常會從“acconfig.h”文件中複製用戶附加的符號定義,因此此處沒有附加符號定義,所以不需要創建“acconfig.h”文件。如下所示: [root@localhost automake]# autoheader 6. 執行 automake --add-missing ,Automake 會根據 Makefile.am 檔產生一些檔案,包含最重要的 Makefile.in #automake --add-missing 7. 最後執行 ./configure , checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for gawk... gawk checking whether make sets $(MAKE)... yes checking for gcc... gcc checking for C compiler default output file name... a.out checking whether the C compiler works... yes checking whether we are cross compiling... no checking for suffix of executables... checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ANSI C... none needed checking for style of include used by make... GNU checking dependency style of gcc... gcc3 checking how to run the C preprocessor... gcc -E checking for egrep... grep -E checking for ANSI C header files... yes checking for sys/types.h... yes checking for sys/stat.h... yes checking for stdlib.h... yes checking for string.h... yes checking for memory.h... yes checking for strings.h... yes checking for inttypes.h... yes checking for stdint.h... yes checking for unistd.h... yes checking for stdlib.h... (cached) yes checking for string.h... (cached) yes checking for unistd.h... (cached) yes checking for an ANSI C-conforming const... yes configure: creating ./config.status config.status: creating Makefile config.status: executing depfiles commands 現在你的目錄下已經產生了一個 Makefile 檔,下個 ``make'' 指令就可以開始編譯 drcom.c 成執行檔 你還可以試試 ``make clean'',''make install'',''make dist'' 看看會有什麼結果。 8. 一探究竟 上述產生 Makefile 的過程和以往自行編寫的方式非常不一樣,捨棄傳統自行定義 make 的規則,使用 Automake 只需用到一些已經定義好的巨集即可。我們把巨集及目標 (target)寫在 Makefile.am 檔內,Automake讀入 Makefile.am 檔後會把這一串已經定義好的巨集展 開並且產生對應的Makefile.in 檔, 然後再由 configure 這個 shell script 根據Makefile.in 產生適合的 Makefile。 在此範例中可藉由 Autoconf 及 Automake 工具所產生的檔案有 configure.scan、aclocal.m4、configure、Makefile.in,需要我們加入設定者爲 configure.in 及 Makefile.am。 9.1 編輯 configure.in 檔 Autoconf 是用來產生 'configure' 檔的工具。'configure' 是一個shell script,它可以自動設定原始程式以符合各種不同平臺上 Unix 系統的特性,並且根據系統叄數及環境產生合適的 Makefile 檔或是C 的標頭檔 (header file),讓原始程式可以很方便地在這些不同 的平臺上被編譯出來。Autoconf 會讀取 configure.in 檔然後產生 'configure' 這個shell script。 configure.in 檔的內容是一連串 GNU m4 的巨集,這些巨集經過autoconf 處理後會變成檢查系統特徵的 shell script。configure.in 內巨集的順序並沒有特別的規定,但是每一個 configure.in 檔必須在所有巨集前加入 AC_INIT 巨集,然後在所有巨集的最後面加上 AC _OUTPUT 巨集。我們可先用 autoscan 掃描原始檔以產生一個 configure.scan 檔,再對 configure.scan 做些修改成 configure.in 檔。在範例中所用到的巨集如下: dnl 這個巨集後面的字不會被處理,可視爲註解。 AC_INIT(FILE) 這個巨集用來檢查原始碼所在的路徑,autoscan 會自動產生,我們不必修改它。 AM_INIT_AUTOMAKE(PACKAGE,VERSION) 這是使用 Automake 所必備的巨集,PACKAGE 是我們所要產生軟體套件的名稱,VERSION 是版本編號。 AC_PROG_CC 檢查系統可用的 C 編譯器,如果原始程式是用 C 寫的就需要這個巨集。 AC_OUTPUT(FILE) 設定 configure 所要產生的檔案,如果是 Makefile 的話,configure 便會把它檢查出來的結果帶入 Makefile.in 檔然後產生 合適的 Makefile。 實際上,我們使用 Automake 時,還須要一些其它的巨集,這些額外的巨集我們用 aclocal來幫我們產生。執行 aclocal 會產生 aclocal.m4檔,如果沒有特別的用途,我們可以不必修改它,用 aclocal 所產生的巨集會告訴 Automake 怎麼做。 有了 configure.in 及 aclocal.m4 兩個檔案後,便可以執行 autoconf來產生 configure檔了。 9.2 編輯 Makefile.am 檔 接下來我們要編輯 Makefile.am 檔,Automake 會根據 configure.in 中的巨集把Makefile.am 轉成 Makefile.in 檔。Makefile.am 檔定義我們所要產的目標: AUTOMAKE_OPTIONS 設定 automake 的選項。Automake 主要是幫助開發 GNU 軟體的人員維護軟體套件,所以在執行 automake 時,會檢查目錄下是否存在標準 GNU 軟體套件中應具備的文件檔案,例如 'NEWS'、'AUTHOR'、'ChangeLog' 等文件檔。設成 foreign 時,automake 會改用一般軟 體套件的標準來檢查。 bin_PROGRAMS 定義我們所要產生的執行檔檔名。如果要產生多個執行檔,每個檔名用空白字元隔開。 hello_SOURCES 定義 'hello' 這個執行檔所需要的原始檔。如果 'hello' 這個程式是由多個原始檔所產生,必須把它所用到的原始檔都列出來,以空白字元隔開。假設 'hello' 這個程式需要 'hello.c'、'main.c'、 'hello.h' 三個檔案的話,則定義 hello_SOURCES= hello.c main.c hello.h 如果我們定義多個執行檔,則對每個執行檔都要定義相對的filename_SOURCES。 編輯好 Makefile.am 檔,就可以用 automake --add-missing 來產生Makefile.in。加上 --add-missing 選項是告訴 automake 順便幫我們加入包裝一個軟體套件所必備的檔案。Automake 產生出來的 Makefile.in檔是完全符合 GNU Makefile 的慣例,我們只要執行 confi gure 這個shell script 便可以產生合適的 Makefile 檔了。 9.3 使用 Makefile 利用 configure 所產生的 Makefile 檔有幾個預設的目標可供使用,我們只拿其中幾個簡述如下: make all 產生我們設定的目標,即此範例中的執行檔。只打 make 也可以,此時會開始編譯原始碼,然後連結,並且產生執行檔。 make clean 清除之前所編譯的執行檔及目的檔 (object file, *.o)。 make distclean 除了清除執行檔和目的檔外,也把 configure 所產生的 Makefile也清除掉。 make install 將程式安裝至系統中。如果原始碼編譯無誤,且執行結果正確,便可以把程式安裝至系統預設的執行檔存放路徑。如果我們用bin_PROGRAMS 巨集的話,程式會被安裝至 /usr/local/bin 這個目錄。 make dist 將程式和相關的檔案包裝成一個壓縮檔以供散播 (distribution) 。執行完在目錄下會產生一個以 PACKAGE-VERSION.tar.gz 爲名稱的檔案。PACKAGE 和 VERSION 這兩個變數是根據 configure.in 檔中AM_INIT_AUTOMAKE(PACKAGE, VERSION) 的定義。在此範例中會產生 'hello-1.0.tar.gz' 的檔案。 make distcheck 和 make dist 類似,但是加入檢查包裝後的壓縮檔是否正常。這個目標除了把程式和相關檔案包裝成 tar.gz 檔外,還會自動把這個壓 縮檔解開,執行 configure,並且進行 make all 的動作,確認編譯無誤後,會顯示這個 tar.gz 檔已經準備好可供散播了。這個檢查非 常有用,檢查過關的套件,基本上可以給任何一個具備 GNU 發展境的人去重新編譯。就 hello-1.tar.gz 這個範例而言,除了在 Red Hat linux 上,在 FreeBSD 2.2.x 版也可以正確地重新編譯。 要注意的是,利用 Autoconf 及 Automake 所產生出來的軟體套件是可以在沒有安裝 Autoconf 及 Automake 的環境上使用的,因爲 configure 是一個 shell script,它己被設計可以在一般 Unix 的 sh 這個 shell 下執行。但是如果要修改 configure.in 及 Makefile.a m 檔再產生新的configure 及 Makefile.in 檔時就一定要有 Autoconf 及 Automake 了。 10. 相關訊息 Autoconf 和 Automake 功能十分強大,你可以從它們所附的 info 檔找到詳細的用法。你也可以從許多現存的 GNU 軟體或 Open Source 軟體中找到相關的 configure.in 或 Makefile.am 檔,它們是學習 Autoconf 及Automake 更多技巧的最佳範例。 這篇簡介只用到了 Autoconf 及 Automake 的皮毛罷了,如果你有心加入Open Source 軟體開發的行列,希望這篇文件能幫助你對產生 Makefile有個簡單的依據。其它有關開發 GNU程式或 C 程式設計及 Makefile 的詳細運用及技巧,我建議你從 GNU Coding Standards3 (GNU 編碼標準規定) 讀起,裏面包含了 GNU Makefile 慣例,還有發展 GNU 軟體套件的標準程序和慣例。這些 GNU 軟體的線上說明文件可以在http://www.gnu.org/ 這個網站上找到。 11. 結語 經由 Autoconf 及 Automake 的輔助,產生一個 Makefile 似乎不再像以前那麼困難了,而使用 Autoconf 也使得我們在不同平臺上或各家 Unix之間散播及編譯程式變得簡單,這對於在 Unix 系統上開發程式的人員來說減輕了許多負擔。妥善運用這些 GNU 的工具軟體,可 以幫助我們更容易去發展程式,而且更容易維護原始程式碼。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章