使用BUCK進(jìn)行iOS項(xiàng)目打包

關(guān)于BUCK

BUCK是Facebook開源的快速打包工具罗标,可以用于多種語(yǔ)言及平臺(tái)的項(xiàng)目打包,例如:C锈锤、C++院崇、Java肆氓、iOS、Android等等底瓣。用于大型的iOS谢揪、Android項(xiàng)目,可以顯著提升打包效率捐凭。
關(guān)于BUCK介紹的一些鏈接如下:
BUCK官網(wǎng)
What Makes Buck so Fast?:介紹了BUCK如何做到性能提升
BUCK源碼: 里面有源碼和大量Unit Test提供了很多示例拨扶,同時(shí)查看Issues可以找到很多問(wèn)題的解決方案
iOS快速編譯BUCK
基于Facebook Buck改造Android構(gòu)建系統(tǒng)之初體驗(yàn)

核心概念

Build Rule

A **build rule **is a procedure for producing an output file from a set of input files.

Build Target

A **build target **is a string that is used to identify a build rule in your project.

Build File

A **build file **is a file named BUCK that defines one or more build rules.

.buckconfig

The root of your project must contain a configuration file named.buckconfig.

iOS打包相關(guān)Rule

Rule 產(chǎn)出 作用
apple_asset_catalog() 沒有特定產(chǎn)出,可以作為apple_bundle()的依賴 Contains resources stored in Apple asset catalog directories
apple_binary() 靜態(tài)庫(kù):.a file An apple_binary() rule builds a native executable from the supplied set of Objective-C/C++ source files
apple_bundle() .app 或者 .appex (apple watch extension) An apple_bundle() rule takes an Apple binary and all of the resources and asset catalogs in the rule's transitive dependencies and generates a bundle containing all of those files.
apple_library() 靜態(tài)庫(kù):.a file An apple_library() rule represents a set of Objective-C/C++ source files
apple_package() ipa file An apple_package() rule takes the output of an apple_bundle() rule and compresses it in an IPA (iOS App Store Package) file.
apple_resource() This rule does not have any output on its own and can be built only as a dependency (either direct or transitive) of an apple_bundle() rule. An apple_resource() rule contains sets of resource directories, files and file variants that can be bundled in an application bundle.
apple_test() 無(wú) An apple_test() rule contains Objective-C/C++ code which can be built and used to test code contained in other rules.
core_data_model() This rule does not have any output on its own and can be built only as a dependency (either direct or transitive) of an apple_bundle() rule, in which case all core_data_model() rules that the bundle rule depends on are merged and placed into the final output bundle together. An core_data_model() rule contains models for Apple's Core Data framework.
prebuilt_apple_framework() 無(wú) 引用.framework庫(kù)

使用BUCK用于iOS工程打包

目錄組織結(jié)構(gòu)

對(duì)于一個(gè)多個(gè)子工程組成柑营,通過(guò)依賴關(guān)系最終集成為單個(gè)可執(zhí)行文件屈雄。使用BUCK,需要為每一個(gè)子工程都創(chuàng)建BUCK文件官套,在根目錄配置.buckconfig酒奶。大致目錄結(jié)構(gòu)如下:

|—.buckconfig
|—BUCK
|—SubProject1
|---------src
|---------BUCK
|—SubProject2
|---------src
|---------BUCK
|—SubProject3
|---------src
|---------BUCK
|—SubProject4
|---------src
|---------BUCK
......

每個(gè)子工程的BUCK文件,負(fù)責(zé)配置build rule奶赔,生成靜態(tài).a 文件惋嚎,然后最終通過(guò)根目錄中的BUCK,來(lái)生成.ipa文件站刑。

.buckconfig配置

[cache]
mode=dir

[cxx]
cflags= -std=gnu11
cxxflags= -std=c++14-stdlib=libc++default_platform= iphonesimulator-x86_64
combined_preprocess_and_compile=true

[alias]
SubProject1=//SubProject1: SubProject1Lib
SubProject2 =//SubProject2: SubProject2Lib
SubProject3 =//SubProject3: SubProject3Lib
SubProject4 =//SubProject4: SubProject4Lib

[apple]
xctool_zip_target=//third-party/ios/xctool:xctool-minimal-zip[project]
ignore=.buckd, \
.hg, \
.git, \
.idea, \
buck-cache, \
buck-out, \

