build_cache.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/build_cache">https://xmake.io/#/zh-cn/features/build_cache</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="">本地编译缓存</h3>
  86. <p>默认,Xmake 就会开启本地缓存,2.6.5 之前的版本默认使用外置的 ccache,而 2.6.6 之后版本,Xmake 提供了内置的跨平台本地缓存方案。</p>
  87. <p>相比 ccache 等第三方独立进程,xmake 内部状态维护,更加便于优化,也避免了频繁的独立进程加载耗时,也避免了与守护进程额外的通信。</p>
  88. <p>另外,内置的缓存能够更好的支持跨平台,Windows 上 msvc 也能够很好的支持,而 ccache 仅仅支持 gcc/clang。</p>
  89. <p>当然,我们也可以通过下面的命令禁用缓存。</p>
  90. <pre><code class="lang-bash">$ xmake f --ccache=n
  91. </code></pre>
  92. <p>注:不管是否使用内置本地缓存,配置名都是 <code>--ccache=</code>,意思是 c/c++ 构建缓存,而不仅仅是指 ccache 工具的名字。</p>
  93. <p>我们如果想继续使用外置的其他缓存工具,我们也是可以通过下面的方式来配置。</p>
  94. <pre><code class="lang-bash">$ xmake f --ccache=n --cxx="ccache gcc" --cc="ccache gcc"
  95. $ xmake
  96. </code></pre>
  97. <h3 id="">远程编译缓存</h3>
  98. <p>除了本地缓存,我们也提供了远程缓存服务,类似 mozilla 的 sscache,如果只是个人开发,平常不会用到它。</p>
  99. <p>但是,如果是公司内部多人协同开发一个大型项目,仅仅靠分布式编译和本地缓存,是不够的。我们还需要对编译的对象文件缓存到独立的服务器上进行共享。</p>
  100. <p>这样,其他人即使首次编译,也不需要每次都去分布式编译它,直接从远程拉取缓存来加速编译。</p>
  101. <p>另外,Xmake 提供的远程缓存服务,也是全平台支持的,不仅支持 gcc/clang 还支持 msvc。</p>
  102. <h4 id="">开启服务</h4>
  103. <p>我们可以指定 <code>--ccache</code> 参数来开启远程编译缓存服务,当然如果不指定这个参数,xmake 会默认开启所有服务端配置的服务。</p>
  104. <pre><code class="lang-console">$ xmake service --ccache
  105. <remote_cache_server>: listening 0.0.0.0:9092 ..
  106. </code></pre>
  107. <p>我们也可以开启服务的同时,回显详细日志信息。</p>
  108. <pre><code class="lang-console">$ xmake service --ccache -vD
  109. <remote_cache_server>: listening 0.0.0.0:9092 ..
  110. </code></pre>
  111. <h4 id="daemon">以 Daemon 模式开启服务</h4>
  112. <pre><code class="lang-console">$ xmake service --ccache --start
  113. $ xmake service --ccache --restart
  114. $ xmake service --ccache --stop
  115. </code></pre>
  116. <h4 id="">配置服务端</h4>
  117. <p>我们首先,运行 <code>xmake service</code> 命令,它会自动生成一个默认的 <code>server.conf</code> 配置文件,存储到 <code>~/.xmake/service/server.conf</code>。</p>
  118. <pre><code class="lang-bash">$ xmake service
  119. generating the config file to /Users/ruki/.xmake/service/server.conf ..
  120. an token(590234653af52e91b9e438ed860f1a2b) is generated, we can use this token to connect service.
  121. generating the config file to /Users/ruki/.xmake/service/client.conf ..
  122. <remote_cache_server>: listening 0.0.0.0:9692 ..
  123. </code></pre>
  124. <p>然后,我们编辑它,修复服务器的监听端口(可选)。</p>
  125. <pre><code class="lang-bash">$ cat ~/.xmake/service/server.conf
  126. {
  127. distcc_build = {
  128. listen = "0.0.0.0:9692",
  129. workdir = "/Users/ruki/.xmake/service/server/remote_cache"
  130. },
  131. known_hosts = { },
  132. logfile = "/Users/ruki/.xmake/service/server/logs.txt",
  133. tokens = {
  134. "590234653af52e91b9e438ed860f1a2b"
  135. }
  136. }
  137. </code></pre>
  138. <h4 id="">配置客户端</h4>
  139. <p>客户端配置文件在 <code>~/.xmake/service/client.conf</code>,我们可以在里面配置客户端需要连接的服务器地址。</p>
  140. <p>我们可以在 hosts 列表里面配置多个服务器地址,以及对应的 token。</p>
  141. <pre><code class="lang-console">$cat ~/.xmake/service/client.conf
  142. {
  143. remote_cache = {
  144. connect = "127.0.0.1:9692,
  145. token = "590234653af52e91b9e438ed860f1a2b"
  146. }
  147. }
  148. }
  149. </code></pre>
  150. <h5 id="">配置超时</h5>
  151. <p>默认情况下,客户端连接,收发数据都是无限等待不超时的,但是如果访问服务端的网络不稳定,那么有可能会导致访问卡死,这个时候可以配置超时来解决。</p>
  152. <p>如果发生超时异常,就会自动退化到本地缓存,不会永远卡死。</p>
  153. <p>我们可以配置,<code>send_timeout</code>, <code>recv_timeout</code> 和 <code>connect_timeout</code> 三种超时,如果在根节点设置,那么所有客户端服务都会生效。</p>
  154. <pre><code class="lang-console">$ cat ~/.xmake/service/client.conf
  155. {
  156. send_timeout = 5000,
  157. recv_timeout = 5000,
  158. connect_timeout = 5000
  159. }
  160. </code></pre>
  161. <p>我们也可以仅仅针对当前远程缓存服务配置超时,其他服务还是默认超时。</p>
  162. <pre><code class="lang-console">$ cat ~/.xmake/service/client.conf
  163. {
  164. distcc_build = {
  165. send_timeout = 5000,
  166. recv_timeout = 5000,
  167. connect_timeout = 5000,
  168. }
  169. }
  170. </code></pre>
  171. <p>!> 服务端配置同样支持超时配置。</p>
  172. <h4 id="">用户认证和授权</h4>
  173. <p>关于用户认证和授权,可以参考 <a href="/#/zh-cn/guide/other_features?id=%e7%94%a8%e6%88%b7%e8%ae%a4%e8%af%81%e5%92%8c%e6%8e%88%e6%9d%83">远程编译/用户认证和授权</a> 里面的详细说明,用法是完全一致的。</p>
  174. <h4 id="">连接服务器</h4>
  175. <p>配置完认证和服务器地址后,就可以输入下面的命令,将当前工程连接到配置的服务器上了。</p>
  176. <p>我们需要在连接时候,输入 <code>--ccache</code>,指定仅仅连接远程编译缓存服务。</p>
  177. <pre><code class="lang-bash">$ cd projectdir
  178. $ xmake service --connect --ccache
  179. <client>: connect 127.0.0.1:9692 ..
  180. <client>: 127.0.0.1:9692 connected!
  181. </code></pre>
  182. <p>我们也可以同时连接多个服务,比如分布式编译和远程编译缓存服务。</p>
  183. <pre><code class="lang-hash">$ xmake service --connect --distcc --ccache
  184. </code></pre>
  185. <p>!> 如果不带任何参数,默认连接的是远程编译服务。</p>
  186. <h4 id="">断开连接</h4>
  187. <pre><code class="lang-bash">$ xmake service --disconnect --ccache
  188. </code></pre>
  189. <h4 id="">清理服务器缓存</h4>
  190. <p>我们也可以通过下面的命令,清理当前工程对应的远程服务器上的缓存。</p>
  191. <pre><code class="lang-bash">$ xmake service --clean --ccache
  192. </code></pre>
  193. <p>而如果我们执行 <code>xmake clean --all</code>,在连接了远程服务的状态下,也会去自动清理所有的缓存。</p>
  194. <h4 id="">一些内部优化</h4>
  195. <ol>
  196. <li>拉取远程缓存的快照,通过 bloom filter + lz4 回传本地后,用于快速判断缓存是否存在,避免频繁的查询服务端缓存信息</li>
  197. <li>配合本地缓存,可以避免频繁地请求远程服务器,拉取缓存。</li>
  198. <li>内部状态维护,相比 sscache 等独立工具,避免了频繁的独立进程加载耗时,也避免了与守护进程额外的通信</li>
  199. </ol>
  200. </article>
  201. </body>
  202. </html>