remote_build.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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/remote_build">https://xmake.io/#/zh-cn/features/remote_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>2.6.5 版本提供了远程编译支持,我们可以通过它可以远程服务器上编译代码,远程运行和调试。</p>
  86. <p>服务器可以部署在 Linux/MacOS/Windows 上,实现跨平台编译,例如:在 Linux 上编译运行 Windows 程序,在 Windows 上编译运行 macOS/Linux 程序。</p>
  87. <p>相比 ssh 远程登入编译,它更加的稳定,使用更加流畅,不会因为网络不稳定导致 ssh 终端输入卡顿,也可以实现本地快速编辑代码文件。</p>
  88. <p>甚至我们可以在 vs/sublime/vscode/idea 等编辑器和IDE 中无缝实现远程编译,而不需要依赖 IDE 本身对远程编译的支持力度。</p>
  89. <h3 id="">开启服务</h3>
  90. <pre><code class="lang-console">$ xmake service
  91. <remote_build_server>: listening 0.0.0.0:9091 ..
  92. </code></pre>
  93. <p>我们也可以开启服务的同时,回显详细日志信息。</p>
  94. <pre><code class="lang-console">$ xmake service -vD
  95. <remote_build_server>: listening 0.0.0.0:9091 ..
  96. </code></pre>
  97. <h3 id="daemon">以 Daemon 模式开启服务</h3>
  98. <pre><code class="lang-console">$ xmake service --start
  99. $ xmake service --restart
  100. $ xmake service --stop
  101. </code></pre>
  102. <h3 id="">配置服务端</h3>
  103. <p>我们首先,运行 <code>xmake service</code> 命令,它会自动生成一个默认的 <code>server.conf</code> 配置文件,存储到 <code>~/.xmake/service/server.conf</code>。</p>
  104. <p>!> 2.6.5 版本,配置地址在 <code>~/.xmake/service.conf</code>,后续版本做了大量改进,分离了配置文件,如果用的是 2.6.6 以上版本,请使用新的配置文件。</p>
  105. <p>然后,我们编辑它,修复服务器的监听端口(可选)。</p>
  106. <pre><code class="lang-bash">$ cat ~/.xmake/service/server.conf
  107. {
  108. known_hosts = { },
  109. logfile = "/Users/ruki/.xmake/service/server/logs.txt",
  110. remote_build = {
  111. listen = "0.0.0.0:9691",
  112. workdir = "/Users/ruki/.xmake/service/server/remote_build"
  113. },
  114. tokens = {
  115. "e438d816c95958667747c318f1532c0f"
  116. }
  117. }
  118. </code></pre>
  119. <h3 id="">配置客户端</h3>
  120. <p>客户端配置文件在 <code>~/.xmake/service/client.conf</code>,我们可以在里面配置客户端需要连接的服务器地址。</p>
  121. <p>!> 2.6.5 版本,配置地址在 <code>~/.xmake/service.conf</code>,后续版本做了大量改进,分离了配置文件,如果用的是 2.6.6 以上版本,请使用新的配置文件。</p>
  122. <pre><code class="lang-console">$ cat ~/.xmake/service/client.conf
  123. {
  124. remote_build = {
  125. connect = "127.0.0.1:9691",
  126. token = "e438d816c95958667747c318f1532c0f"
  127. }
  128. }
  129. </code></pre>
  130. <h3 id="">用户认证和授权</h3>
  131. <p>!> 2.6.6 以上版本才支持用户认证,2.6.5 版本只能匿名连接。</p>
  132. <p>在实际连接之前,我们简单介绍下 xmake 提供的服务目前提供的几种认证机制。</p>
  133. <ol>
  134. <li>Token 认证</li>
  135. <li>密码认证</li>
  136. <li>可信主机验证</li>
  137. </ol>
  138. <h4 id="token">Token 认证</h4>
  139. <p>这也是我们默认推荐的方式,更加安全,配置和连接也更加方便,每次连接也不用输入密码。</p>
  140. <p>我们在执行 <code>xmake service</code> 命令时候,默认就会生成一个服务端和客户端的配置文件,并且自动生成一个默认的 token,因此本地直连是不需要任何配置的。</p>
  141. <h5 id="">服务端认证配置</h5>
  142. <p>服务端可以配置多个 token 用于对不同用户主机进行授权连接,当然也可以共用一个 token。</p>
  143. <pre><code class="lang-bash">$ cat ~/.xmake/service/server.conf
  144. {
  145. known_hosts = { },
  146. logfile = "/Users/ruki/.xmake/service/server/logs.txt",
  147. remote_build = {
  148. listen = "0.0.0.0:9691",
  149. workdir = "/Users/ruki/.xmake/service/server/remote_build"
  150. },
  151. tokens = {
  152. "e438d816c95958667747c318f1532c0f"
  153. }
  154. }
  155. </code></pre>
  156. <h5 id="">客户端认证配置</h5>
  157. <p>客户端只需要添加服务器上的 token 到对应的客户端配置中即可。</p>
  158. <pre><code class="lang-bash">$ cat ~/.xmake/service/client.conf
  159. {
  160. remote_build = {
  161. connect = "127.0.0.1:9691",
  162. token = "e438d816c95958667747c318f1532c0f"
  163. }
  164. }
  165. </code></pre>
  166. <h5 id="token">手动生成新 token</h5>
  167. <p>我们也可以执行下面的命令,手动生成一个新的 token,自己添加到服务器配置中。</p>
  168. <pre><code class="lang-bash">$ xmake service --gen-token
  169. New token a7b9fc2d3bfca1472aabc38bb5f5d612 is generated!
  170. </code></pre>
  171. <h4 id="">密码认证</h4>
  172. <p>我们也提供密码认证的授权模式,相比 token 认证,它需要用户每次连接的时候,输入密码,验证通过后,才能连接上。</p>
  173. <h5 id="">服务端认证配置</h5>
  174. <p>密码认证,我们不需要手动配置 token,只需要执行下面的命令,添加用户就行了,添加过程中,会提示用户输入密码。</p>
  175. <pre><code class="lang-bash">$ xmake service --add-user=ruki
  176. Please input user ruki password:
  177. 123456
  178. Add user ruki ok!
  179. </code></pre>
  180. <p>然后,xmake 就会通过用户名,密码生成一个新的 token 添加到服务器配置的 token 列表中去。</p>
  181. <pre><code class="lang-bash">$ cat ~/.xmake/service/server.conf
  182. {
  183. known_hosts = { },
  184. logfile = "/Users/ruki/.xmake/service/server/logs.txt",
  185. remote_build = {
  186. listen = "0.0.0.0:9691",
  187. workdir = "/Users/ruki/.xmake/service/server/remote_build"
  188. },
  189. tokens = {
  190. "e438d816c95958667747c318f1532c0f",
  191. "7889e25402413e93fd37395a636bf942"
  192. }
  193. }
  194. </code></pre>
  195. <p>当然,我们也可以删除指定的用户和密码。</p>
  196. <pre><code class="lang-bash">$xmake service --rm-user=ruki
  197. Please input user ruki password:
  198. 123456
  199. Remove user ruki ok!
  200. </code></pre>
  201. <h5 id="">客户端认证配置</h5>
  202. <p>对于客户端,我们不再需要设置服务器的 token 了,只需要在连接配置中,追加需要连接的用户名即可开启密码认证,格式:<code>user@address:port</code></p>
  203. <pre><code class="lang-bash">$ cat ~/.xmake/service/client.conf
  204. {
  205. remote_build = {
  206. connect = "[email protected]:9691"
  207. }
  208. }
  209. </code></pre>
  210. <p>!> 如果去掉用户名,也没配置 token,那就是匿名模式,如果服务器也没配置 token,就是完全禁用认证,直接连接。</p>
  211. <h4 id="">可信主机验证</h4>
  212. <p>另外,为了更进一步提高安全性,我们还提供了服务端可信主机验证,如果在服务器配置的 known_hosts 列表中,配置了可以连接的客户端主机 ip 地址,<br>那么只有这些主机可以成功连接上这台服务器,其他主机对它的连接都会被提示为不可信而拒绝连接,即使 token 和密码认证都没问题也不行。</p>
  213. <pre><code class="lang-bash">$ cat ~/.xmake/service/server.conf
  214. {
  215. logfile = "/Users/ruki/.xmake/service/logs.txt",
  216. server = {
  217. tokens = {
  218. "4b928c7563a0cba10ff4c3f5ca0c8e24"
  219. },
  220. known_hosts = { "127.0.0.1", "xx.xx.xx.xx"}
  221. }
  222. }
  223. </code></pre>
  224. <h3 id="">连接远程的服务器</h3>
  225. <p>接下来,我们只需要进入需要远程编译的工程根目录,执行 <code>xmake service --connect</code> 命令,进行连接。</p>
  226. <p>如果是 token 认证模式,那么不需要的额外的密码输入,直接连接。</p>
  227. <pre><code class="lang-console">$ xmake create test
  228. $ cd test
  229. $ xmake service --connect
  230. <remote_build_client>: connect 192.168.56.110:9091 ..
  231. <remote_build_client>: connected!
  232. <remote_build_client>: sync files in 192.168.56.110:9091 ..
  233. Scanning files ..
  234. Comparing 3 files ..
  235. [+]: src/main.cpp
  236. [+]: .gitignore
  237. [+]: xmake.lua
  238. 3 files has been changed!
  239. Archiving files ..
  240. Uploading files with 1372 bytes ..
  241. <remote_build_client>: sync files ok!
  242. </code></pre>
  243. <p>如果是密码认证,那么会提示用户输入密码,才能继续连接。</p>
  244. <pre><code class="lang-bash">$ xmake service --connect
  245. Please input user root password:
  246. 000000
  247. <remote_build_client>: connect 127.0.0.1:9691 ..
  248. <remote_build_client>: connected!
  249. <remote_build_client>: sync files in 127.0.0.1:9691 ..
  250. Scanning files ..
  251. Comparing 3 files ..
  252. [+]: xmake.lua
  253. [+]: .gitignore
  254. [+]: src/main.cpp
  255. 3 files has been changed!
  256. Archiving files ..
  257. Uploading files with 1591 bytes ..
  258. <remote_build_client>: sync files ok!
  259. </code></pre>
  260. <p>如果密码不对,就会提示错误。</p>
  261. <pre><code class="lang-bash">$ xmake service --connect
  262. Please input user root password:
  263. 123
  264. <remote_build_client>: connect 127.0.0.1:9691 ..
  265. <remote_build_client>: connect 127.0.0.1:9691 failed, user and password are incorrect!
  266. </code></pre>
  267. <h3 id="">远程构建工程</h3>
  268. <p>连接成功后,我们就可以像正常本地编译一样,进行远程编译。</p>
  269. <pre><code class="lang-console">$ xmake
  270. <remote_build_client>: run xmake in 192.168.56.110:9091 ..
  271. checking for platform ... macosx
  272. checking for architecture ... x86_64
  273. checking for Xcode directory ... /Applications/Xcode.app
  274. checking for Codesign Identity of Xcode ... Apple Development: [email protected] (T3NA4MRVPU)
  275. checking for SDK version of Xcode for macosx (x86_64) ... 11.3
  276. checking for Minimal target version of Xcode for macosx (x86_64) ... 11.4
  277. [ 25%]: cache compiling.release src/main.cpp
  278. [ 50%]: linking.release test
  279. [100%]: build ok!
  280. <remote_build_client>: run command ok!
  281. </code></pre>
  282. <h3 id="">远程运行目标程序</h3>
  283. <p>我们也可以像本地运行调试那样,远程运行调试编译的目标程序。</p>
  284. <pre><code class="lang-console">$ xmake run
  285. <remote_build_client>: run xmake run in 192.168.56.110:9091 ..
  286. hello world!
  287. <remote_build_client>: run command ok!
  288. </code></pre>
  289. <h3 id="">远程重建工程</h3>
  290. <pre><code class="lang-console">$ xmake -rv
  291. <remote_build_client>: run xmake -rv in 192.168.56.110:9091 ..
  292. [ 25%]: cache compiling.release src/main.cpp
  293. /usr/local/bin/ccache /usr/bin/xcrun -sdk macosx clang -c -Qunused-arguments -arch x86_64 -mmacosx-version-min=11.4 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -o build/.objs/test/macosx/x86_64/release/src/main.cpp.o src/main.cpp
  294. [ 50%]: linking.release test
  295. "/usr/bin/xcrun -sdk macosx clang++" -o build/macosx/x86_64/release/test build/.objs/test/macosx/x86_64/release/src/main.cpp.o -arch x86_64 -mmacosx-version-min=11.4 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -stdlib=libc++ -Wl,-x -lz
  296. [100%]: build ok!
  297. <remote_build_client>: run command ok!
  298. </code></pre>
  299. <h3 id="">远程配置编译参数</h3>
  300. <pre><code class="lang-console">$ xmake f --xxx --yy
  301. </code></pre>
  302. <h3 id="">手动同步工程文件</h3>
  303. <p>连接的时候,会自动同步一次代码,后期代码改动,可以执行此命令来手动同步改动的文件。</p>
  304. <pre><code class="lang-console">$ xmake service --sync
  305. <remote_build_client>: sync files in 192.168.56.110:9091 ..
  306. Scanning files ..
  307. Comparing 3 files ..
  308. [+]: src/main.cpp
  309. [+]: .gitignore
  310. [+]: xmake.lua
  311. 3 files has been changed!
  312. Archiving files ..
  313. Uploading files with 1372 bytes ..
  314. <remote_build_client>: sync files ok!
  315. </code></pre>
  316. <h3 id="">拉取远程文件</h3>
  317. <p>v2.7.1 版本我们新增了一个参数用于拉取远程指定的文件,通常我们可以用来完成构建后目标文件的拉取,将编译后的库文件下载到本地。</p>
  318. <p>例如:</p>
  319. <pre><code class="lang-bash">xmake service --pull &#39;build/**&#39; outputdir
  320. </code></pre>
  321. <p>我们可以指定远程路径 <code>build/**</code> ,将所有匹配的文件全部拉取到本地 outputdir 目录下。</p>
  322. <h3 id="">断开远程连接</h3>
  323. <p>针对当前工程,断开连接,这仅仅影响当前工程,其他项目还是可以同时连接和编译。</p>
  324. <pre><code class="lang-console">$ xmake service --disconnect
  325. <remote_build_client>: disconnect 192.168.56.110:9091 ..
  326. <remote_build_client>: disconnected!
  327. </code></pre>
  328. <h3 id="">查看服务器日志</h3>
  329. <pre><code class="lang-console">$ xmake service --logs
  330. </code></pre>
  331. <h3 id="">清理远程服务缓存和构建文件</h3>
  332. <p>我们也可以手动清理远程的任何缓存和构建生成的文件。</p>
  333. <pre><code class="lang-console">$ cd projectdir
  334. $ xmake service --clean
  335. </code></pre>
  336. </article>
  337. </body>
  338. </html>