| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <!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/features/unity_build">https://xmake.io/#/zh-cn/features/unity_build</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>我们知道,C++ 代码编译速度通常很慢,因为每个代码文件都需要解析引入的头文件。</p>
- <p>而通过 Unity Build,我们通过将多个 cpp 文件组合成一个来加速项目的编译,其主要好处是减少了解析和编译包含在多个源文件中的头文件内容的重复工作,头文件的内容通常占预处理后源文件中的大部分代码。</p>
- <p>Unity 构建还通过减少编译链创建和处理的目标文件的数量来减轻由于拥有大量小源文件而导致的开销,并允许跨形成统一构建任务的文件进行过程间分析和优化(类似于效果链接时优化)。</p>
- <p>它可以极大提升 C/C++ 代码的编译速度,通常会有 30% 的速度提升,不过根据项目的复杂程度不同,其带来的效益还是要根据自身项目情况而定。</p>
- <p>xmake 在 v2.5.9 版本中,也已经支持了这种构建模式。相关 issues 见 <a href="https://github.com/xmake-io/xmake/issues/1019">#1019</a>。</p>
- <h3 id="">如何启用?</h3>
- <p>我们提供了两个内置规则,分别处理对 C 和 C++ 代码的 Unity Build。</p>
- <pre><code class="lang-lua">add_rules("c.unity_build")
- add_rules("c++.unity_build")
- </code></pre>
- <h3 id="batch">Batch 模式</h3>
- <p>默认情况下,只要设置上述规则,就会启用 Batch 模式的 Unity Build,也就是 xmake 自动根据项目代码文件,自动组织合并。</p>
- <pre><code class="lang-lua">target("test")
- set_kind("binary")
- add_includedirs("src")
- add_rules("c++.unity_build", {batchsize = 2})
- add_files("src/*.c", "src/*.cpp")
- </code></pre>
- <p>我们可以额外通过设置 <code>{batchsize = 2}</code> 参数到规则,来指定每个合并 Batch 的大小数量,这里也就是每两个 C++ 文件自动合并编译。</p>
- <p>编译效果大概如下:</p>
- <pre><code class="lang-console">$ xmake -r
- [ 11%]: cache compiling.release build/.gens/test/unity_build/unity_642A245F.cpp
- [ 11%]: cache compiling.release build/.gens/test/unity_build/unity_bar.cpp
- [ 11%]: cache compiling.release build/.gens/test/unity_build/unity_73161A20.cpp
- [ 11%]: cache compiling.release build/.gens/test/unity_build/unity_F905F036.cpp
- [ 11%]: cache compiling.release build/.gens/test/unity_build/unity_foo.cpp
- [ 11%]: cache compiling.release build/.gens/test/unity_build/main.c
- [ 77%]: linking.release test
- [100%]: build ok
- </code></pre>
- <p>由于我们仅仅启用了 C++ 的 Unity Build,所以 C 代码还是正常挨个编译。另外在 Unity Build 模式下,我们还是可以做到尽可能的并行编译加速,互不冲突。</p>
- <p>如果没有设置 <code>batchsize</code> 参数,那么默认会把所有文件合并到一个文件中进行编译。</p>
- <h3 id="group">Group 模式</h3>
- <p>如果上面的 Batch 模式自动合并效果不理想,我们也可以使用自定义分组,来手动配置哪些文件合并到一起参与编译,这使得用户更加地灵活可控。</p>
- <pre><code class="lang-lua">target("test")
- set_kind("binary")
- add_rules("c++.unity_build", {batchsize = 0}) -- disable batch mode
- add_files("src/*.c", "src/*.cpp")
- add_files("src/foo/*.c", {unity_group = "foo"})
- add_files("src/bar/*.c", {unity_group = "bar"})
- </code></pre>
- <p>我们使用 <code>{unity_group = "foo"}</code> 来指定每个分组的名字,以及包含了哪些文件,每个分组的文件都会单独被合并到一个代码文件中去。</p>
- <p>另外,<code>batchsize = 0</code> 也强行禁用了 Batch 模式,也就是说,没有设置 unity_group 分组的代码文件,我们还是会单独编译它们,也不会自动开启自动合并。</p>
- <h3 id="batchgroup">Batch 和 Group 混合模式</h3>
- <p>我们只要把上面的 <code>batchsize = 0</code> 改成非 0 值,就可以让分组模式下,剩余的代码文件继续开启 Batch 模式自动合并编译。</p>
- <pre><code class="lang-lua">target("test")
- set_kind("binary")
- add_includedirs("src")
- add_rules("c++.unity_build", {batchsize = 2})
- add_files("src/*.c", "src/*.cpp")
- add_files("src/foo/*.c", {unity_group = "foo"})
- add_files("src/bar/*.c", {unity_group = "bar"})
- </code></pre>
- <h3 id="">忽略指定文件</h3>
- <p>如果是 Batch 模式下,由于是自动合并操作,所以默认会对所有文件执行合并,但如果有些代码文件我们不想让它参与合并,那么我们也可以通过 <code>{unity_ignored = true}</code> 去忽略它们。</p>
- <pre><code class="lang-lua">target("test")
- set_kind("binary")
- add_includedirs("src")
- add_rules("c++.unity_build", {batchsize = 2})
- add_files("src/*.c", "src/*.cpp")
- add_files("src/test/*.c", {unity_ignored = true}) -- ignore these files
- </code></pre>
- <h3 id="uniqueid">Unique ID</h3>
- <p>尽管 Unity Build 带啦的收益不错,但是我们还是会遇到一些意外的情况,比如我们的两个代码文件里面,全局命名空间下,都存在相同名字的全局变量和函数。</p>
- <p>那么,合并编译就会带来编译冲突问题,编译器通常会报全局变量重定义错误。</p>
- <p>为了解决这个问题,我们需要用户代码上做一些修改,然后配合构建工具来解决。</p>
- <p>比如,我们的 foo.cpp 和 bar.cpp 都有全局变量 i。</p>
- <p>foo.cpp</p>
- <pre><code class="lang-c">namespace {
- int i = 42;
- }
- int foo()
- {
- return i;
- }
- </code></pre>
- <p>bar.cpp</p>
- <pre><code class="lang-c">namespace {
- int i = 42;
- }
- int bar()
- {
- return i;
- }
- </code></pre>
- <p>那么,我们合并编译就会冲突,我们可以引入一个 Unique ID 来隔离全局的匿名空间。</p>
- <p>foo.cpp</p>
- <pre><code class="lang-c">namespace MY_UNITY_ID {
- int i = 42;
- }
- int foo()
- {
- return MY_UNITY_ID::i;
- }
- </code></pre>
- <p>bar.cpp</p>
- <pre><code class="lang-c">namespace MY_UNITY_ID {
- int i = 42;
- }
- int bar()
- {
- return MY_UNITY_ID::i;
- }
- </code></pre>
- <p>接下来,我们还需要保证代码合并后, <code>MY_UNITY_ID</code> 在 foo 和 bar 中的定义完全不同,可以按文件名算一个唯一 ID 值出来,互不冲突,也就是实现下面的合并效果:</p>
- <pre><code class="lang-c">#define MY_UNITY_ID <hash(foo.cpp)>
- #include "foo.c"
- #undef MY_UNITY_ID
- #define MY_UNITY_ID <hash(bar.cpp)>
- #include "bar.c"
- #undef MY_UNITY_ID
- </code></pre>
- <p>这看上去似乎很麻烦,但是用户不需要关心这些,xmake 会在合并时候自动处理它们,用户只需要指定这个 Unique ID 的名字就行了,例如下面这样:</p>
- <pre><code class="lang-lua">target("test")
- set_kind("binary")
- add_includedirs("src")
- add_rules("c++.unity_build", {batchsize = 2, uniqueid = "MY_UNITY_ID"})
- add_files("src/*.c", "src/*.cpp")
- </code></pre>
- <p>处理全局变量,还有全局的重名宏定义,函数什么的,都可以采用这种方式来避免冲突。</p>
- </article>
- </body>
- </html>
|