build_cache.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/#/features/build_cache">https://xmake.io/#/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. <h1 id="buildcaching">Build caching</h1>
  86. <p>Xmake supports various methods of caching in the build process, which can speed up your builds:</p>
  87. <p><img src="/assets/img/speed_skating.svg" alt=""></p>
  88. <p>Your build might be as fast as the above speed skater!</p>
  89. <h2 id="localcompilationcache">Local compilation cache</h2>
  90. <p>By default, Xmake will enable the local cache. The version before 2.6.5 uses the external ccache by default, and after 2.6.6, Xmake provides a built-in cross-platform local cache solution.</p>
  91. <p>Compared with third-party independent processes such as ccache, Xmake&#39;s internal state maintenance is easier to optimize, and it also avoids frequent independent process loading and time-consuming, and avoids additional communication with the daemon process. In addition, the built-in cache works better cross-platform, and it also supports MSVC on Windows well, while ccache only supports the GCC and Clang toolchains.</p>
  92. <p>If it&#39;s causing you problems, we can also disable the cache with the following command.</p>
  93. <pre><code class="lang-bash">$ xmake f --ccache=n
  94. </code></pre>
  95. <p>Note: Regardless of whether the built-in local cache is used, the configuration name is <code>--ccache=</code>, which means the C/C++ build cache, not just the name of the ccache tool.</p>
  96. <p>If we want to continue to use other external caching tools, we can also configure it in the following way.</p>
  97. <pre><code class="lang-bash">$ xmake f --ccache=n --cxx="ccache gcc" --cc="ccache gcc"
  98. $ xmake
  99. </code></pre>
  100. <h2 id="remotecompilationcache">Remote compilation cache</h2>
  101. <p>In addition to local caching, we also provide remote caching services, similar to Mozilla&#39;s sscache, which is usually not used if it is only for personal development. However, if a large-scale project is developed collaboratively by multiple people within an organization, distributed compilation and local caching alone are not enough. We also need to cache the compiled object files to a separate server for sharing.</p>
  102. <p>This way, even if other people compile it for the first time, they do not need to compile it every time, and instead can directly pull the cache from the remote to speed up the compilation. In addition, the remote cache service provided by Xmake is also supported by all platforms, not only GCC and Clang, but also MSVC.</p>
  103. <h3 id="starttheservice">Start the service</h3>
  104. <p>We can specify the <code>--ccache</code> parameter to enable the remote compilation cache service. Of course, if this parameter is not specified, Xmake will enable all server-configured services by default.</p>
  105. <pre><code class="lang-bash">$ xmake service --ccache
  106. <remote_cache_server>: listening 0.0.0.0:9092 ..
  107. </code></pre>
  108. <p>We can also start the service and echo detailed log information with the <code>-vD</code> flag.</p>
  109. <pre><code class="lang-bash">$ xmake service --ccache -vD
  110. <remote_cache_server>: listening 0.0.0.0:9092 ..
  111. </code></pre>
  112. <h3 id="starttheserviceindaemonmode">Start the service in Daemon mode</h3>
  113. <p>To start, restart, or stop the cache service in daemon mode, you can issue the below commands:</p>
  114. <pre><code class="lang-bash">$ xmake service --ccache --start
  115. $ xmake service --ccache --restart
  116. $ xmake service --ccache --stop
  117. </code></pre>
  118. <h3 id="configuretheserver">Configure the server</h3>
  119. <p>To configure the server, you must first generate the configuration file, or grab one off the internet (the first option is the easier). To do so, run the <code>xmake service</code> command, and it will automatically generate a default <code>server.conf</code> configuration file, stored in <code>~/.xmake/service/server.conf</code>.</p>
  120. <pre><code class="lang-bash">$ xmake service
  121. generating the config file to /Users/ruki/.xmake/service/server.conf ..
  122. a token(590234653af52e91b9e438ed860f1a2b) is generated, we can use this token to connect service.
  123. generating the config file to /Users/ruki/.xmake/service/client.conf ..
  124. <remote_cache_server>: listening 0.0.0.0:9692 ..
  125. </code></pre>
  126. <p>Then, we edit it, fixing the server&#39;s listening port (optional).</p>
  127. <pre><code class="lang-bash">{
  128. distcc_build = {
  129. listen = "0.0.0.0:9692",
  130. workdir = "/Users/ruki/.xmake/service/server/remote_cache"
  131. },
  132. known_hosts = { },
  133. logfile = "/Users/ruki/.xmake/service/server/logs.txt",
  134. tokens = {
  135. "590234653af52e91b9e438ed860f1a2b"
  136. }
  137. }
  138. </code></pre>
  139. <h3 id="configuretheclient">Configure the client</h3>
  140. <p>The client configuration file is in <code>~/.xmake/service/client.conf</code>, where it can be specified where to connect to. We can configure multiple server addresses and corresponding tokens in the hosts list.</p>
  141. <pre><code class="lang-bash">{
  142. remote_cache = {
  143. connect = "127.0.0.1:9692",
  144. token = "590234653af52e91b9e438ed860f1a2b"
  145. }
  146. }
  147. }
  148. </code></pre>
  149. <h4 id="timeoutconfiguration">Timeout configuration</h4>
  150. <p>By default, clients connect, send and receive data with unlimited waiting without timeout, but if the network to access the server is unstable, then there is a chance that access may get stuck, and this can be solved by configuring a timeout. If a timeout exception occurs, it will automatically degrade to local cache and will not be stuck forever.</p>
  151. <p>We can configure, <code>send_timeout</code>, <code>recv_timeout</code> and <code>connect_timeout</code> to take effect for all client services if set at the root.</p>
  152. <pre><code class="lang-bash">{
  153. send_timeout = 5000,
  154. recv_timeout = 5000,
  155. connect_timeout = 5000
  156. }
  157. </code></pre>
  158. <p>We can also configure the timeout just for the current remote cache service, leaving the other services with the default timeout.</p>
  159. <pre><code class="lang-bash">{
  160. distcc_build = {
  161. send_timeout = 5000,
  162. recv_timeout = 5000,
  163. connect_timeout = 5000,
  164. }
  165. }
  166. </code></pre>
  167. <p>!> The server-side configuration also supports timeout configuration.</p>
  168. <h3 id="userauthorization">User authorization</h3>
  169. <p>For user authorization, please refer to <a href="/#/guide/other_features?id=user-authorization">Remote Compilation/User Authorization</a></p>
  170. <h3 id="connecttotheserver">Connect to the server</h3>
  171. <p>After configuring the authentication and server address, you can enter the following command to connect the current project to the configured server. We need to enter <code>--ccache</code> when connecting to specify that only the remote compilation cache service is connected.</p>
  172. <pre><code class="lang-bash">$ cd projectdir
  173. $ xmake service --connect --ccache
  174. <client>: connect 127.0.0.1:9692 ..
  175. <client>: 127.0.0.1:9692 connected!
  176. </code></pre>
  177. <p>We can also connect to multiple services at the same time, such as distributed compilation and remote compilation cache services.</p>
  178. <pre><code class="lang-bash">$ xmake service --connect --distcc --ccache
  179. </code></pre>
  180. <p>If there is no parameter, the default connection is the remote compilation service.</p>
  181. <h4 id="disconnect">Disconnect</h4>
  182. <pre><code class="lang-bash">$ xmake service --disconnect --ccache
  183. </code></pre>
  184. <h4 id="cleantheservercache">Clean the server cache</h4>
  185. <p>We can also use the following command to clear the cache on the remote server corresponding to the current project.</p>
  186. <pre><code class="lang-bash">$ xmake service --clean --ccache
  187. </code></pre>
  188. <p>...and if we execute <code>xmake clean --all</code>, when the remote service is connected, all caches will be automatically cleaned up.</p>
  189. <h4 id="someinternaloptimizations">Some internal optimizations</h4>
  190. <ol>
  191. <li>Pull the snapshot of the remote cache and send it back to the local through bloom filter + lz4, which is used to quickly determine whether the cache exists and avoid frequently querying the server cache information</li>
  192. <li>With the local cache, you can avoid frequent requests to the remote server and pull the cache.</li>
  193. <li>Internal state maintenance, compared with independent tools such as sscache, avoids frequent independent process loading and time-consuming, and avoids additional communication with the daemon process</li>
  194. </ol>
  195. </article>
  196. </body>
  197. </html>