custom_toolchain.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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/#/manual/custom_toolchain">https://xmake.io/#/manual/custom_toolchain</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>After version 2.3.4, xmake has supported custom toolchain in user&#39;s project xmake.lua, for example:</p>
  86. <pre><code class="lang-lua">-- define toolchain
  87. toolchain("myclang")
  88. -- mark as standalone toolchain
  89. set_kind("standalone")
  90. -- set toolset
  91. set_toolset("cc", "clang")
  92. set_toolset("cxx", "clang", "clang++")
  93. set_toolset("ld", "clang++", "clang")
  94. set_toolset("sh", "clang++", "clang")
  95. set_toolset("ar", "ar")
  96. set_toolset("ex", "ar")
  97. set_toolset("strip", "strip")
  98. set_toolset("mm", "clang")
  99. set_toolset("mxx", "clang", "clang++")
  100. set_toolset("as", "clang")
  101. add_defines("MYCLANG")
  102. -- check toolchain
  103. on_check(function (toolchain)
  104. return import("lib.detect.find_tool")("clang")
  105. end)
  106. -- on load
  107. on_load(function (toolchain)
  108. -- get march
  109. local march = is_arch("x86_64", "x64") and "-m64" or "-m32"
  110. -- init flags for c/c++
  111. toolchain:add("cxflags", march)
  112. toolchain:add("ldflags", march)
  113. toolchain:add("shflags", march)
  114. if not is_plat("windows") and os.isdir("/usr") then
  115. for _, includedir in ipairs({"/usr/local/include", "/usr/include"}) do
  116. if os.isdir(includedir) then
  117. toolchain:add("includedirs", includedir)
  118. end
  119. end
  120. for _, linkdir in ipairs({"/usr/local/lib", "/usr/lib"}) do
  121. if os.isdir(linkdir) then
  122. toolchain:add("linkdirs", linkdir)
  123. end
  124. end
  125. end
  126. -- init flags for objc/c++ (with ldflags and shflags)
  127. toolchain:add("mxflags", march)
  128. -- init flags for asm
  129. toolchain:add("asflags", march)
  130. end)
  131. </code></pre>
  132. <p>Then use the following command to cut to the toolchain you defined:</p>
  133. <pre><code class="lang-bash">$ xmake f --toolchain=myclang
  134. </code></pre>
  135. <p>Of course, we can also switch to the specified target directly to the custom toolchain through the <code>set_toolchains</code> interface.</p>
  136. <p>Before customizing the tool, we can run the following command to view the complete list of built-in toolchains to ensure that xmake does not provide it. If so, just use it directly. There is no need to define it yourself:</p>
  137. <pre><code class="lang-bash">$ xmake show -l toolchains
  138. </code></pre>
  139. <p>The following is a list of interfaces currently supported by the custom toolchain:</p>
  140. <table>
  141. <thead>
  142. <tr>
  143. <th>Interface</th>
  144. <th>Description</th>
  145. <th>Supported Version</th>
  146. </tr>
  147. </thead>
  148. <tbody>
  149. <tr>
  150. <td><a href="#toolchain">toolchain</a></td>
  151. <td>Define Toolchain</td>
  152. <td>>= 2.3.4</td>
  153. </tr>
  154. <tr>
  155. <td><a href="#toolchainset_kind">set_kind</a></td>
  156. <td>Set toolchain type</td>
  157. <td>>= 2.3.4</td>
  158. </tr>
  159. <tr>
  160. <td><a href="#toolchainset_toolset">set_toolset</a></td>
  161. <td>Set toolset</td>
  162. <td>>= 2.3.4</td>
  163. </tr>
  164. <tr>
  165. <td><a href="#toolchainset_sdkdir">set_sdkdir</a></td>
  166. <td>Set toolchain sdk directory path</td>
  167. <td>>= 2.3.4</td>
  168. </tr>
  169. <tr>
  170. <td><a href="#toolchainset_bindir">set_bindir</a></td>
  171. <td>Set toolchain bin directory path</td>
  172. <td>>= 2.3.4</td>
  173. </tr>
  174. <tr>
  175. <td><a href="#toolchainon_check">on_check</a></td>
  176. <td>Check toolchain</td>
  177. <td>>= 2.3.4</td>
  178. </tr>
  179. <tr>
  180. <td><a href="#toolchainonon_load">on_load</a></td>
  181. <td>Load Toolchain</td>
  182. <td>>= 2.3.4</td>
  183. </tr>
  184. <tr>
  185. <td><a href="#toolchain_end">toolchain_end</a></td>
  186. <td>End defining toolchain</td>
  187. <td>>= 2.3.4</td>
  188. </tr>
  189. <tr>
  190. <td><a href="/mirror/manual/project_target.html#targetadd_includedirs">add_includedirs</a></td>
  191. <td>Add header file search directory</td>
  192. <td>>= 2.3.4</td>
  193. </tr>
  194. <tr>
  195. <td><a href="/mirror/manual/project_target.html#targetadd_defines">add_defines</a></td>
  196. <td>Add Macro Definition</td>
  197. <td>>= 2.3.4</td>
  198. </tr>
  199. <tr>
  200. <td><a href="/mirror/manual/project_target.html#targetadd_undefines">add_undefines</a></td>
  201. <td>Cancel macro definition</td>
  202. <td>>= 2.3.4</td>
  203. </tr>
  204. <tr>
  205. <td><a href="/mirror/manual/project_target.html#targetadd_cflags">add_cflags</a></td>
  206. <td>Add c compilation option</td>
  207. <td>>= 2.3.4</td>
  208. </tr>
  209. <tr>
  210. <td><a href="/mirror/manual/project_target.html#targetadd_cxflags">add_cxflags</a></td>
  211. <td>Add c/c++ compilation options</td>
  212. <td>>= 2.3.4</td>
  213. </tr>
  214. <tr>
  215. <td><a href="/mirror/manual/project_target.html#targetadd_cxxflags">add_cxxflags</a></td>
  216. <td>Add c++ compilation options</td>
  217. <td>>= 2.3.4</td>
  218. </tr>
  219. <tr>
  220. <td><a href="/mirror/manual/project_target.html#targetadd_mflags">add_mflags</a></td>
  221. <td>Add objc compilation option</td>
  222. <td>>= 2.3.4</td>
  223. </tr>
  224. <tr>
  225. <td><a href="/mirror/manual/project_target.html#targetadd_mxflags">add_mxflags</a></td>
  226. <td>Add objc/objc++ compilation options</td>
  227. <td>>= 2.3.4</td>
  228. </tr>
  229. <tr>
  230. <td><a href="/mirror/manual/project_target.html#targetadd_mxxflags">add_mxxflags</a></td>
  231. <td>Add objc++ compilation options</td>
  232. <td>>= 2.3.4</td>
  233. </tr>
  234. <tr>
  235. <td><a href="/mirror/manual/project_target.html#targetadd_scflags">add_scflags</a></td>
  236. <td>Add swift compilation options</td>
  237. <td>>= 2.3.4</td>
  238. </tr>
  239. <tr>
  240. <td><a href="/mirror/manual/project_target.html#targetadd_asflags">add_asflags</a></td>
  241. <td>Add assembly compilation options</td>
  242. <td>>= 2.3.4</td>
  243. </tr>
  244. <tr>
  245. <td><a href="/mirror/manual/project_target.html#targetadd_gcflags">add_gcflags</a></td>
  246. <td>Add go compilation options</td>
  247. <td>>= 2.3.4</td>
  248. </tr>
  249. <tr>
  250. <td><a href="/mirror/manual/project_target.html#targetadd_dcflags">add_dcflags</a></td>
  251. <td>Add dlang compilation options</td>
  252. <td>>= 2.3.4</td>
  253. </tr>
  254. <tr>
  255. <td><a href="/mirror/manual/project_target.html#targetadd_rcflags">add_rcflags</a></td>
  256. <td>Add rust compilation option</td>
  257. <td>>= 2.3.4</td>
  258. </tr>
  259. <tr>
  260. <td><a href="/mirror/manual/project_target.html#targetadd_cuflags">add_cuflags</a></td>
  261. <td>Add cuda compilation options</td>
  262. <td>>= 2.3.4</td>
  263. </tr>
  264. <tr>
  265. <td><a href="/mirror/manual/project_target.html#targetadd_culdflags">add_culdflags</a></td>
  266. <td>Add cuda device link option</td>
  267. <td>>= 2.3.4</td>
  268. </tr>
  269. <tr>
  270. <td><a href="/mirror/manual/project_target.html#targetadd_ldflags">add_ldflags</a></td>
  271. <td>Add link options</td>
  272. <td>>= 2.3.4</td>
  273. </tr>
  274. <tr>
  275. <td><a href="/mirror/manual/project_target.html#targetadd_arflags">add_arflags</a></td>
  276. <td>Add static library archive option</td>
  277. <td>>= 2.3.4</td>
  278. </tr>
  279. <tr>
  280. <td><a href="/mirror/manual/project_target.html#targetadd_shflags">add_shflags</a></td>
  281. <td>Add dynamic library link option</td>
  282. <td>>= 2.3.4</td>
  283. </tr>
  284. <tr>
  285. <td><a href="/mirror/manual/project_target.html#targetadd_languages">add_languages</a></td>
  286. <td>Add language standards</td>
  287. <td>>= 2.3.4</td>
  288. </tr>
  289. <tr>
  290. <td><a href="/mirror/manual/project_target.html#targetadd_frameworks">add_frameworks</a></td>
  291. <td>Add Link Frame</td>
  292. <td>>= 2.3.4</td>
  293. </tr>
  294. <tr>
  295. <td><a href="/mirror/manual/project_target.html#targetadd_frameworkdirs">add_frameworkdirs</a></td>
  296. <td>Add Link Framework</td>
  297. <td>>= 2.3.4</td>
  298. </tr>
  299. </tbody>
  300. </table>
  301. <h3 id="toolchain">toolchain</h3>
  302. <h4 id="definetoolchain">Define toolchain</h4>
  303. <p>It can be defined in the user project xmake.lua, or it can be independently defined by a separate xmake.lua to specifically define various toolchains</p>
  304. <pre><code class="lang-lua">toolchain("myclang")
  305. set_toolset("cc", "clang")
  306. set_toolset("cxx", "clang", "clang++")
  307. toolchain_end()
  308. </code></pre>
  309. <h4 id="definecrosstoolchain">Define cross toolchain</h4>
  310. <p>We can also customize the configuration for different cross toolchain sdk in xmake.lua. Usually only need to specify sdkdir, xmake can automatically detect other configurations, such as cross and other information, for example:</p>
  311. <pre><code class="lang-lua">toolchain("my_toolchain")
  312. set_kind("standalone")
  313. set_sdkdir("/tmp/arm-linux-musleabi-cross")
  314. toolchain_end()
  315. target("hello")
  316. set_kind("binary")
  317. add_files("apps/hello/*.c")
  318. </code></pre>
  319. <p>This is the most streamlined cross-toolchain configuration. It only sets the corresponding SDK path, and then marks it as a complete and independent toolchain by <code>set_kind("standalone")</code>.</p>
  320. <p>At this time, we can use the command line <code>--toolchain=my_toolchain</code> to manually switch to this toolchain.</p>
  321. <pre><code class="lang-console">xmake f --toolchain=my_toolchain
  322. xmake
  323. </code></pre>
  324. <p>In addition, we can also directly bind it to the corresponding target through <code>set_toolchains</code> in xmake.lua, then only when this target is compiled, will we switch to our custom toolchain.</p>
  325. <pre><code class="lang-lua">toolchain("my_toolchain")
  326. set_kind("standalone")
  327. set_sdkdir("/tmp/arm-linux-musleabi-cross")
  328. toolchain_end()
  329. target("hello")
  330. set_kind("binary")
  331. add_files("apps/hello/*.c")
  332. set_toolchains("my_toolchain")
  333. </code></pre>
  334. <p>In this way, we no longer need to switch the toolchain manually, just execute xmake, and it will automatically switch to the my_toolchain toolchain by default.</p>
  335. <p>This is especially useful for embedded development, because there are many cross-compilation tool chains for embedded platforms, and we often need various switches to complete the compilation of different platforms.</p>
  336. <p>Therefore, we can place all toolchain definitions in a separate lua file to define, for example:</p>
  337. <pre><code>projectdir
  338. -xmake.lua
  339. -toolchains
  340. -my_toolchain1.lua
  341. -my_toolchain2.lua
  342. -...
  343. </code></pre><p>Then, we only need to introduce them through includes in xmake.lua, and bind different tool chains according to different custom platforms:</p>
  344. <pre><code class="lang-lua">includes("toolchains/*.lua")
  345. target("hello")
  346. set_kind("binary")
  347. add_files("apps/hello/*.c")
  348. if is_plat("myplat1") then
  349. set_toolchains("my_toolchain1")
  350. elseif is_plat("myplat2") then
  351. set_toolchains("my_toolchain2")
  352. end
  353. </code></pre>
  354. <p>In this way, we can quickly switch the designated platform directly when compiling to automatically switch the corresponding tool chain.</p>
  355. <pre><code class="lang-console">xmake f -p myplat1
  356. xmake
  357. </code></pre>
  358. <p>If some cross-compilation toolchains are complex in structure and automatic detection is not enough, you can use <code>set_toolset</code>, <code>set_cross</code> and <code>set_bindir</code> interfaces according to the actual situation to configure other settings in a targeted manner.</p>
  359. <p>For example, in the following example, we also added some cxflags/ldflags and the built-in system library links.</p>
  360. <pre><code class="lang-lua">toolchain("my_toolchain")
  361. set_kind("standalone")
  362. set_sdkdir("/tmp/arm-linux-musleabi-cross")
  363. on_load(function (toolchain)
  364. - add flags for arch
  365. if toolchain:is_arch("arm") then
  366. toolchain:add("cxflags", "-march=armv7-a", "-msoft-float", {force = true})
  367. toolchain:add("ldflags", "-march=armv7-a", "-msoft-float", {force = true})
  368. end
  369. toolchain:add("ldflags", "--static", {force = true})
  370. toolchain:add("syslinks", "gcc", "c")
  371. end)
  372. </code></pre>
  373. <p>For more examples of custom toolchains, we can see the following interface documents, or refer to the built-in toolchain definition in the directory of xmake source code: <a href="https://github.com/xmake-io /xmake/blob/master/xmake/toolchains/">Internal Toolchain List</a></p>
  374. <h3 id="toolchainset_kind">toolchain:set_kind</h3>
  375. <h4 id="settoolchaintype">Set toolchain type</h4>
  376. <p>Currently only supports the setting of <code>standalone</code> type, which means that the current toolchain is an independent and complete toolchain, including a complete set of tool set configurations such as cc/cxx/ld/sh/ar and other compilers, archivers, and linkers.</p>
  377. <p>Usually used when a target is set with multiple toolchains at the same time, but only one independent toolchain can be effective at the same time. This configuration can ensure that the toolchains in effect are mutually exclusive. For example, the gcc/clang toolchain will not be simultaneously. Take effect.</p>
  378. <p>However, local toolchains such as yasm/nasm belong to the extension of additional local toolchains, and there is no need to set up standalone because two toolchains of clang/yasm may exist at the same time.</p>
  379. <p>!> Just remember that the toolchain with a complete compilation environment is set to standalone</p>
  380. <h3 id="toolchainset_toolset">toolchain:set_toolset</h3>
  381. <h4 id="settoolset">Set Tool Set</h4>
  382. <p>Used to set the name and path of each individual tool, for example:</p>
  383. <pre><code class="lang-lua">toolchain("myclang")
  384. set_kind("standalone")
  385. set_toolset("cc", "clang")
  386. set_toolset("cxx", "clang", "clang++")
  387. set_toolset("ld", "clang++", "clang")
  388. set_toolset("sh", "clang++", "clang")
  389. set_toolset("ar", "ar")
  390. set_toolset("ex", "ar")
  391. set_toolset("strip", "strip")
  392. set_toolset("mm", "clang")
  393. set_toolset("mxx", "clang", "clang++")
  394. set_toolset("as", "clang")
  395. </code></pre>
  396. <p>If you provide multiple tool options, they will be searched in order.<br>For example, the following will attempt to find <code>clang</code> and will then try to use <code>gcc</code> if <code>clang</code> cannot be found:</p>
  397. <pre><code class="lang-lua"> set_toolset("cc", "clang", "gcc")
  398. </code></pre>
  399. <p>If your tool has the same name as a supported tool but a different name, you can specify this as <code>{generic tool name}@{tool name}</code>.<br>For example the following will look for a C compiler called <code>clang-mytarget</code> suffix but will then assume that the tool behaves like clang:</p>
  400. <pre><code class="lang-lua"> set_toolset("cc", "clang@clang-mytarget")
  401. </code></pre>
  402. <p>For details about this interface, you can see: <a href="/mirror/manual/project_target.html#targetset_toolset">target.set_toolset</a></p>
  403. <h3 id="toolchainset_sdkdir">toolchain:set_sdkdir</h3>
  404. <h4 id="settoolchainsdkdirectorypath">Set toolchain sdk directory path</h4>
  405. <p>Usually we can configure the sdk directory through <code>xmake f --toolchain=myclang --sdk=xxx</code>, but each time the configuration is more cumbersome, we can also pre-configure to xmake.lua through this interface to facilitate quick switching.</p>
  406. <pre><code class="lang-lua">toolchain("myclang")
  407. set_kind("standalone")
  408. set_sdkdir("/tmp/sdkdir")
  409. set_toolset("cc", "clang")
  410. </code></pre>
  411. <h3 id="toolchainset_bindir">toolchain:set_bindir</h3>
  412. <h4 id="settoolchainbindirectorypath">Set toolchain bin directory path</h4>
  413. <p>Normally, we can configure the SDK directory through <code>xmake f --toolchain=myclang --bin=xxx</code>, but each time the configuration is more cumbersome, we can also pre-configure to xmake.lua through this interface, which is convenient for quick switching.</p>
  414. <pre><code class="lang-lua">toolchain("myclang")
  415. set_kind("standalone")
  416. set_bindir("/tmp/sdkdir/bin")
  417. set_toolset("cc", "clang")
  418. </code></pre>
  419. <h3 id="toolchainon_check">toolchain:on_check</h3>
  420. <h4 id="detectiontoolchain">Detection toolchain</h4>
  421. <p>It is used to detect whether the sdk or program where the specified toolchain exists exists on the current system. It is usually used in the case of multiple standalone toolchains to automatically detect and select an effective toolchain.</p>
  422. <p>For scenes specified manually by <code>xmake f --toolchain=myclang</code>, this detection configuration is not necessary and can be omitted.</p>
  423. <pre><code class="lang-lua">toolchain("myclang")
  424. on_check(function (toolchain)
  425. return import("lib.detect.find_tool")("clang")
  426. end)
  427. </code></pre>
  428. <h3 id="toolchainon_load">toolchain:on_load</h3>
  429. <h4 id="loadtoolchain">Load toolchain</h4>
  430. <p>For some complex scenarios, we can dynamically and flexibly set various toolchain configurations in on_load, which is more flexible and powerful than setting in the description field:</p>
  431. <pre><code class="lang-lua">toolchain("myclang")
  432. set_kind("standalone")
  433. on_load(function (toolchain)
  434. -- set toolset
  435. toolchain:set("toolset", "cc", "clang")
  436. toolchain:set("toolset", "ld", "clang++")
  437. -- init flags
  438. local march = toolchain:is_arch("x86_64", "x64") and "-m64" or "-m32"
  439. toolchain:add("cxflags", march)
  440. toolchain:add("ldflags", march)
  441. toolchain:add("shflags", march)
  442. end)
  443. </code></pre>
  444. <h3 id="toolchain_end">toolchain_end</h3>
  445. <h4 id="enddefinitiontoolchain">End definition toolchain</h4>
  446. <p>This is optional, if you want to manually end the definition of toolchain, you can call it:</p>
  447. <pre><code class="lang-lua">toolchain("myclang")
  448. - ..
  449. toolchain_end()
  450. </code></pre>
  451. </article>
  452. </body>
  453. </html>