Conditional Compilation
Inspect one language lane at a time so line-level text and code deltas stay readable.
Diff Lane
English
0 modified sections0 code block delta0 anchor delta
Diff Lane
中文
4 modified sections1 code block delta3 anchor delta
modified使用方法text+3 lines, -1 line
v1.0.5
Section Text
1
以内置 os 编译条件为例,其使用方法如下:2
3
<!-- run -->4
5
6
在上面代码中,开发者在 `Linux` 系统中可以正确编译执行;在`非 Linux` 系统中,则会遇到找不到 `mc` 类定义的编译错误。7
8
值得注意的是:9
10
- 仓颉不支持编译条件嵌套,以下写法均不允许:11
12
```cangjie13
@When[os == "Windows"]14
@When[os == "Linux"] // Error, illegal nested when conditional compilation15
import std.ast.*16
@When[os == "Windows"]17
@When[os == "Linux"] // Error, illegal nested when conditional compilation18
func A(){}19
```20
21
- `@When[...]` 作为内置编译标记,在导入前处理,由宏展开生成的代码中含有 `@When[...]` 会编译报错,如:22
23
```cangjie24
@M0 // macro which returns the input25
@When[os == "Linux"] // Error, unexpected when conditional compilation directive26
func A(){}27
```Code 1 · cangjie
1
@When[os == "Linux"]2
class mc{}3
4
main(): Int64 {5
var a = mc()6
return 07
}v1.1.0
Section Text
1
以内置 os 编译条件为例,其使用方法如下:2
3
<!-- run -->4
5
6
在上面代码中,开发者在 `Linux` 系统中可以正确编译执行;在`非 Linux` 系统中,则会遇到找不到 `mc` 类定义的编译错误。7
8
值得注意的是:9
10
- 仓颉不支持编译条件嵌套,以下写法均不允许:11
12
<!-- compile.error -->13
14
```cangjie15
@When[os == "Windows"]16
@When[os == "Linux"] // Error, illegal nested when conditional compilation17
import std.ast.*18
@When[os == "Windows"]19
@When[os == "Linux"] // Error, illegal nested when conditional compilation20
func A(){}21
```22
23
- `@When[...]` 作为内置编译标记,在导入前处理,由宏展开生成的代码中含有 `@When[...]` 会编译报错,如:24
25
<!-- compile.error -->26
27
```cangjie28
@Derive[ToString]29
@When[os == "Linux"] // Error, unexpected when conditional compilation directive30
class A {}31
```Code 1 · cangjie
1
@When[os == "Linux"]2
class mc{}3
4
main(): Int64 {5
var a = mc()6
return 07
}modified内置编译条件变量text+1 line, -1 line
v1.0.5
Section Text
1
仓颉提供的内置条件变量有: `os`、 `backend`、 `arch`、 `cjc_version`、 `debug` 和 `test`。v1.1.0
Section Text
1
仓颉提供的内置条件变量有: `os`、 `arch`、 `env`、 `backend`、 `cjc_version`、 `debug` 和 `test`。modifiedostext+1 line, -1 line
v1.0.5
Section Text
1
os 表示目标平台的操作系统。`os` 支持 `==` 和 `!=` 两种操作符。支持的操作系统有:`Windows`、`Linux`、`macOS`。2
3
使用方式如下:4
5
<!-- run -->6
7
8
如果在 `Windows` 环境下编译执行,会得到 `Windows, NOT Linux` 的信息;如果是在 `Linux` 环境下,则会得到 `Linux, NOT Windows` 的信息。Code 1 · cangjie
1
@When[os == "Linux"]2
func foo() {3
print("Linux, ")4
}5
@When[os == "Windows"]6
func foo() {7
print("Windows, ")8
}9
@When[os != "Windows"]10
func fee() {11
println("NOT Windows")12
}13
@When[os != "Linux"]14
func fee() {15
println("NOT Linux")16
}17
main() {18
foo()19
fee()20
}v1.1.0
Section Text
1
os 表示目标平台的操作系统。`os` 支持 `==` 和 `!=` 两种操作符。支持的操作系统有:`Windows`、`Linux`、`macOS`、`iOS`。2
3
使用方式如下:4
5
<!-- run -->6
7
8
如果在 `Windows` 环境下编译执行,会得到 `Windows, NOT Linux` 的信息;如果是在 `Linux` 环境下,则会得到 `Linux, NOT Windows` 的信息。Code 1 · cangjie
1
@When[os == "Linux"]2
func foo() {3
print("Linux, ")4
}5
@When[os == "Windows"]6
func foo() {7
print("Windows, ")8
}9
@When[os != "Windows"]10
func fee() {11
println("NOT Windows")12
}13
@When[os != "Linux"]14
func fee() {15
println("NOT Linux")16
}17
main() {18
foo()19
fee()20
}modified多条件编译textcode+5 lines, -1 line
v1.0.5
Section Text
1
仓颉条件编译允许开发者自由组合多个条件编译选项。支持逻辑运算符组合多个条件,支持括号运算符明确优先级。2
3
使用方式如下:4
5
6
使用如下编译命令编译运行上段代码:7
8
9
会得到输出结果如下:Code 1 · cangjie
1
//source.cj2
@When[(test || feature == "lion") && !debug]3
func fee() {4
println("feature lion")5
}6
main() {7
fee()8
}Code 2 · shell
1
$ cjc --cfg="feature=lion" source.cj -o runner.outCode 3 · text
1
feature lionCode 4 · cangjie
1
v1.1.0
Section Text
1
仓颉条件编译允许开发者自由组合多个条件编译选项。支持逻辑运算符组合多个条件,支持括号运算符明确优先级。2
3
使用方式示例一:4
5
<!-- verify -->6
<!-- cfg="--cfg='feature=lion'" -->7
8
9
使用如下编译命令编译运行上段代码:10
11
12
会得到输出结果如下:13
14
15
使用方式示例二:16
17
仓颉交叉编译至目标平台 `aarch64-linux-android31`,条件变量设置如下面代码所示,若需交叉编译至其他平台,请参考 [目标平台和条件编译映射表](#目标平台和条件编译映射表) 配置相应的条件编译选项。Code 1 · cangjie
1
//source.cj2
@When[(test || feature == "lion") && !debug]3
func fee() {4
println("feature lion")5
}6
main() {7
fee()8
}Code 2 · shell
1
$ cjc --cfg="feature=lion" source.cj -o runner.outCode 3 · text
1
feature lionCode 4 · cangjie
1
@When[os == "Linux" && arch == "aarch64" && env == "android"]2
func foo() {3
"target aarch64-linux-android31 run"4
}5
6
main() {7
println(foo())8
}addedenv
v1.1.0
1
env2
3
`env` 在其他条件变量的基础上提供额外信息,比如目标平台的 ABI (Application Binary Interface),用于消除目标平台之间的歧义。`env` 条件支持 `==` 和 `!=` 两种操作符。4
5
支持的 `env` 选项有:`ohos`、`gnu`、`simulator`、`android`以及缺省(空字符串)。6
7
使用方式如下:8
9
<!-- run -->10
11
12
在 OpenHarmony 目标平台上编译执行,会得到 `ohos` 的信息;在其他目标平台编译执行,会得到 `other` 的信息。13
14
@When[env == "ohos"]15
var env = "ohos"16
17
@When[env != "ohos"]18
var env = "other"19
20
main() {21
println(env)22
}added附录
v1.1.0
1
附录added目标平台和条件编译映射表
v1.1.0
1
目标平台和条件编译映射表2
3
仓颉交叉编译支持的目标平台由内置条件变量 `os`、 `arch`、 `env` 共同确定,三者与目标平台的对应关系如下表所示:4
5
| 目标平台 | arch | os | env |6
| ----------------------------- | --------- | --------- | ----------- |7
| x86_64-windows-gnu | "x86_64" | "Windows" | "gnu" |8
| x86_64-linux-gnu | "x86_64" | "Linux" | "gnu" |9
| x86_64-apple-darwin | "x86_64" | "macOS" | "" |10
| x86_64-linux-ohos | "x86_64" | "Linux" | "ohos" |11
| x86_64-w64-mingw32 | "x86_64" | "Windows" | "gnu" |12
| x86_64-linux-android[26+]<sup>[android target]</sup> | "x86_64" | "Linux" | "android" |13
| aarch64-linux-gnu | "aarch64" | "Linux" | "gnu" |14
| aarch64-linux-android[26+]<sup>[android target]</sup> | "aarch64" | "Linux" | "android" |15
| aarch64-apple-darwin | "aarch64" | "macOS" | "" |16
| aarch64-linux-ohos | "aarch64" | "Linux" | "ohos" |17
| arm64-apple-ios[11+]<sup>[ios target]</sup> | "aarch64" | "iOS" | "" |18
| arm64-apple-ios[11+]-simulator<sup>[ios target]</sup> | "aarch64" | "iOS" | "simulator" |19
20
<sup>[android target]</sup> x86_64-linux-android[26+] 中 android 后缀的数字用于指定 Android API Level。未指定数字时,默认 API Level 为 26;指定数字(如 x86_64-linux-android33)表示 Android API Level 为 33,指定的数字应大于等于 26。21
<sup>[ios target]</sup> arm64-apple-ios[11+] 中 ios 后缀的数字用于指定 ios 版本信息。未指定数字时,默认为 11;指定数字(如 arm64-apple-ios26)表示 ios 版本为 26,指定的数字应大于等于 11。