Skip to content

Commit d6fe41f

Browse files
improve formatting and wording for condition-and-select-compile guide (#286)
* polish wording for condition-and-select-compile.md * add dollar signs for commands in condition-and-select-compile.md
1 parent 9a39580 commit d6fe41f

1 file changed

Lines changed: 67 additions & 44 deletions

File tree

docs/zh/posts/condition-and-select-compile.md

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,31 @@ author: Ruki
66
outline: deep
77
---
88

9-
xmake 提供了一些内置的条件判断api,用于在选择性编译时,获取到一些工程状态的相关信息,来调整编译逻辑。
9+
xmake 提供了一些内置的条件判断api,用于在选择性编译时,获取到一些工程状态的相关信息,来调整编译逻辑。
1010

1111
例如:`is_os`, `is_plat`, `is_arch`, `is_kind`, `is_mode`, `is_option`
1212

1313
### `is_mode`
1414

15-
我们先拿最常用的`is_mode`来讲讲如何使用,这个api主要用来判断当前的编译模式,例如平常编译配置的时候,会执行:
15+
我们先讲讲最常用的`is_mode`,这个api主要用来判断当前的编译模式,例如平常编译配置的时候,会执行:
1616

1717
```bash
1818
$ xmake f -m debug
1919
$ xmake
2020
```
2121

22-
来编译`debug`版本,那么模式就是`debug`,那么`release`版本,也就是`release`
22+
来编译`debug`版本,那么模式就是`debug`,那么`release`版本,也就是`release`
2323

2424
```bash
2525
$ xmake f -m release
2626
$ xmake
2727
```
2828

29-
但是如果仅仅只是这么配置,xmake还是不知道如果为debug进行编译,如何编译release版本,因为这些模式的值不是内置的
30-
31-
我们可以随便设置,例如:profile, checking等等,用来编译性能模式,检测模式,这些就看咱们项目实际的需求了。。
32-
33-
一般情况下只需要`debug``release`就行了,那如何区分呢,这就需要在`xmake.lua`进行配置了,一般可参考如下配置:
34-
29+
但是如果仅仅只是这么配置,xmake不知道在debug模式和release模式分别应该怎么编译,因为模式名只是个代号,没有默认行为。
3530

31+
我们可以随便设置模式名,例如profile、checking,用来代表性能模式、检测模式,具体使用什么名字看咱们项目实际的需求。
3632

33+
一般情况下只需要`debug``release`就行了。如何区分编译行为呢,这就需要在`xmake.lua`进行配置了,一般可参考如下配置:
3734

3835
```lua
3936
-- 如果当前编译模式是debug
@@ -68,14 +65,17 @@ end
6865

6966
通过判断是否在编译debug版本,来启用和禁用调试符号信息,并且判断是否禁用和启用优化。
7067

71-
当然,如果我们的项目还设置了其他模式,例如性能分析模式:profile,那么还可以通过这个来判断是否需要添加一些分析分析上的编译选项
68+
当然,如果我们的项目还设置了其他模式,例如性能分析模式:profile,那么还可以通过这个来判断是否需要添加性能分析方面的编译选项
7269

70+
自 xmake 2.2.1 起,部分常用模式配置可通过内置规则简化,请参阅[内置规则](zh/api/description/builtin-rules)
7371

7472
### `is_plat`
7573

76-
接下来我们讲讲这个编译平台的判断,这个也非常实用哦,虽然我们的工具是为了跨平台开发,通常的配置肯定都是通用的
74+
接下来我们讲讲编译平台的判断,这个也非常实用哦。
75+
76+
虽然 xmake 是跨平台的工具,其中的设置选项大多是全平台通用的。
7777

78-
但是毕竟项目成千上万,需求各不相同,总归会有些项目需要针对不同的平台做些编译上的特殊处理
78+
但是毕竟项目成千上万,需求各不相同,总归会有项目需要针对不同的平台做些编译上的特殊处理。
7979

8080
这个时候,我们就需要这个api了,例如:
8181

@@ -85,45 +85,63 @@ if is_plat("android") then
8585
add_files("src/xxx/*.c")
8686
end
8787

88-
--如果当前平台是macosx或者iphoneos
89-
if is_plat("macosx", "iphoneos") then
88+
-- 如果当前平台是macosx或iphoneos
89+
if is_plat("macosx") or is_plat("iphoneos") then
9090
add_mxflags("-framework Foundation")
9191
add_ldflags("-framework Foundation")
9292
end
9393
```
9494

9595
这里针对android平台,增加了一些特殊代码的编译,针对macosx和iphoneos平台,增加了Foundation框架的链接。
9696

97-
这里还有个比较实用的小技巧,`is_xxx`系列接口,都是可以同时传递多个参数的,逻辑上是or的关系
97+
这里还有个比较实用的小技巧,`is_xxx`系列接口,都可以同时传递多个参数,相当于用or连接。
9898

99-
我们可以像上面那么写法
99+
我们可以像把刚刚的判断逻辑,改成下面这种写法
100100

101101
```lua
102-
if is_plat("macosx", "iphoneos", "android", "linux") then
102+
-- 如果当前平台是android
103+
if is_plat("android") then
104+
add_files("src/xxx/*.c")
105+
end
106+
107+
-- 如果当前平台是macosx或iphoneos
108+
if is_plat("macosx", "iphoneos") then
109+
add_mxflags("-framework Foundation")
110+
add_ldflags("-framework Foundation")
103111
end
104112
```
105113

106-
否则如果用lua的原生语法的话,虽然也可以,但是会很臃肿,例如:
114+
这对统一处理在特定方面一致的多个平台很有帮助。
115+
116+
一个极端的例子:
107117

108118
```lua
109-
if is_plat("macosx") or is_plat("iphoneos") or is_plat("android") or is_plat("linux") then
119+
-- 需要判断是否为主流平台
120+
121+
if is_plat("macosx") or is_plat("iphoneos") or is_plat("android") or is_plat("linux") or is_plat("windows") then
122+
-- ...
123+
end
124+
125+
-- 等价于
126+
if is_plat("macosx", "iphoneos", "android", "linux", "windows") then
127+
-- ...
110128
end
111129
```
112130

113-
除了`is_xxx`系列,像:`add_xxxs` 这种后缀有`s`的复数api,都是可以传递多个参数的哦,例如`add_files`
131+
顺带一提,除了`is_xxx`系列,像:`add_xxxs` 这种后缀有`s`的复数api,都可以传递多个参数哦,例如`add_files`
114132

115133
```lua
116134
add_files("src/*.c", "test.c", "hello.cpp")
117135
```
118136

119-
等等,这里就不一一介绍了。。。
137+
等等,这里就不一一介绍了。
120138

121139
### `is_arch`
122140

123-
这个跟`is_plat`类似,不过是用来判断当前编译的目标架构的,也就是:
141+
用法和`is_plat`类似,用来判断当前编译的目标架构,也就是:
124142

125143
```bash
126-
xmake f --arch=x86_64
144+
$ xmake f --arch=x86_64
127145
```
128146

129147
然后,我们在工程描述中,进行判断:
@@ -134,39 +152,44 @@ if is_arch("x86_64", "i386") then
134152
add_files("src/xxx/*.c")
135153
end
136154

137-
--如果当前平台是armv7, arm64, armv7s, armv7-a
155+
-- 如果当前架构是armv7, arm64, armv7s, armv7-a
138156
if is_arch("armv7", "arm64", "armv7s", "armv7-a") then
139157
-- ...
140158
end
141159
```
142160

143-
如果像上面那样一个个去判断所有arm架构,也许会很繁琐,毕竟每个平台的架构类型很多,xmake提供了类似`add_files`中的通配符匹配模式,来更加简洁的进行判断:
161+
如果像上面那样一个个列出架构名,有时会很繁琐,毕竟同一个架构可能会有很多细分。
162+
163+
xmake为`is_arch`提供了类似`add_files`中的通配符匹配,用来简化架构判断。
164+
165+
例如,判断arm架构时,可以使用下面的写法:
144166

145167
```lua
146-
--如果当前平台是arm平台
168+
-- arm架构通常以arm开头
147169
if is_arch("arm*") then
148170
-- ...
149171
end
150172
```
151173

152-
*就可以匹配所有了。
174+
可以直接匹配到`armv7``arm64``armv7s``armv7-a`等架构
153175

154176
### `is_os`
155177

156-
这个很简单,用来判断当前编译目标,例如:
178+
`is_plat`相似,但用来判断当前编译目标所属的操作系统类型。
179+
180+
例如,iphoneos和watchos等平台都是ios系统,使用`is_plat`会显得繁琐,此时可通过`is_os`进行统一处理。
157181

158182
```lua
159-
-- 如果当前操作系统是ios
160183
if is_os("ios") then
161184
add_files("src/xxx/*.m")
162185
end
163186
```
164187

165-
目前支持的操作系统有:windows、linux、android、macosx、ios
188+
`is_os`目前支持的操作系统有:windows、linux、android、macosx、ios
166189

167190
### `is_kind`
168191

169-
用来判断当前是否编译的是动态库还是静态库
192+
用来判断当前是否编译的是动态库还是静态库
170193

171194
一般用于如下场景:
172195

@@ -175,7 +198,7 @@ end
175198
target("test")
176199
-- 通过配置设置目标的kind
177200
set_kind("$(kind)")
178-
add_files("src/*c")
201+
add_files("src/*.c")
179202

180203
-- 如果当前编译的是静态库,那么添加指定文件
181204
if is_kind("static") then
@@ -184,23 +207,21 @@ target("test")
184207

185208
```
186209

187-
编译配置的时候,可手动切换,编译类型
210+
编译配置时,可手动切换编译类型
188211

189-
```lua
190-
191-
-- 编译静态库
192-
xmake f -k static
193-
xmake
212+
```bash
213+
# 编译静态库
214+
$ xmake f -k static
215+
$ xmake
194216

195-
-- 编译动态库
196-
xmake f -k shared
197-
xmake
217+
# 编译动态库
218+
$ xmake f -k shared
219+
$ xmake
198220
```
199221

200222
### `is_option`
201223

202-
203-
如果某个自动检测选项、手动设置选项被启用,那么可以通过`is_option`接口来判断,例如:
224+
如果某个 自动检测 或 手动设置 的选项被启用,那么可以通过`is_option`接口来判断,例如:
204225

205226
```lua
206227

@@ -210,4 +231,6 @@ if is_option("demo") then
210231
-- 编译demo目录下的代码
211232
add_subdirs("src/demo")
212233
end
213-
```
234+
```
235+
236+
关于`option`的详细用法,请参阅[配置选项](zh/api/description/configuration-option.html)

0 commit comments

Comments
 (0)