2
0

conditions.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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/#/manual/conditions">https://xmake.io/#/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>Conditions are generally used to handle some special compilation platforms.</p>
  86. <table>
  87. <thead>
  88. <tr>
  89. <th>Interfaces</th>
  90. <th>Description</th>
  91. <th>Support version</th>
  92. </tr>
  93. </thead>
  94. <tbody>
  95. <tr>
  96. <td><a href="#is_os">is_os</a></td>
  97. <td>Is the current compilation target system?</td>
  98. <td>>= 2.0.1</td>
  99. </tr>
  100. <tr>
  101. <td><a href="#is_arch">is_arch</a></td>
  102. <td>Is the current compilation architecture?</td>
  103. <td>>= 2.0.1</td>
  104. </tr>
  105. <tr>
  106. <td><a href="#is_plat">is_plat</a></td>
  107. <td>Is the current compilation platform?</td>
  108. <td>>= 2.0.1</td>
  109. </tr>
  110. <tr>
  111. <td><a href="#is_host">is_host</a></td>
  112. <td>Is the current compilation host system?</td>
  113. <td>>= 2.1.4</td>
  114. </tr>
  115. <tr>
  116. <td><a href="#is_mode">is_mode</a></td>
  117. <td>Is the current compilation mode?</td>
  118. <td>>= 2.0.1</td>
  119. </tr>
  120. <tr>
  121. <td><a href="#is_kind">is_kind</a></td>
  122. <td>Is the current target kind?</td>
  123. <td>>= 2.0.1</td>
  124. </tr>
  125. <tr>
  126. <td><a href="#is_config">is_config</a></td>
  127. <td>Is the given config values?</td>
  128. <td>>= 2.2.2</td>
  129. </tr>
  130. <tr>
  131. <td><a href="#has_config">has_config</a></td>
  132. <td>Is the given configs enabled?</td>
  133. <td>>= 2.2.2</td>
  134. </tr>
  135. <tr>
  136. <td><a href="#has_package">has_package</a></td>
  137. <td>Is the given dependent package enabled?</td>
  138. <td>>= 2.2.3</td>
  139. </tr>
  140. </tbody>
  141. </table>
  142. <h3 id="is_os">is_os</h3>
  143. <h4 id="isthecurrentcompilationtargetsystem">Is the current compilation target system</h4>
  144. <pre><code class="lang-lua">if is_os("ios") then
  145. add_files("src/xxx/*.m")
  146. end
  147. </code></pre>
  148. <p>Support operation systems:</p>
  149. <ul>
  150. <li>windows</li>
  151. <li>linux</li>
  152. <li>android</li>
  153. <li>macosx</li>
  154. <li>ios</li>
  155. </ul>
  156. <h3 id="is_arch">is_arch</h3>
  157. <h4 id="isthecurrentcompilationarchitecture">Is the current compilation architecture</h4>
  158. <p>You can use this api to check the configuration command: <code>xmake f -a armv7</code></p>
  159. <pre><code class="lang-lua">-- if the current architecture is x86_64 or i386
  160. if is_arch("x86_64", "i386") then
  161. add_files("src/xxx/*.c")
  162. end
  163. -- if the current architecture is armv7 or arm64 or armv7s or armv7-a
  164. if is_arch("armv7", "arm64", "armv7s", "armv7-a") then
  165. -- ...
  166. end
  167. </code></pre>
  168. <p>And you can also use the lua regular expression: <code>.*</code> to check all matched architectures.</p>
  169. <pre><code class="lang-lua">-- if the current architecture is arm which contains armv7, arm64, armv7s and armv7-a ...
  170. if is_arch("arm.*") then
  171. -- ...
  172. end
  173. </code></pre>
  174. <h3 id="is_plat">is_plat</h3>
  175. <h4 id="isthecurrentcompilationplatform">Is the current compilation platform</h4>
  176. <p>You can use this api to check the configuration command: <code>xmake f -p iphoneos</code></p>
  177. <pre><code class="lang-lua">-- if the current platform is android
  178. if is_plat("android") then
  179. add_files("src/xxx/*.c")
  180. end
  181. -- if the current platform is macosx or iphoneos
  182. if is_plat("macosx", "iphoneos") then
  183. add_frameworks("Foundation")
  184. end
  185. </code></pre>
  186. <p>Available platforms:</p>
  187. <table>
  188. <thead>
  189. <tr>
  190. <th>Platform</th>
  191. </tr>
  192. </thead>
  193. <tbody>
  194. <tr>
  195. <td>android</td>
  196. </tr>
  197. <tr>
  198. <td>appletvos</td>
  199. </tr>
  200. <tr>
  201. <td>applexros</td>
  202. </tr>
  203. <tr>
  204. <td>bsd</td>
  205. </tr>
  206. <tr>
  207. <td>cross</td>
  208. </tr>
  209. <tr>
  210. <td>cygwin</td>
  211. </tr>
  212. <tr>
  213. <td>haiku</td>
  214. </tr>
  215. <tr>
  216. <td>iphoneos</td>
  217. </tr>
  218. <tr>
  219. <td>linux</td>
  220. </tr>
  221. <tr>
  222. <td>macosx</td>
  223. </tr>
  224. <tr>
  225. <td>mingw</td>
  226. </tr>
  227. <tr>
  228. <td>msys</td>
  229. </tr>
  230. <tr>
  231. <td>wasm</td>
  232. </tr>
  233. <tr>
  234. <td>watchos</td>
  235. </tr>
  236. <tr>
  237. <td>windows</td>
  238. </tr>
  239. </tbody>
  240. </table>
  241. <h3 id="is_host">is_host</h3>
  242. <h4 id="isthecurrentcompilationhostsystem">Is the current compilation host system</h4>
  243. <p>Some compilation platforms can be built on multiple different operating systems, for example: android ndk (on linux, macOS and windows).</p>
  244. <p>So, we can use this api to determine the current host operating system.</p>
  245. <pre><code class="lang-lua">if is_host("windows") then
  246. add_includedirs("C:\\includes")
  247. else
  248. add_includedirs("/usr/includess")
  249. end
  250. </code></pre>
  251. <p>Support hosts:</p>
  252. <ul>
  253. <li>windows</li>
  254. <li>linux</li>
  255. <li>macosx</li>
  256. </ul>
  257. <p>We can also get it from <a href="/mirror/manual/builtin_variables.html#varhost">$(host)</a> or <a href="/mirror/manual/builtin_modules.html#oshost">os.host</a>.</p>
  258. <h3 id="is_subhost">is_subhost</h3>
  259. <h4 id="determinethesubsystemenvironmentofthecurrenthost">Determine the subsystem environment of the current host</h4>
  260. <p>At present, it is mainly used for detection of cygwin, msys2 and other subsystem environments on windows systems. If you run xmake in the msys2 shell environment, then <code>is_subhost("windows")</code> will return false, and <code>is_host("windows")</code> It will still return true.</p>
  261. <p>Currently supported subsystems:</p>
  262. <ul>
  263. <li>msys</li>
  264. <li>cygwin</li>
  265. </ul>
  266. <p>Configuration example:</p>
  267. <pre><code class="lang-lua">if is_subhost("msys", "cygwin") then
  268. - Currently in the shell environment of msys2/cygwin
  269. end
  270. </code></pre>
  271. <p>We can also quickly check the current subsystem platform by executing <code>xmake l os.subhost</code>.</p>
  272. <p>!> It may also support other subsystem environments under linux and macos systems later, if they exist.</p>
  273. <h3 id="is_subarch">is_subarch</h3>
  274. <h4 id="determinethearchitectureofthecurrenthostsubsystemenvironment">Determine the architecture of the current host subsystem environment</h4>
  275. <p>At present, it is mainly used for the detection of the architecture under the subsystem environment such as cygwin and msys2 on the windows system. The msvc tool chain is usually used on the windows compilation platform, and the architecture is x64, x86.<br>In the msys/cygwin subsystem environment, the compiler architecture defaults to x86_64/i386, which is different.</p>
  276. <p>We can also quickly view the current subsystem architecture by executing <code>xmake l os.subarch</code>.</p>
  277. <h3 id="is_cross">is_cross</h3>
  278. <h4 id="determineswhetherthecurrentplatformiscrosscompiledornot">Determines whether the current platform is cross-compiled or not.</h4>
  279. <p>This interface returns true if the current target architecture and platform, which is not the current host platform, is cross-compiled.</p>
  280. <h3 id="is_mode">is_mode</h3>
  281. <h4 id="isthecurrentcompilationmode">Is the current compilation mode</h4>
  282. <p>You can use this api to check the configuration command: <code>xmake f -m debug</code></p>
  283. <p>The compilation mode is not builtin mode for xmake, so you can set the mode value by yourself.</p>
  284. <p>We often use these configuration values: <code>debug</code>, <code>release</code>, <code>profile</code>, etc.</p>
  285. <pre><code class="lang-lua">-- if the current compilation mode is debug?
  286. if is_mode("debug") then
  287. -- add macro: DEBUG
  288. add_defines("DEBUG")
  289. -- enable debug symbols
  290. set_symbols("debug")
  291. -- disable optimization
  292. set_optimize("none")
  293. end
  294. -- if the current compilation mode is release or profile?
  295. if is_mode("release", "profile") then
  296. if is_mode("release") then
  297. -- mark symbols visibility as hidden
  298. set_symbols("hidden")
  299. -- strip all symbols
  300. set_strip("all")
  301. -- fomit frame pointer
  302. add_cxflags("-fomit-frame-pointer")
  303. add_mxflags("-fomit-frame-pointer")
  304. else
  305. -- enable debug symbols
  306. set_symbols("debug")
  307. end
  308. -- add vectorexts
  309. add_vectorexts("sse2", "sse3", "ssse3", "mmx")
  310. end
  311. </code></pre>
  312. <h3 id="is_kind">is_kind</h3>
  313. <h4 id="isthecurrenttargetkind">Is the current target kind</h4>
  314. <p>You can use this api to check the configuration command: <code>xmake f -k [static|shared]</code></p>
  315. <pre><code class="lang-lua">target("test")
  316. -- set target kind from the configuration command
  317. set_kind("$(kind)")
  318. add_files("src/*c")
  319. -- compile target for static?
  320. if is_kind("static") then
  321. add_files("src/xxx.c")
  322. end
  323. </code></pre>
  324. <p>You can switch the target kind by configuration command.</p>
  325. <pre><code class="lang-bash"># compile as static library
  326. $ xmake f -k static
  327. $ xmake
  328. </code></pre>
  329. <pre><code class="lang-bash"># compile as shared library
  330. $ xmake f -k shared
  331. $ xmake
  332. </code></pre>
  333. <h3 id="is_config">is_config</h3>
  334. <h4 id="isthegivenconfigvalues">Is the given config values?</h4>
  335. <p>This interface is introduced from version 2.2.2 to determine whether the specified configuration is a given value.</p>
  336. <p>For example:</p>
  337. <pre><code class="lang-console">$ xmake f --test=hello1
  338. </code></pre>
  339. <pre><code class="lang-lua">option("test")
  340. set_showmenu(true)
  341. set_description("The test config option")
  342. option_end()
  343. if is_config("test", "hello1", "hello2") then
  344. add_defines("HELLO")
  345. end
  346. </code></pre>
  347. <p>Can be used for conditional package requirements, eg. :</p>
  348. <pre><code class="lang-lua">-- Add lua or luajit package requirements, depending on the lua_flavor option value
  349. option("lua_flavor")
  350. set_showmenu(true)
  351. set_values("luajit", "lua")
  352. option_end()
  353. if is_config("lua_flavor", "luajit") then
  354. add_requires("luajit")
  355. elseif is_config("lua_flavor", "lua") then
  356. add_requires("lua")
  357. end
  358. </code></pre>
  359. <p>Not only that, we can also set pattern matching rules to determine values, such as:</p>
  360. <pre><code class="lang-lua">if is_config("test", "hello.*") then
  361. add_defines("HELLO")
  362. end
  363. </code></pre>
  364. <p><p class="tip"><br>This interface is not only able to determine the custom options defined through the <a href="#option">option</a>,<br>but also to determine the built-in global and local configuration.<br></p>
  365. </p>
  366. <h3 id="has_config">has_config</h3>
  367. <h4 id="isthegivenconfigsenabled">Is the given configs enabled?</h4>
  368. <p>This interface is introduced from version 2.2.2 to detect whether a custom or built-in option/configuration exists or is enabled.</p>
  369. <p>For example, the following configuration will be true:</p>
  370. <pre><code class="lang-console"># enable the given config or option (if be boolean type)
  371. $ xmake f --test1=y
  372. $ xmake f --test1=yes
  373. $ xmake f --test1=true
  374. # set the config value
  375. $ xmake f --test2=value
  376. </code></pre>
  377. <pre><code class="lang-lua">if has_config("test1", "test2") then
  378. add_defines("TEST")
  379. end
  380. </code></pre>
  381. <p>And the following configuration will be false:</p>
  382. <pre><code class="lang-console"># disable config/option(if be boolean type)
  383. $ xmake f --test1=n
  384. $ xmake f --test1=no
  385. $ xmake f --test1=false
  386. </code></pre>
  387. <p><p class="tip"><br>This interface can determine not only the built-in global and local configs,<br>but also the custom options defined through the <a href="#option">option</a>.<br></p>
  388. </p>
  389. <h3 id="has_package">has_package</h3>
  390. <h4 id="isthegivendependentpackageenabled">Is the given dependent package enabled?</h4>
  391. <p>This interface is introduced from version 2.2.3 to detect whether a dependent package exists or is enabled.</p>
  392. <p>It is usually used to <a href="/mirror/zh-cn/manual/global_interfaces.html#add_requires">add_requires</a>.</p>
  393. <pre><code class="lang-lua">add_requires("tbox", {optional = true})
  394. target("test")
  395. set_kind("binary")
  396. add_files("src/*.c")
  397. add_packages("tbox")
  398. if has_package("tbox") then
  399. add_defines("HAVE_TBOX")
  400. end
  401. </code></pre>
  402. <p>If the remote dependencies are added via the optional add-on package added by <code>add_requires</code>, or the current platform does not support the actual installation, then <code>has_package</code> will return false.<br>Indicates that it does not exist, and then does some special processing for other flags definitions and even source file compilation controls.</p>
  403. <p><p class="tip"><br>The difference between this interface and <a href="#has_config">has_config</a> is that <a href="#has_config">has_config</a> is used for <a href="#option">option</a> whereas this is used for <a href="#add_requires">add_requires</a>.<br></p>
  404. </p>
  405. </article>
  406. </body>
  407. </html>