conditions.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>xmake</title>
  6. <link rel="icon" href="/assets/img/favicon.ico">
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  8. <meta name="description" content="Description">
  9. <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  10. <link href="/assets/npm/github-markdown/github-markdown.min.css" rel="stylesheet">
  11. <style>
  12. .markdown-body {
  13. box-sizing: border-box;
  14. min-width: 200px;
  15. max-width: 980px;
  16. margin: 0 auto;
  17. padding: 45px;
  18. }
  19. @media (max-width: 767px) {
  20. .markdown-body {
  21. padding: 15px;
  22. }
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <article class="markdown-body">
  28. <h4>This is a mirror page, please see the original page: </h4><a href="https://xmake.io/#/zh-cn/manual/conditions">https://xmake.io/#/zh-cn/manual/conditions</a>
  29. <div id="wwads-panel" class="wwads-cn wwads-vertical wwads-sticky" data-id="239" style="max-width:180px;bottom:20px;right:20px;width:200px;height:260px;background:#fff;position:fixed"></div>
  30. </br>
  31. <script type="text/javascript" charset="UTF-8" src="https://cdn.wwads.cn/js/makemoney.js" async></script>
  32. <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CE7I52QU&placement=xmakeio" id="_carbonads_js"></script>
  33. <style>
  34. #carbonads {
  35. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu,
  36. Cantarell, "Helvetica Neue", Helvetica, Arial, sans-serif;
  37. }
  38. #carbonads {
  39. display: flex;
  40. max-width: 330px;
  41. background-color: hsl(0, 0%, 98%);
  42. box-shadow: 0 1px 4px 1px hsla(0, 0%, 0%, .1);
  43. }
  44. #carbonads a {
  45. color: inherit;
  46. text-decoration: none;
  47. }
  48. #carbonads a:hover {
  49. color: inherit;
  50. }
  51. #carbonads span {
  52. position: relative;
  53. display: block;
  54. overflow: hidden;
  55. }
  56. #carbonads .carbon-wrap {
  57. display: flex;
  58. }
  59. .carbon-img {
  60. display: block;
  61. margin: 0;
  62. line-height: 1;
  63. }
  64. .carbon-img img {
  65. display: block;
  66. }
  67. .carbon-text {
  68. font-size: 13px;
  69. padding: 10px;
  70. line-height: 1.5;
  71. text-align: left;
  72. }
  73. .carbon-poweredby {
  74. display: block;
  75. padding: 8px 10px;
  76. background: repeating-linear-gradient(-45deg, transparent, transparent 5px, hsla(0, 0%, 0%, .025) 5px, hsla(0, 0%, 0%, .025) 10px) hsla(203, 11%, 95%, .4);
  77. text-align: center;
  78. text-transform: uppercase;
  79. letter-spacing: .5px;
  80. font-weight: 600;
  81. font-size: 9px;
  82. line-height: 1;
  83. }
  84. </style>
  85. <p>条件判断的api,一般用于必须要处理特定平台的编译逻辑的场合。。通常跟 lua 的 if 语句配合使用。</p>
  86. <h3 id="is_os">is_os</h3>
  87. <h4 id="">判断当前构建目标的操作系统</h4>
  88. <pre><code class="lang-lua">-- 如果当前操作系统是ios
  89. if is_os("ios") then
  90. add_files("src/xxx/*.m")
  91. end
  92. </code></pre>
  93. <p>目前支持的操作系统有:</p>
  94. <ul>
  95. <li>windows</li>
  96. <li>linux</li>
  97. <li>android</li>
  98. <li>macosx</li>
  99. <li>ios</li>
  100. </ul>
  101. <h3 id="is_arch">is_arch</h3>
  102. <h4 id="">判断当前编译架构</h4>
  103. <p>用于检测编译配置:<code>xmake f -a armv7</code></p>
  104. <pre><code class="lang-lua">-- 如果当前架构是x86_64或者i386
  105. if is_arch("x86_64", "i386") then
  106. add_files("src/xxx/*.c")
  107. end
  108. -- 如果当前平台是armv7, arm64, armv7s, armv7-a
  109. if is_arch("armv7", "arm64", "armv7s", "armv7-a") then
  110. -- ...
  111. end
  112. </code></pre>
  113. <p>如果像上面那样一个个去判断所有arm架构,也许会很繁琐,毕竟每个平台的架构类型很多,xmake提供了比<a href="#targetadd_files">add_files</a>更强的lua正则表达式匹配模式,来更加简洁的进行判断:</p>
  114. <pre><code class="lang-lua">--如果当前平台是arm平台
  115. if is_arch("arm.*") then
  116. -- ...
  117. end
  118. </code></pre>
  119. <p>用<code>.*</code>就可以匹配所有了。</p>
  120. <h3 id="is_plat">is_plat</h3>
  121. <h4 id="">判断当前编译平台</h4>
  122. <p>用于检测编译配置:<code>xmake f -p iphoneos</code></p>
  123. <pre><code class="lang-lua">-- 如果当前平台是android
  124. if is_plat("android") then
  125. add_files("src/xxx/*.c")
  126. end
  127. -- 如果当前平台是macosx或者iphoneos
  128. if is_plat("macosx", "iphoneos") then
  129. add_frameworks("Foundation")
  130. end
  131. </code></pre>
  132. <p>目前支持的平台有:</p>
  133. <ul>
  134. <li>windows</li>
  135. <li>cross</li>
  136. <li>linux</li>
  137. <li>macosx</li>
  138. <li>android</li>
  139. <li>iphoneos</li>
  140. <li>watchos</li>
  141. </ul>
  142. <p>当然你也可以自己扩展添加自己的平台,甚至直接指定自己的平台名:</p>
  143. <pre><code class="lang-bash">$ xmake f -p other --sdk=...
  144. </code></pre>
  145. <p>如果指定的平台名不存在,就会自动切到<code>cross</code>平台进行交叉编译,但是却可以通过<code>is_plat("other")</code>来判断自己的平台逻辑。</p>
  146. <h3 id="is_host">is_host</h3>
  147. <h4 id="">判断当前主机环境的操作系统</h4>
  148. <p>有些编译平台是可以在多个不同的操作系统进行构建的,例如:android的ndk就支持linux,macOS还有windows环境。</p>
  149. <p>这个时候就可以通过这个接口,区分当前是在哪个系统环境下进行的构建。</p>
  150. <pre><code class="lang-lua">-- 如果当前主机环境是windows
  151. if is_host("windows") then
  152. add_includedirs("C:\\includes")
  153. else
  154. add_includedirs("/usr/includess")
  155. end
  156. </code></pre>
  157. <p>目前支持的主机环境有:</p>
  158. <ul>
  159. <li>windows</li>
  160. <li>linux</li>
  161. <li>macosx</li>
  162. </ul>
  163. <p>你也可以通过<a href="/mirror/zh-cn/manual/builtin_variables.html#varhost">$(host)</a>内置变量或者<a href="/mirror/zh-cn/manual/builtin_modules.html#oshost">os.host</a>接口,来进行获取</p>
  164. <h3 id="is_subhost">is_subhost</h3>
  165. <h4 id="">判断当前主机的子系统环境</h4>
  166. <p>目前主要用于 windows 系统上 cygwin, msys2 等子系统环境的探测,如果在 msys2 shell 环境下运行 xmake,那么 <code>is_subhost("windows")</code> 想将会返回 false,而 <code>is_host("windows")</code> 依旧会返回 true。</p>
  167. <p>目前支持的子系统:</p>
  168. <ul>
  169. <li>msys</li>
  170. <li>cygwin</li>
  171. </ul>
  172. <p>配置例子:</p>
  173. <pre><code class="lang-lua">if is_subhost("msys", "cygwin") then
  174. -- 当前在 msys2/cygwin 的 shell 环境下
  175. end
  176. </code></pre>
  177. <p>我们也可以通过执行 <code>xmake l os.subhost</code> 来快速查看当前的子系统平台。</p>
  178. <p>!> 后期也有可能会支持 linux 和 macos 系统下的其他子系统环境,如果存在话。</p>
  179. <h3 id="is_subarch">is_subarch</h3>
  180. <h4 id="">判断当前主机子系统环境下的架构</h4>
  181. <p>目前主要用于 windows 系统上 cygwin, msys2 等子系统环境下架构的探测,通常在 windows 编译平台采用 msvc 工具链,那边编译架构时 x64,x86。<br>而在 msys/cygwin 子系统环境下,编译架构默认为 x86_64/i386,是有差异的。</p>
  182. <p>我们也可以通过执行 <code>xmake l os.subarch</code> 来快速查看当前的子系统架构。</p>
  183. <h3 id="is_cross">is_cross</h3>
  184. <h4 id="">判断当前平台是否为交叉编译</h4>
  185. <p>如果当前的目标架构和平台,不是当前的主机平台,属于交叉编译,这个接口就会返回 true。</p>
  186. <h3 id="is_mode">is_mode</h3>
  187. <h4 id="">判断当前编译模式</h4>
  188. <p>用于检测编译配置:<code>xmake f -m debug</code></p>
  189. <p>编译模式的类型并不是内置的,可以自由指定,一般指定:<code>debug</code>, <code>release</code>, <code>profile</code> 这些就够用了,当然你也可以在xmake.lua使用其他模式名来判断。</p>
  190. <pre><code class="lang-lua">-- 如果当前编译模式是debug
  191. if is_mode("debug") then
  192. -- 添加DEBUG编译宏
  193. add_defines("DEBUG")
  194. -- 启用调试符号
  195. set_symbols("debug")
  196. -- 禁用优化
  197. set_optimize("none")
  198. end
  199. -- 如果是release或者profile模式
  200. if is_mode("release", "profile") then
  201. -- 如果是release模式
  202. if is_mode("release") then
  203. -- 隐藏符号
  204. set_symbols("hidden")
  205. -- strip所有符号
  206. set_strip("all")
  207. -- 忽略帧指针
  208. add_cxflags("-fomit-frame-pointer")
  209. add_mxflags("-fomit-frame-pointer")
  210. -- 如果是profile模式
  211. else
  212. -- 启用调试符号
  213. set_symbols("debug")
  214. end
  215. -- 添加扩展指令集
  216. add_vectorexts("sse2", "sse3", "ssse3", "mmx")
  217. end
  218. </code></pre>
  219. <h3 id="is_kind">is_kind</h3>
  220. <h4 id="">判断当前编译类型</h4>
  221. <p>判断当前是否编译的是动态库还是静态库,用于检测编译配置:<code>xmake f -k [static|shared]</code></p>
  222. <p>一般用于如下场景:</p>
  223. <pre><code class="lang-lua">target("test")
  224. -- 通过配置设置目标的kind
  225. set_kind("$(kind)")
  226. add_files("src/*c")
  227. -- 如果当前编译的是静态库,那么添加指定文件
  228. if is_kind("static") then
  229. add_files("src/xxx.c")
  230. end
  231. </code></pre>
  232. <p>编译配置的时候,可手动切换,编译类型:</p>
  233. <pre><code class="lang-bash"># 编译静态库
  234. $ xmake f -k static
  235. $ xmake
  236. </code></pre>
  237. <pre><code class="lang-bash"># 编译动态库
  238. $ xmake f -k shared
  239. $ xmake
  240. </code></pre>
  241. <h3 id="is_config">is_config</h3>
  242. <h4 id="">判断指定配置是否为给定的值</h4>
  243. <p>此接口从2.2.2版本开始引入,用于判断指定配置是否为给定的值,可用于描述域。</p>
  244. <p>例如:</p>
  245. <pre><code class="lang-console">$ xmake f --test=hello1
  246. </code></pre>
  247. <pre><code class="lang-lua">-- 自定义一个配置选项到命令行菜单
  248. option("test")
  249. set_showmenu(true)
  250. set_description("The test config option")
  251. option_end()
  252. -- 如果自定义的test配置值是hello1或者hello2
  253. if is_config("test", "hello1", "hello2") then
  254. add_defines("HELLO")
  255. end
  256. </code></pre>
  257. <p>可以用来根据配置值增加对应的依赖包,例如:</p>
  258. <pre><code class="lang-lua">-- 根据lua_flavor的配置值,选择依赖lua还是luajit
  259. option("lua_flavor")
  260. set_showmenu(true)
  261. set_values("luajit", "lua")
  262. option_end()
  263. if is_config("lua_flavor", "luajit") then
  264. add_requires("luajit")
  265. elseif is_config("lua_flavor", "lua") then
  266. add_requires("lua")
  267. end
  268. </code></pre>
  269. <p>不仅如此,我们还可以设置模式匹配规则去判断值,例如:</p>
  270. <pre><code class="lang-lua">-- 如果自定义的test配置值带有hello前缀
  271. if is_config("test", "hello.*") then
  272. add_defines("HELLO")
  273. end
  274. </code></pre>
  275. <p><p class="tip"><br>此接口不仅能够判断通过<a href="#option">option</a>定义的自定义配置选项,同时还能判断内置的全局配置、本地配置。<br></p>
  276. </p>
  277. <h3 id="has_config">has_config</h3>
  278. <h4 id="">判断配置是否启用或者存在</h4>
  279. <p>此接口从2.2.2版本开始引入,用于检测自定义或者内置的编译配置是否存在或启用,可用于描述域。</p>
  280. <p>例如以下配置情况,都会返回true:</p>
  281. <pre><code class="lang-console"># 启用某个配置选项(如果是boolean类型配置)
  282. $ xmake f --test1=y
  283. $ xmake f --test1=yes
  284. $ xmake f --test1=true
  285. # 设置某个配置选项的值
  286. $ xmake f --test2=value
  287. </code></pre>
  288. <pre><code class="lang-lua">-- 如果test1或者test2被设置或者启用
  289. if has_config("test1", "test2") then
  290. add_defines("TEST")
  291. end
  292. </code></pre>
  293. <p>而下面的情况则会禁用配置,返回false:</p>
  294. <pre><code class="lang-console"># 禁用配置(如果是boolean类型配置)
  295. $ xmake f --test1=n
  296. $ xmake f --test1=no
  297. $ xmake f --test1=false
  298. </code></pre>
  299. <p><p class="tip"><br>此接口不仅能够判断内置的全局配置、本地配置,同时还可以判断通过<a href="#option">option</a>定义的自定义配置选项。<br></p>
  300. </p>
  301. <h3 id="has_package">has_package</h3>
  302. <h4 id="">判断依赖包是否启用或者存在</h4>
  303. <p>此接口从2.2.3版本开始引入,用于检测远程依赖包是否存在或启用,可用于描述域。</p>
  304. <p>一般配合<a href="/mirror/zh-cn/manual/global_interfaces.html#add_requires">add_requires</a>一起使用,例如:</p>
  305. <pre><code class="lang-lua">add_requires("tbox", {optional = true})
  306. target("test")
  307. set_kind("binary")
  308. add_files("src/*.c")
  309. add_packages("tbox")
  310. if has_package("tbox") then
  311. add_defines("HAVE_TBOX")
  312. end
  313. </code></pre>
  314. <p>如果通过<code>add_requires</code>添加的可选依赖包,远程下载安装失败,或者当前平台不支持导致实际上没有被正常安装上,那么<code>has_package</code>就会返回false,<br>表示不存在,然后对其他flags定义甚至源文件编译控制做一些特殊处理。</p>
  315. <p><p class="tip"><br>此接口跟<a href="#has_config">has_config</a>的区别在于,<a href="#has_config">has_config</a>用于<a href="#option">option</a>,而它用于<a href="#add_requires">add_requires</a>。<br></p>
  316. </p>
  317. </article>
  318. </body>
  319. </html>