helper_interfaces.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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/helper_interfaces">https://xmake.io/#/manual/helper_interfaces</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. <h3 id="automaticdetectionofauxiliaryinterface">Automatic detection of auxiliary interface</h3>
  86. <p>In addition, the version of this interface after 2.2.5 provides some built-in helper functions, which can be imported directly using includes. See the specific built-in functions: <a href="https://github.com/xmake- io/xmake/tree/master/xmake/includes">Helper functions</a></p>
  87. <p>We can use these interfaces to detect links, c/c++ type, includes and compiler features, and write macro definitions to config.h</p>
  88. <p>Among them, we provide two types of interfaces, <code>check_xxx</code> and <code>configvar_check_xxx</code>. The interfaces prefixed with <code>configvar_</code> will be written into the config.h.in template file specified by <code>add_configfiles</code> after passing the test.</p>
  89. <p>And <code>check_xxx</code> only defines related macros to participate in compilation, but it will not be persisted in <code>config.h.in</code>.</p>
  90. <p>For related issues, see:</p>
  91. <p>-<a href="https://github.com/xmake-io/xmake/issues/342">#342</a><br>-<a href="https://github.com/xmake-io/xmake/issues/1715">#1715</a></p>
  92. <p>Note: Instead of introducing these interfaces separately via <code>includes("check_links.lua")</code>,<br>for versions 2.8.5 and above, you can use the more convenient</p>
  93. <pre><code class="lang-lua">includes("@builtin/check")
  94. </code></pre>
  95. <p>to bring in all the checking interfaces at once, but of course we can also bring in individual scripts on an as-needed basis:</p>
  96. <pre><code class="lang-lua">includes("@builtin/check/check_links.lua")
  97. </code></pre>
  98. <p>The original introduction path, which does not distinguish whether it is a user path or not, is not easy to manage and maintain,<br>and is easily interfered by user configurations, so it will be gradually deprecated later.</p>
  99. <h4 id="checklinks">Check links</h4>
  100. <p>We can check whether the specified links pass or not by trying to link.</p>
  101. <pre><code class="lang-lua">includes("check_links.lua")
  102. target("test")
  103. set_kind("binary")
  104. add_files("*.c")
  105. add_configfiles("config.h.in")
  106. configvar_check_links("HAS_PTHREAD", {"pthread", "m", "dl"})
  107. </code></pre>
  108. <p>config.h.in</p>
  109. <pre><code class="lang-c">${define HAS_PTHREAD}
  110. </code></pre>
  111. <p>config.h</p>
  112. <pre><code class="lang-c">#define HAS_PTHREAD 1
  113. /* #undef HAS_PTHREAD */
  114. </code></pre>
  115. <h4 id="detectcctype">Detect c/c++ type</h4>
  116. <p>We can also detect the existence of c/c++ types.</p>
  117. <p><code>configvar_check_ctypes</code> is used to detect c code types, and <code>configvar_check_cxxtypes</code> is used to detect c++ code types.</p>
  118. <pre><code class="lang-lua">includes("check_ctypes.lua")
  119. target("test")
  120. set_kind("binary")
  121. add_files("*.c")
  122. add_configfiles("config.h.in")
  123. configvar_check_ctypes("HAS_WCHAR", "wchar_t")
  124. configvar_check_ctypes("HAS_WCHAR_AND_FLOAT", {"wchar_t", "float"})
  125. </code></pre>
  126. <p>config.h.in</p>
  127. <pre><code class="lang-c">${define HAS_WCHAR}
  128. ${define HAS_WCHAR_AND_FLOAT}
  129. </code></pre>
  130. <p>config.h</p>
  131. <pre><code class="lang-c">/* #undef HAS_WCHAR */
  132. /* #undef HAS_WCHAR_AND_FLOAT */
  133. </code></pre>
  134. <h4 id="detectccfunctions">Detect c/c++ functions</h4>
  135. <p><code>configvar_check_cfuncs</code> is used to detect c code functions, and <code>configvar_check_cxxfuncs</code> is used to detect c++ code functions.</p>
  136. <pre><code class="lang-lua">includes("check_cfuncs.lua")
  137. target("test")
  138. set_kind("binary")
  139. add_files("*.c")
  140. add_configfiles("config.h.in")
  141. configvar_check_cfuncs("HAS_SETJMP", "setjmp", {includes = {"signal.h", "setjmp.h"}})
  142. </code></pre>
  143. <p>config.h.in</p>
  144. <pre><code class="lang-c">${define HAS_SETJMP}
  145. </code></pre>
  146. <p>config.h</p>
  147. <pre><code class="lang-c">#define HAS_SETJMP 1
  148. /* #undef HAS_SETJMP */
  149. </code></pre>
  150. <h4 id="detectccheaderfiles">Detect c/c++ header files</h4>
  151. <p><code>configvar_check_cincludes</code> is used to detect c code header files, and <code>configvar_check_cxxincludes</code> is used to detect c++ code header files.</p>
  152. <pre><code class="lang-lua">includes("check_cincludes.lua")
  153. target("test")
  154. set_kind("binary")
  155. add_files("*.c")
  156. add_configfiles("config.h.in")
  157. configvar_check_cincludes("HAS_STRING_H", "string.h")
  158. configvar_check_cincludes("HAS_STRING_AND_STDIO_H", {"string.h", "stdio.h"})
  159. </code></pre>
  160. <p>config.h.in</p>
  161. <pre><code class="lang-c">${define HAS_STRING_H}
  162. ${define HAS_STRING_AND_STDIO_H}
  163. </code></pre>
  164. <p>config.h</p>
  165. <pre><code class="lang-c">/* #undef HAS_STRING_H */
  166. #define HAS_STRING_AND_STDIO_H 1
  167. </code></pre>
  168. <h4 id="detectcccodesnippets">Detect c/c++ code snippets</h4>
  169. <p><code>configvar_check_csnippets</code> is used to detect c code snippets, and <code>configvar_check_cxxsnippets</code> is used to detect c++ code snippets.</p>
  170. <pre><code class="lang-lua">includes("check_csnippets.lua")
  171. target("test")
  172. set_kind("binary")
  173. add_files("*.c")
  174. add_configfiles("config.h.in")
  175. configvar_check_csnippets("HAS_STATIC_ASSERT", "_Static_assert(1, \"\");")
  176. </code></pre>
  177. <p>config.h.in</p>
  178. <pre><code class="lang-c">${define HAS_STATIC_ASSERT}
  179. </code></pre>
  180. <p>config.h</p>
  181. <pre><code class="lang-c">#define HAS_STATIC_ASSERT 1
  182. </code></pre>
  183. <p>After v2.5.7, check_csnippets has been improved, adding <code>tryrun</code> and <code>output</code> parameters to try to run and capture output.</p>
  184. <pre><code class="lang-lua">includes("check_csnippets.lua")
  185. target("test")
  186. set_kind("binary")
  187. add_files("*.c")
  188. add_configfiles("config.h.in")
  189. check_csnippets("HAS_INT_4", "return (sizeof(int) == 4)? 0: -1;", {tryrun = true})
  190. check_csnippets("INT_SIZE",&#39;printf("%d", sizeof(int)); return 0;&#39;, {output = true, number = true})
  191. configvar_check_csnippets("HAS_LONG_8", "return (sizeof(long) == 8)? 0: -1;", {tryrun = true})
  192. configvar_check_csnippets("PTR_SIZE",&#39;printf("%d", sizeof(void*)); return 0;&#39;, {output = true, number = true})
  193. </code></pre>
  194. <p>If capture output is enabled, <code>${define PTR_SIZE}</code> in <code>config.h.in</code> will automatically generate <code>#define PTR_SIZE 4</code>.</p>
  195. <p>Among them, the <code>number = true</code> setting can be forced as a number instead of a string value, otherwise it will be defined as <code>#define PTR_SIZE "4"</code> by default</p>
  196. <h4 id="detectingcompilerfeatures">Detecting compiler features</h4>
  197. <pre><code class="lang-lua">includes("check_features.lua")
  198. target("test")
  199. set_kind("binary")
  200. add_files("*.c")
  201. add_configfiles("config.h.in")
  202. configvar_check_features("HAS_CONSTEXPR", "cxx_constexpr")
  203. configvar_check_features("HAS_CONSEXPR_AND_STATIC_ASSERT", {"cxx_constexpr", "c_static_assert"}, {languages = "c++11"})
  204. </code></pre>
  205. <p>config.h.in</p>
  206. <pre><code class="lang-c">${define HAS_CONSTEXPR}
  207. ${define HAS_CONSEXPR_AND_STATIC_ASSERT}
  208. </code></pre>
  209. <p>config.h</p>
  210. <pre><code class="lang-c">/* #undef HAS_CONSTEXPR */
  211. #define HAS_CONSEXPR_AND_STATIC_ASSERT 1
  212. </code></pre>
  213. <p>List of all c compiler features:</p>
  214. <table>
  215. <thead>
  216. <tr>
  217. <th>Feature name</th>
  218. </tr>
  219. </thead>
  220. <tbody>
  221. <tr>
  222. <td>c_static_assert</td>
  223. </tr>
  224. <tr>
  225. <td>c_restrict</td>
  226. </tr>
  227. <tr>
  228. <td>c_variadic_macros</td>
  229. </tr>
  230. <tr>
  231. <td>c_function_prototypes</td>
  232. </tr>
  233. </tbody>
  234. </table>
  235. <p>List of all c++ compiler features:</p>
  236. <table>
  237. <thead>
  238. <tr>
  239. <th>Feature name</th>
  240. </tr>
  241. </thead>
  242. <tbody>
  243. <tr>
  244. <td>cxx_variable_templates</td>
  245. </tr>
  246. <tr>
  247. <td>cxx_relaxed_constexpr</td>
  248. </tr>
  249. <tr>
  250. <td>cxx_aggregate_default_initializers</td>
  251. </tr>
  252. <tr>
  253. <td>cxx_contextual_conversions</td>
  254. </tr>
  255. <tr>
  256. <td>cxx_attribute_deprecated</td>
  257. </tr>
  258. <tr>
  259. <td>cxx_decltype_auto</td>
  260. </tr>
  261. <tr>
  262. <td>cxx_digit_separators</td>
  263. </tr>
  264. <tr>
  265. <td>cxx_generic_lambdas</td>
  266. </tr>
  267. <tr>
  268. <td>cxx_lambda_init_captures</td>
  269. </tr>
  270. <tr>
  271. <td>cxx_binary_literals</td>
  272. </tr>
  273. <tr>
  274. <td>cxx_return_type_deduction</td>
  275. </tr>
  276. <tr>
  277. <td>cxx_decltype_incomplete_return_types</td>
  278. </tr>
  279. <tr>
  280. <td>cxx_reference_qualified_functions</td>
  281. </tr>
  282. <tr>
  283. <td>cxx_alignof</td>
  284. </tr>
  285. <tr>
  286. <td>cxx_attributes</td>
  287. </tr>
  288. <tr>
  289. <td>cxx_inheriting_constructors</td>
  290. </tr>
  291. <tr>
  292. <td>cxx_thread_local</td>
  293. </tr>
  294. <tr>
  295. <td>cxx_alias_templates</td>
  296. </tr>
  297. <tr>
  298. <td>cxx_delegating_constructors</td>
  299. </tr>
  300. <tr>
  301. <td>cxx_extended_friend_declarations</td>
  302. </tr>
  303. <tr>
  304. <td>cxx_final</td>
  305. </tr>
  306. <tr>
  307. <td>cxx_nonstatic_member_init</td>
  308. </tr>
  309. <tr>
  310. <td>cxx_override</td>
  311. </tr>
  312. <tr>
  313. <td>cxx_user_literals</td>
  314. </tr>
  315. <tr>
  316. <td>cxx_constexpr</td>
  317. </tr>
  318. <tr>
  319. <td>cxx_defaulted_move_initializers</td>
  320. </tr>
  321. <tr>
  322. <td>cxx_enum_forward_declarations</td>
  323. </tr>
  324. <tr>
  325. <td>cxx_noexcept</td>
  326. </tr>
  327. <tr>
  328. <td>cxx_nullptr</td>
  329. </tr>
  330. <tr>
  331. <td>cxx_range_for</td>
  332. </tr>
  333. <tr>
  334. <td>cxx_unrestricted_unions</td>
  335. </tr>
  336. <tr>
  337. <td>cxx_explicit_conversions</td>
  338. </tr>
  339. <tr>
  340. <td>cxx_lambdas</td>
  341. </tr>
  342. <tr>
  343. <td>cxx_local_type_template_args</td>
  344. </tr>
  345. <tr>
  346. <td>cxx_raw_string_literals</td>
  347. </tr>
  348. <tr>
  349. <td>cxx_auto_type</td>
  350. </tr>
  351. <tr>
  352. <td>cxx_defaulted_functions</td>
  353. </tr>
  354. <tr>
  355. <td>cxx_deleted_functions</td>
  356. </tr>
  357. <tr>
  358. <td>cxx_generalized_initializers</td>
  359. </tr>
  360. <tr>
  361. <td>cxx_inline_namespaces</td>
  362. </tr>
  363. <tr>
  364. <td>cxx_sizeof_member</td>
  365. </tr>
  366. <tr>
  367. <td>cxx_strong_enums</td>
  368. </tr>
  369. <tr>
  370. <td>cxx_trailing_return_types</td>
  371. </tr>
  372. <tr>
  373. <td>cxx_unicode_literals</td>
  374. </tr>
  375. <tr>
  376. <td>cxx_uniform_initialization</td>
  377. </tr>
  378. <tr>
  379. <td>cxx_variadic_templates</td>
  380. </tr>
  381. <tr>
  382. <td>cxx_decltype</td>
  383. </tr>
  384. <tr>
  385. <td>cxx_default_function_template_args</td>
  386. </tr>
  387. <tr>
  388. <td>cxx_long_long_type</td>
  389. </tr>
  390. <tr>
  391. <td>cxx_right_angle_brackets</td>
  392. </tr>
  393. <tr>
  394. <td>cxx_rvalue_references</td>
  395. </tr>
  396. <tr>
  397. <td>cxx_static_assert</td>
  398. </tr>
  399. <tr>
  400. <td>cxx_extern_templates</td>
  401. </tr>
  402. <tr>
  403. <td>cxx_func_identifier</td>
  404. </tr>
  405. <tr>
  406. <td>cxx_variadic_macros</td>
  407. </tr>
  408. <tr>
  409. <td>cxx_template_template_parameters</td>
  410. </tr>
  411. </tbody>
  412. </table>
  413. <p>v2.5.9 adds c++17 feature detection:</p>
  414. <table>
  415. <thead>
  416. <tr>
  417. <th>Feature name</th>
  418. </tr>
  419. </thead>
  420. <tbody>
  421. <tr>
  422. <td>cxx_aggregate_bases</td>
  423. </tr>
  424. <tr>
  425. <td>cxx_aligned_new</td>
  426. </tr>
  427. <tr>
  428. <td>cxx_capture_star_this</td>
  429. </tr>
  430. <tr>
  431. <td>cxx_constexpr</td>
  432. </tr>
  433. <tr>
  434. <td>cxx_deduction_guides</td>
  435. </tr>
  436. <tr>
  437. <td>cxx_enumerator_attributes</td>
  438. </tr>
  439. <tr>
  440. <td>cxx_fold_expressions</td>
  441. </tr>
  442. <tr>
  443. <td>cxx_guaranteed_copy_elision</td>
  444. </tr>
  445. <tr>
  446. <td>cxx_hex_float</td>
  447. </tr>
  448. <tr>
  449. <td>cxx_if_constexpr</td>
  450. </tr>
  451. <tr>
  452. <td>cxx_inheriting_constructors</td>
  453. </tr>
  454. <tr>
  455. <td>cxx_inline_variables</td>
  456. </tr>
  457. <tr>
  458. <td>cxx_namespace_attributes</td>
  459. </tr>
  460. <tr>
  461. <td>cxx_noexcept_function_type</td>
  462. </tr>
  463. <tr>
  464. <td>cxx_nontype_template_args</td>
  465. </tr>
  466. <tr>
  467. <td>cxx_nontype_template_parameter_auto</td>
  468. </tr>
  469. <tr>
  470. <td>cxx_range_based_for</td>
  471. </tr>
  472. <tr>
  473. <td>cxx_static_assert</td>
  474. </tr>
  475. <tr>
  476. <td>cxx_structured_bindings</td>
  477. </tr>
  478. <tr>
  479. <td>cxx_template_template_args</td>
  480. </tr>
  481. <tr>
  482. <td>cxx_variadic_using</td>
  483. </tr>
  484. </tbody>
  485. </table>
  486. <p>v2.5.9 adds c++20 feature detection:</p>
  487. <table>
  488. <thead>
  489. <tr>
  490. <th>Feature name</th>
  491. </tr>
  492. </thead>
  493. <tbody>
  494. <tr>
  495. <td>cxx_aggregate_paren_init</td>
  496. </tr>
  497. <tr>
  498. <td>cxx_char8_t</td>
  499. </tr>
  500. <tr>
  501. <td>cxx_concepts</td>
  502. </tr>
  503. <tr>
  504. <td>cxx_conditional_explicit</td>
  505. </tr>
  506. <tr>
  507. <td>cxx_consteval</td>
  508. </tr>
  509. <tr>
  510. <td>cxx_constexpr</td>
  511. </tr>
  512. <tr>
  513. <td>cxx_constexpr_dynamic_alloc</td>
  514. </tr>
  515. <tr>
  516. <td>cxx_constexpr_in_decltype</td>
  517. </tr>
  518. <tr>
  519. <td>cxx_constinit</td>
  520. </tr>
  521. <tr>
  522. <td>cxx_deduction_guides</td>
  523. </tr>
  524. <tr>
  525. <td>cxx_designated_initializers</td>
  526. </tr>
  527. <tr>
  528. <td>cxx_generic_lambdas</td>
  529. </tr>
  530. <tr>
  531. <td>cxx_impl_coroutine</td>
  532. </tr>
  533. <tr>
  534. <td>cxx_impl_destroying_delete</td>
  535. </tr>
  536. <tr>
  537. <td>cxx_impl_three_way_comparison</td>
  538. </tr>
  539. <tr>
  540. <td>cxx_init_captures</td>
  541. </tr>
  542. <tr>
  543. <td>cxx_modules</td>
  544. </tr>
  545. <tr>
  546. <td>cxx_nontype_template_args</td>
  547. </tr>
  548. <tr>
  549. <td>cxx_using_enum</td>
  550. </tr>
  551. </tbody>
  552. </table>
  553. <p>After 2.5.8, support for cstd and c++ std versions are added, related issues: <a href="https://github.com/xmake-io/xmake/issues/1715">#1715</a></p>
  554. <pre><code class="lang-lua">configvar_check_features("HAS_CXX_STD_98", "cxx_std_98")
  555. configvar_check_features("HAS_CXX_STD_11", "cxx_std_11", {languages = "c++11"})
  556. configvar_check_features("HAS_CXX_STD_14", "cxx_std_14", {languages = "c++14"})
  557. configvar_check_features("HAS_CXX_STD_17", "cxx_std_17", {languages = "c++17"})
  558. configvar_check_features("HAS_CXX_STD_20", "cxx_std_20", {languages = "c++20"})
  559. configvar_check_features("HAS_C_STD_89", "c_std_89")
  560. configvar_check_features("HAS_C_STD_99", "c_std_99")
  561. configvar_check_features("HAS_C_STD_11", "c_std_11", {languages = "c11"})
  562. configvar_check_features("HAS_C_STD_17", "c_std_17", {languages = "c17"})
  563. </code></pre>
  564. <h4 id="detectbuiltinmacrodefinitions">Detect built-in macro definitions</h4>
  565. <p>There are some built-in macro definitions in the compiler, such as <code>__GNUC__</code>, etc. We can use the <code>check_macros</code> and <code>configvar_check_macros</code> auxiliary scripts to detect their existence.</p>
  566. <p>Related issues: <a href="https://github.com/xmake-io/xmake/issues/1715">#1715</a></p>
  567. <pre><code class="lang-lua">- Check whether the macro is defined
  568. configvar_check_macros("HAS_GCC", "__GNUC__")
  569. - The detection macro is not defined
  570. configvar_check_macros("NO_GCC", "__GNUC__", {defined = false})
  571. - Detect macro conditions
  572. configvar_check_macros("HAS_CXX20", "__cplusplus >= 202002L", {languages = "c++20"})
  573. </code></pre>
  574. <h4 id="detecttypesize">Detect type size</h4>
  575. <p>In previous versions, type detection was possible with <code>check_csnippets</code> and <code>output = true</code>.</p>
  576. <pre><code class="lang-lua">check_csnippets("INT_SIZE", &#39;printf("%d", sizeof(int)); return 0;&#39;, {output = true, number = true})
  577. </code></pre>
  578. <p>This way, however, extracts the type size information by trying to run the test code and then getting the run output.</p>
  579. <p>This doesn&#39;t work for cross-compilation.</p>
  580. <p>In version 2.8.5, we added the <code>check_sizeof</code> helper interface, which allows you to extract type size information by parsing the binary file of the test program directly.</p>
  581. <p>Since there is no need to run tests, this approach not only supports cross-compilation, but also greatly improves the efficiency of testing and is much easier to use.</p>
  582. <pre><code class="lang-lua">includes("@builtin/check")
  583. target("test")
  584. set_kind("static")
  585. add_files("*.cpp")
  586. check_sizeof("LONG_SIZE", "long")
  587. check_sizeof("STRING_SIZE", "std::string", {includes = "string"})
  588. </code></pre>
  589. <pre><code class="lang-bash">$ xmake f -c
  590. checking for LONG_SIZE ... 8
  591. checking for STRING_SIZE ... 24
  592. </code></pre>
  593. <p>Alternatively, I can check for it in the script field with <code>target:check_sizeof</code>.</p>
  594. <h4 id="detectingbigendian">Detecting big-endian</h4>
  595. <p>After version 2.8.9, we added the <code>check_bigendian</code> interface to determine if the current compilation target is in bigendian mode.</p>
  596. <pre><code class="lang-lua">includes("@builtin/check")
  597. target("test")
  598. set_kind("static")
  599. add_files("*.cpp")
  600. check_bigendian("IS_BIG_ENDIAN")
  601. </code></pre>
  602. <p>If the test passes and it is currently in big endian mode, then <code>IS_BIG_ENDIAN=1</code> is defined.</p>
  603. </article>
  604. </body>
  605. </html>