android blueprint介绍

一 blueprint介绍

之前我们介绍,blueprint负责解析Android.bp文件内容,我的理解是blueprint类似一个处理相关语法的库文件,soong则是定义具体如何处理相应的语法以及命令实现。也就是soong借助于blueprint定义Android.bp语法,完成Android.bp的解析。

Blueprint is a meta-build system that reads in Blueprints files that describe modules that need to be built, and produces a Ninja (http://martine.github.io/ninja/) manifest describing the commands that need to be run and their dependencie。

通过上面的定义,blueprint将blueprint file转换为ninja,如果要使用定制语法的话,需要使用golang自己进行自定义。blueprint语法如下:

cc_library {

name: "cmd",

srcs: [

"main.c",

],

deps: [

"libc",

],

}

subdirs = ["subdir1", "subdir2"]

Once all modules are read, Blueprint calls any registered Mutators, in registration order. Mutators can visit each module top-down or bottom-up, and modify them as necessar。每个目标已Module为单位,对于module的处理有注册mutator规则来处理,

After all Mutators have run, each module is asked to generate build rules based on property values, and then singletons can generate any build rules from the output of all modules.

还有一个singleton名词,针对所有的module生成build规则。

二 目录介绍

blueprint的目录结构如下:

bootstrapThe Blueprint bootstrapping mechanism is intended to enable building a source tree with minimal prebuilts.

bootstrap/bpdoc

bootstrap/bpglo bbpglob is the command line tool that checks if the list of files matching a glob has changed, and only updates the output file list if it has changed.bootstrap/minibp

bpfmt

bpmodify

deptools

gotestmain

gotestrunner

loadplugins

microfactory Microfactory is a tool to incrementally compile a go program.microfactory/main

parser

pathtools

proptools

这里面比较重要的有bootstarp目录,android需要先将blueprint和soong进行编译,然后才能识别项目中的android.bp文件,如何生成blueprint和soong,就在bootstarp中实现,具体的实现步骤如下:

step 1: 通过build/blueprint/http://build.ninja.in生成out/soong/.minibootstrap/build.ninja,其中out/soong/.minibootstrap/build.ninja用来生成minibp

step 2: minibp分析Android.bp用来生成out/soong/.bootstrap/build.ninja。其中的语法如下:

build ${g.bootstrap.buildDir}/.bootstrap/build.ninja: g.bootstrap.build.ninja $

${g.bootstrap.srcDir}/Android.bp | ${builder}

builder = ${g.bootstrap.BinDir}/minibp

extra = --build-primary

default ${g.bootstrap.buildDir}/.bootstrap/build.ninja

step 3:生成out/soong/build.ninja文件

build ${g.bootstrap.buildDir}/build.ninja: g.bootstrap.build.ninja $

${g.bootstrap.srcDir}/Android.bp | ${builder}

builder = ${g.bootstrap.BinDir}/soong_build

extra = $ -t

default ${g.bootstrap.buildDir}/build.ninja

out/soong/build.ninja是通过soong_build分析android目录下Android.bp文件生成。

build ${g.bootstrap.BinDir}/soong_build: g.bootstrap.cp $

${g.bootstrap.buildDir}/.bootstrap/soong_build/obj/a.out || $

${g.bootstrap.buildDir}/.bootstrap/soong-android/test/test.passed $

${g.bootstrap.buildDir}/.bootstrap/soong-cc-config/test/test.passed $

${g.bootstrap.buildDir}/.bootstrap/soong-cc/test/test.passed $

${g.bootstrap.buildDir}/.bootstrap/soong-java/test/test.passed $

${g.bootstrap.buildDir}/.bootstrap/soong-python/test/test.passed

default ${g.bootstrap.BinDir}/soong_build

通过上面的分析可以得出,bootstarp主要作用就是编译blueprint soong以及生成out/soong/build.ninja文件。因此bootstrap有个别称叫 "primary builder".

结合之前的分析,大体执行流程是

- Runs microfactory.bash to build minibp microfactory.bash在soong_ui.bash中执行

- Runs the .minibootstrap/build.ninja to build .bootstrap/build.ninja

- Runs .bootstrap/build.ninja to build and run the primary builder

上面两步对应的函数为runSoongBootstrap 和 runSoong(具体执行soong.bash)

- Runs build.ninja to build your code runNinja执行

看soong_build,用来生成最后的build.ninja,依赖blueprint 和soong等相关库。

bootstrap_go_binary {

name: "soong_build",

deps: [

"blueprint",

"blueprint-bootstrap",

"soong",

"soong-android",

"soong-env",

],

srcs: [

"main.go",

],

primaryBuilder: true,

}

三 blueprint重要函数

blueprint有几个函数比较重要,比如自定义一个module

my_module {

name: "myName",

foo: "my foo string",

bar: ["my", "bar", "strings"],

}

可以使用如下函数进行自定义规则

func (c *Context) RegisterModuleType(name string, factory ModuleFactory)

type myModule struct {

properties struct {

Foo string

Bar []string

}

}

func NewMyModule() (blueprint.Module, []interface{}) {

module := new(myModule)

properties := &module.properties

return module, []interface{}{properties}

}

func main() {

ctx := blueprint.NewContext()

ctx.RegisterModuleType("my_module", NewMyModule)

// ...

}

blueprint具体的步骤如下:

A Context contains all the state needed to parse a set of Blueprints files

and generate a Ninja file. The process of generating a Ninja file proceeds

through a series of four phases. Each phase corresponds with a some methods

on the Context object

Phase Methods

------------ -------------------------------------------

1. Registration RegisterModuleType, RegisterSingletonType

2. Parse ParseBlueprintsFiles, Parse

3. Generate ResolveDependencies, PrepareBuildActions

4. Write WriteBuildFile

The registration phase prepares the context to process Blueprints files

containing various types of modules. The parse phase reads in one or more

Blueprints files and validates their contents against the module types that

have been registered. The generate phase then analyzes the parsed Blueprints

contents to create an internal representation for the build actions that must

be performed. This phase also performs validation of the module dependencies

and property values defined in the parsed Blueprints files. Finally, the

write phase generates the Ninja manifest text based on the generated build

actions.

可以看到在soong下每个init函数下都有 RegisterModuleType或者 RegisterSingletonType进行module注册。具体的流程在build/blueprint/bootstrap/command.go

func Main(ctx *blueprint.Context, config interface{}, extraNinjaFileDeps ...string) {

ctx.RegisterBottomUpMutator("bootstrap_plugin_deps", pluginDeps)

ctx.RegisterModuleType("bootstrap_go_package", newGoPackageModuleFactory(bootstrapConfig))

ctx.RegisterModuleType("bootstrap_core_go_binary", newGoBinaryModuleFactory(bootstrapConfig, StageBootstrap))

ctx.RegisterModuleType("bootstrap_go_binary", newGoBinaryModuleFactory(bootstrapConfig, StagePrimary))

ctx.RegisterModuleType("blueprint_go_binary", newGoBinaryModuleFactory(bootstrapConfig, StageMain))

ctx.RegisterTopDownMutator("bootstrap_stage", propagateStageBootstrap)

ctx.RegisterSingletonType("bootstrap", newSingletonFactory(bootstrapConfig))

ctx.RegisterSingletonType("glob", globSingletonFactory(ctx))

deps, errs := ctx.ParseBlueprintsFiles(bootstrapConfig.topLevelBlueprintsFile)

if len(errs) > 0 {

fatalErrors(errs)

}

errs = ctx.ResolveDependencies(config)

extraDeps, errs := ctx.PrepareBuildActions(config)

err := ctx.WriteBuildFile(buf)

soong_build和minibp的入口都是在上面的Main中,只不过minibp规则没有soong复杂,soong_build添加了soong中定义的规则。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,036评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,046评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,411评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,622评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,661评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,521评论 1 304
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,288评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,200评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,644评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,837评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,953评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,673评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,281评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,889评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,011评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,119评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,901评论 2 355

推荐阅读更多精彩内容

  • 一 概述 1.1 编译系统变化 随着android工程越来越大,包含的module越来越多,以makefile...
    Little熊猫阅读 30,473评论 4 15
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,130评论 25 707
  • 村子里总会有几个特殊的存在,每天让留守家里的妇女,老人议论。我们村子里有一个特殊的家庭。一个寡妇和她的三...
    whxia阅读 210评论 0 1
  • 今天无意间发现了一种学习已经比较有效的方式,那就是花费一些时间来看曾仕强先生的,易经的智慧,对不视频,虽然总共15...
    智囊团阅读 1,117评论 0 4
  • 小寞有话说:其实本来是打算放开箱视频的……但是我还没有改完,所以先插一个短的黑五海淘攻略。 最近黑五要到啦。之前双...
    用力生活的小寞阅读 499评论 0 2