cxx:定義了一些C++編譯的參數(shù)
alias: 定義了一些build target的別名另伍。例如CTFoundation為例,在CTFoundation中的BUCK文件中定義了CTFoundationLib的rule,所以如果要打包CTFoundation摆尝,可以通過(guò)別名的方式温艇,命令如下:

# 未用別名
buck build//SubProject1:SubProject1Lib
# 使用別名
buck build SubProject1

apple: 指定了xctool的文件地址。Buck的iOS打包是依賴于xctool堕汞,所以需要把xctool的相關(guān)代碼引入勺爱,具體內(nèi)容可以參考示例:

git clone git@github.com:fbsamples/bucksamples.git
cd bucksamples/cross-platform-scale-2015-demo/

BUCK文件配置

獨(dú)立子工程的BUCK配置

下面以一個(gè)獨(dú)立的子工程作為示例,而且沒有其他依賴讯检,所以可以作為第一個(gè)示例琐鲁。它的BUCK文件配置如下:

apple_library(
  name='SubProject1Lib',
  preprocessor_flags= ['-fobjc-arc','-Wno-deprecated-declarations','-fmodules'],
  compiler_flags= ['-Wno-objc-designated-initializers','-fembed-bitcode'],
  linker_flags=['-F$DEVELOPER_DIR/Platforms/iPhoneSimulator.platform/Developer/Library/PrivateFrameworks','-F$DEVELOPER_DIR/../SharedFrameworks','-F$DEVELOPER_DIR/Library/PrivateFrameworks',
  ],
  srcs= glob(['src/**/**/**/*.m',]),
  frameworks=['$SDKROOT/System/Library/Frameworks/Foundation.framework',],
  exported_headers={
    'xxx1.h':'./src/xxx.h',
    'xxx2.h':'./src/xxx2.h',
    ...
  },
  #header_namespace='',
  visibility=['PUBLIC'],
)

整個(gè)BUCK文件就一個(gè)apple_library,產(chǎn)出libCTLocation.a文件人灼∥Ф危可以看到里面可以指定一下編譯的flags、依賴的framework投放、源代碼奈泪、對(duì)外暴露的頭文件等等。
通過(guò)命令 buck build CTLocation就可以打包查看灸芳,在buck-out目錄中可以看到生成出來(lái)的.a文件段磨。

需要注意的exported_headers的配置:
The set of header files that are made available for inclusion to the source files in this target and all targets that transitively depend on this one. These should be specified as either a list of header files or a dictionary of header names to header files. The header names can contain forward slashes (/). If a list of header files is specified, the headers can be imported with#import "$HEADER_PATH_PREFIX/$HEADER_NAME"or, if a header file that belongs to the same rule is being imported, with#import "$HEADER_NAME", where$HEADER_PATH_PREFIXis the value of the target'sheader_path_prefixattribute, and$HEADER_NAMEis the filename of the header file. If a dictionary is specified, each header can be imported with#import "$HEADER_NAME", where$HEADER_NAMEis the key corresponding to this file. In this case, theheader_path_prefixattribute is ignored. In either case, quotes in the import statements can be replaced with angle brackets.

可以有兩種配置格式,數(shù)組和字典耗绿。
使用數(shù)組的時(shí)候,其他代碼引用是需要加上前綴例如:#import "SubProject1/xxx.h"砾隅,默認(rèn)前綴和apple_library的name一致误阻,可以通過(guò)設(shè)置header_path_prefix改變。
使用字典的時(shí)候晴埂,其他代碼引用時(shí)候可以通過(guò)key來(lái)引用究反,例如:

//BUCK配置:
exported_headers ={'xxx.h':'./src/xxx.h',}

//其他代碼引用
#import"xxx.h"

依賴資源文件

如果代碼中有資源文件,需要通過(guò)apple_resource來(lái)引用

apple_resource(
name='SubProject1Resource',
files= glob(['*.png']),
dirs=[],
)

特殊的Compiler flag

在iOS中儒洛,有些源代碼需要一些特殊的compiler flag精耐,例如非ARC的源碼。在src里面可以進(jìn)行配置:

srcs = glob(['SubProject1/**/**/**/*.m',], excludes = ['**/**/xxx1.m','**/**/xxx2.m'])+[('src/xxx1.m', ['-Wno-shorten-64-to-32']),
('src/xxx2.m', ['-fno-objc-arc'])]

引用外部framework

prebuilt_apple_framework(
name='BuckTest',
framework='BuckTest.framework',
preferred_linkage='shared',
visibility= ['PUBLIC'],
)

引用外部.a 靜態(tài)庫(kù)

