builtin_plugins.html 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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/plugin/builtin_plugins">https://xmake.io/#/zh-cn/plugin/builtin_plugins</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. <h2 id="ide">生成IDE工程文件</h2>
  86. <h3 id="">简介</h3>
  87. <p>XMake跟<code>cmake</code>, <code>premake</code>等其他一些构建工具的区别在于:</p>
  88. <p>!> <code>xmake</code>默认是直接构建运行的,生成第三方的IDE的工程文件仅仅作为<code>插件</code>来提供。</p>
  89. <p>这样做的一个好处是:插件更加容易扩展,维护也更加独立和方便。</p>
  90. <h3 id="makefile">生成Makefile</h3>
  91. <pre><code class="lang-console">$ xmake project -k makefile
  92. </code></pre>
  93. <h3 id="cmakeliststxt">生成CMakelists.txt</h3>
  94. <pre><code class="lang-console">$ xmake project -k cmakelists
  95. </code></pre>
  96. <h3 id="buildninja">生成build.ninja</h3>
  97. <p>!> 2.3.1以上版本才支持</p>
  98. <pre><code class="lang-console">$ xmake project -k ninja
  99. </code></pre>
  100. <h3 id="compiler_flags">生成compiler_flags</h3>
  101. <pre><code class="lang-console">$ xmake project -k compiler_flags
  102. </code></pre>
  103. <h3 id="compile_commands">生成compile_commands</h3>
  104. <p>导出每个源文件的编译信息,生成基于clang的编译数据库文件,json格式,可用于跟ide,编辑器,静态分析工具进行交互。</p>
  105. <pre><code class="lang-console">$ xmake project -k compile_commands
  106. </code></pre>
  107. <p>输出的内容格式如下:</p>
  108. <pre><code>[
  109. { "directory": "/home/user/llvm/build",
  110. "command": "/usr/bin/clang++ -Irelative -DSOMEDEF=\"With spaces, quotes and \\-es.\" -c -o file.o file.cc",
  111. "file": "file.cc" },
  112. ...
  113. ]
  114. </code></pre><p>对于<code>compile_commands</code>的详细说明见:<a href="#https://clang.llvm.org/docs/JSONCompilationDatabase.html">JSONCompilationDatabase</a></p>
  115. <h3 id="xcode">生成Xcode工程文件</h3>
  116. <p>目前,我们还没有时间去自己实现xcode工程的生成,但不代表不支持,因为xmake支持生成cmakelists.txt文件,而cmake是支持xcode工程文件生成的,在官方还没有实现之前,<br>我们也可以通过cmake变相支持它,xmake会自动内部调用cmake中转下生成结果,对用户而言使用上没啥区别,只需要确保cmake已经安装即可:</p>
  117. <pre><code class="lang-console">$ xmake project -k xcode
  118. </code></pre>
  119. <p>!> 等之后有时间,我们会重新自己实现各更加完善的xcode输出插件,也欢迎大家帮忙贡献。</p>
  120. <h3 id="visualstudio">生成VisualStudio工程</h3>
  121. <h4 id="xmake">使用xmake集成编译</h4>
  122. <p>v2.2.8以上版本,提供了新版本的vs工程生成插件扩展,跟之前的生成vs的插件处理模式上有很大的不同,之前生成的vs工程是把所有文件的编译展开后,转交给vs来处理编译。</p>
  123. <p>但是这种模式,对xmake的rules是没法支持的。因为xmake的rules里面用了很多的<code>on_build</code>此类自定义脚本,无法展开,所以像qt, wdk此类的项目就没法支持导出到vs里面进行编译了。</p>
  124. <p>因此,为了解决这个问题,新版本的vs生成插件通过在vs下直接调用xmake命令,去执行编译操作,并且对intellsence和定义跳转,还有断点调试也做了支持。</p>
  125. <p>具体使用方式跟老版本类似:</p>
  126. <pre><code class="lang-console">$ xmake project -k [vsxmake2010|vsxmake2013|vsxmake2015|..] -m "debug;release"
  127. </code></pre>
  128. <p>如果没指明版本,那么xmake会自动探测当前已有的vs版本来生成:</p>
  129. <pre><code class="lang-bash">$ xmake project -k vsxmake -m "debug,release"
  130. </code></pre>
  131. <p><img src="/assets/img/manual/qt_vs.png" alt=""></p>
  132. <p>另外,vsxmake插件还会额外生成一个自定义的配置属性页,用于在vs里面,方便灵活的修改和追加一些xmake编译配置,甚至可以在里面配置切换到其他交叉工具链,实现在vs中对android, linux等其他平台的交叉编译。</p>
  133. <p><img src="/assets/img/manual/property_page_vsxmake.png" alt=""></p>
  134. <p>v2.5.1 版本提供了一个 <code>add_rules("plugin.vsxmake.autoupdate")</code> 规则,如果应用此规则,生产的vs工程在编译完成后,会检测 xmake.lua 和代码文件列表的改动,如果有变化,就会自动更新 vs 工程。</p>
  135. <pre><code class="lang-lua">add_rules("plugin.vsxmake.autoupdate")
  136. target("test")
  137. set_kind("binary")
  138. add_files("src/*.c")
  139. </code></pre>
  140. <p>另外,我们可以通过 <code>set_group</code> 接口对每个 target 设置分组,使得生成的 vs 工程可以按指定结构进行分组。更多详情见:<a href="https://github.com/xmake-io/xmake/issues/1026">issue 1026</a></p>
  141. <h4 id="vs">使用vs内置编译机制</h4>
  142. <p>!> 建议尽量使用上文提到的v2.2.8之后提供的新版的vs生成插件,支持更加完善,此处的生成方式不支持xmake的rules,以及对qt等工程的生成。</p>
  143. <pre><code class="lang-console">$ xmake project -k [vs2008|vs2013|vs2015|..]
  144. </code></pre>
  145. <p>v2.1.2以上版本,增强了vs201x版本工程的生成,支持多模式+多架构生成,生成的时候只需要指定:</p>
  146. <pre><code class="lang-console">$ xmake project -k vs2017 -m "debug,release"
  147. </code></pre>
  148. <p>生成后的工程文件,同时支持<code>debug|x86</code>, <code>debug|x64</code>, <code>release|x86</code>, <code>release|x64</code>四种配置模式。</p>
  149. <p>如果不想每次生成的时候,指定模式,可以把模式配置加到<code>xmake.lua</code>的中,例如:</p>
  150. <pre><code class="lang-lua">-- 配置当前的工程,支持哪些编译模式
  151. add_rules("mode.debug", "mode.release")
  152. </code></pre>
  153. <p>另外,我们可以通过 <code>set_group</code> 接口对每个 target 设置分组,使得生成的 vs 工程可以按指定结构进行分组。更多详情见:<a href="https://github.com/xmake-io/xmake/issues/1026">issue 1026</a></p>
  154. <h2 id="lua">运行自定义lua脚本</h2>
  155. <p>这个跟宏脚本类似,只是省去了导入导出操作,直接指定lua脚本来加载运行,这对于想要快速测试一些接口模块,验证自己的某些思路,都是一个不错的方式。</p>
  156. <h3 id="">运行指定的脚本文件</h3>
  157. <p>我们先写个简单的lua脚本:</p>
  158. <pre><code class="lang-lua">function main()
  159. print("hello xmake!")
  160. end
  161. </code></pre>
  162. <p>然后直接运行它就行了:</p>
  163. <pre><code class="lang-console">$ xmake lua /tmp/test.lua
  164. </code></pre>
  165. <p><p class="tip"><br> 当然,你也可以像宏脚本那样,使用<code>import</code>接口导入扩展模块,实现复杂的功能。<br></p>
  166. </p>
  167. <h3 id="">运行内置的脚本命令</h3>
  168. <p>你可以运行 <code>xmake lua -l</code> 来列举所有内置的脚本名,例如:</p>
  169. <pre><code class="lang-console">$ xmake lua -l
  170. scripts:
  171. cat
  172. cp
  173. echo
  174. versioninfo
  175. ...
  176. </code></pre>
  177. <p>并且运行它们:</p>
  178. <pre><code class="lang-console">$ xmake lua cat ~/file.txt
  179. $ xmake lua echo "hello xmake"
  180. $ xmake lua cp /tmp/file /tmp/file2
  181. $ xmake lua versioninfo
  182. </code></pre>
  183. <h3 id="repl">运行交互命令 (REPL)</h3>
  184. <p>有时候在交互模式下,运行命令更加的方便测试和验证一些模块和api,也更加的灵活,不需要再去额外写一个脚本文件来加载。</p>
  185. <p>我们先看下,如何进入交互模式:</p>
  186. <pre><code class="lang-console"># 不带任何参数执行,就可以进入
  187. $ xmake lua
  188. >
  189. # 进行表达式计算
  190. > 1 + 2
  191. 3
  192. # 赋值和打印变量值
  193. > a = 1
  194. > a
  195. 1
  196. # 多行输入和执行
  197. > for _, v in pairs({1, 2, 3}) do
  198. >> print(v)
  199. >> end
  200. 1
  201. 2
  202. 3
  203. </code></pre>
  204. <p>我们也能够通过 <code>import</code> 来导入扩展模块:</p>
  205. <pre><code class="lang-console">> task = import("core.project.task")
  206. > task.run("hello")
  207. hello xmake!
  208. </code></pre>
  209. <p>如果要中途取消多行输入,只需要输入字符:<code>q</code> 就行了</p>
  210. <pre><code class="lang-console">> for _, v in ipairs({1, 2}) do
  211. >> print(v)
  212. >> q <-- 取消多行输入,清空先前的输入数据
  213. > 1 + 2
  214. 3
  215. </code></pre>
  216. <h2 id="">显示指定信息和列表</h2>
  217. <h3 id="xmake">显示xmake自身和当前项目的基础信息</h3>
  218. <pre><code class="lang-bash">$ xmake show
  219. The information of xmake:
  220. version: 2.3.3+202006011009
  221. host: macosx/x86_64
  222. programdir: /Users/ruki/.local/share/xmake
  223. programfile: /Users/ruki/.local/bin/xmake
  224. globaldir: /Users/ruki/.xmake
  225. tmpdir: /var/folders/32/w9cz0y_14hs19lkbs6v6_fm80000gn/T/.xmake501/200603
  226. workingdir: /Users/ruki/projects/personal/tbox
  227. packagedir: /Users/ruki/.xmake/packages
  228. packagedir(cache): /Users/ruki/.xmake/cache/packages/2006
  229. The information of project: tbox
  230. version: 1.6.5
  231. plat: macosx
  232. arch: x86_64
  233. mode: release
  234. buildir: build
  235. configdir: /Users/ruki/projects/personal/tbox/.xmake/macosx/x86_64
  236. projectdir: /Users/ruki/projects/personal/tbox
  237. projectfile: /Users/ruki/projects/personal/tbox/xmake.lua
  238. </code></pre>
  239. <h3 id="">显示工具链列表</h3>
  240. <pre><code class="lang-bash">$ xmake show -l toolchains
  241. xcode Xcode IDE
  242. vs VisualStudio IDE
  243. yasm The Yasm Modular Assembler
  244. clang A C language family frontend for LLVM
  245. go Go Programming Language Compiler
  246. dlang D Programming Language Compiler
  247. sdcc Small Device C Compiler
  248. cuda CUDA Toolkit
  249. ndk Android NDK
  250. rust Rust Programming Language Compiler
  251. llvm A collection of modular and reusable compiler and toolchain technologies
  252. cross Common cross compilation toolchain
  253. nasm NASM Assembler
  254. gcc GNU Compiler Collection
  255. mingw Minimalist GNU for Windows
  256. gnu-rm GNU Arm Embedded Toolchain
  257. envs Environment variables toolchain
  258. fasm Flat Assembler
  259. </code></pre>
  260. <h3 id="target">显示指定 target 配置信息</h3>
  261. <p>我们可以用它来快速追溯定位一些特定配置的位置。</p>
  262. <pre><code class="lang-bash">$ xmake show -t tbox
  263. The information of target(tbox):
  264. at: /Users/ruki/projects/personal/tbox/src/tbox/xmake.lua
  265. kind: static
  266. targetfile: build/macosx/x86_64/release/libtbox.a
  267. rules:
  268. -> mode.release -> ./xmake.lua:26
  269. -> mode.debug -> ./xmake.lua:26
  270. -> mode.profile -> ./xmake.lua:26
  271. -> mode.coverage -> ./xmake.lua:26
  272. -> utils.install.cmake_importfiles -> ./src/tbox/xmake.lua:15
  273. -> utils.install.pkgconfig_importfiles -> ./src/tbox/xmake.lua:16
  274. options:
  275. -> info -> ./src/tbox/xmake.lua:50
  276. -> float -> ./src/tbox/xmake.lua:50
  277. -> wchar -> ./src/tbox/xmake.lua:50
  278. -> exception -> ./src/tbox/xmake.lua:50
  279. -> force-utf8 -> ./src/tbox/xmake.lua:50
  280. -> deprecated -> ./src/tbox/xmake.lua:50
  281. -> xml -> ./src/tbox/xmake.lua:53
  282. -> zip -> ./src/tbox/xmake.lua:53
  283. -> hash -> ./src/tbox/xmake.lua:53
  284. -> regex -> ./src/tbox/xmake.lua:53
  285. -> coroutine -> ./src/tbox/xmake.lua:53
  286. -> object -> ./src/tbox/xmake.lua:53
  287. -> charset -> ./src/tbox/xmake.lua:53
  288. -> database -> ./src/tbox/xmake.lua:53
  289. packages:
  290. -> mbedtls -> ./src/tbox/xmake.lua:43
  291. -> polarssl -> ./src/tbox/xmake.lua:43
  292. -> openssl -> ./src/tbox/xmake.lua:43
  293. -> pcre2 -> ./src/tbox/xmake.lua:43
  294. -> pcre -> ./src/tbox/xmake.lua:43
  295. -> zlib -> ./src/tbox/xmake.lua:43
  296. -> mysql -> ./src/tbox/xmake.lua:43
  297. -> sqlite3 -> ./src/tbox/xmake.lua:43
  298. links:
  299. -> pthread -> option(__keyword_thread_local) -> @programdir/includes/check_csnippets.lua:100
  300. syslinks:
  301. -> pthread -> ./xmake.lua:71
  302. -> dl -> ./xmake.lua:71
  303. -> m -> ./xmake.lua:71
  304. -> c -> ./xmake.lua:71
  305. defines:
  306. -> __tb_small__ -> ./xmake.lua:42
  307. -> __tb_prefix__="tbox" -> ./src/tbox/xmake.lua:19
  308. -> _GNU_SOURCE=1 -> option(__systemv_semget) -> @programdir/includes/check_cfuncs.lua:104
  309. cxflags:
  310. -> -Wno-error=deprecated-declarations -> ./xmake.lua:22
  311. -> -fno-strict-aliasing -> ./xmake.lua:22
  312. -> -Wno-error=expansion-to-defined -> ./xmake.lua:22
  313. -> -fno-stack-protector -> ./xmake.lua:51
  314. frameworks:
  315. -> CoreFoundation -> ./src/tbox/xmake.lua:38
  316. -> CoreServices -> ./src/tbox/xmake.lua:38
  317. mxflags:
  318. -> -Wno-error=deprecated-declarations -> ./xmake.lua:23
  319. -> -fno-strict-aliasing -> ./xmake.lua:23
  320. -> -Wno-error=expansion-to-defined -> ./xmake.lua:23
  321. includedirs:
  322. -> src -> ./src/tbox/xmake.lua:26
  323. -> build/macosx/x86_64/release -> ./src/tbox/xmake.lua:27
  324. headerfiles:
  325. -> src/(tbox/**.h)|**/impl/**.h -> ./src/tbox/xmake.lua:30
  326. -> src/(tbox/prefix/**/prefix.S) -> ./src/tbox/xmake.lua:31
  327. -> src/(tbox/math/impl/*.h) -> ./src/tbox/xmake.lua:32
  328. -> src/(tbox/utils/impl/*.h) -> ./src/tbox/xmake.lua:33
  329. -> build/macosx/x86_64/release/tbox.config.h -> ./src/tbox/xmake.lua:34
  330. files:
  331. -> src/tbox/*.c -> ./src/tbox/xmake.lua:56
  332. -> src/tbox/hash/bkdr.c -> ./src/tbox/xmake.lua:57
  333. -> src/tbox/hash/fnv32.c -> ./src/tbox/xmake.lua:57
  334. -> src/tbox/hash/adler32.c -> ./src/tbox/xmake.lua:57
  335. -> src/tbox/math/**.c -> ./src/tbox/xmake.lua:58
  336. -> src/tbox/libc/**.c|string/impl/**.c -> ./src/tbox/xmake.lua:59
  337. -> src/tbox/utils/*.c|option.c -> ./src/tbox/xmake.lua:60
  338. -> src/tbox/prefix/**.c -> ./src/tbox/xmake.lua:61
  339. -> src/tbox/memory/**.c -> ./src/tbox/xmake.lua:62
  340. -> src/tbox/string/**.c -> ./src/tbox/xmake.lua:63
  341. -> src/tbox/stream/**.c|**/charset.c|**/zip.c -> ./src/tbox/xmake.lua:64
  342. -> src/tbox/network/**.c|impl/ssl/*.c -> ./src/tbox/xmake.lua:65
  343. -> src/tbox/algorithm/**.c -> ./src/tbox/xmake.lua:66
  344. -> src/tbox/container/**.c|element/obj.c -> ./src/tbox/xmake.lua:67
  345. -> src/tbox/libm/impl/libm.c -> ./src/tbox/xmake.lua:68
  346. -> src/tbox/libm/idivi8.c -> ./src/tbox/xmake.lua:73
  347. -> src/tbox/libm/ilog2i.c -> ./src/tbox/xmake.lua:70
  348. -> src/tbox/libm/isqrti.c -> ./src/tbox/xmake.lua:71
  349. -> src/tbox/libm/isqrti64.c -> ./src/tbox/xmake.lua:72
  350. -> src/tbox/platform/*.c|context.c|exception.c -> ./src/tbox/xmake.lua:74
  351. -> src/tbox/platform/impl/*.c|charset.c|poller_fwatcher.c -> ./src/tbox/xmake.lua:74
  352. -> src/tbox/libm/*.c -> ./src/tbox/xmake.lua:77
  353. compiler (cc): /usr/bin/xcrun -sdk macosx clang
  354. -> -Qunused-arguments -target x86_64-apple-macos12.6 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk
  355. linker (ar): /usr/bin/xcrun -sdk macosx ar
  356. -> -cr
  357. compflags (cc):
  358. -> -Qunused-arguments -target x86_64-apple-macos12.6 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk -Wall -Werror -Oz -std=c99 -Isrc -Ibuild/macosx/x86_64/release -D__tb_small__ -D__tb_prefix__=\"tbox\" -D_GNU_SOURCE=1 -framework CoreFoundation -framework CoreServices -Wno-error=deprecated-declarations -fno-strict-aliasing -Wno-error=expansion-to-defined -fno-stack-protector
  359. linkflags (ar):
  360. -> -cr
  361. </code></pre>
  362. <h3 id="">显示内置编译模式列表</h3>
  363. <pre><code class="lang-bash">$ xmake show -l buildmodes
  364. </code></pre>
  365. <h3 id="">显示内置编译规则列表</h3>
  366. <pre><code class="lang-bash">$ xmake show -l rules
  367. </code></pre>
  368. <h3 id="">显示其他信息</h3>
  369. <p>还在完善中,详情见:<a href="https://github.com/xmake-io/xmake/issues/798">https://github.com/xmake-io/xmake/issues/798</a></p>
  370. <p>或者运行:</p>
  371. <pre><code class="lang-bash">$ xmake show --help
  372. </code></pre>
  373. <h2 id="">监视文件更新</h2>
  374. <p>v2.7.1 版本新增了 <code>xmake watch</code> 插件命令,可以自动监视项目文件更新,然后触发自动构建,或者运行一些自定义命令。</p>
  375. <p>这通常用于个人开发时候,实现快速的实时增量编译,而不需要每次手动执行编译命令,提高开发效率。</p>
  376. <h3 id="">项目更新后自动构建</h3>
  377. <p>默认行为就是监视整个项目根目录,任何文件改动都会触发项目的增量编译。</p>
  378. <pre><code class="lang-bash">$ xmake watch
  379. watching /private/tmp/test/src/** ..
  380. watching /private/tmp/test/* ..
  381. /private/tmp/test/src/main.cpp modified
  382. [ 25%]: cache compiling.release src/main.cpp
  383. [ 50%]: linking.release test
  384. [100%]: build ok!
  385. </code></pre>
  386. <h3 id="">监视指定目录</h3>
  387. <p>我们也可以监视指定的代码目录,缩小监视范围,提升监视性能。</p>
  388. <pre><code class="lang-bash">$ xmake watch -d src
  389. $ xmake watch -d "src;tests/*"
  390. </code></pre>
  391. <p>上面的命令,会去递归监视所有子目录,如果想要紧紧监视当前目录下的文件,不进行递归监视,可以使用下面的命令。</p>
  392. <pre><code class="lang-bash">$ xmake watch -p src
  393. $ xmake watch -p "src;tests/*"
  394. </code></pre>
  395. <h3 id="">监视并运行指定命令</h3>
  396. <p>如果想在自动构建后,还想自动运行构建的程序,我们可以使用自定义的命令集。</p>
  397. <pre><code class="lang-bash">$ xmake watch -c "xmake; xmake run"
  398. </code></pre>
  399. <p>上面的命令列表是作为字符串传递,这对于复杂命令参数,需要转义比较繁琐不够灵活,那么我们可以使用下面的方式进行任意命令的设置。</p>
  400. <pre><code class="lang-bash">$ xmake watch -- echo hello xmake!
  401. $ xmake watch -- xmake run --help
  402. </code></pre>
  403. <h3 id="">监视并运行目标程序</h3>
  404. <p>尽管我们可以通过自定义命令来实现目标程序的自动运行,但是我们也提供了更加方便的参数来实现这个行为。</p>
  405. <pre><code class="lang-bash">$ xmake watch -r
  406. $ xmake watch --run
  407. [100%]: build ok!
  408. hello world!
  409. </code></pre>
  410. <h3 id="lua">监视并运行 lua 脚本</h3>
  411. <p>我们还可以监视文件更新后,运行指定的 lua 脚本,实现更加灵活复杂的命令定制。</p>
  412. <pre><code class="lang-bash">$ xmake watch -s /tmp/test.lua
  413. </code></pre>
  414. <p>我们还可以再脚本中获取所有更新的文件路径列表和事件。</p>
  415. <pre><code class="lang-lua">function main(events)
  416. -- TODO handle events
  417. end
  418. </code></pre>
  419. <h2 id="">分析诊断工程配置和代码</h2>
  420. <h3 id="">检测工程配置</h3>
  421. <h4 id="api">默认检测所有 API</h4>
  422. <pre><code class="lang-lua">set_lanuages("c91") -- typo
  423. </code></pre>
  424. <pre><code class="lang-console">$ xmake check
  425. ./xmake.lua:15: warning: unknown language value &#39;c91&#39;, it may be &#39;c90&#39;
  426. 0 notes, 1 warnings, 0 errors
  427. </code></pre>
  428. <p>默认也可以指定检测特定组:</p>
  429. <pre><code class="lang-console">$ xmake check api
  430. $ xmake check api.target
  431. </code></pre>
  432. <h4 id="">显示详细输出</h4>
  433. <p>这会额外提供 note 级别的检测信息。</p>
  434. <pre><code class="lang-console">$ xmake check -v
  435. ./xmake.lua:15: warning: unknown language value &#39;cxx91&#39;, it may be &#39;cxx98&#39;
  436. ./src/tbox/xmake.lua:43: note: unknown package value &#39;mbedtls&#39;
  437. ./src/tbox/xmake.lua:43: note: unknown package value &#39;polarssl&#39;
  438. ./src/tbox/xmake.lua:43: note: unknown package value &#39;openssl&#39;
  439. ./src/tbox/xmake.lua:43: note: unknown package value &#39;pcre2&#39;
  440. ./src/tbox/xmake.lua:43: note: unknown package value &#39;pcre&#39;
  441. ./src/tbox/xmake.lua:43: note: unknown package value &#39;zlib&#39;
  442. ./src/tbox/xmake.lua:43: note: unknown package value &#39;mysql&#39;
  443. ./src/tbox/xmake.lua:43: note: unknown package value &#39;sqlite3&#39;
  444. 8 notes, 1 warnings, 0 errors
  445. </code></pre>
  446. <h4 id="api">检测指定的 API</h4>
  447. <pre><code class="lang-console">$ xmake check api.target.languages
  448. ./xmake.lua:15: warning: unknown language value &#39;cxx91&#39;, it may be &#39;cxx98&#39;
  449. 0 notes, 1 warnings, 0 errors
  450. </code></pre>
  451. <h4 id="flags">检测编译 flags</h4>
  452. <pre><code class="lang-console">$ xmake check
  453. ./xmake.lua:10: warning: clang: unknown c compiler flag &#39;-Ox&#39;
  454. 0 notes, 1 warnings, 0 errors
  455. </code></pre>
  456. <h4 id="includedirs">检测 includedirs</h4>
  457. <p>除了 includedirs,还有 linkdirs 等路径都会去检测。</p>
  458. <pre><code class="lang-console">$ xmake check
  459. ./xmake.lua:11: warning: includedir &#39;xxx&#39; not found
  460. 0 notes, 1 warnings, 0 errors
  461. </code></pre>
  462. <h3 id="clangtidy">检测工程代码(clang-tidy)</h3>
  463. <h4 id="clangtidy">显示 clang-tidy 检测列表</h4>
  464. <pre><code class="lang-console">$ xmake check clang.tidy --list
  465. Enabled checks:
  466. clang-analyzer-apiModeling.StdCLibraryFunctions
  467. clang-analyzer-apiModeling.TrustNonnull
  468. clang-analyzer-apiModeling.google.GTest
  469. clang-analyzer-apiModeling.llvm.CastValue
  470. clang-analyzer-apiModeling.llvm.ReturnValue
  471. ...
  472. </code></pre>
  473. <h4 id="targets">检测所有 targets 中的源码</h4>
  474. <pre><code class="lang-console">$ xmake check clang.tidy
  475. 1 error generated.
  476. Error while processing /private/tmp/test2/src/main.cpp.
  477. /tmp/test2/src/main.cpp:1:10: error: &#39;iostr&#39; file not found [clang-diagnostic-error]
  478. #include <iostr>
  479. ^~~~~~~
  480. Found compiler error(s).
  481. error: execv(/usr/local/opt/llvm/bin/clang-tidy -p compile_commands.json /private/tmp/test2/src
  482. /main.cpp) failed(1)
  483. </code></pre>
  484. <h4 id="">指定检测类型</h4>
  485. <p>我们可以在 <code>--check=</code> 中指定需要检测的类型,具体用法可以参考 <code>clang-tidy</code> 的 <code>--check=</code> 参数,完全一致的。</p>
  486. <pre><code class="lang-console">$ xmake check clang.tidy --checks="*"
  487. 6 warnings and 1 error generated.
  488. Error while processing /private/tmp/test2/src/main.cpp.
  489. /tmp/test2/src/main.cpp:1:10: error: &#39;iostr&#39; file not found [clang-diagnostic-error]
  490. #include <iostr>
  491. ^~~~~~~
  492. /tmp/test2/src/main.cpp:3:1: warning: do not use namespace using-directives; use using-declarat
  493. ions instead [google-build-using-namespace]
  494. using namespace std;
  495. ^
  496. /tmp/test2/src/main.cpp:3:17: warning: declaration must be declared within the &#39;__llvm_libc&#39; na
  497. mespace [llvmlibc-implementation-in-namespace]
  498. using namespace std;
  499. ^
  500. /tmp/test2/src/main.cpp:5:5: warning: declaration must be declared within the &#39;__llvm_libc&#39; nam
  501. espace [llvmlibc-implementation-in-namespace]
  502. int main(int argc, char **argv) {
  503. ^
  504. /tmp/test2/src/main.cpp:5:5: warning: use a trailing return type for this function [modernize-u
  505. se-trailing-return-type]
  506. int main(int argc, char **argv) {
  507. ~~~ ^
  508. auto -> int
  509. /tmp/test2/src/main.cpp:5:14: warning: parameter &#39;argc&#39; is unused [misc-unused-parameters]
  510. int main(int argc, char **argv) {
  511. ^~~~
  512. /*argc*/
  513. /tmp/test2/src/main.cpp:5:27: warning: parameter &#39;argv&#39; is unused [misc-unused-parameters]
  514. int main(int argc, char **argv) {
  515. ^~~~
  516. /*argv*/
  517. Found compiler error(s).
  518. error: execv(/usr/local/opt/llvm/bin/clang-tidy --checks=* -p compile_commands.json /private/tm
  519. p/test2/src/main.cpp) failed(1)
  520. </code></pre>
  521. <h4 id="target">检测指定 target 的代码</h4>
  522. <pre><code class="lang-console">$ xmake check clang.tidy [targetname]
  523. </code></pre>
  524. <h4 id="">检测给定的源文件列表</h4>
  525. <pre><code class="lang-console">$ xmake check clang.tidy -f src/main.c
  526. $ xmake check clang.tidy -f &#39;src/*.c:src/**.cpp&#39;
  527. </code></pre>
  528. <h4 id="clangtidy">设置 .clang-tidy 配置文件</h4>
  529. <pre><code class="lang-console">$ xmake check clang.tidy --configfile=/tmp/.clang-tidy
  530. </code></pre>
  531. <h4 id="clangtidy">创建 .clang-tidy 配置文件</h4>
  532. <pre><code class="lang-console">$ xmake check clang.tidy --checks="*" --create
  533. $ cat .clang-tidy
  534. ---
  535. Checks: &#39;clang-diagnostic-*,clang-analyzer-*,*&#39;
  536. WarningsAsErrors: &#39;&#39;
  537. HeaderFilterRegex: &#39;&#39;
  538. AnalyzeTemporaryDtors: false
  539. FormatStyle: none
  540. User: ruki
  541. CheckOptions:
  542. - key: readability-suspicious-call-argument.PrefixSimilarAbove
  543. value: &#39;30&#39;
  544. - key: cppcoreguidelines-no-malloc.Reallocations
  545. value: &#39;::realloc&#39;
  546. </code></pre>
  547. <h4 id="">自动修复错误代码</h4>
  548. <p>我们可以使用下面的命令参数,自动修复 clang tidy 检测出来的问题代码。</p>
  549. <pre><code class="lang-console">$ xmake check clang.tidy --fix
  550. $ xmake check clang.tidy --fix_errors
  551. $ xmake check clang.tidy --fix_notes
  552. </code></pre>
  553. <h2 id="xpack">生成安装包 (XPack)</h2>
  554. <h3 id="">简介</h3>
  555. <p>这个插件可以帮助用户快速生成不同平台的安装包,源码包,它会生成下面一些安装包格式:</p>
  556. <ul>
  557. <li>Windows NSIS 二进制安装包</li>
  558. <li>Windows WIX 二进制安装包</li>
  559. <li>runself (shell) 自编译安装包</li>
  560. <li>zip/tar.gz 二进制包</li>
  561. <li>zip/tar.gz 源码包</li>
  562. <li>RPM 二进制安装包</li>
  563. <li>SRPM 源码安装包</li>
  564. <li>DEB 二进制安装包</li>
  565. </ul>
  566. <p>下面是一个完整例子,我们可以先简单看下:</p>
  567. <pre><code class="lang-lua">set_version("1.0.0")
  568. add_rules("mode.debug", "mode.release")
  569. includes("@builtin/xpack")
  570. target("test")
  571. set_kind("binary")
  572. add_files("src/*.cpp")
  573. xpack("test")
  574. set_formats("nsis", "zip", "targz", "runself")
  575. set_title("hello")
  576. set_author("ruki")
  577. set_description("A test installer.")
  578. set_homepage("https://xmake.io")
  579. set_licensefile("LICENSE.md")
  580. add_targets("test")
  581. add_installfiles("src/(assets/*.png)", {prefixdir = "images"})
  582. add_sourcefiles("(src/**)")
  583. set_iconfile("src/assets/xmake.ico")
  584. after_installcmd(function (package, batchcmds)
  585. batchcmds:mkdir(package:installdir("resources"))
  586. batchcmds:cp("src/assets/*.txt", package:installdir("resources"), {rootdir = "src"})
  587. batchcmds:mkdir(package:installdir("stub"))
  588. end)
  589. after_uninstallcmd(function (package, batchcmds)
  590. batchcmds:rmdir(package:installdir("resources"))
  591. batchcmds:rmdir(package:installdir("stub"))
  592. end)
  593. </code></pre>
  594. <p>我们通过 <code>includes("@builtin/xpack")</code> 引入 xpack 的所有配置接口,包括 xpack 配置域,以及它的所有域接口。</p>
  595. <p>然后我们执行:</p>
  596. <pre><code class="lang-bash">$ xmake pack
  597. </code></pre>
  598. <p>即可生成所有安装包。</p>
  599. <h3 id="nsis">生成 NSIS 安装包</h3>
  600. <p>只要配置了 <code>set_formats("nsis")</code> 格式,然后执行 <code>xmake pack</code> 命令,就能生成 NSIS 格式的安装包。</p>
  601. <p>另外,xmake 还会自动安装生成 NSIS 包所需的工具,实现真正的一键打包。</p>
  602. <pre><code class="lang-bash">$ xmake pack
  603. note: install or modify (m) these packages (pass -y to skip confirm)?
  604. in xmake-repo:
  605. -> nsis 3.09
  606. please input: y (y/n/m)
  607. => install nsis 3.09 .. ok
  608. [ 25%]: compiling.release src\main.cpp
  609. [ 37%]: compiling.release src\main.cpp
  610. [ 50%]: linking.release foo.dll
  611. [ 62%]: linking.release test.exe
  612. packing build\xpack\test\test-windows-x64-v1.0.0.exe
  613. pack ok
  614. </code></pre>
  615. <p><code>test-windows-x64-v1.0.0.exe</code> 就是我们生成的安装包,双击运行它,就能安装我们的二进制文件到指定目录。</p>
  616. <p><img src="/assets/img/manual/nsis_1.png" alt=""><br><img src="/assets/img/manual/nsis_2.png" alt=""><br><img src="/assets/img/manual/nsis_3.png" alt=""></p>
  617. <h4 id="">增加组件安装</h4>
  618. <p>我们还可以给 NSIS 增加组件安装命令,只有当用户选择指定组件的时候,它的安装命令才会被执行。</p>
  619. <pre><code class="lang-lua">xpack("test")
  620. add_components("LongPath")
  621. xpack_component("LongPath")
  622. set_default(false)
  623. set_title("Enable Long Path")
  624. set_description("Increases the maximum path length limit, up to 32,767 characters (before 256).")
  625. on_installcmd(function (component, batchcmds)
  626. batchcmds:rawcmd("nsis", [[
  627. ${If} $NoAdmin == "false"
  628. ; Enable long path
  629. WriteRegDWORD ${HKLM} "SYSTEM\CurrentControlSet\Control\FileSystem" "LongPathsEnabled" 1
  630. ${EndIf}]])
  631. end)
  632. </code></pre>
  633. <p>这个例子中,我们在里面添加了一个 NSIS 特有的自定义命令,去实现对长路径的支持。</p>
  634. <p><img src="/assets/img/manual/nsis_4.png" alt=""></p>
  635. <h3 id="">生成自安装包</h3>
  636. <p>我们也可以生成基于 shell 脚本的自编译安装包。我们需要配置 runself 打包格式,然后通过 <code>add_sourcefiles</code> 添加需要参与编译安装的源文件。</p>
  637. <p>接着,我们需要自定义 on_installcmd 安装脚本,里面去配置如果编译源码包,我们可以简单的调用一个内置的编译安装脚本文件,也可以直接配置 <code>make install</code> 等编译安装命令。</p>
  638. <p>例如:</p>
  639. <pre><code class="lang-lua">xpack("test")
  640. set_formats("runself")
  641. add_sourcefiles("(src/**)")
  642. on_installcmd(function (package, batchcmds)
  643. batchcmds:runv("make", {"install"})
  644. end)
  645. </code></pre>
  646. <p>然后,我们执行 <code>xmake pack</code> 命令,就可以生成一个自安装的 xxx.gz.run 包,默认采用 gzip 压缩。</p>
  647. <pre><code class="lang-bash">$ xmake pack
  648. packing build/xpack/test/test-macosx-src-v1.0.0.gz.run
  649. pack ok
  650. </code></pre>
  651. <p>我们可以使用 sh 去加载运行它来安装我们的程序。</p>
  652. <pre><code class="lang-bash">$ sh ./build/xpack/test/test-macosx-src-v1.0.0.gz.run
  653. </code></pre>
  654. <p>我们也可以看一个比较完整的例子:</p>
  655. <pre><code class="lang-lua">xpack("xmakesrc")
  656. set_formats("runself")
  657. set_basename("xmake-v$(version)")
  658. set_prefixdir("xmake-$(version)")
  659. before_package(function (package)
  660. import("devel.git")
  661. local rootdir = path.join(os.tmpfile(package:basename()) .. ".dir", "repo")
  662. if not os.isdir(rootdir) then
  663. os.tryrm(rootdir)
  664. os.cp(path.directory(os.projectdir()), rootdir)
  665. git.clean({repodir = rootdir, force = true, all = true})
  666. git.reset({repodir = rootdir, hard = true})
  667. if os.isfile(path.join(rootdir, ".gitmodules")) then
  668. git.submodule.clean({repodir = rootdir, force = true, all = true})
  669. git.submodule.reset({repodir = rootdir, hard = true})
  670. end
  671. end
  672. local extraconf = {rootdir = rootdir}
  673. package:add("sourcefiles", path.join(rootdir, "core/**|src/pdcurses/**|src/luajit/**|src/tbox/tbox/src/demo/**"), extraconf)
  674. package:add("sourcefiles", path.join(rootdir, "xmake/**"), extraconf)
  675. package:add("sourcefiles", path.join(rootdir, "*.md"), extraconf)
  676. package:add("sourcefiles", path.join(rootdir, "configure"), extraconf)
  677. package:add("sourcefiles", path.join(rootdir, "scripts/*.sh"), extraconf)
  678. package:add("sourcefiles", path.join(rootdir, "scripts/man/**"), extraconf)
  679. package:add("sourcefiles", path.join(rootdir, "scripts/debian/**"), extraconf)
  680. package:add("sourcefiles", path.join(rootdir, "scripts/msys/**"), extraconf)
  681. end)
  682. on_installcmd(function (package, batchcmds)
  683. batchcmds:runv("./scripts/get.sh", {"__local__"})
  684. end)
  685. </code></pre>
  686. <p>它是 xmake 自身源码的安装包配置脚本,更完整的配置可以参考:<a href="https://github.com/xmake-io/xmake/blob/master/core/xpack.lua">xpack.lua</a></p>
  687. <p>这里,它通过调用源码包内置的 <code>./scripts/get.sh</code> 安装脚本去执行编译安装。</p>
  688. <h3 id="">生成源码归档包</h3>
  689. <p>另外,我们也可以配置 <code>srczip</code> 和 <code>srctargz</code> 格式,来生成源码压缩包,它不是完整的安装包,也没有安装命令,仅仅用于源码包分发。</p>
  690. <pre><code class="lang-lua">xpack("test")
  691. set_formats("srczip", "srctargz")
  692. add_sourcefiles("(src/**)")
  693. </code></pre>
  694. <pre><code class="lang-bash">$ xmake pack
  695. packing build/xpack/test/test-macosx-src-v1.0.0.zip ..
  696. packing build/xpack/test/test-macosx-src-v1.0.0.tar.gz ..
  697. pack ok
  698. </code></pre>
  699. <h3 id="">生成二进制归档包</h3>
  700. <p>我们也可以配置 <code>zip</code> 和 <code>targz</code> 来生成二进制的压缩包,它会先自动编译所有绑定的 target 目标程序,将所有需要的二进制程序,库文件打包到 zip/tar.gz 格式。</p>
  701. <p>这通常用于制作绿色版的安装包,内部不太任何自动安装脚本,用户需要自己设置 PATH 等环境变量。</p>
  702. <pre><code class="lang-lua">xpack("test")
  703. set_formats("zip", "targz")
  704. add_installfiles("(src/**)")
  705. </code></pre>
  706. <pre><code class="lang-bash">$ xmake pack
  707. packing build/xpack/test/test-macosx-v1.0.0.zip ..
  708. packing build/xpack/test/test-macosx-v1.0.0.tar.gz ..
  709. pack ok
  710. </code></pre>
  711. <p>!> 需要注意的是,打二进制文件到包里,使用的是 <code>add_installfiles</code> 而不是 <code>add_sourcefiles</code>。</p>
  712. <p>我们也可以通过 <code>add_targets</code> 去绑定需要安装的 target 目标程序和库。更多详情见下面关于 <code>add_targets</code> 的接口描述。</p>
  713. <h3 id="srpm">生成 SRPM 源码安装包</h3>
  714. <p>它可以生成 <code>.src.rpm</code> 格式的源码安装包。</p>
  715. <p>我们可以通过配置 add_targets 关联需要构建的目标,在生成的 srpm 包中,它会自动调用 <code>xmake build</code> 和 <code>xmake install</code> 去构建和安装包。</p>
  716. <pre><code class="lang-lua">xpack("test")
  717. set_homepage("https://xmake.io")
  718. set_license("Apache-2.0")
  719. set_description("A cross-platform build utility based on Lua.")
  720. set_formats("srpm")
  721. add_sourcefiles("(src/**)")
  722. add_sourcefiles("./xmake.lua")
  723. add_targets("demo")
  724. </code></pre>
  725. <p>它会生成类似下面的 spec 文件,然后自动调用 rpmbuild 去生成 <code>.src.rpm</code> 包。</p>
  726. <pre><code>Name: test
  727. Version: 1.0.0
  728. Release: 1%{?dist}
  729. Summary: hello
  730. License: Apache-2.0
  731. URL: https://xmake.io
  732. Source0: test-linux-src-v1.0.0.tar.gz
  733. BuildRequires: xmake
  734. BuildRequires: gcc
  735. BuildRequires: gcc-c++
  736. %description
  737. A test installer.
  738. %prep
  739. %autosetup -n test-1.0.0 -p1
  740. %build
  741. xmake build -y test
  742. %install
  743. xmake install -o %{buildroot}/%{_exec_prefix} test
  744. cd %{buildroot}
  745. find . -type f | sed &#39;s!^\./!/!&#39; > %{_builddir}/_installedfiles.txt
  746. %check
  747. %files -f %{_builddir}/_installedfiles.txt
  748. %changelog
  749. * Fri Dec 22 2023 ruki - 1.0.0-1
  750. - Update to 1.0.0
  751. </code></pre><p>我们也可以通过 <code>on_buildcmd</code> 和 <code>on_installcmd</code> 自定义构建和安装脚本。</p>
  752. <pre><code class="lang-lua">xpack("test")
  753. set_homepage("https://xmake.io")
  754. set_license("Apache-2.0")
  755. set_description("A cross-platform build utility based on Lua.")
  756. set_formats("srpm")
  757. add_sourcefiles("(src/**)")
  758. add_sourcefiles("./configure")
  759. on_buildcmd(function (package, batchcmds)
  760. batchcmds:runv("./configure")
  761. batchcmds:runv("make")
  762. end)
  763. on_installcmd(function (package, batchcmds)
  764. batchcmds:runv("make", {"install", "PREFIX=%{buildroot}"})
  765. end)
  766. </code></pre>
  767. <h3 id="rpm">生成 RPM 二进制安装包</h3>
  768. <p>RPM 包将会直接生成编译好的二进制安装包。xmake 会自动调用 <code>rpmbuild --rebuild</code> 命令去构建 SRPM 包生成它。</p>
  769. <p>而在 XPack 中,我们仅仅只需要配置 <code>set_formats("rpm")</code> 即可支持 rpm 包生成,其他配置与 srpm 包完全一致。</p>
  770. <pre><code class="lang-lua">xpack("test")
  771. set_formats("rpm")
  772. -- TODO
  773. </code></pre>
  774. <h3 id="">打包命令参数</h3>
  775. <h4 id="">指定打包格式</h4>
  776. <p>如果我们在配置文件中已经使用 <code>set_formats</code> 配置了多个打包格式,那么默认情况下,<code>xmake pack</code> 会自动生成所有这些格式的包。</p>
  777. <p>当然,我们也可以通过 <code>xmake pack --formats=nsis,targz</code> 来选择性指定当前需要打哪些格式的包。</p>
  778. <h4 id="">修改打包文件名</h4>
  779. <p>我们可以在配置文件中,通过 <code>set_basename()</code> 来修改包名,也可以通过命令行去修改它。</p>
  780. <pre><code class="lang-bash">$ xmake pack --basename="foo"
  781. packing build/xpack/test/foo.zip ..
  782. pack ok
  783. </code></pre>
  784. <h4 id="">指定输出目录</h4>
  785. <p>默认的输出目录是在 build 目录下,但我们也可以修改输出的路径。</p>
  786. <pre><code class="lang-bash">$ xmake pack -o /tmp/output
  787. </code></pre>
  788. <h4 id="">禁用自动构建</h4>
  789. <p>如果是打 NSIS 等二进制包,<code>xmake pack</code> 会先自动编译所有被绑定的 target 目标文件,然后再去执行打包逻辑。</p>
  790. <p>但是如果我们已经编译过了,不想每次都去编译它,而是直接去打包,可以通过下面的参数禁用自动构建。</p>
  791. <pre><code class="lang-bash">$ xmake pack --autobuild=n
  792. </code></pre>
  793. <h3 id="">接口描述</h3>
  794. <p>更多 XPack 打包接口描述见:<a href="https://xmake.io/#/zh-cn/manual/xpack">XPack 打包接口文档</a>。</p>
  795. <h2 id="">宏记录和回放</h2>
  796. <h3 id="">简介</h3>
  797. <p>我们可以通过这个插件,快速记录和回放我们平常频繁使用到的一些xmake操作,来简化我们日常的开发工作。</p>
  798. <p>它提供了一些功能:</p>
  799. <ul>
  800. <li>手动记录和回放多条执行过的xmake命令</li>
  801. <li>支持快速的匿名宏创建和回放</li>
  802. <li>支持命名宏的长久记录和重用</li>
  803. <li>支持宏脚本的批量导入和导出</li>
  804. <li>支持宏脚本的删除、显示等管理功能</li>
  805. <li>支持自定义高级宏脚本,以及参数配置</li>
  806. </ul>
  807. <h3 id="">记录操作</h3>
  808. <pre><code class="lang-console"># 开始记录宏
  809. $ xmake macro --begin
  810. # 执行一些xmake命令
  811. $ xmake f -p android --ndk=/xxx/ndk -a arm64-v8a
  812. $ xmake p
  813. $ xmake f -p mingw --sdk=/mingwsdk
  814. $ xmake p
  815. $ xmake f -p linux --sdk=/toolsdk --toolchains=/xxxx/bin
  816. $ xmake p
  817. $ xmake f -p iphoneos -a armv7
  818. $ xmake p
  819. $ xmake f -p iphoneos -a arm64
  820. $ xmake p
  821. $ xmake f -p iphoneos -a armv7s
  822. $ xmake p
  823. $ xmake f -p iphoneos -a i386
  824. $ xmake p
  825. $ xmake f -p iphoneos -a x86_64
  826. $ xmake p
  827. # 结束宏记录,这里不设置宏名字,所以记录的是一个匿名宏
  828. xmake macro --end
  829. </code></pre>
  830. <h3 id="">回放</h3>
  831. <pre><code class="lang-console"># 回放一个匿名宏
  832. $ xmake macro .
  833. </code></pre>
  834. <h3 id="">命名宏</h3>
  835. <p>匿名宏的好处就是快速记录,快速回放,如果需要长久保存,就需要给宏取个名字。</p>
  836. <pre><code class="lang-console">$ xmake macro --begin
  837. $ ...
  838. $ xmake macro --end macroname
  839. $ xmake macro macroname
  840. </code></pre>
  841. <h3 id="">导入导出宏</h3>
  842. <p>导入指定的宏脚本或者宏目录:</p>
  843. <pre><code class="lang-console">$ xmake macro --import=/xxx/macro.lua macroname
  844. $ xmake macro --import=/xxx/macrodir
  845. </code></pre>
  846. <p>导出指定的宏到脚本或者目录:</p>
  847. <pre><code class="lang-console">$ xmake macro --export=/xxx/macro.lua macroname
  848. $ xmake macro --export=/xxx/macrodir
  849. </code></pre>
  850. <h3 id="">列举显示宏</h3>
  851. <p>列举所有<code>xmake</code>内置的宏脚本:</p>
  852. <pre><code class="lang-console">$ xmake macro --list
  853. </code></pre>
  854. <p>显示指定的宏脚本内容:</p>
  855. <pre><code class="lang-console">$ xmake macro --show macroname
  856. </code></pre>
  857. <h3 id="">自定义宏脚本</h3>
  858. <p>我们也可以自己编写个宏脚本 <code>macro.lua</code> 然后导入到xmake中去。</p>
  859. <pre><code class="lang-lua">function main()
  860. os.exec("xmake f -p android --ndk=/xxx/ndk -a arm64-v8a")
  861. os.exec("xmake p")
  862. os.exec("xmake f -p mingw --sdk=/mingwsdk")
  863. os.exec("xmake p")
  864. os.exec("xmake f -p linux --sdk=/toolsdk --toolchains=/xxxx/bin")
  865. os.exec("xmake p")
  866. os.exec("xmake f -p iphoneos -a armv7")
  867. os.exec("xmake p")
  868. os.exec("xmake f -p iphoneos -a arm64")
  869. os.exec("xmake p")
  870. os.exec("xmake f -p iphoneos -a armv7s")
  871. os.exec("xmake p")
  872. os.exec("xmake f -p iphoneos -a i386")
  873. os.exec("xmake p")
  874. os.exec("xmake f -p iphoneos -a x86_64")
  875. os.exec("xmake p")
  876. end
  877. </code></pre>
  878. <p>导入到xmake,并且定义宏名字:</p>
  879. <pre><code class="lang-console">$ xmake macro --import=/xxx/macro.lua [macroname]
  880. </code></pre>
  881. <p>回放这个宏脚本:</p>
  882. <pre><code class="lang-console">$ xmake macro [.|macroname]
  883. </code></pre>
  884. <h3 id="">内置的宏脚本</h3>
  885. <p>XMake 提供了一些内置的宏脚本,来简化我们的日常开发工作。</p>
  886. <p>例如,我们可以使用 <code>package</code> 宏来对<code>iphoneos</code>平台的所有架构,一次性批量构建和打包:</p>
  887. <pre><code class="lang-console">$ xmake macro package -p iphoneos
  888. </code></pre>
  889. <h3 id="">高级的宏脚本编写</h3>
  890. <p>以上面提到的<code>package</code>宏为例,我们看下其具体代码,里面通过<code>import</code>导入一些扩展模块,实现了复杂的脚本操作。</p>
  891. <pre><code class="lang-lua">-- imports
  892. import("core.base.option")
  893. import("core.project.config")
  894. import("core.project.project")
  895. import("core.platform.platform")
  896. -- the options
  897. local options =
  898. {
  899. {&#39;p&#39;, "plat", "kv", os.host(), "Set the platform." }
  900. , {&#39;f&#39;, "config", "kv", nil, "Pass the config arguments to \"xmake config\" .." }
  901. , {&#39;o&#39;, "outputdir", "kv", nil, "Set the output directory of the package." }
  902. }
  903. -- package all
  904. --
  905. -- .e.g
  906. -- xmake m package
  907. -- xmake m package -f "-m debug"
  908. -- xmake m package -p linux
  909. -- xmake m package -p iphoneos -f "-m debug --xxx ..." -o /tmp/xxx
  910. -- xmake m package -f \"--mode=debug\"
  911. --
  912. function main(argv)
  913. -- parse arguments
  914. local args = option.parse(argv, options, "Package all architectures for the given the platform."
  915. , ""
  916. , "Usage: xmake macro package [options]")
  917. -- package all archs
  918. local plat = args.plat
  919. for _, arch in ipairs(platform.archs(plat)) do
  920. -- config it
  921. os.exec("xmake f -p %s -a %s %s -c %s", plat, arch, args.config or "", (option.get("verbose") and "-v" or ""))
  922. -- package it
  923. if args.outputdir then
  924. os.exec("xmake p -o %s %s", args.outputdir, (option.get("verbose") and "-v" or ""))
  925. else
  926. os.exec("xmake p %s", (option.get("verbose") and "-v" or ""))
  927. end
  928. end
  929. -- package universal for iphoneos, watchos ...
  930. if plat == "iphoneos" or plat == "watchos" then
  931. -- load configure
  932. config.load()
  933. -- load project
  934. project.load()
  935. -- enter the project directory
  936. os.cd(project.directory())
  937. -- the outputdir directory
  938. local outputdir = args.outputdir or config.get("buildir")
  939. -- package all targets
  940. for _, target in pairs(project.targets()) do
  941. -- get all modes
  942. local modedirs = os.match(format("%s/%s.pkg/lib/*", outputdir, target:name()), true)
  943. for _, modedir in ipairs(modedirs) do
  944. -- get mode
  945. local mode = path.basename(modedir)
  946. -- make lipo arguments
  947. local lipoargs = nil
  948. for _, arch in ipairs(platform.archs(plat)) do
  949. local archfile = format("%s/%s.pkg/lib/%s/%s/%s/%s", outputdir, target:name(), mode, plat, arch, path.filename(target:targetfile()))
  950. if os.isfile(archfile) then
  951. lipoargs = format("%s -arch %s %s", lipoargs or "", arch, archfile)
  952. end
  953. end
  954. if lipoargs then
  955. -- make full lipo arguments
  956. lipoargs = format("-create %s -output %s/%s.pkg/lib/%s/%s/universal/%s", lipoargs, outputdir, target:name(), mode, plat, path.filename(target:targetfile()))
  957. -- make universal directory
  958. os.mkdir(format("%s/%s.pkg/lib/%s/%s/universal", outputdir, target:name(), mode, plat))
  959. -- package all archs
  960. os.execv("xmake", {"l", "lipo", lipoargs})
  961. end
  962. end
  963. end
  964. end
  965. end
  966. </code></pre>
  967. <p>!> 如果你想要获取更多宏参数选项信息,请运行: <code>xmake macro --help</code></p>
  968. <h2 id="doxygen">生成doxygen文档</h2>
  969. <p>请先确保本机已安装<code>doxygen</code>工具,然后在工程目录下运行:</p>
  970. <pre><code class="lang-console">$ xmake doxygen
  971. </code></pre>
  972. </article>
  973. </body>
  974. </html>