前言
源码:https://github.com/h42330789/StudyBazel
一直想研究Telegram的源码,下载下来按步骤能正常在模拟器运行,想使用
LookIn
这个工具研究下,方便查看研究,发现Telegram是使用Bazel
管理的大门,不知道如何集成LookIn,特研究学习下Bazel
一、安装Bzael
参考:
在 macOS 上安装 Bazel
Bazel教程:构建iOS应用[翻译]
基于 Bazel 的 iOS Monorepo 工程实践
Bazel iOS Hello World (Objective-C, Mac M1)
Build an iOS Framework (Objective-C) by Bazel
https://github.com/deeplearningmakeeasy/bazelioshelloworld
https://medium.com/@DeepLearningMakeEasy
# 使用brew安装 bazel
brew install bazel
# 查看bazel版本
bazel --version
二、准备一个空项目HelloOC
2.1 使用Xcode创建一个HelloOC的项目,然后删掉配置文件,只保留源码文件
2.2 在HelloOC同层文件夹下创建WORKSPACE
文件,内容如下:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# rules required for ios-app
http_archive(
name = "build_bazel_rules_apple",
sha256 = "cd3b62efed8eaf306050817205d5139de3189be422ee009038783c3da0b04483",
strip_prefix = "rules_apple-5b7c929ec4f3618a3f558c7ccae64f155f2646b7",
url = "https://github.com/bazelbuild/rules_apple/archive/5b7c929ec4f3618a3f558c7ccae64f155f2646b7.tar.gz",
)
load(
"@build_bazel_rules_apple//apple:repositories.bzl",
"apple_rules_dependencies",
)
apple_rules_dependencies()
load(
"@build_bazel_rules_swift//swift:repositories.bzl",
"swift_rules_dependencies",
)
swift_rules_dependencies()
load(
"@build_bazel_rules_swift//swift:extras.bzl",
"swift_rules_extra_dependencies",
)
swift_rules_extra_dependencies()
load(
"@build_bazel_apple_support//lib:repositories.bzl",
"apple_support_dependencies",
)
apple_support_dependencies()
# rules required by ios-app Xcode integration
http_archive(
name = "rules_xcodeproj",
sha256 = "7967b372bd1777214ce65c87a82ac0630150b7504b443de0315ea52e45758e0c",
url = "https://github.com/MobileNativeFoundation/rules_xcodeproj/releases/download/1.3.3/release.tar.gz",
)
load(
"@rules_xcodeproj//xcodeproj:repositories.bzl",
"xcodeproj_rules_dependencies",
)
xcodeproj_rules_dependencies()
# rules required by backend
http_archive(
name = "io_bazel_rules_appengine",
strip_prefix = "rules_appengine-03121ae8efa634f7219f53970650a4445a72b759",
# TODO: update to a release version that contains 339f6aba67fcedb7268cf54d1163cf7704a277ca.
# This commit fixes the Maven artifact URLs to use "https" instead of "http".
# We don't specify sha256, because the sha256 of GitHub-served non-release archives isn't
# stable.
urls = ["https://github.com/bazelbuild/rules_appengine/archive/03121ae8efa634f7219f53970650a4445a72b759.tar.gz"],
)
load(
"@io_bazel_rules_appengine//appengine:java_appengine.bzl",
"java_appengine_repositories",
)
2.3
在HelloOC里创建BUILD
文件,内如如下:
# load ios application rule
load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application")
# load build in objc_library rule
load("@rules_cc//cc:defs.bzl", "objc_library")
load(
"@rules_xcodeproj//xcodeproj:defs.bzl",
"top_level_target",
"xcodeproj",
)
# 源代码的库
objc_library(
# 名字随意取
name = "HelloWorldOC_source",
srcs = [
# 这里可以全部单独列出来,也可以使用*.m这样的通配符方式
"Source/AppDelegate.m",
"Source/SceneDelegate.m",
"Source/ViewController.m",
"Source/main.m",
],
hdrs = glob(["Source/*.h"]),
# 资源文件
data = [
"Source/Base.lproj/Main.storyboard",
"Source/Base.lproj/LaunchScreen.storyboard",
],
deps = []
)
# 生成ipa,bazel build //BUILD所在文件夹名称:ios_application的名称
# bazel build //HelloOC:My-HelloOC-app
# 模拟器运行,bazel run //BUILD所在文件夹名称:ios_application的名称
# bazel run //HelloOC:My-HelloOC-app
ios_application(
# 名字随便起,用于生成ipa使用
name = "My-HelloOC-app",
# 这个bundle_id一定要与Info.plist里的一致
bundle_id = "studyBazel.helloOC.aa",
# 支持的版本
families = [
"iphone",
"ipad",
],
infoplists = ["Source/Info.plist"],
launch_storyboard = "Source/Base.lproj/LaunchScreen.storyboard",
app_icons = glob(["Source/Assets.xcassets/AppIcon.appiconset/*.png"])
+ glob(["Source/Assets.xcassets/AppIcon.appiconset/*.json"]),
# 支持的iOS最小版本
minimum_os_version = "13.0",
# provisioning_profile = "<your_profile_name>.mobileprovision", # Uncomment and set your own profile.
visibility = ["//visibility:public"],
# 依赖的源代码
deps = [":HelloWorldOC_source"],
)
# 这里是为了生存Xcodeproj文件
# bazel run //BUILD所在文件夹名称:xcodeproj的名称,会生成一个xxx.xcodeproj的文件
# bazel run //HelloOC:my_xcodeproj
xcodeproj(
name = "my_xcodeproj",
build_mode = "bazel",
# 生成的xxx.codeproj的名称
project_name = "My_helloOC",
tags = ["manual"],
top_level_targets = [
top_level_target(
# 要与ios_application里定义的名字一样
":My-HelloOC-app",
target_environments = ["simulator"],
),
],
)
三、运行命令:
# 生成ipa,bazel build //BUILD所在文件夹名称:ios_application的名称
bazel build //HelloOC:My-HelloOC-app
# 模拟器运行,bazel run //BUILD所在文件夹名称:ios_application的名称
bazel run //HelloOC:My-HelloOC-app
# 生成xcode项目
# bazel run //BUILD所在文件夹名称:xcodeproj的名称,会生成一个xxx.xcodeproj的文件
bazel run //HelloOC:my_xcodeproj
四、截图:
整体结构如下:
运行效果: