2
0

unity_build.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/#/zh-cn/features/unity_build">https://xmake.io/#/zh-cn/features/unity_build</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>我们知道,C++ 代码编译速度通常很慢,因为每个代码文件都需要解析引入的头文件。</p>
  86. <p>而通过 Unity Build,我们通过将多个 cpp 文件组合成一个来加速项目的编译,其主要好处是减少了解析和编译包含在多个源文件中的头文件内容的重复工作,头文件的内容通常占预处理后源文件中的大部分代码。</p>
  87. <p>Unity 构建还通过减少编译链创建和处理的目标文件的数量来减轻由于拥有大量小源文件而导致的开销,并允许跨形成统一构建任务的文件进行过程间分析和优化(类似于效果链接时优化)。</p>
  88. <p>它可以极大提升 C/C++ 代码的编译速度,通常会有 30% 的速度提升,不过根据项目的复杂程度不同,其带来的效益还是要根据自身项目情况而定。</p>
  89. <p>xmake 在 v2.5.9 版本中,也已经支持了这种构建模式。相关 issues 见 <a href="https://github.com/xmake-io/xmake/issues/1019">#1019</a>。</p>
  90. <h3 id="">如何启用?</h3>
  91. <p>我们提供了两个内置规则,分别处理对 C 和 C++ 代码的 Unity Build。</p>
  92. <pre><code class="lang-lua">add_rules("c.unity_build")
  93. add_rules("c++.unity_build")
  94. </code></pre>
  95. <h3 id="batch">Batch 模式</h3>
  96. <p>默认情况下,只要设置上述规则,就会启用 Batch 模式的 Unity Build,也就是 xmake 自动根据项目代码文件,自动组织合并。</p>
  97. <pre><code class="lang-lua">target("test")
  98. set_kind("binary")
  99. add_includedirs("src")
  100. add_rules("c++.unity_build", {batchsize = 2})
  101. add_files("src/*.c", "src/*.cpp")
  102. </code></pre>
  103. <p>我们可以额外通过设置 <code>{batchsize = 2}</code> 参数到规则,来指定每个合并 Batch 的大小数量,这里也就是每两个 C++ 文件自动合并编译。</p>
  104. <p>编译效果大概如下:</p>
  105. <pre><code class="lang-console">$ xmake -r
  106. [ 11%]: cache compiling.release build/.gens/test/unity_build/unity_642A245F.cpp
  107. [ 11%]: cache compiling.release build/.gens/test/unity_build/unity_bar.cpp
  108. [ 11%]: cache compiling.release build/.gens/test/unity_build/unity_73161A20.cpp
  109. [ 11%]: cache compiling.release build/.gens/test/unity_build/unity_F905F036.cpp
  110. [ 11%]: cache compiling.release build/.gens/test/unity_build/unity_foo.cpp
  111. [ 11%]: cache compiling.release build/.gens/test/unity_build/main.c
  112. [ 77%]: linking.release test
  113. [100%]: build ok
  114. </code></pre>
  115. <p>由于我们仅仅启用了 C++ 的 Unity Build,所以 C 代码还是正常挨个编译。另外在 Unity Build 模式下,我们还是可以做到尽可能的并行编译加速,互不冲突。</p>
  116. <p>如果没有设置 <code>batchsize</code> 参数,那么默认会把所有文件合并到一个文件中进行编译。</p>
  117. <h3 id="group">Group 模式</h3>
  118. <p>如果上面的 Batch 模式自动合并效果不理想,我们也可以使用自定义分组,来手动配置哪些文件合并到一起参与编译,这使得用户更加地灵活可控。</p>
  119. <pre><code class="lang-lua">target("test")
  120. set_kind("binary")
  121. add_rules("c++.unity_build", {batchsize = 0}) -- disable batch mode
  122. add_files("src/*.c", "src/*.cpp")
  123. add_files("src/foo/*.c", {unity_group = "foo"})
  124. add_files("src/bar/*.c", {unity_group = "bar"})
  125. </code></pre>
  126. <p>我们使用 <code>{unity_group = "foo"}</code> 来指定每个分组的名字,以及包含了哪些文件,每个分组的文件都会单独被合并到一个代码文件中去。</p>
  127. <p>另外,<code>batchsize = 0</code> 也强行禁用了 Batch 模式,也就是说,没有设置 unity_group 分组的代码文件,我们还是会单独编译它们,也不会自动开启自动合并。</p>
  128. <h3 id="batchgroup">Batch 和 Group 混合模式</h3>
  129. <p>我们只要把上面的 <code>batchsize = 0</code> 改成非 0 值,就可以让分组模式下,剩余的代码文件继续开启 Batch 模式自动合并编译。</p>
  130. <pre><code class="lang-lua">target("test")
  131. set_kind("binary")
  132. add_includedirs("src")
  133. add_rules("c++.unity_build", {batchsize = 2})
  134. add_files("src/*.c", "src/*.cpp")
  135. add_files("src/foo/*.c", {unity_group = "foo"})
  136. add_files("src/bar/*.c", {unity_group = "bar"})
  137. </code></pre>
  138. <h3 id="">忽略指定文件</h3>
  139. <p>如果是 Batch 模式下,由于是自动合并操作,所以默认会对所有文件执行合并,但如果有些代码文件我们不想让它参与合并,那么我们也可以通过 <code>{unity_ignored = true}</code> 去忽略它们。</p>
  140. <pre><code class="lang-lua">target("test")
  141. set_kind("binary")
  142. add_includedirs("src")
  143. add_rules("c++.unity_build", {batchsize = 2})
  144. add_files("src/*.c", "src/*.cpp")
  145. add_files("src/test/*.c", {unity_ignored = true}) -- ignore these files
  146. </code></pre>
  147. <h3 id="uniqueid">Unique ID</h3>
  148. <p>尽管 Unity Build 带啦的收益不错,但是我们还是会遇到一些意外的情况,比如我们的两个代码文件里面,全局命名空间下,都存在相同名字的全局变量和函数。</p>
  149. <p>那么,合并编译就会带来编译冲突问题,编译器通常会报全局变量重定义错误。</p>
  150. <p>为了解决这个问题,我们需要用户代码上做一些修改,然后配合构建工具来解决。</p>
  151. <p>比如,我们的 foo.cpp 和 bar.cpp 都有全局变量 i。</p>
  152. <p>foo.cpp</p>
  153. <pre><code class="lang-c">namespace {
  154. int i = 42;
  155. }
  156. int foo()
  157. {
  158. return i;
  159. }
  160. </code></pre>
  161. <p>bar.cpp</p>
  162. <pre><code class="lang-c">namespace {
  163. int i = 42;
  164. }
  165. int bar()
  166. {
  167. return i;
  168. }
  169. </code></pre>
  170. <p>那么,我们合并编译就会冲突,我们可以引入一个 Unique ID 来隔离全局的匿名空间。</p>
  171. <p>foo.cpp</p>
  172. <pre><code class="lang-c">namespace MY_UNITY_ID {
  173. int i = 42;
  174. }
  175. int foo()
  176. {
  177. return MY_UNITY_ID::i;
  178. }
  179. </code></pre>
  180. <p>bar.cpp</p>
  181. <pre><code class="lang-c">namespace MY_UNITY_ID {
  182. int i = 42;
  183. }
  184. int bar()
  185. {
  186. return MY_UNITY_ID::i;
  187. }
  188. </code></pre>
  189. <p>接下来,我们还需要保证代码合并后, <code>MY_UNITY_ID</code> 在 foo 和 bar 中的定义完全不同,可以按文件名算一个唯一 ID 值出来,互不冲突,也就是实现下面的合并效果:</p>
  190. <pre><code class="lang-c">#define MY_UNITY_ID <hash(foo.cpp)>
  191. #include "foo.c"
  192. #undef MY_UNITY_ID
  193. #define MY_UNITY_ID <hash(bar.cpp)>
  194. #include "bar.c"
  195. #undef MY_UNITY_ID
  196. </code></pre>
  197. <p>这看上去似乎很麻烦,但是用户不需要关心这些,xmake 会在合并时候自动处理它们,用户只需要指定这个 Unique ID 的名字就行了,例如下面这样:</p>
  198. <pre><code class="lang-lua">target("test")
  199. set_kind("binary")
  200. add_includedirs("src")
  201. add_rules("c++.unity_build", {batchsize = 2, uniqueid = "MY_UNITY_ID"})
  202. add_files("src/*.c", "src/*.cpp")
  203. </code></pre>
  204. <p>处理全局变量,还有全局的重名宏定义,函数什么的,都可以采用这种方式来避免冲突。</p>
  205. </article>
  206. </body>
  207. </html>