如下項(xiàng)目有依賴外部的.a庫(kù)琅锻,可以通過(guò)以下方法引用


cxx_library(
  name='lib1',
  srcs=[],
  exported_headers={'xxx.h':'libs/xxx.h',},
  visibility= ['PUBLIC'],
)

apple_library(
  name='SubProject1Lib',
  deps= [':lib1'],
  ...
  libraries=['libs/xxx.a',],
)

當(dāng)前BUCK的局限性

BUCK本身目前還在快速迭代中卦停,所以很多rule還沒有完善、文檔不全恼蓬,社區(qū)也夠活躍惊完,遇到問(wèn)題會(huì)比較難找到解決方案。
目前iOS項(xiàng)目中碰到的一些限制問(wèn)題:

  • 自定義的script处硬,在XCode的build phase中可以自定義一些shell腳本小槐,但是在BUCK中沒有找到對(duì)應(yīng)的方式
  • 無(wú)法生成.bundle資源包,iOS打包過(guò)程中每個(gè)子工程都會(huì)產(chǎn)出一個(gè).a和.bundle文件荷辕,但是BUCK打包不會(huì)產(chǎn)出.bundle文件只會(huì)有.a凿跳,資源文件只能通過(guò)apple_resource()來(lái)管理作為其他rule的依賴件豌。
  • 通過(guò)apple_library()生成.a文件似乎目前還沒辦法指定valid architectures。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末控嗜,一起剝皮案震驚了整個(gè)濱河市茧彤,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌躬审,老刑警劉巖棘街,帶你破解...
    沈念sama閱讀 222,627評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異承边,居然都是意外死亡遭殉,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門博助,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)险污,“玉大人,你說(shuō)我怎么就攤上這事富岳』着矗” “怎么了?”我有些...
    開封第一講書人閱讀 169,346評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵窖式,是天一觀的道長(zhǎng)蚁飒。 經(jīng)常有香客問(wèn)我,道長(zhǎng)萝喘,這世上最難降的妖魔是什么淮逻? 我笑而不...
    開封第一講書人閱讀 60,097評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮阁簸,結(jié)果婚禮上爬早,老公的妹妹穿的比我還像新娘。我一直安慰自己启妹,他們只是感情好筛严,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著饶米,像睡著了一般桨啃。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上檬输,一...
    開封第一講書人閱讀 52,696評(píng)論 1 312
  • 那天优幸,我揣著相機(jī)與錄音,去河邊找鬼褪猛。 笑死网杆,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播碳却,決...
    沈念sama閱讀 41,165評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼队秩,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了昼浦?” 一聲冷哼從身側(cè)響起馍资,我...
    開封第一講書人閱讀 40,108評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎关噪,沒想到半個(gè)月后鸟蟹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,646評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡使兔,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評(píng)論 3 342
  • 正文 我和宋清朗相戀三年建钥,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片虐沥。...
    茶點(diǎn)故事閱讀 40,861評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡熊经,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出欲险,到底是詐尸還是另有隱情镐依,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布天试,位于F島的核電站槐壳,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏喜每。R本人自食惡果不足惜宏粤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望灼卢。 院中可真熱鬧,春花似錦来农、人聲如沸鞋真。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)涩咖。三九已至,卻和暖如春繁莹,著一層夾襖步出監(jiān)牢的瞬間檩互,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工咨演, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留闸昨,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,287評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像饵较,于是被迫代替她去往敵國(guó)和親拍嵌。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理循诉,服務(wù)發(fā)現(xiàn)横辆,斷路器,智...
    卡卡羅2017閱讀 134,715評(píng)論 18 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,324評(píng)論 25 707
  • 很喜歡帶窗戶的房間茄猫,尤其是那種大大的落地窗狈蚤,配上白色的紗簾,在夏夜的微風(fēng)中飄蕩划纽。倘若是墻體窗脆侮,一定要有一個(gè)小窗臺(tái),...
    Log君閱讀 884評(píng)論 0 1
  • 大漠風(fēng)暴中阿浓,卷走了一棵樹他嚷。那棵樹的根扎的很深,可即便如此芭毙,還是被風(fēng)暴連根拔起筋蓖。 那一日與往日并沒有什么不同,都是太...
    恬冼閱讀 209評(píng)論 0 0
  • 七十二賢學(xué)眾籌 籌人籌智共謀求 沉積資源齊分享 山后青山樓外樓 2017.2.27.宋剛. 京西南郊
    宋剛易海游龍閱讀 159評(píng)論 0 1