| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- <!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/#/zh-cn/manual/conditions">https://xmake.io/#/zh-cn/manual/conditions</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>
- <p>条件判断的api,一般用于必须要处理特定平台的编译逻辑的场合。。通常跟 lua 的 if 语句配合使用。</p>
- <h3 id="is_os">is_os</h3>
- <h4 id="">判断当前构建目标的操作系统</h4>
- <pre><code class="lang-lua">-- 如果当前操作系统是ios
- if is_os("ios") then
- add_files("src/xxx/*.m")
- end
- </code></pre>
- <p>目前支持的操作系统有:</p>
- <ul>
- <li>windows</li>
- <li>linux</li>
- <li>android</li>
- <li>macosx</li>
- <li>ios</li>
- </ul>
- <h3 id="is_arch">is_arch</h3>
- <h4 id="">判断当前编译架构</h4>
- <p>用于检测编译配置:<code>xmake f -a armv7</code></p>
- <pre><code class="lang-lua">-- 如果当前架构是x86_64或者i386
- if is_arch("x86_64", "i386") then
- add_files("src/xxx/*.c")
- end
- -- 如果当前平台是armv7, arm64, armv7s, armv7-a
- if is_arch("armv7", "arm64", "armv7s", "armv7-a") then
- -- ...
- end
- </code></pre>
- <p>如果像上面那样一个个去判断所有arm架构,也许会很繁琐,毕竟每个平台的架构类型很多,xmake提供了比<a href="#targetadd_files">add_files</a>更强的lua正则表达式匹配模式,来更加简洁的进行判断:</p>
- <pre><code class="lang-lua">--如果当前平台是arm平台
- if is_arch("arm.*") then
- -- ...
- end
- </code></pre>
- <p>用<code>.*</code>就可以匹配所有了。</p>
- <h3 id="is_plat">is_plat</h3>
- <h4 id="">判断当前编译平台</h4>
- <p>用于检测编译配置:<code>xmake f -p iphoneos</code></p>
- <pre><code class="lang-lua">-- 如果当前平台是android
- if is_plat("android") then
- add_files("src/xxx/*.c")
- end
- -- 如果当前平台是macosx或者iphoneos
- if is_plat("macosx", "iphoneos") then
- add_frameworks("Foundation")
- end
- </code></pre>
- <p>目前支持的平台有:</p>
- <ul>
- <li>windows</li>
- <li>cross</li>
- <li>linux</li>
- <li>macosx</li>
- <li>android</li>
- <li>iphoneos</li>
- <li>watchos</li>
- </ul>
- <p>当然你也可以自己扩展添加自己的平台,甚至直接指定自己的平台名:</p>
- <pre><code class="lang-bash">$ xmake f -p other --sdk=...
- </code></pre>
- <p>如果指定的平台名不存在,就会自动切到<code>cross</code>平台进行交叉编译,但是却可以通过<code>is_plat("other")</code>来判断自己的平台逻辑。</p>
- <h3 id="is_host">is_host</h3>
- <h4 id="">判断当前主机环境的操作系统</h4>
- <p>有些编译平台是可以在多个不同的操作系统进行构建的,例如:android的ndk就支持linux,macOS还有windows环境。</p>
- <p>这个时候就可以通过这个接口,区分当前是在哪个系统环境下进行的构建。</p>
- <pre><code class="lang-lua">-- 如果当前主机环境是windows
- if is_host("windows") then
- add_includedirs("C:\\includes")
- else
- add_includedirs("/usr/includess")
- end
- </code></pre>
- <p>目前支持的主机环境有:</p>
- <ul>
- <li>windows</li>
- <li>linux</li>
- <li>macosx</li>
- </ul>
- <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>
- <h3 id="is_subhost">is_subhost</h3>
- <h4 id="">判断当前主机的子系统环境</h4>
- <p>目前主要用于 windows 系统上 cygwin, msys2 等子系统环境的探测,如果在 msys2 shell 环境下运行 xmake,那么 <code>is_subhost("windows")</code> 想将会返回 false,而 <code>is_host("windows")</code> 依旧会返回 true。</p>
- <p>目前支持的子系统:</p>
- <ul>
- <li>msys</li>
- <li>cygwin</li>
- </ul>
- <p>配置例子:</p>
- <pre><code class="lang-lua">if is_subhost("msys", "cygwin") then
- -- 当前在 msys2/cygwin 的 shell 环境下
- end
- </code></pre>
- <p>我们也可以通过执行 <code>xmake l os.subhost</code> 来快速查看当前的子系统平台。</p>
- <p>!> 后期也有可能会支持 linux 和 macos 系统下的其他子系统环境,如果存在话。</p>
- <h3 id="is_subarch">is_subarch</h3>
- <h4 id="">判断当前主机子系统环境下的架构</h4>
- <p>目前主要用于 windows 系统上 cygwin, msys2 等子系统环境下架构的探测,通常在 windows 编译平台采用 msvc 工具链,那边编译架构时 x64,x86。<br>而在 msys/cygwin 子系统环境下,编译架构默认为 x86_64/i386,是有差异的。</p>
- <p>我们也可以通过执行 <code>xmake l os.subarch</code> 来快速查看当前的子系统架构。</p>
- <h3 id="is_cross">is_cross</h3>
- <h4 id="">判断当前平台是否为交叉编译</h4>
- <p>如果当前的目标架构和平台,不是当前的主机平台,属于交叉编译,这个接口就会返回 true。</p>
- <h3 id="is_mode">is_mode</h3>
- <h4 id="">判断当前编译模式</h4>
- <p>用于检测编译配置:<code>xmake f -m debug</code></p>
- <p>编译模式的类型并不是内置的,可以自由指定,一般指定:<code>debug</code>, <code>release</code>, <code>profile</code> 这些就够用了,当然你也可以在xmake.lua使用其他模式名来判断。</p>
- <pre><code class="lang-lua">-- 如果当前编译模式是debug
- if is_mode("debug") then
- -- 添加DEBUG编译宏
- add_defines("DEBUG")
- -- 启用调试符号
- set_symbols("debug")
- -- 禁用优化
- set_optimize("none")
- end
- -- 如果是release或者profile模式
- if is_mode("release", "profile") then
- -- 如果是release模式
- if is_mode("release") then
- -- 隐藏符号
- set_symbols("hidden")
- -- strip所有符号
- set_strip("all")
- -- 忽略帧指针
- add_cxflags("-fomit-frame-pointer")
- add_mxflags("-fomit-frame-pointer")
- -- 如果是profile模式
- else
- -- 启用调试符号
- set_symbols("debug")
- end
- -- 添加扩展指令集
- add_vectorexts("sse2", "sse3", "ssse3", "mmx")
- end
- </code></pre>
- <h3 id="is_kind">is_kind</h3>
- <h4 id="">判断当前编译类型</h4>
- <p>判断当前是否编译的是动态库还是静态库,用于检测编译配置:<code>xmake f -k [static|shared]</code></p>
- <p>一般用于如下场景:</p>
- <pre><code class="lang-lua">target("test")
- -- 通过配置设置目标的kind
- set_kind("$(kind)")
- add_files("src/*c")
- -- 如果当前编译的是静态库,那么添加指定文件
- if is_kind("static") then
- add_files("src/xxx.c")
- end
- </code></pre>
- <p>编译配置的时候,可手动切换,编译类型:</p>
- <pre><code class="lang-bash"># 编译静态库
- $ xmake f -k static
- $ xmake
- </code></pre>
- <pre><code class="lang-bash"># 编译动态库
- $ xmake f -k shared
- $ xmake
- </code></pre>
- <h3 id="is_config">is_config</h3>
- <h4 id="">判断指定配置是否为给定的值</h4>
- <p>此接口从2.2.2版本开始引入,用于判断指定配置是否为给定的值,可用于描述域。</p>
- <p>例如:</p>
- <pre><code class="lang-console">$ xmake f --test=hello1
- </code></pre>
- <pre><code class="lang-lua">-- 自定义一个配置选项到命令行菜单
- option("test")
- set_showmenu(true)
- set_description("The test config option")
- option_end()
- -- 如果自定义的test配置值是hello1或者hello2
- if is_config("test", "hello1", "hello2") then
- add_defines("HELLO")
- end
- </code></pre>
- <p>可以用来根据配置值增加对应的依赖包,例如:</p>
- <pre><code class="lang-lua">-- 根据lua_flavor的配置值,选择依赖lua还是luajit
- option("lua_flavor")
- set_showmenu(true)
- set_values("luajit", "lua")
- option_end()
- if is_config("lua_flavor", "luajit") then
- add_requires("luajit")
- elseif is_config("lua_flavor", "lua") then
- add_requires("lua")
- end
- </code></pre>
- <p>不仅如此,我们还可以设置模式匹配规则去判断值,例如:</p>
- <pre><code class="lang-lua">-- 如果自定义的test配置值带有hello前缀
- if is_config("test", "hello.*") then
- add_defines("HELLO")
- end
- </code></pre>
- <p><p class="tip"><br>此接口不仅能够判断通过<a href="#option">option</a>定义的自定义配置选项,同时还能判断内置的全局配置、本地配置。<br></p>
- </p>
- <h3 id="has_config">has_config</h3>
- <h4 id="">判断配置是否启用或者存在</h4>
- <p>此接口从2.2.2版本开始引入,用于检测自定义或者内置的编译配置是否存在或启用,可用于描述域。</p>
- <p>例如以下配置情况,都会返回true:</p>
- <pre><code class="lang-console"># 启用某个配置选项(如果是boolean类型配置)
- $ xmake f --test1=y
- $ xmake f --test1=yes
- $ xmake f --test1=true
- # 设置某个配置选项的值
- $ xmake f --test2=value
- </code></pre>
- <pre><code class="lang-lua">-- 如果test1或者test2被设置或者启用
- if has_config("test1", "test2") then
- add_defines("TEST")
- end
- </code></pre>
- <p>而下面的情况则会禁用配置,返回false:</p>
- <pre><code class="lang-console"># 禁用配置(如果是boolean类型配置)
- $ xmake f --test1=n
- $ xmake f --test1=no
- $ xmake f --test1=false
- </code></pre>
- <p><p class="tip"><br>此接口不仅能够判断内置的全局配置、本地配置,同时还可以判断通过<a href="#option">option</a>定义的自定义配置选项。<br></p>
- </p>
- <h3 id="has_package">has_package</h3>
- <h4 id="">判断依赖包是否启用或者存在</h4>
- <p>此接口从2.2.3版本开始引入,用于检测远程依赖包是否存在或启用,可用于描述域。</p>
- <p>一般配合<a href="/mirror/zh-cn/manual/global_interfaces.html#add_requires">add_requires</a>一起使用,例如:</p>
- <pre><code class="lang-lua">add_requires("tbox", {optional = true})
- target("test")
- set_kind("binary")
- add_files("src/*.c")
- add_packages("tbox")
- if has_package("tbox") then
- add_defines("HAVE_TBOX")
- end
- </code></pre>
- <p>如果通过<code>add_requires</code>添加的可选依赖包,远程下载安装失败,或者当前平台不支持导致实际上没有被正常安装上,那么<code>has_package</code>就会返回false,<br>表示不存在,然后对其他flags定义甚至源文件编译控制做一些特殊处理。</p>
- <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>
- </p>
- </article>
- </body>
- </html>
|