【homebrew 系列文章】
- HomeBrew常規使用教程
- Homebrew進階使用教程(一)
- Homebrew進階使用教程(二)-用一個命令列天氣客戶端構建自己的倉庫
- Homebrew進階使用教程(三)-apue.h在mac下安裝並使用連線
我的github地址:github地址:https://github.com/rangaofei/homebrew-saka
上篇文章講了如何建立自己的倉庫,這次簡單講解如何上傳自己的庫或者程式到自己的倉庫。最近正在學習apue這本書,但是需要依賴作者的一個apue庫,本篇將以unix環境高階程式設計的庫apue.h和libapue.a檔案的關聯講解如何上傳庫到自己的倉庫然後用brew安裝
1. 建立安裝指令碼
首先把自己的檔案壓縮為tar.gz(官方推薦為這種壓縮格式,其他的沒有嘗試)格式檔案,名稱以"庫名稱-版本號"格式書寫,這樣便於homebrew識別自己的版本號自動填寫。
此處我已下載好apue的原始碼並且編譯好,我們只需要兩個檔案include資料夾下的apue.h檔案和lib資料夾下的libapue.a檔案,將這兩個檔案隨便拷貝到一個資料夾下,只包含
apue.h
和libapue.a
,命令列進入這個資料夾並將這兩個檔案打包:
tar -cvzf apue-1.0.tar.gz ./*
複製程式碼
此時會自動生成這個檔案,將這個檔案上傳到某個所有人能訪問的地址,此處我上傳到了https://raw.githubusercontent.com/rangaofei/apue/master/lib/apue-1.0.tar.gz
這個地址。
執行命令
brew create <url>
複製程式碼
即可建立我們的安裝指令碼,指令碼名稱預設為前邊提到的庫名稱,對應我的命令為
brew create https://raw.githubusercontent.com/rangaofei/apue/master/lib/apue-1.0.tar.gz
複製程式碼
生成的檔案為apue.rb
。
2. 改寫安裝指令碼
執行上邊的命令後會自動開啟安裝指令碼為可編輯狀態,此處我的電腦自動使用vim開啟,並生成一系列的程式碼:
# Documentation: https://docs.brew.sh/Formula-Cookbook.html
# http://www.rubydoc.info/github/Homebrew/brew/master/Formula
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
class Apue < Formula
desc ""
homepage ""
url "https://raw.githubusercontent.com/rangaofei/apue/master/lib/apue-1.0.tar.gz"
sha256 "7e84d03563f7f0119f2d946cc9f439192e582b65a504c39f4158fea7f38f7cbd"
def install
# ENV.deparallelize
system "./configure", "--disable-debug",
"--disable-dependency-tracking",
"--disable-silent-rules",
"--prefix=#{prefix}"
# system "cmake", ".", *std_cmake_args
system "make", "install"
end
test do
# `test do` will create, run in and delete a temporary directory.
#
# This test will fail and we won't accept that! For Homebrew/homebrew-core
# this will need to be a test that verifies the functionality of the
# software. Run the test with `brew test apue`. Options passed
# to `brew install` such as `--HEAD` also need to be provided to `brew test`.
#
# The installed folder is not in the path, so use the entire path to any
# executables being tested: `system "#{bin}/program", "do", "something"`.
system "false"
end
end
複製程式碼
遵循ruby語法(我完全不懂ruby,現學現賣)。
class即為我們的目標庫。
裡邊比較重要的欄位就是列出來的幾個desc
,homepage
,url
,sha256
。
url和sha256是建立時自動生成的,url是下載地址,sha256是驗證碼,假如不匹配則會停止安裝,所以此處一定要填寫正確。 desc是描述欄位,對庫的簡介,homepage按官方的說法是這個庫的官方網址。
3. 修改安裝方式
install函式是安裝時執行的動作,預設提供了make安裝和cmake(註釋部分)的安裝方式。這次我們不用兩個方式,而是採用直接複製檔案到目標資料夾的方式。
def install
lib.install "libapue.a"
include.install "apue.h"
end
複製程式碼
注意此處的lib和include這兩個欄位,和cmake中的install基本雷同。 看一下官方的說明:
prefix.install "file1", "file2" #安裝部分檔案
prefix.install Dir["output/*"] #安裝整個output資料夾下的所有檔案
複製程式碼
prefix是一個字首,這個字首帶包一系列的目標資料夾。
字首代表的資料夾
字首名稱 | 目標資料夾 | 示例 |
---|---|---|
HOMEBREW_PREFIX | /usr/local | |
prefix | #{HOMEBREW_PREFIX}/Cellar/#{name}/#{version} | /usr/local/Cellar/foo/0.1 |
opt_prefix | #{HOMEBREW_PREFIX}/opt/#{name} | /usr/local/opt/foo |
bin | #{prefix}/bin | /usr/local/Cellar/foo/0.1/bin |
doc | #{prefix}/share/doc/foo | /usr/local/Cellar/foo/0.1/share/doc/foo |
include | #{prefix}/include | /usr/local/Cellar/foo/0.1/include |
info | #{prefix}/share/info | /usr/local/Cellar/foo/0.1/share/info |
lib | #{prefix}/lib | /usr/local/Cellar/foo/0.1/lib |
libexec | #{prefix}/libexec | /usr/local/Cellar/foo/0.1/libexec |
man | #{prefix}/share/man | /usr/local/Cellar/foo/0.1/share/man |
man[1-8] | #{prefix}/share/man/man[1-8] | /usr/local/Cellar/foo/0.1/share/man/man[1-8] |
sbin | #{prefix}/sbin | /usr/local/Cellar/foo/0.1/sbin |
share | #{prefix}/share | /usr/local/Cellar/foo/0.1/share |
pkgshare | #{prefix}/share/foo | /usr/local/Cellar/foo/0.1/share/foo |
etc | #{HOMEBREW_PREFIX}/etc | /usr/local/etc |
var | #{HOMEBREW_PREFIX}/var | /usr/local/var |
buildpath | A temporary directory somewhere on your system | /private/tmp/[formula-name]-0q2b/[formula-name] |
解釋一下:
lib.install libapue.a
將會將libapue.a
檔案複製到/usr/local/Celler/apue/lib
資料夾下,同理include.iinstall apue.h
將會將apue.h
檔案複製到/usr/local/Celler/apue/include
資料夾下。
4. 釋出到倉庫
剛才我們編寫的apue.rb
檔案在哪裡呢?
brew預設會在
core`倉庫中建立這個檔案,執行如下命令
cd $(brew --repo homebrew/core)
cd Formula/
ls |grep apue
複製程式碼
在這裡即可看到輸出的檔案中有我們剛才編輯的apue.rb
。copy到自己的倉庫資料夾下並加入git管理推送到遠端倉庫,此時其他人只要tap了這個倉庫就可以安裝這個庫了。
mv apue.rb ../../../rangaofei/homebrew-saka/Formula #移動檔案到自己的倉庫
cd $(brew --repo rangaofei/saka) #開啟自己的倉庫
cd Formula #進入資料夾
git add --all #加入git管理
git commit -m 'add new formula apue' #提交
git push #遠端倉庫提交
複製程式碼
然後看一下我們的庫的資訊
通過brew install apue
來安裝庫,安裝成功後,來看一下有沒有生成對應的檔案
可以看到在/usr/local/lib
與usr/local/include
資料夾下已經生成了兩個軟連線,連線到我們的brew安裝目錄。這些工作都是homebrew自動執行的,當執行brew uninstall apue
後這些軟連線也會自動刪除。這樣我們的庫就釋出完成了。
5. 驗證使用
我們已經安裝好了apue這個庫,那我們就可以立即使用了。這裡我用clion編寫了第一個示例程式碼:
#include "apue.h"
#include <dirent.h>
int main(int argc, char *argv[]) {
DIR *dp;
struct dirent *dirp;
if (argc != 2) {
err_quit("usage:ls directory_name");
}
if ((dp = opendir(argv[1])) == NULL) {
err_sys("can't open %s", argv[1]);
}
while ((dirp = readdir(dp)) != NULL) {
printf("%s\n", dirp->d_name);
}
closedir(dp);
exit(0);
}
複製程式碼
第一行的標頭檔案就是剛才我們安裝的庫,此處是可以正確引用的,然後修改cmakelists檔案
cmake_minimum_required(VERSION 3.9)
project(apue C)
set(CMAKE_C_STANDARD 99)
add_executable(apue main.c)
target_link_libraries(apue apue.a)
複製程式碼
在最後一行為我們的庫新增了連線庫apue.a。 然後執行外部構建,進入工程目錄
mkdir build
cd build/
cmake ..
make
複製程式碼
構建過程中沒有發生錯誤,執行檔案可正確輸出。
至此驗證玩意