| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>xmake</title>
- <link rel="icon" href="/assets/img/favicon.ico">
- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
- <meta name="description" content="Description">
- <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
- <link href="/assets/npm/github-markdown/github-markdown.min.css" rel="stylesheet">
- <style>
- .markdown-body {
- box-sizing: border-box;
- min-width: 200px;
- max-width: 980px;
- margin: 0 auto;
- padding: 45px;
- }
- @media (max-width: 767px) {
- .markdown-body {
- padding: 15px;
- }
- }
- </style>
- </head>
- <body>
- <article class="markdown-body">
- <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>
- <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>
- </br>
- <script type="text/javascript" charset="UTF-8" src="https://cdn.wwads.cn/js/makemoney.js" async></script>
- <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CE7I52QU&placement=xmakeio" id="_carbonads_js"></script>
- <style>
- #carbonads {
- font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu,
- Cantarell, "Helvetica Neue", Helvetica, Arial, sans-serif;
- }
- #carbonads {
- display: flex;
- max-width: 330px;
- background-color: hsl(0, 0%, 98%);
- box-shadow: 0 1px 4px 1px hsla(0, 0%, 0%, .1);
- }
- #carbonads a {
- color: inherit;
- text-decoration: none;
- }
- #carbonads a:hover {
- color: inherit;
- }
- #carbonads span {
- position: relative;
- display: block;
- overflow: hidden;
- }
- #carbonads .carbon-wrap {
- display: flex;
- }
- .carbon-img {
- display: block;
- margin: 0;
- line-height: 1;
- }
- .carbon-img img {
- display: block;
- }
- .carbon-text {
- font-size: 13px;
- padding: 10px;
- line-height: 1.5;
- text-align: left;
- }
- .carbon-poweredby {
- display: block;
- padding: 8px 10px;
- 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);
- text-align: center;
- text-transform: uppercase;
- letter-spacing: .5px;
- font-weight: 600;
- font-size: 9px;
- line-height: 1;
- }
- </style>
- <h3 id="automaticdetectionofauxiliaryinterface">Automatic detection of auxiliary interface</h3>
- <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>
- <p>We can use these interfaces to detect links, c/c++ type, includes and compiler features, and write macro definitions to config.h</p>
- <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>
- <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>
- <p>For related issues, see:</p>
- <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>
- <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>
- <pre><code class="lang-lua">includes("@builtin/check")
- </code></pre>
- <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>
- <pre><code class="lang-lua">includes("@builtin/check/check_links.lua")
- </code></pre>
- <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>
- <h4 id="checklinks">Check links</h4>
- <p>We can check whether the specified links pass or not by trying to link.</p>
- <pre><code class="lang-lua">includes("check_links.lua")
- target("test")
- set_kind("binary")
- add_files("*.c")
- add_configfiles("config.h.in")
- configvar_check_links("HAS_PTHREAD", {"pthread", "m", "dl"})
- </code></pre>
- <p>config.h.in</p>
- <pre><code class="lang-c">${define HAS_PTHREAD}
- </code></pre>
- <p>config.h</p>
- <pre><code class="lang-c">#define HAS_PTHREAD 1
- /* #undef HAS_PTHREAD */
- </code></pre>
- <h4 id="detectcctype">Detect c/c++ type</h4>
- <p>We can also detect the existence of c/c++ types.</p>
- <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>
- <pre><code class="lang-lua">includes("check_ctypes.lua")
- target("test")
- set_kind("binary")
- add_files("*.c")
- add_configfiles("config.h.in")
- configvar_check_ctypes("HAS_WCHAR", "wchar_t")
- configvar_check_ctypes("HAS_WCHAR_AND_FLOAT", {"wchar_t", "float"})
- </code></pre>
- <p>config.h.in</p>
- <pre><code class="lang-c">${define HAS_WCHAR}
- ${define HAS_WCHAR_AND_FLOAT}
- </code></pre>
- <p>config.h</p>
- <pre><code class="lang-c">/* #undef HAS_WCHAR */
- /* #undef HAS_WCHAR_AND_FLOAT */
- </code></pre>
- <h4 id="detectccfunctions">Detect c/c++ functions</h4>
- <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>
- <pre><code class="lang-lua">includes("check_cfuncs.lua")
- target("test")
- set_kind("binary")
- add_files("*.c")
- add_configfiles("config.h.in")
- configvar_check_cfuncs("HAS_SETJMP", "setjmp", {includes = {"signal.h", "setjmp.h"}})
- </code></pre>
- <p>config.h.in</p>
- <pre><code class="lang-c">${define HAS_SETJMP}
- </code></pre>
- <p>config.h</p>
- <pre><code class="lang-c">#define HAS_SETJMP 1
- /* #undef HAS_SETJMP */
- </code></pre>
- <h4 id="detectccheaderfiles">Detect c/c++ header files</h4>
- <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>
- <pre><code class="lang-lua">includes("check_cincludes.lua")
- target("test")
- set_kind("binary")
- add_files("*.c")
- add_configfiles("config.h.in")
- configvar_check_cincludes("HAS_STRING_H", "string.h")
- configvar_check_cincludes("HAS_STRING_AND_STDIO_H", {"string.h", "stdio.h"})
- </code></pre>
- <p>config.h.in</p>
- <pre><code class="lang-c">${define HAS_STRING_H}
- ${define HAS_STRING_AND_STDIO_H}
- </code></pre>
- <p>config.h</p>
- <pre><code class="lang-c">/* #undef HAS_STRING_H */
- #define HAS_STRING_AND_STDIO_H 1
- </code></pre>
- <h4 id="detectcccodesnippets">Detect c/c++ code snippets</h4>
- <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>
- <pre><code class="lang-lua">includes("check_csnippets.lua")
- target("test")
- set_kind("binary")
- add_files("*.c")
- add_configfiles("config.h.in")
- configvar_check_csnippets("HAS_STATIC_ASSERT", "_Static_assert(1, \"\");")
- </code></pre>
- <p>config.h.in</p>
- <pre><code class="lang-c">${define HAS_STATIC_ASSERT}
- </code></pre>
- <p>config.h</p>
- <pre><code class="lang-c">#define HAS_STATIC_ASSERT 1
- </code></pre>
- <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>
- <pre><code class="lang-lua">includes("check_csnippets.lua")
- target("test")
- set_kind("binary")
- add_files("*.c")
- add_configfiles("config.h.in")
- check_csnippets("HAS_INT_4", "return (sizeof(int) == 4)? 0: -1;", {tryrun = true})
- check_csnippets("INT_SIZE",'printf("%d", sizeof(int)); return 0;', {output = true, number = true})
- configvar_check_csnippets("HAS_LONG_8", "return (sizeof(long) == 8)? 0: -1;", {tryrun = true})
- configvar_check_csnippets("PTR_SIZE",'printf("%d", sizeof(void*)); return 0;', {output = true, number = true})
- </code></pre>
- <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>
- <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>
- <h4 id="detectingcompilerfeatures">Detecting compiler features</h4>
- <pre><code class="lang-lua">includes("check_features.lua")
- target("test")
- set_kind("binary")
- add_files("*.c")
- add_configfiles("config.h.in")
- configvar_check_features("HAS_CONSTEXPR", "cxx_constexpr")
- configvar_check_features("HAS_CONSEXPR_AND_STATIC_ASSERT", {"cxx_constexpr", "c_static_assert"}, {languages = "c++11"})
- </code></pre>
- <p>config.h.in</p>
- <pre><code class="lang-c">${define HAS_CONSTEXPR}
- ${define HAS_CONSEXPR_AND_STATIC_ASSERT}
- </code></pre>
- <p>config.h</p>
- <pre><code class="lang-c">/* #undef HAS_CONSTEXPR */
- #define HAS_CONSEXPR_AND_STATIC_ASSERT 1
- </code></pre>
- <p>List of all c compiler features:</p>
- <table>
- <thead>
- <tr>
- <th>Feature name</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>c_static_assert</td>
- </tr>
- <tr>
- <td>c_restrict</td>
- </tr>
- <tr>
- <td>c_variadic_macros</td>
- </tr>
- <tr>
- <td>c_function_prototypes</td>
- </tr>
- </tbody>
- </table>
- <p>List of all c++ compiler features:</p>
- <table>
- <thead>
- <tr>
- <th>Feature name</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>cxx_variable_templates</td>
- </tr>
- <tr>
- <td>cxx_relaxed_constexpr</td>
- </tr>
- <tr>
- <td>cxx_aggregate_default_initializers</td>
- </tr>
- <tr>
- <td>cxx_contextual_conversions</td>
- </tr>
- <tr>
- <td>cxx_attribute_deprecated</td>
- </tr>
- <tr>
- <td>cxx_decltype_auto</td>
- </tr>
- <tr>
- <td>cxx_digit_separators</td>
- </tr>
- <tr>
- <td>cxx_generic_lambdas</td>
- </tr>
- <tr>
- <td>cxx_lambda_init_captures</td>
- </tr>
- <tr>
- <td>cxx_binary_literals</td>
- </tr>
- <tr>
- <td>cxx_return_type_deduction</td>
- </tr>
- <tr>
- <td>cxx_decltype_incomplete_return_types</td>
- </tr>
- <tr>
- <td>cxx_reference_qualified_functions</td>
- </tr>
- <tr>
- <td>cxx_alignof</td>
- </tr>
- <tr>
- <td>cxx_attributes</td>
- </tr>
- <tr>
- <td>cxx_inheriting_constructors</td>
- </tr>
- <tr>
- <td>cxx_thread_local</td>
- </tr>
- <tr>
- <td>cxx_alias_templates</td>
- </tr>
- <tr>
- <td>cxx_delegating_constructors</td>
- </tr>
- <tr>
- <td>cxx_extended_friend_declarations</td>
- </tr>
- <tr>
- <td>cxx_final</td>
- </tr>
- <tr>
- <td>cxx_nonstatic_member_init</td>
- </tr>
- <tr>
- <td>cxx_override</td>
- </tr>
- <tr>
- <td>cxx_user_literals</td>
- </tr>
- <tr>
- <td>cxx_constexpr</td>
- </tr>
- <tr>
- <td>cxx_defaulted_move_initializers</td>
- </tr>
- <tr>
- <td>cxx_enum_forward_declarations</td>
- </tr>
- <tr>
- <td>cxx_noexcept</td>
- </tr>
- <tr>
- <td>cxx_nullptr</td>
- </tr>
- <tr>
- <td>cxx_range_for</td>
- </tr>
- <tr>
- <td>cxx_unrestricted_unions</td>
- </tr>
- <tr>
- <td>cxx_explicit_conversions</td>
- </tr>
- <tr>
- <td>cxx_lambdas</td>
- </tr>
- <tr>
- <td>cxx_local_type_template_args</td>
- </tr>
- <tr>
- <td>cxx_raw_string_literals</td>
- </tr>
- <tr>
- <td>cxx_auto_type</td>
- </tr>
- <tr>
- <td>cxx_defaulted_functions</td>
- </tr>
- <tr>
- <td>cxx_deleted_functions</td>
- </tr>
- <tr>
- <td>cxx_generalized_initializers</td>
- </tr>
- <tr>
- <td>cxx_inline_namespaces</td>
- </tr>
- <tr>
- <td>cxx_sizeof_member</td>
- </tr>
- <tr>
- <td>cxx_strong_enums</td>
- </tr>
- <tr>
- <td>cxx_trailing_return_types</td>
- </tr>
- <tr>
- <td>cxx_unicode_literals</td>
- </tr>
- <tr>
- <td>cxx_uniform_initialization</td>
- </tr>
- <tr>
- <td>cxx_variadic_templates</td>
- </tr>
- <tr>
- <td>cxx_decltype</td>
- </tr>
- <tr>
- <td>cxx_default_function_template_args</td>
- </tr>
- <tr>
- <td>cxx_long_long_type</td>
- </tr>
- <tr>
- <td>cxx_right_angle_brackets</td>
- </tr>
- <tr>
- <td>cxx_rvalue_references</td>
- </tr>
- <tr>
- <td>cxx_static_assert</td>
- </tr>
- <tr>
- <td>cxx_extern_templates</td>
- </tr>
- <tr>
- <td>cxx_func_identifier</td>
- </tr>
- <tr>
- <td>cxx_variadic_macros</td>
- </tr>
- <tr>
- <td>cxx_template_template_parameters</td>
- </tr>
- </tbody>
- </table>
- <p>v2.5.9 adds c++17 feature detection:</p>
- <table>
- <thead>
- <tr>
- <th>Feature name</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>cxx_aggregate_bases</td>
- </tr>
- <tr>
- <td>cxx_aligned_new</td>
- </tr>
- <tr>
- <td>cxx_capture_star_this</td>
- </tr>
- <tr>
- <td>cxx_constexpr</td>
- </tr>
- <tr>
- <td>cxx_deduction_guides</td>
- </tr>
- <tr>
- <td>cxx_enumerator_attributes</td>
- </tr>
- <tr>
- <td>cxx_fold_expressions</td>
- </tr>
- <tr>
- <td>cxx_guaranteed_copy_elision</td>
- </tr>
- <tr>
- <td>cxx_hex_float</td>
- </tr>
- <tr>
- <td>cxx_if_constexpr</td>
- </tr>
- <tr>
- <td>cxx_inheriting_constructors</td>
- </tr>
- <tr>
- <td>cxx_inline_variables</td>
- </tr>
- <tr>
- <td>cxx_namespace_attributes</td>
- </tr>
- <tr>
- <td>cxx_noexcept_function_type</td>
- </tr>
- <tr>
- <td>cxx_nontype_template_args</td>
- </tr>
- <tr>
- <td>cxx_nontype_template_parameter_auto</td>
- </tr>
- <tr>
- <td>cxx_range_based_for</td>
- </tr>
- <tr>
- <td>cxx_static_assert</td>
- </tr>
- <tr>
- <td>cxx_structured_bindings</td>
- </tr>
- <tr>
- <td>cxx_template_template_args</td>
- </tr>
- <tr>
- <td>cxx_variadic_using</td>
- </tr>
- </tbody>
- </table>
- <p>v2.5.9 adds c++20 feature detection:</p>
- <table>
- <thead>
- <tr>
- <th>Feature name</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>cxx_aggregate_paren_init</td>
- </tr>
- <tr>
- <td>cxx_char8_t</td>
- </tr>
- <tr>
- <td>cxx_concepts</td>
- </tr>
- <tr>
- <td>cxx_conditional_explicit</td>
- </tr>
- <tr>
- <td>cxx_consteval</td>
- </tr>
- <tr>
- <td>cxx_constexpr</td>
- </tr>
- <tr>
- <td>cxx_constexpr_dynamic_alloc</td>
- </tr>
- <tr>
- <td>cxx_constexpr_in_decltype</td>
- </tr>
- <tr>
- <td>cxx_constinit</td>
- </tr>
- <tr>
- <td>cxx_deduction_guides</td>
- </tr>
- <tr>
- <td>cxx_designated_initializers</td>
- </tr>
- <tr>
- <td>cxx_generic_lambdas</td>
- </tr>
- <tr>
- <td>cxx_impl_coroutine</td>
- </tr>
- <tr>
- <td>cxx_impl_destroying_delete</td>
- </tr>
- <tr>
- <td>cxx_impl_three_way_comparison</td>
- </tr>
- <tr>
- <td>cxx_init_captures</td>
- </tr>
- <tr>
- <td>cxx_modules</td>
- </tr>
- <tr>
- <td>cxx_nontype_template_args</td>
- </tr>
- <tr>
- <td>cxx_using_enum</td>
- </tr>
- </tbody>
- </table>
- <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>
- <pre><code class="lang-lua">configvar_check_features("HAS_CXX_STD_98", "cxx_std_98")
- configvar_check_features("HAS_CXX_STD_11", "cxx_std_11", {languages = "c++11"})
- configvar_check_features("HAS_CXX_STD_14", "cxx_std_14", {languages = "c++14"})
- configvar_check_features("HAS_CXX_STD_17", "cxx_std_17", {languages = "c++17"})
- configvar_check_features("HAS_CXX_STD_20", "cxx_std_20", {languages = "c++20"})
- configvar_check_features("HAS_C_STD_89", "c_std_89")
- configvar_check_features("HAS_C_STD_99", "c_std_99")
- configvar_check_features("HAS_C_STD_11", "c_std_11", {languages = "c11"})
- configvar_check_features("HAS_C_STD_17", "c_std_17", {languages = "c17"})
- </code></pre>
- <h4 id="detectbuiltinmacrodefinitions">Detect built-in macro definitions</h4>
- <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>
- <p>Related issues: <a href="https://github.com/xmake-io/xmake/issues/1715">#1715</a></p>
- <pre><code class="lang-lua">- Check whether the macro is defined
- configvar_check_macros("HAS_GCC", "__GNUC__")
- - The detection macro is not defined
- configvar_check_macros("NO_GCC", "__GNUC__", {defined = false})
- - Detect macro conditions
- configvar_check_macros("HAS_CXX20", "__cplusplus >= 202002L", {languages = "c++20"})
- </code></pre>
- <h4 id="detecttypesize">Detect type size</h4>
- <p>In previous versions, type detection was possible with <code>check_csnippets</code> and <code>output = true</code>.</p>
- <pre><code class="lang-lua">check_csnippets("INT_SIZE", 'printf("%d", sizeof(int)); return 0;', {output = true, number = true})
- </code></pre>
- <p>This way, however, extracts the type size information by trying to run the test code and then getting the run output.</p>
- <p>This doesn't work for cross-compilation.</p>
- <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>
- <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>
- <pre><code class="lang-lua">includes("@builtin/check")
- target("test")
- set_kind("static")
- add_files("*.cpp")
- check_sizeof("LONG_SIZE", "long")
- check_sizeof("STRING_SIZE", "std::string", {includes = "string"})
- </code></pre>
- <pre><code class="lang-bash">$ xmake f -c
- checking for LONG_SIZE ... 8
- checking for STRING_SIZE ... 24
- </code></pre>
- <p>Alternatively, I can check for it in the script field with <code>target:check_sizeof</code>.</p>
- <h4 id="detectingbigendian">Detecting big-endian</h4>
- <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>
- <pre><code class="lang-lua">includes("@builtin/check")
- target("test")
- set_kind("static")
- add_files("*.cpp")
- check_bigendian("IS_BIG_ENDIAN")
- </code></pre>
- <p>If the test passes and it is currently in big endian mode, then <code>IS_BIG_ENDIAN=1</code> is defined.</p>
- </article>
- </body>
- </html>
|