使用BUCK进行iOS项目打包
關(guān)于BUCK
BUCK是Facebook開源的快速打包工具,可以用于多種語言及平臺(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可以找到很多問題的解決方案
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
| apple_asset_catalog() | 沒有特定產(chǎn)出,可以作為apple_bundle()的依賴 | Contains resources stored in Apple asset catalog directories |
| apple_binary() | 靜態(tài)庫:.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)庫:.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() | 無 | 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() | 無 | 引用.framework庫 |
| ? | ? | ? |
?
使用BUCK用于iOS工程打包
目錄組織結(jié)構(gòu)
對(duì)于一個(gè)多個(gè)子工程組成,通過依賴關(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 文件,然后最終通過根目錄中的BUCK,來生成.ipa文件。
?
.buckconfig配置
[cache]mode = dir[cxx]cflags = -std=gnu11cxxflags = -std=c++14 -stdlib=libc++default_platform = iphonesimulator-x86_64combined_preprocess_and_compile = true[alias]SubProject1 = //SubProject1: SubProject1LibSubProject2 = //SubProject2: SubProject2LibSubProject3 = //SubProject3: SubProject3LibSubProject4 = //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,可以通過別名的方式,命令如下:
# 未用別名 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ì)外暴露的頭文件等等。
通過命令 buck build CTLocation就可以打包查看,在buck-out目錄中可以看到生成出來的.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_PREFIX?is the value of the target's?header_path_prefix?attribute, and?$HEADER_NAME?is the filename of the header file. If a dictionary is specified, each header can be imported with?#import "$HEADER_NAME", where?$HEADER_NAME?is the key corresponding to this file. In this case, the?header_path_prefix?attribute 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一致,可以通過設(shè)置header_path_prefix改變。
使用字典的時(shí)候,其他代碼引用時(shí)候可以通過key來引用,例如:
// BUCK配置: exported_headers = {'xxx.h': './src/xxx.h', }// 其他代碼引用 #import "xxx.h"依賴資源文件
如果代碼中有資源文件,需要通過apple_resource來引用
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)庫
如下項(xiàng)目有依賴外部的.a庫,可以通過以下方法引用
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ū)也夠活躍,遇到問題會(huì)比較難找到解決方案。
目前iOS項(xiàng)目中碰到的一些限制問題:
- 自定義的script,在XCode的build phase中可以自定義一些shell腳本,但是在BUCK中沒有找到對(duì)應(yīng)的方式
- 無法生成.bundle資源包,iOS打包過程中每個(gè)子工程都會(huì)產(chǎn)出一個(gè).a和.bundle文件,但是BUCK打包不會(huì)產(chǎn)出.bundle文件只會(huì)有.a,資源文件只能通過apple_resource()來管理作為其他rule的依賴。
- 通過apple_library()生成.a文件似乎目前還沒辦法指定valid architectures。
轉(zhuǎn)載于:https://www.cnblogs.com/wdsunny/p/7481500.html
總結(jié)
以上是生活随笔為你收集整理的使用BUCK进行iOS项目打包的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows中使用wget整站下载
- 下一篇: 二维数组求和 团队开发