编译 Flutter Engine
安装 depot_tools :
$ pwd
/Users/sunhao/developmchromium.googlesource.com/chromium/tools/depot_tools.git
复制代码配置环境变量:建议在 ~/.bash_profile 文件中添加
export PATH=/Users/sunhao/development/depot_tools/:$PATH
复制代码然后执行
$ source ~/.bash_profile
复制代码当然,你也可以配置临时环境变量
$ export PATH=$PATH:/Users/sunhao/development/depot_tools/
复制代码2、将 engine 项目 fork 到自己的github
关于 github 的使用,这里不多说了,fork 过来,添加 github 的 ssh key 信任。
我这里的地址为:github.com/loosaSH/eng…
3、创建项目目录
你可以创建一个目录,名称推荐为 engine,后面的步骤会自动执行 clone 。
$ pwd
/Users/sunhao/development
$ mkdir engine
复制代码4、创建 .gclient 文件
进入engine 目录,创建 .gclient 文件
$ cd engine
$ vim .gclient
复制代码写入如下内容:
solutions = [
{
“managed”: False,
“name”: “src/flutter”,
“url”: “git@github.com:<your_name_here>/engine.git”,
“custom_deps”: {},
“deps_file”: “DEPS”,
“safesync_url”: “”,
},
]
复制代码将 <your_name_here> 换成你的 GitHub 名字,例如我的是:
solutions = [
{
“managed”: False,
“name”: “src/flutter”,
“url”: “git@github.com:loosaSH/engine.git”,
“custom_deps”: {},
“deps_file”: “DEPS”,
“safesync_url”: “”,
},
]
复制代码:wq 保存退出。
5、执行 gclient sync 命令
这里的执行需要梯子,速度根据你的网络状况,同步下来的文件大小大概有 10G 多吧
$ pwd
/Users/sunhao/development/engine
$ gclient sync
复制代码需要注意的是,这里需要等待文件自动完成,并且尽量不要打断该命令,显示 100% 后仍有很多操作。
6、重新 fetch flutter/engine 仓库
$ cd src/flutter
$ pwd
/Users/sunhao/development/engine/src/flutter
$ git remote add upstream git@github.com:flutter/engine.git
复制代码7、安装辅助工具
安装 JDK 1.8 以上
ant 安装
$ brew install ant
复制代码8、编译 android 相关
确保本地 flutter/engine 仓库是最新的
$ pwd
/Users/sunhao/development/engine/src/flutter
$ git pull upstream master
$ cd /Users/sunhao/development/engine
$ gclient sync
复制代码执行以下命令编译
// 准备文件,生成 compile_commands.json 文件
$ ./flutter/tools/gn --unoptimized
$ ./flutter/tools/gn --android --unoptimized
// 构建
$ ninja -C out/android_debug_unopt && ninja -C out/host_debug_unopt
复制代码整个过程比较漫长,耐心等待。
9、编译 iOS 相关
确保本地 flutter/engine 仓库是最新的
$ pwd
/Users/sunhao/development/engine/src/flutter
$ git pull upstream master
$ cd /Users/sunhao/development/engine
$ gclient sync
复制代码执行以下命令编译
// 准备文件,生成 compile_commands.json 文件
$ ./flutter/tools/gn --ios --unoptimized
$ ./flutter/tools/gn --unoptimized
// 构建
$ ninja -C out/ios_debug_unopt && ninja -C out/host_debug_unopt
复制代码这样 flutter engine 的编译工作就基本完成了。生成的一些编译文件目录为 src/out 。
阅读 Engine 代码 ——Clion
个人认为 Clion 来阅读 C++ 代码比较方便,并且打开速度明显要快于 VSCode
操作比较简单,将上面生成的 compile_commands.json 文件复制到 src/flutter 目录中,然后使用 Clion 打开项目,indexing 之后便可以跟踪跳转(这一步可能也要一点时间)
阅读 Engine 代码 ——VSCode
官方推荐使用 cquery 进行对 engine 的源码依赖跟踪,
1、cmake 安装
安装 cquery 的过程中需要使用到 cmake
$ brew install cmake
复制代码2、cquery 安装
$ brew install --HEAD cquery
复制代码编译 cquery 源码
github.com/cquery-proj…
$ pwd
/Users/sunhao/development
$ git clone --recursive https://github.com/cquery-project/cquery.git
$ cd cquery
$ git submodule update --init
$ mkdir build
$ cd build
$ cmake … -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=release -DCMAKE_EXPORT_COMPILE_COMMANDS=YES
$ cmake --build .
$ cmake --build . --target install
复制代码3、配置 cquery 环境变量
配置环境变量:建议在 ~/.bash_profile 文件中添加
export PATH=/Users/sunhao/development/cquery/build/release/bin:$PATH
复制代码然后执行
$ source ~/.bash_profile
复制代码3、安装 VSCode、以及cquery插件
4、拷贝 cquery 需要的项目配置文件
将 compile_commands.json 文件从 src/out/compile_commands.json 拷贝到 src/
5、配置 VSCode 设置(还是使用clion吧,舒服多了)
github.com/cquery-proj…
因为我们配置了全局的环境变量,所以可以根据官方的推荐设置一些 highlighting。
在 VSCode 中打开 settings.json,在 json 中增加:
“cquery.highlighting.enabled.types”: true,
“cquery.highlighting.enabled.freeStandingFunctions”: true,
“cquery.highlighting.enabled.memberFunctions”: true,
“cquery.highlighting.enabled.freeStandingVariables”: true,
“cquery.highlighting.enabled.memberVariables”: true,
“cquery.highlighting.enabled.namespaces”: true,
“cquery.highlighting.enabled.macros”: true,
“cquery.highlighting.enabled.enums”: true,
“cquery.highlighting.enabled.typeAliases”: true,
“cquery.highlighting.enabled.enumConstants”: true,
“cquery.highlighting.enabled.staticMemberFunctions”: true,
“cquery.highlighting.enabled.parameters”: true,
“cquery.highlighting.enabled.templateParameters”: true,
“cquery.highlighting.enabled.staticMemberVariables”: true,
“cquery.highlighting.enabled.globalVariables”: true,
复制代码这样你就可以在 VSCode 中跟踪 flutter/engine 代码了。
总结
阅读源码只是第一步,后面我会分享关于如何使用本地的 Flutter Engine 以及如何进行调试。
来源:CSDN
作者:weixin_【sjk6070】
链接:https://blog.csdn.net/weixin_46369542/article/details/104484359