实施方法

LocationShare编译

这里以iOS_Public模块中的LocationShare为例说明编译规则的书写要求。LocationShare所在的路径如下所示:

locationshare

其中根目录的WORKSPACE需要较高权限,模块负责任人无权修改,各个模块负责任人编写各自的BUILD构建脚本,LocationShare内容如下:

objc_library(
    name = "LocationShare",
    srcs = glob(["**/*.m"]),
    hdrs = glob(["**/*.h"]),
    deps = [
        "//Frameworks:iOSFrameworks",
        "//iOS_Third_Party_Source/Reachability:Reachability",
        "//iOS_Public/lib/CommonKits:CommonKits",
        "//iOS_Public/lib/CSKits:CSKits",
        "//iOS_Public/lib/JingoalSystemConfig:JingoalSystemConfig",
        "//iOS_Binary_File:binaryHeaders",
        "//iOS_Third_Party_Framework:Map",
    ],

    includes = [
        "../../../iOS_Binary_File/include/Public/CommonKits",
        "../../../iOS_Binary_File/include/Public/CSKits",
        "../../../iOS_Binary_File/include/Public/JingoalSystemConfig",
        "../../../iOS_Binary_File/include/ThirdParty/Reachability",
    ],
    pch = "LocationShare/LocationShare-Prefix.pch",
    visibility = ["//visibility:public"]
)

其中Frameworks文件夹没有源代码将公共系统库放置其中,防止重复编写,内容如下:

objc_library(
    name = "iOSFrameworks",
    sdk_frameworks = ["CoreGraphics","SystemConfiguration","Security","MobileCoreServices","CFNetwork","ImageIO","QuickLook","AVFoundation","AssetsLibrary","QuartzCore","ImageIO","Security","CoreTelephony","MapKit","CoreLocation","AudioToolBox","CoreMedia","AddressBook","AddressBookUI"],
    sdk_dylibs = ["libsqlite3","libxml2"],
    visibility = ["//visibility:public"]
)

CommonKits,CSKits,JingoalSystemConfig,Reachability为LocationShare依赖的本地库,这里只演示CSKits的BUILD文件:

objc_library(
    name = "CSKits",
    srcs = glob(["**/*.m"]),
    hdrs = glob(["**/*.h"]),
    deps = [
        "//Frameworks:iOSFrameworks",
        "//iOS_Third_Party_Source/SDWebImage:SDWebImage",
        "//iOS_Third_Party_Source/DACircularProgress:DACircularProgress",
        "//iOS_Public/lib/CommonKits:CommonKits",
        "//iOS_Binary_File:binaryHeaders",
    ],

    includes = [
        "CSKits",
        "CSKits/BLTableViewCell",
        "CSKits/CustomUI",
        "CSKits/FRDLivelyButton",
        "CSKits/HPGrowingTextView",
        "CSKits/HZActivityIndicatorView",
        "CSKits/JingoalUI",
        "CSKits/MBProgressHUD",
        "CSKits/MTStatusBarOverlay",
        "CSKits/MWPhotoBrowser",
        "CSKits/PBActivityIndicatorView",
        "CSKits/SWTableViewCell",
        "../../../iOS_Binary_File/include/Public/CommonKits",
        "../../../iOS_Binary_File/include/ThirdParty/SDWebImage",
        "../../../iOS_Binary_File/include/ThirdParty/DACircularProgress",
    ],
    pch = "CSKits/CSKits-Prefix.pch",
    visibility = ["//visibility:public"]
)

注意:CSKits项目中大量使用子文件夹分类源代码文件,需要将所有的源代码文件夹加入Includes,否则找不到头文件。

为了减少引用含有较多子文件夹的本地库时书写大量的includes子项,我们使用iOS_Binary_File下的头文件,BUILD文件如下:

objc_library(
    name = "binaryHeaders",
    srcs = glob(["**/*.h"]),
    visibility = ["//visibility:public"]
)

第三方库的使用一般包含两种:

  • 编译为库文件、头文件和bundle
  • 编译为framework

iOS_Third_Party_Framework下的BUILD如下所示,导入了需要用的第三方库,主要规则为objc_framework,objc_import,"//iOS_Third_Party_Framework:Map"说明项目使用了AMapSearchKit_V3.3.0及AMap-iOS-API-2DMap-Lib-V3.3.0文件夹下的库文件。

 objc_library(
    name = "thirdHeaders",
    srcs = glob(["**/*.h"]),
    visibility = ["//visibility:public"]
)

objc_framework(
    name = "Map",
    framework_imports = ["AMapSearchKit_V3.3.0/AMapSearchKit.framework","AMap-iOS-API-2DMap-Lib-V3.3.0/MAMapKit.framework"],
    visibility = ["//visibility:public"],
)


objc_framework(
    name = "QQ",
    framework_imports = ["QQSDK/TencentOpenAPI.framework"],
    visibility = ["//visibility:public"],
)


objc_import(
    name = "Weibo",
    hdrs = glob(["libWeiboSDK/*.h"]),
    archives = ["libWeiboSDK/libWeiboSDK.a"],
    bundles = [":weiboBundle"],
    visibility = ["//visibility:public"],
)

objc_bundle(
    name = "weiboBundle",
    bundle_imports = ["libWeiboSDK/WeiboSDK.bundle",],
)

objc_import(
    name = "WeChat",
    hdrs = glob(["WeChatSDK/*.h"]),
    archives = ["WeChatSDK/libWeChatSDK.a"],
    visibility = ["//visibility:public"],

)

规则的书写顺序

1.自上而下

上例中的LocationShare编写便为自上而下的例子,从交顶层的库开始,找到齐依赖,如果依赖没有编写BUILD,继续重复前面的方法,直到LocationShare编译通过。

2.自下而上

先把依赖关系最少的模块编译通过例如:iOS_Third_Party_Source中的代码,然后是iOS_Public,逐步构建依赖关系较多的复杂代码。