iOS.Build.Bazel.2-Starlark

2022/9/6 23:24:18

本文主要是介绍iOS.Build.Bazel.2-Starlark,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Starlark: Introduction

 

1. What's Starlark? 

"Starlark is the domain-specific language people use to configure and extend Bazel.

It's conceived as a restricted subset of Python

It is not Turing-complete, which discourages some (but not all) users from trying to accomplish

general programming tasks within the language." Ref[1]

 

"Starlark is implemented in the com.google.devtools.build.lib.syntax package" Ref[1]

 

1.1 Starlark and Python3

Ref[2]

Starlark的语法受Python3语法的启发。

 

1.2 Starlark的实现

Ref[6]

Starlark有多种实现: Go, Java, Rust。

 

2. 可变性 

Ref[2]

 

 

3. BUILD vs. .bzl files

Ref[2]

BUILD通过调用规则(rule)来注册目标(target)。

.bzl 文件为常量、规则、宏、函数提供定义。

 

在BUILD文件中, 原生函数(Native functions)和原生规则(Native rules)是全局符号。

.bzl 文件需要使用原生模块(Native module)来加载原生函数、原生规则。

 

3.1 BUILD文件的限制

BUILD文件中不允许:

A. 声明函数

B. 使用*args和**kwargs

 

4. Starlark vs. Python

Ref[2]

4.1 Starlark的约束

1. 全局变量是不可变的。

2. for语句不能出现在顶级作用域。可以在函数中使用for语句。在BUILD文件中,for语句可以出现在列表推导中。

3. if语句不能出现在顶级作用域。但是if表达式可以,例如: "first = data[0] if len(data) > 0 else None"

4. 在遍历字典时,顺序是明确的。

5. 递归不被允许。

6. Int类型是32位有符号整数。如果溢出,会抛出Error。

7. 在迭代中修改集合(Collection)会产生Error。

8. 只有相等性测试(equality tests)被允许跨越类型。其它比较操作符在跨越类型时会产生Error。

5 == "5" # will return false
5 < 'foo'  # will throw an error

 

9. 元组中,尾部逗号只有在小括号之间时是合法的。

10. 字典字面常量不允许有重复的key。

11. 字符串使用双引号来表示。

12. 字符串不允许遍历。

 

 

 


Reference

1. Starlark

https://bazel.build/contribute/codebase#starlark

 

2. Starlark Language

https://bazel.build/rules/language

 

3. Starlark in Go

Starlark in Go: the Starlark configuration language, implemented in Go

https://github.com/google/starlark-go

 

4. API Overview

https://bazel.build/rules/lib/starlark-overview

 

5. Starlark Language Specification

https://github.com/bazelbuild/starlark/blob/master/spec.md

 

6. Starlark implementations, tools, and users 

https://github.com/bazelbuild/starlark/blob/master/users.md

 



这篇关于iOS.Build.Bazel.2-Starlark的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程