project_target.html 152 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113
  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/project_target">https://xmake.io/#/manual/project_target</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>We can use <code>target("test")</code> to define a project target named "test", each target generates an executable program, a static library, or a dynamic library.</p>
  86. <p>!> All interfaces of target can be set in the global scope, which affects all sub-targets.</p>
  87. <p>For example:</p>
  88. <pre><code class="lang-lua">-- affects both test and test2 targets
  89. add_defines("DEBUG")
  90. target("test")
  91. add_files("*.c")
  92. target("test2")
  93. add_files("*.c")
  94. </code></pre>
  95. <p>!> `target()&#39; interface can be repeatedly invoked in different places to set the same target.</p>
  96. <h3 id="target">target</h3>
  97. <h4 id="defineaprojecttarget">Define a project target</h4>
  98. <p>Defines a console target named <code>test</code> in project and the default target filename is <code>test</code>.</p>
  99. <pre><code class="lang-lua">target("test")
  100. set_kind("binary")
  101. add_files("src/*.c")
  102. </code></pre>
  103. <p>And we can call <code>target("demo")</code> repeatedly to enter the target scope for modifying it&#39;s configuration.</p>
  104. <pre><code class="lang-lua">-- defines target: demo and enter it&#39;s scope to set configuration
  105. target("demo")
  106. set_kind("binary")
  107. add_files("src/demo.c")
  108. -- defines and set `other` target
  109. target("other")
  110. ...
  111. -- re-enter demo target scope and add file `test.c` to `demo`
  112. target("demo")
  113. add_files("src/test.c")
  114. </code></pre>
  115. <p>!> All configuration in root scope affects all targets, but does not affect the configuration of <code>option()</code>.</p>
  116. <p>For example:</p>
  117. <pre><code class="lang-lua">add_defines("DEBUG")
  118. target("demo") -- add -DDEBUG
  119. set_kind("binary")
  120. add_files("src/demo.c")
  121. target("test") -- add -DDEBUG
  122. set_kind("binary")
  123. add_files("src/test.c")
  124. </code></pre>
  125. <h3 id="target_end">target_end</h3>
  126. <h4 id="endtargetdefinition">End target definition</h4>
  127. <p>This is an optional api. If not called, then all settings after<br><code>target("xxx")</code> are made for that target, unless you enter other<br><code>target</code>, <code>option</code> or <code>task</code> scope. If you want to leave the current<br><code>target</code> and enter the root scope setting, then you can use this api. For example:</p>
  128. <pre><code class="lang-lua">target("test")
  129. set_kind("static")
  130. add_files("src/*.c")
  131. target_end()
  132. -- Here we are in the root scope
  133. -- ...
  134. </code></pre>
  135. <p>If you don&#39;t call this api:</p>
  136. <pre><code class="lang-lua">target("test")
  137. set_kind("static")
  138. add_files("src/*.c")
  139. -- Here we are in the target scope above, the subsequent settings are still
  140. set for test
  141. -- ...
  142. -- Enter another target scope
  143. target("test2")
  144. ...
  145. </code></pre>
  146. <h3 id="targetset_kind">target:set_kind</h3>
  147. <h4 id="settargetkind">Set target kind</h4>
  148. <p>Set the target type, currently supported types are:</p>
  149. <table>
  150. <thead>
  151. <tr>
  152. <th>Value</th>
  153. <th>Description</th>
  154. </tr>
  155. </thead>
  156. <tbody>
  157. <tr>
  158. <td>phony</td>
  159. <td>Phony target program</td>
  160. </tr>
  161. <tr>
  162. <td>binary</td>
  163. <td>binary program</td>
  164. </tr>
  165. <tr>
  166. <td>static</td>
  167. <td>Static library program</td>
  168. </tr>
  169. <tr>
  170. <td>shared</td>
  171. <td>Dynamic library program</td>
  172. </tr>
  173. <tr>
  174. <td>object</td>
  175. <td>Only compile a collection of objects</td>
  176. </tr>
  177. <tr>
  178. <td>headeronly</td>
  179. <td>header file collection only</td>
  180. </tr>
  181. </tbody>
  182. </table>
  183. <h5 id="binary">binary</h5>
  184. <ul>
  185. <li>Executable file type</li>
  186. </ul>
  187. <pre><code class="lang-lua">target("demo")
  188. set_kind("binary")
  189. add_files("src/*.c")
  190. </code></pre>
  191. <p>!> Starting from 2.5.5, if the set_kind interface is not set, the default is binary type.</p>
  192. <p>So we simplify to:</p>
  193. <pre><code class="lang-lua">target("demo")
  194. add_files("src/*.c")
  195. </code></pre>
  196. <p>even:</p>
  197. <pre><code class="lang-lua">target("demo", {files = "src/*.c"})
  198. </code></pre>
  199. <h5 id="static">static</h5>
  200. <ul>
  201. <li>Static library target type</li>
  202. </ul>
  203. <pre><code class="lang-lua">target("demo")
  204. set_kind("static")
  205. add_files("src/*.c")
  206. </code></pre>
  207. <h5 id="shared">shared</h5>
  208. <ul>
  209. <li>Dynamic library target type</li>
  210. </ul>
  211. <pre><code class="lang-lua">target("demo")
  212. set_kind("shared")
  213. add_files("src/*.c")
  214. </code></pre>
  215. <h5 id="object">object</h5>
  216. <ul>
  217. <li>Pure object file list type</li>
  218. </ul>
  219. <p>Usually used between two target programs, part of the object file is shared, and only compiled once. It can also be used to separate the object file list and configure different compilation parameters.</p>
  220. <h5 id="phony">phony</h5>
  221. <ul>
  222. <li>Empty target type</li>
  223. </ul>
  224. <p>It is a special target program type. It does not generate any actual program files, but is only used to combine the dependencies of other target programs.</p>
  225. <pre><code class="lang-lua">target("test1")
  226. set_kind("binary")
  227. add_files("src/*.c")
  228. target("test2")
  229. set_kind("binary")
  230. add_files("src/*.c")
  231. target("demo")
  232. set_kind("phony")
  233. add_deps("test1", "test2")
  234. </code></pre>
  235. <p>For example, with the above configuration, we can compile two dependent programs at the same time: test1 and test2 when executing <code>xmake build demo</code>.</p>
  236. <h5 id="headeronly">headeronly</h5>
  237. <p>-Pure header file target type</p>
  238. <p>After 2.5.9, we added the <code>headeronly</code> target type. For target programs of this type, we will not actually compile them because it has no source files to be compiled.</p>
  239. <p>But it contains a list of header files, which are usually used for the installation of headeronly library projects, the generation of file lists for IDE projects, and the generation of cmake/pkgconfig import files during the installation phase.</p>
  240. <p>E.g:</p>
  241. <pre><code class="lang-lua">add_rules("mode.release", "mode.debug")
  242. target("foo")
  243. set_kind("headeronly")
  244. add_headerfiles("src/foo.h")
  245. add_rules("utils.install.cmake_importfiles")
  246. add_rules("utils.install.pkgconfig_importfiles")
  247. </code></pre>
  248. <p>For more details, please see: <a href="https://github.com/xmake-io/xmake/issues/1747">#1747</a></p>
  249. <h3 id="targetset_strip">target:set_strip</h3>
  250. <h4 id="striptargetsymbols">Strip target symbols</h4>
  251. <p>Set the current target strip mode, currently supports the mode:</p>
  252. <table>
  253. <thead>
  254. <tr>
  255. <th>Value</th>
  256. <th>Description</th>
  257. </tr>
  258. </thead>
  259. <tbody>
  260. <tr>
  261. <td>debug</td>
  262. <td>When you link, strip off debugging symbols</td>
  263. </tr>
  264. <tr>
  265. <td>all</td>
  266. <td>When you link, strip all symbols, including debugging symbols</td>
  267. </tr>
  268. </tbody>
  269. </table>
  270. <p>This api is generally used in release mode and can generate smaller binary programs.</p>
  271. <pre><code class="lang-lua">target("xxxx")
  272. set_strip("all")
  273. </code></pre>
  274. <p><p class="tip"><br>This api does not have to be used after the target. If no target is specified, it will be set to global mode. .<br></p>
  275. </p>
  276. <h3 id="targetset_enabled">target:set_enabled</h3>
  277. <h4 id="enableordisabletarget">Enable or disable target</h4>
  278. <p>If <code>set_enabled(false)</code> is set, the corresponding target will be directly disabled, including target loading and information acquisition, while <a href="#targetset_default">set_default</a> is just set to not compile by default, but the target can still get related information. , the default will also be loaded.</p>
  279. <h3 id="targetset_default">target:set_default</h3>
  280. <h4 id="markasdefaulttarget">Mark as default target</h4>
  281. <p>This interface is used to set whether the given project target is the default build. If this interface is not called for setting, then this target is built by default, for example:</p>
  282. <pre><code class="lang-lua">target("test1")
  283. set_default(false)
  284. target("test2")
  285. set_default(true)
  286. target("test3")
  287. ...
  288. </code></pre>
  289. <p>The three goals of the above code, when executing the <code>xmake</code>, <code>xmake install</code>, <code>xmake package</code>, <code>xmake run</code> and other commands, if you do not specify the target name, then:</p>
  290. <table>
  291. <thead>
  292. <tr>
  293. <th>Target Name</th>
  294. <th>Behavior</th>
  295. </tr>
  296. </thead>
  297. <tbody>
  298. <tr>
  299. <td>test1</td>
  300. <td>will not be built, installed, packaged, and run by default</td>
  301. </tr>
  302. <tr>
  303. <td>test2</td>
  304. <td>Default build, install, package, and run</td>
  305. </tr>
  306. <tr>
  307. <td>test3</td>
  308. <td>Default build, install, package, and run</td>
  309. </tr>
  310. </tbody>
  311. </table>
  312. <p>Through the above example, you can see that the default target can be set more than one, and it will run in turn when running.</p>
  313. <p><p class="tip"><br> Note that the <code>xmake uninstall</code> and <code>xmake clean</code> commands are not affected by this interface setting, as most users prefer to clean and unload all of them.<br></p>
  314. </p>
  315. <p>If you don&#39;t want to use the default target, you can manually specify which targets you need to build the installation:</p>
  316. <pre><code class="lang-bash">$ xmake build targetname
  317. $ xmake install targetname
  318. </code></pre>
  319. <p>If you want to force the build to install all targets, you can pass in the <code>[-a|--all]</code> parameter:</p>
  320. <pre><code class="lang-bash">$ xmake build [-a|--all]
  321. $ xmake install [-a|--all]
  322. </code></pre>
  323. <h3 id="targetset_options">target:set_options</h3>
  324. <h4 id="setconfigurationoptions">Set configuration options</h4>
  325. <p>Add option dependencies. If you have customized some options through the <a href="#option">option</a> interface, you can add associations only if you specify this option under the target target field.</p>
  326. <pre><code class="lang-lua">-- Define a hello option
  327. option("hello")
  328. set_default(false)
  329. set_showmenu(true)
  330. add_defines("HELLO_ENABLE")
  331. target("test")
  332. -- If the hello option is enabled, this time the -DHELLO_ENABLE macro will be applied to the test target.
  333. set_options("hello")
  334. </code></pre>
  335. <p>!> Some settings defined in <a href="#option">option</a> will affect this <code>target</code> target only after calling <code>set_options</code> for the association to take effect, such as macro definitions, link libraries, compile options, etc.</p>
  336. <h3 id="targetset_symbols">target:set_symbols</h3>
  337. <h4 id="setsymbolinfo">Set symbol info</h4>
  338. <p>Set the symbol mode of the target. If no target is currently defined, it will be set to the global state, affecting all subsequent targets.</p>
  339. <p>At present, we mainly support several levels:</p>
  340. <table>
  341. <thead>
  342. <tr>
  343. <th>Value</th>
  344. <th>Description</th>
  345. <th>gcc/clang</th>
  346. <th>msvc</th>
  347. </tr>
  348. </thead>
  349. <tbody>
  350. <tr>
  351. <td>debug</td>
  352. <td>Add debugging symbols</td>
  353. <td>-g</td>
  354. <td>/Zi /Pdxxx.pdb</td>
  355. </tr>
  356. <tr>
  357. <td>debug, edit</td>
  358. <td>Only for msvc, used with debug level</td>
  359. <td>Ignore</td>
  360. <td>/ZI /Pdxxx.pdb</td>
  361. </tr>
  362. <tr>
  363. <td>debug, embed</td>
  364. <td>Only for msvc, used with debug level</td>
  365. <td>Ignore</td>
  366. <td>/Z7</td>
  367. </tr>
  368. <tr>
  369. <td>hidden</td>
  370. <td>Set symbol invisible</td>
  371. <td>-fvisibility=hidden</td>
  372. <td>Ignore</td>
  373. </tr>
  374. </tbody>
  375. </table>
  376. <p>These two values can also be set at the same time, for example:</p>
  377. <pre><code class="lang-lua">-- add debug symbols, set symbols are not visible
  378. set_symbols("debug", "hidden")
  379. </code></pre>
  380. <p>If this api is not called, the debug symbol is disabled by default. .</p>
  381. <p>!> In v2.3.3 and above, you can automatically generate independent debugging symbols by setting at the same time with <code>set_strip("all")</code>. For example, for iOS programs, it is a .dSYM file, for Android and other programs, it is .sym Symbol file.</p>
  382. <p>If target sets both of the following settings, symbol file generation will be enabled</p>
  383. <pre><code class="lang-lua">target("test")
  384. set_symbols("debug")
  385. set_strip("all")
  386. </code></pre>
  387. <p>For the built-in release mode, symbol generation is not enabled by default, it is just the strip targetfile. If you want to enable it, you only need to enable the debug symbol, because mode.release internally has strip enabled by default.</p>
  388. <pre><code class="lang-lua">add_rules("mode.release")
  389. target("test")
  390. set_symbols("debug")
  391. </code></pre>
  392. <p>The ios program will generate a .dSYM file, and then Strip itself symbol</p>
  393. <pre><code class="lang-console">[62%]: linking.release libtest.dylib
  394. [62%]: generating.release test.dSYM
  395. </code></pre>
  396. <p>The android program will generate a .sym file (actually a symbolic so/binary program), and then strip itself</p>
  397. <pre><code class="lang-console">[62%]: linking.release libtest.so
  398. [62%]: generating.release test.sym
  399. </code></pre>
  400. <p>In v2.3.9 and above, two additional symbol levels, <code>edit</code> and <code>embed</code> have been added, which need to be combined with <code>debug</code> levels to further subdivide the debugging symbol format of the msvc compiler, for example:</p>
  401. <pre><code class="lang-lua">set_symbols("debug", "edit")
  402. </code></pre>
  403. <p>It will switch from the default <code>-Zi -Pdxxx.pdb</code> to <code>-ZI -Pdxxx.pdb</code> compilation option, enable <code>Edit and Continue</code> debugging symbol format information, of course, this will not affect the processing of gcc/clang, so it is Fully compatible.</p>
  404. <h3 id="targetset_basename">target:set_basename</h3>
  405. <h4 id="setthebasenameoftargetfile">Set the base name of target file</h4>
  406. <p>By default, the generated target file name is based on the value configured in <code>target("name")</code>, for example:</p>
  407. <pre><code class="lang-lua">-- The target file name is: libxxx.a
  408. target("xxx")
  409. set_kind("static")
  410. -- The target file name is: libxxx2.so
  411. target("xxx2")
  412. set_kind("shared")
  413. </code></pre>
  414. <p>The default naming method basically meets the needs of most situations, but if you want to customize the target file name sometimes</p>
  415. <p>For example, to distinguish the target name by compile mode and architecture, this time you can use this interface to set:</p>
  416. <pre><code class="lang-lua">target("xxx")
  417. set_kind("static")
  418. set_basename("xxx_$(mode)_$(arch)")
  419. </code></pre>
  420. <p>if this time, the build configuration is: <code>xmake f -m debug -a armv7</code>, then the generated file name is: <code>libxxx_debug_armv7.a</code></p>
  421. <p>If you want to further customize the directory name of the target file, refer to: <a href="#targetset_targetdir">set_targetdir</a>.</p>
  422. <p>Or implement more advanced logic by writing custom scripts, see: <a href="#targetafter_build">after_build</a> and <a href="/mirror/manual/builtin_modules.html#osmv">os.mv</a>.</p>
  423. <h3 id="targetset_filename">target:set_filename</h3>
  424. <h4 id="setthefullnameoftargetfile">Set the full name of target file</h4>
  425. <p>The difference between it and <a href="#targetset_basename">set_basename</a> is that <a href="#targetset_basename">set_basename</a> sets the name without a suffix and a prefix, for example: <code>libtest.a</code>, if the basename is changed to test2, it becomes <code>libtest2.a</code>.</p>
  426. <p>The modification of filename is to modify the entire target file name, including the prefix and suffix. For example, you can directly change <code>libtest.a</code> to <code>test.dll</code>, which is not available for <a href="#targetset_basename">set_basename</a>.</p>
  427. <h3 id="targetset_prefixname">target:set_prefixname</h3>
  428. <h4 id="settheleadingnameofthetargetfile">Set the leading name of the target file</h4>
  429. <p>Only supported after version 2.5.5, you can modify the prefix name of the target file, for example, change the default: <code>libtest.so</code> to <code>test.so</code></p>
  430. <pre><code class="lang-lua">target("test")
  431. set_prefixname("")
  432. </code></pre>
  433. <h3 id="targetset_suffixname">target:set_suffixname</h3>
  434. <h4 id="setthepostnameofthetargetfile">Set the postname of the target file</h4>
  435. <p>Only supported after version 2.5.5, you can modify the postname of the target file, for example, change the default: <code>libtest.so</code> to <code>libtest-d.so</code></p>
  436. <pre><code class="lang-lua">target("test")
  437. set_suffixname("-d")
  438. </code></pre>
  439. <h3 id="targetset_extension">target:set_extension</h3>
  440. <h4 id="settheextensionofthetargetfile">Set the extension of the target file</h4>
  441. <p>Only supported after version 2.5.5, you can modify the extension of the set target file, for example, change the default: <code>libtest.so</code> to <code>test.dll</code></p>
  442. <pre><code class="lang-lua">target("test")
  443. set_prefixname("")
  444. set_extension(".dll")
  445. </code></pre>
  446. <h3 id="targetset_warnings">target:set_warnings</h3>
  447. <h4 id="setcompilationwarninglevel">Set compilation warning level</h4>
  448. <p>Set the warning level of the compilation of the current target, generally supporting several levels:</p>
  449. <table>
  450. <thead>
  451. <tr>
  452. <th>Value</th>
  453. <th>Description</th>
  454. <th>gcc/clang</th>
  455. <th>msvc</th>
  456. </tr>
  457. </thead>
  458. <tbody>
  459. <tr>
  460. <td>none</td>
  461. <td>disable all warnings</td>
  462. <td>-w</td>
  463. <td>-W0</td>
  464. </tr>
  465. <tr>
  466. <td>less</td>
  467. <td>Enable fewer warnings</td>
  468. <td>-W1</td>
  469. <td>-W1</td>
  470. </tr>
  471. <tr>
  472. <td>more</td>
  473. <td>Enable more warnings</td>
  474. <td>-W3</td>
  475. <td>-W3</td>
  476. </tr>
  477. <tr>
  478. <td>extra</td>
  479. <td>Enable extra warnings</td>
  480. <td>-Wextra</td>
  481. <td></td>
  482. </tr>
  483. <tr>
  484. <td>pedantic</td>
  485. <td>Enable non-standard warnings</td>
  486. <td>-Wpedantic</td>
  487. <td></td>
  488. </tr>
  489. <tr>
  490. <td>all</td>
  491. <td>Enable all warnings</td>
  492. <td>-Wall</td>
  493. <td>-W3</td>
  494. </tr>
  495. <tr>
  496. <td>allextra</td>
  497. <td>Enable all warnings + extra warnings</td>
  498. <td>-Wall -Wextra</td>
  499. <td>-W4</td>
  500. </tr>
  501. <tr>
  502. <td>everything</td>
  503. <td>Enable all supported warnings</td>
  504. <td>-Wall -Wextra -Weffc++ / -Weverything</td>
  505. <td>-Wall</td>
  506. </tr>
  507. <tr>
  508. <td>error</td>
  509. <td>Use all warnings as compilation errors</td>
  510. <td>-Werror</td>
  511. <td>-WX</td>
  512. </tr>
  513. </tbody>
  514. </table>
  515. <p>The parameters of this api can be added in combination, for example:</p>
  516. <pre><code class="lang-lua">-- Enable all warnings and handle them as compilation errors
  517. set_warnings("all", "error")
  518. </code></pre>
  519. <p>If there is no target currently, calling this api will set it to global mode. .</p>
  520. <h3 id="targetset_optimize">target:set_optimize</h3>
  521. <h4 id="setcompetitionoptimizationlevel">Set competition optimization level</h4>
  522. <p>Set the compile optimization level of the target. If no target is currently set, it will be set to the global state, affecting all subsequent targets.</p>
  523. <p>At present, we mainly support several levels:</p>
  524. <table>
  525. <thead>
  526. <tr>
  527. <th>Value</th>
  528. <th>Description</th>
  529. <th>gcc/clang</th>
  530. <th>msvc</th>
  531. </tr>
  532. </thead>
  533. <tbody>
  534. <tr>
  535. <td>none</td>
  536. <td>disable optimization</td>
  537. <td>-O0</td>
  538. <td>-Od</td>
  539. </tr>
  540. <tr>
  541. <td>fast</td>
  542. <td>quick optimization</td>
  543. <td>-O1</td>
  544. <td>default</td>
  545. </tr>
  546. <tr>
  547. <td>faster</td>
  548. <td>faster optimization</td>
  549. <td>-O2</td>
  550. <td>-O2</td>
  551. </tr>
  552. <tr>
  553. <td>fastest</td>
  554. <td>Optimization of the fastest running speed</td>
  555. <td>-O3</td>
  556. <td>-Ox -fp:fast</td>
  557. </tr>
  558. <tr>
  559. <td>smallest</td>
  560. <td>Minimize code optimization</td>
  561. <td>-Os</td>
  562. <td>-O1 -GL</td>
  563. </tr>
  564. <tr>
  565. <td>aggressive</td>
  566. <td>over-optimization</td>
  567. <td>-Ofast</td>
  568. <td>-Ox -fp:fast</td>
  569. </tr>
  570. </tbody>
  571. </table>
  572. <p>E.g:</p>
  573. <pre><code class="lang-lua">-- Optimization of the fastest running speed
  574. set_optimize("fastest")
  575. </code></pre>
  576. <h3 id="targetset_languages">target:set_languages</h3>
  577. <h4 id="setsourcecodelanguagestandards">Set source code language standards</h4>
  578. <p>Set the language standard for target code compilation. If no target exists, it will be set to global mode. . .</p>
  579. <p>The supported language standards currently have the following main ones:</p>
  580. <table>
  581. <thead>
  582. <tr>
  583. <th>Value</th>
  584. <th>C language standard</th>
  585. </tr>
  586. </thead>
  587. <tbody>
  588. <tr>
  589. <td><code>ansi</code></td>
  590. <td><code>ansi</code></td>
  591. </tr>
  592. <tr>
  593. <td><code>c89</code></td>
  594. <td><code>c89</code></td>
  595. </tr>
  596. <tr>
  597. <td><code>gnu89</code></td>
  598. <td><code>gnu89</code></td>
  599. </tr>
  600. <tr>
  601. <td><code>c90</code></td>
  602. <td><code>c90</code></td>
  603. </tr>
  604. <tr>
  605. <td><code>gnu90</code></td>
  606. <td><code>gnu90</code></td>
  607. </tr>
  608. <tr>
  609. <td><code>c99</code></td>
  610. <td><code>c99</code></td>
  611. </tr>
  612. <tr>
  613. <td><code>gnu99</code></td>
  614. <td><code>gnu99</code></td>
  615. </tr>
  616. <tr>
  617. <td><code>c11</code></td>
  618. <td><code>c11</code></td>
  619. </tr>
  620. <tr>
  621. <td><code>c17</code></td>
  622. <td><code>c17</code></td>
  623. </tr>
  624. <tr>
  625. <td><code>clatest</code></td>
  626. <td><code>clatest</code></td>
  627. </tr>
  628. </tbody>
  629. </table>
  630. <table>
  631. <thead>
  632. <tr>
  633. <th>Value</th>
  634. <th>C++ language standard</th>
  635. </tr>
  636. </thead>
  637. <tbody>
  638. <tr>
  639. <td><code>cxx98</code></td>
  640. <td><code>c++98</code></td>
  641. </tr>
  642. <tr>
  643. <td><code>gnuxx98</code></td>
  644. <td><code>gnu++98</code></td>
  645. </tr>
  646. <tr>
  647. <td><code>cxx03</code></td>
  648. <td><code>c++03</code></td>
  649. </tr>
  650. <tr>
  651. <td><code>gnuxx03</code></td>
  652. <td><code>gnu++03</code></td>
  653. </tr>
  654. <tr>
  655. <td><code>cxx11</code></td>
  656. <td><code>c++11</code></td>
  657. </tr>
  658. <tr>
  659. <td><code>gnuxx11</code></td>
  660. <td><code>gnu++11</code></td>
  661. </tr>
  662. <tr>
  663. <td><code>cxx14</code></td>
  664. <td><code>c++14</code></td>
  665. </tr>
  666. <tr>
  667. <td><code>gnuxx14</code></td>
  668. <td><code>gnu++14</code></td>
  669. </tr>
  670. <tr>
  671. <td><code>cxx1z</code></td>
  672. <td><code>c++1z</code></td>
  673. </tr>
  674. <tr>
  675. <td><code>gnuxx1z</code></td>
  676. <td><code>gnu++1z</code></td>
  677. </tr>
  678. <tr>
  679. <td><code>cxx17</code></td>
  680. <td><code>c++17</code></td>
  681. </tr>
  682. <tr>
  683. <td><code>gnuxx17</code></td>
  684. <td><code>gnu++17</code></td>
  685. </tr>
  686. <tr>
  687. <td><code>cxx20</code></td>
  688. <td><code>c++20</code></td>
  689. </tr>
  690. <tr>
  691. <td><code>gnuxx20</code></td>
  692. <td><code>gnu++20</code></td>
  693. </tr>
  694. <tr>
  695. <td><code>cxx2a</code></td>
  696. <td><code>c++2a</code></td>
  697. </tr>
  698. <tr>
  699. <td><code>gnuxx2a</code></td>
  700. <td><code>gnu++2a</code></td>
  701. </tr>
  702. <tr>
  703. <td><code>cxx23</code></td>
  704. <td><code>c++23</code></td>
  705. </tr>
  706. <tr>
  707. <td><code>gnuxx23</code></td>
  708. <td><code>gnu++23</code></td>
  709. </tr>
  710. <tr>
  711. <td><code>cxx2b</code></td>
  712. <td><code>c++2b</code></td>
  713. </tr>
  714. <tr>
  715. <td><code>gnuxx2b</code></td>
  716. <td><code>gnu++2b</code></td>
  717. </tr>
  718. <tr>
  719. <td><code>cxxlatest</code></td>
  720. <td><code>c++latest</code></td>
  721. </tr>
  722. <tr>
  723. <td><code>gnuxxlatest</code></td>
  724. <td><code>gnu++latest</code></td>
  725. </tr>
  726. </tbody>
  727. </table>
  728. <p>The c standard and the c++ standard can be set at the same time, for example:</p>
  729. <pre><code class="lang-lua">-- Set c code standard: c99, c++ code standard: c++11
  730. set_languages("c99", "cxx11")
  731. </code></pre>
  732. <p>It is not that a specified standard is set, and the compiler will compile according to this standard. After all, each compiler supports different strengths, but xmake will try its best to adapt to the support standards of the current compilation tool.</p>
  733. <p>The msvc compiler does not support compiling c code according to the c99 standard, and can only support c89, but xmake supports it as much as possible, so after setting the c99 standard, xmake will force the c++ code mode to compile c code , To a certain extent, it solves the problem of compiling c99 c code under windows. .<br>The user does not need to make any additional changes.</p>
  734. <p>However, the latest msvc compilation already supports the c11/c17 standard, and xmake will not do additional special processing.</p>
  735. <h3 id="targetset_fpmodels">target:set_fpmodels</h3>
  736. <h4 id="setfloatpointcompilationmode">Set float-point compilation mode</h4>
  737. <p>This interface is used to set the floating-point compilation mode and the compilation abstract settings for mathematical calculation related optimizations. It provides several commonly used levels such as fast, strict, except, precise, etc. Some of them can be set at the same time, and some are conflicting. Effective.</p>
  738. <p>For the description of these levels, you can refer to the Microsoft document: <a href="https://docs.microsoft.com/en-us/cpp/build/reference/fp-specify-floating-point-behavior ?view=vs-2019">Specify floating-point behavior</a></p>
  739. <p>Of course, for other compilers such as gcc/icc, xmake will map to different compilation flags.</p>
  740. <pre><code class="lang-lua">set_fpmodels("fast")
  741. set_fpmodels("strict")
  742. set_fpmodels("fast", "except")
  743. set_fpmodels("precise") - default
  744. </code></pre>
  745. <p>For details about this, see: <a href="https://github.com/xmake-io/xmake/issues/981">https://github.com/xmake-io/xmake/issues/981</a></p>
  746. <h3 id="targetset_targetdir">target:set_targetdir</h3>
  747. <h4 id="setoutputdirectoriesfortargetfiles">Set output directories for target files</h4>
  748. <p>Set the output directory of the target program file. Under normal circumstances, you do not need to set it. The default output will be in the build directory.</p>
  749. <p>The build directory can be manually modified during project configuration:</p>
  750. <pre><code class="lang-bash">xmake f -o /tmp/build
  751. </code></pre>
  752. <p>After modifying to <code>/tmp/build</code>, the target file is output to <code>/tmp/build</code> by default.</p>
  753. <p>And if you use this interface to set, you don&#39;t need to change the command every time, for example:</p>
  754. <pre><code class="lang-lua">target("test")
  755. set_targetdir("/tmp/build")
  756. </code></pre>
  757. <p>!> If the display sets <code>set_targetdir</code>, then the directory specified by <code>set_targetdir</code> is preferred as the output directory of the target file.</p>
  758. <p>Starting from 3.0, we can also configure the subdirectories of the build output such as bindir, libdir, includedir, etc., for example:</p>
  759. <pre><code class="lang-lua">target("test")
  760. set_kind("shared")
  761. add_files("src/x.cpp")
  762. set_targetdir("$(builddir)/out", { bindir = "bin", libdir = "lib" })
  763. </code></pre>
  764. <h3 id="targetset_objectdir">target:set_objectdir</h3>
  765. <h4 id="setoutputdirectoriesforobjectfiles">Set output directories for object files</h4>
  766. <p>Set the output directory of the object file (<code>*.o/obj</code>) of the target target, for example:</p>
  767. <pre><code class="lang-lua">target("test")
  768. set_objectdir("$(buildir)/.objs")
  769. </code></pre>
  770. <h3 id="targetset_dependir">target:set_dependir</h3>
  771. <h4 id="setoutputdirectoriesfordependentfiles">Set output directories for dependent files</h4>
  772. <p>Set the output directory of the compile dependency file (<code>.deps</code>) of the target target, for example:</p>
  773. <pre><code class="lang-lua">target("test")
  774. set_dependir("$(buildir)/.deps")
  775. </code></pre>
  776. <h3 id="targetadd_imports">target:add_imports</h3>
  777. <h4 id="addimportsmodulesforthecustomscript">Add imports modules for the custom script</h4>
  778. <p>Usually, we can import extension modules via <code>import("core.base.task")</code> inside a custom script such as <a href="#targeton_build">on_build</a>.<br>However, in the case of a large number of custom scripts, each custom script is repeatedly imported again, which is very cumbersome. Then you can implement pre-import through this interface, for example:</p>
  779. <pre><code class="lang-lua">target("test")
  780. on_load(function (target)
  781. import("core.base.task")
  782. import("core.project.project")
  783. task.run("xxxx")
  784. end)
  785. on_build(function (target)
  786. import("core.base.task")
  787. import("core.project.project")
  788. task.run("xxxx")
  789. end)
  790. on_install(function (target)
  791. import("core.base.task")
  792. import("core.project.project")
  793. task.run("xxxx")
  794. end)
  795. </code></pre>
  796. <p>This interface can be simplified to:</p>
  797. <pre><code class="lang-lua">target("test")
  798. add_imports("core.base.task", "core.project.project")
  799. on_load(function (target)
  800. task.run("xxxx")
  801. end)
  802. on_build(function (target)
  803. task.run("xxxx")
  804. end)
  805. on_install(function (target)
  806. task.run("xxxx")
  807. end)
  808. </code></pre>
  809. <h3 id="targetadd_rules">target:add_rules</h3>
  810. <h4 id="addcustomcompilationruletotarget">Add custom compilation rule to target</h4>
  811. <p>We can extend the build support for other files by pre-setting the file suffixes supported by the rules:</p>
  812. <pre><code class="lang-lua">-- Define a build rule for a markdown file
  813. rule("markdown")
  814. set_extensions(".md", ".markdown")
  815. on_build(function (target, sourcefile)
  816. os.cp(sourcefile, path.join(target:targetdir(), path.basename(sourcefile) .. ".html"))
  817. end)
  818. target("test")
  819. set_kind("binary")
  820. -- Make the test target support the construction rules of the markdown file
  821. add_rules("markdown")
  822. -- Adding a markdown file to build
  823. add_files("src/*.md")
  824. add_files("src/*.markdown")
  825. </code></pre>
  826. <p>We can send arguments to rule in add_rules:</p>
  827. <pre><code class="lang-lua">rule("my_rule")
  828. on_load(function (target)
  829. local my_arg = target:extraconf("rules", "my_rule", "my_arg") -- "my arg"
  830. end)
  831. target("test")
  832. add_rules("my_rule", { my_arg = "my arg"})
  833. </code></pre>
  834. <p>We can also specify the application of local files to the rules, see: <a href="#targetadd_files">add_files</a>.</p>
  835. <h3 id="targeton_load">target:on_load</h3>
  836. <h4 id="runcustomloadtargetconfigurationscript">Run custom load target configuration script</h4>
  837. <p>This script will be executed when the target is initialized and loaded, and some dynamic target configurations can be made to achieve more flexible target description definitions, for example:</p>
  838. <pre><code class="lang-lua">target("test")
  839. on_load(function (target)
  840. target:add("defines", "DEBUG", "TEST=\"hello\"")
  841. target:add("linkdirs", "/usr/lib", "/usr/local/lib")
  842. target:add({includedirs = "/usr/include", "links" = "pthread"})
  843. end)
  844. </code></pre>
  845. <p>You can dynamically add various target attributes in <code>on_load</code> via <code>target:set</code>, <code>target:add</code>.</p>
  846. <h3 id="targeton_config">target:on_config</h3>
  847. <h4 id="customconfigurationscript">custom configuration script</h4>
  848. <p>After <code>xmake config</code> is executed, this script is executed before Build, which is usually used for configuration work before compilation. It differs from on_load in that on_load is executed as soon as the target is loaded, and the execution timing is earlier.</p>
  849. <p>If some configuration cannot be configured prematurely in on_load, it can be configured in on_config.</p>
  850. <p>In addition, its execution time is earlier than before_build, and the approximate execution flow is as follows:</p>
  851. <pre><code>on_load -> after_load -> on_config -> before_build -> on_build -> after_build
  852. </code></pre><h3 id="targeton_link">target:on_link</h3>
  853. <h4 id="runcustomlinktargetscript">Run custom link target script</h4>
  854. <p>This is a new interface after v2.2.7, which is used to customize the link process of the target.</p>
  855. <pre><code class="lang-lua">target("test")
  856. on_link(function (target)
  857. print("link it")
  858. end)
  859. </code></pre>
  860. <h3 id="targeton_build">target:on_build</h3>
  861. <h4 id="runcustombuildtargetscript">Run custom build target script</h4>
  862. <p>Override the target build behavior of the target target, implement a custom compilation process, in general, do not need to do this, unless you really need to do some compiler operations that xmake does not provide by default.</p>
  863. <p>You can override it by following the steps below to customize the compilation:</p>
  864. <pre><code class="lang-lua">target("test")
  865. -- Set up custom build scripts
  866. on_build(function (target)
  867. print("build it")
  868. end)
  869. </code></pre>
  870. <p>Note: After version 2.1.5, all target custom scripts can be processed separately for different platforms and architectures, for example:</p>
  871. <pre><code class="lang-lua">target("test")
  872. on_build("iphoneos|arm*", function (target)
  873. print("build for iphoneos and arm")
  874. end)
  875. </code></pre>
  876. <p>If the first parameter is a string, then it is specified in which platform_architecture the script needs to be executed, and mode matching is supported, for example, <code>arm*</code> matches all arm architectures.</p>
  877. <p>Of course, you can also set the platform only, do not set the architecture, this is to match the specified platform, execute the script:</p>
  878. <pre><code class="lang-lua">target("test")
  879. on_build("windows", function (target)
  880. print("build for windows")
  881. end)
  882. </code></pre>
  883. <p>!> Once the build process is set for this target target, the default build process for xmake will no longer be executed.</p>
  884. <h3 id="targeton_build_file">target:on_build_file</h3>
  885. <h4 id="runcustombuildsinglefilescript">Run custom build single file script</h4>
  886. <p>Through this interface, you can use hook to specify the built-in build process of the target, replacing each source file compilation process:</p>
  887. <pre><code class="lang-lua">target("test")
  888. set_kind("binary")
  889. add_files("src/*.c")
  890. on_build_file(function (target, sourcefile, opt)
  891. end)
  892. </code></pre>
  893. <p>If you don&#39;t want to rewrite the built-in build script, just add some of your own processing before and after compiling. Its utility: <a href="#targetbefore_build_file">target.before_build_file</a> and <a href="#targetafter_build_file">target.after_build_file</a> will be more convenient and you don&#39;t need to call it. Opt.origin`.</p>
  894. <h3 id="targeton_build_files">target:on_build_files</h3>
  895. <h4 id="runcustombuildfilesscript">Run custom build files script</h4>
  896. <p>Through this interface, you can use hook to specify the built-in build process of the target, and replace a batch of the same type of source file compilation process:</p>
  897. <pre><code class="lang-lua">target("test")
  898. set_kind("binary")
  899. add_files("src/*.c")
  900. on_build_files(function (target, sourcebatch, opt)
  901. end)
  902. </code></pre>
  903. <p>After setting this interface, the corresponding file in the source file list will not appear in the custom <a href="#targeton_build_file">target.on_build_file</a>, because this is an inclusion relationship.</p>
  904. <p>Where sourcebatch describes the same source files of the same type:</p>
  905. <ul>
  906. <li><code>sourcebatch.sourcekind</code>: Get the type of this batch of source files, for example: cc, as, ..</li>
  907. <li><code>sourcebatch.sourcefiles()</code>: get the list of source files</li>
  908. <li><code>sourcebatch.objectfiles()</code>: get the list of object files</li>
  909. <li><code>sourcebatch.dependfiles()</code>: Get the list of corresponding dependent files, compile dependency information in the stored source file, for example: xxx.d</li>
  910. </ul>
  911. <h3 id="targeton_clean">target:on_clean</h3>
  912. <h4 id="runcustomcleanfilesscript">Run custom clean files script</h4>
  913. <p>Override the cleanup operation of the target target&#39;s <code>xmake [c|clean}</code> to implement a custom cleanup process.</p>
  914. <pre><code class="lang-lua">target("test")
  915. -- Set up a custom cleanup script
  916. on_clean(function (target)
  917. -- Delete only target files
  918. os.rm(target:targetfile())
  919. end)
  920. </code></pre>
  921. <p>Some target interfaces are described as follows:</p>
  922. <table>
  923. <thead>
  924. <tr>
  925. <th>target interface</th>
  926. <th>description</th>
  927. </tr>
  928. </thead>
  929. <tbody>
  930. <tr>
  931. <td>target:name()</td>
  932. <td>Get the target name</td>
  933. </tr>
  934. <tr>
  935. <td>target:targetfile()</td>
  936. <td>Get the target file path</td>
  937. </tr>
  938. <tr>
  939. <td>target:get("kind")</td>
  940. <td>Get the build type of the target</td>
  941. </tr>
  942. <tr>
  943. <td>target:get("defines")</td>
  944. <td>Get the macro definition of the target</td>
  945. </tr>
  946. <tr>
  947. <td>target:get("xxx")</td>
  948. <td>Other target information set by the <code>set_/add_</code> interface can be obtained through this interface</td>
  949. </tr>
  950. <tr>
  951. <td>target:add("links", "pthread")</td>
  952. <td>Add target settings</td>
  953. </tr>
  954. <tr>
  955. <td>target:set("links", "pthread", "z")</td>
  956. <td>Override target settings</td>
  957. </tr>
  958. <tr>
  959. <td>target:deps()</td>
  960. <td>Get all dependent targets of the target</td>
  961. </tr>
  962. <tr>
  963. <td>target:dep("depname")</td>
  964. <td>Get the specified dependency target</td>
  965. </tr>
  966. <tr>
  967. <td>target:sourcebatches()</td>
  968. <td>Get a list of all source files for the target</td>
  969. </tr>
  970. </tbody>
  971. </table>
  972. <h3 id="targeton_package">target:on_package</h3>
  973. <h4 id="runcustompackagetargetscript">Run custom package target script</h4>
  974. <p>Override the target object&#39;s <code>xmake [p|package}</code> package operation to implement the custom packaging process. If you want to package the specified target into the format you want, you can customize it through this interface.</p>
  975. <p>This interface is quite practical. For example, after compiling jni, the generated so is packaged into the apk package.</p>
  976. <pre><code class="lang-lua">-- Define a test demo for an android app
  977. target("demo")
  978. -- Generate dynamic libraries: libdemo.so
  979. set_kind("shared")
  980. -- Set the output directory of the object, optional
  981. set_objectdir("$(buildir)/.objs")
  982. -- Every time you compile the build directory of libdemo.so, set it to app/libs/armeabi
  983. set_targetdir("libs/armeabi")
  984. -- Add jni code files
  985. add_files("jni/*.c")
  986. -- Set up a custom package script. After compiling libdemo.so with xmake, execute xmake p to package
  987. -- will automatically compile the app into an apk file using ant
  988. --
  989. on_package(function (target)
  990. -- Use ant to compile the app into an apk file, and redirect the output to a log file.
  991. os.run("ant debug")
  992. end)
  993. </code></pre>
  994. <h3 id="targeton_install">target:on_install</h3>
  995. <h4 id="runcustominstalltargetfilescript">Run custom install target file script</h4>
  996. <p>Override the installation of <code>xmake [i|install}</code> of the target target to implement a custom installation process.</p>
  997. <p>For example, the generated apk package will be installed.</p>
  998. <pre><code class="lang-lua">target("test")
  999. -- Set up a custom installation script to automatically install apk files
  1000. on_install(function (target)
  1001. -- Use adb to install packaged apk files
  1002. os.run("adb install -r ./bin/Demo-debug.apk")
  1003. end)
  1004. </code></pre>
  1005. <h3 id="targeton_uninstall">target:on_uninstall</h3>
  1006. <h4 id="runcustomuninstalltargetfilescript">Run custom uninstall target file script</h4>
  1007. <p>Override the uninstallation of <code>xmake [u|uninstall}</code> of the target target to implement a custom uninstall process.</p>
  1008. <pre><code class="lang-lua">target("test")
  1009. on_uninstall(function (target)
  1010. ...
  1011. end)
  1012. </code></pre>
  1013. <h3 id="targeton_run">target:on_run</h3>
  1014. <h4 id="runcustomruntargetscript">Run custom run target script</h4>
  1015. <p>Override the running operation of the target target&#39;s <code>xmake [r|run}</code> to implement a custom running process.</p>
  1016. <p>For example, run the installed apk program:</p>
  1017. <pre><code class="lang-lua">target("test")
  1018. -- Set custom run scripts, automatically run the installed app, and automatically get device output information
  1019. on_run(function (target)
  1020. os.run("adb shell am start -n com.demo/com.demo.DemoTest")
  1021. os.run("adb logcat")
  1022. end)
  1023. </code></pre>
  1024. <h3 id="targetbefore_link">target:before_link</h3>
  1025. <h4 id="runcustomscriptbeforelinkingtarget">Run custom script before linking target</h4>
  1026. <p>This is a new interface after v2.2.7 to add custom script before linking target.</p>
  1027. <pre><code class="lang-lua">target("test")
  1028. before_link(function (target)
  1029. print("")
  1030. end)
  1031. </code></pre>
  1032. <h3 id="targetbefore_build">target:before_build</h3>
  1033. <h4 id="runcustomscriptbeforebuildingtarget">Run custom script before building target</h4>
  1034. <p>It does not override the default build operation, just add some custom actions before building.</p>
  1035. <pre><code class="lang-lua">target("test")
  1036. before_build(function (target)
  1037. print("")
  1038. end)
  1039. </code></pre>
  1040. <h3 id="targetbefore_build_file">target:before_build_file</h3>
  1041. <h4 id="runcustomscriptbeforebuildingsinglefile">Run custom script before building single file</h4>
  1042. <p>Through this interface, you can use hook to specify the built-in build process of the target, and execute some custom scripts before each source file compilation process:</p>
  1043. <pre><code class="lang-lua">target("test")
  1044. set_kind("binary")
  1045. add_files("src/*.c")
  1046. before_build_file(function (target, sourcefile, opt)
  1047. end)
  1048. </code></pre>
  1049. <h3 id="targetbefore_build_files">target:before_build_files</h3>
  1050. <h4 id="runcustomscriptbeforebuildingfiles">Run custom script before building files</h4>
  1051. <p>Through this interface, you can use hook to specify the built-in build process of the target, and execute some custom scripts before a batch of source files of the same type:</p>
  1052. <pre><code class="lang-lua">target("test")
  1053. set_kind("binary")
  1054. add_files("src/*.c")
  1055. before_build_files(function (target, sourcebatch, opt)
  1056. end)
  1057. </code></pre>
  1058. <h3 id="targetbefore_clean">target:before_clean</h3>
  1059. <h4 id="runcustomscriptbeforecleaningtarget">Run custom script before cleaning target</h4>
  1060. <p>It does not override the default cleanup operation, just add some custom actions before cleaning.</p>
  1061. <pre><code class="lang-lua">target("test")
  1062. before_clean(function (target)
  1063. print("")
  1064. end)
  1065. </code></pre>
  1066. <h3 id="targetbefore_package">target:before_package</h3>
  1067. <h4 id="runcustomscriptbeforepackagingtarget">Run custom script before packaging target</h4>
  1068. <p>It does not override the default packaging operation, just add some custom operations before packaging.</p>
  1069. <pre><code class="lang-lua">target("test")
  1070. before_package(function (target)
  1071. print("")
  1072. end)
  1073. </code></pre>
  1074. <h3 id="targetbefore_install">target:before_install</h3>
  1075. <h4 id="runcustomscriptbeforeinstallingtarget">Run custom script before installing target</h4>
  1076. <p>It does not override the default installation operation, just add some custom actions before installation.</p>
  1077. <pre><code class="lang-lua">target("test")
  1078. before_install(function (target)
  1079. print("")
  1080. end)
  1081. </code></pre>
  1082. <h3 id="targetbefore_uninstall">target:before_uninstall</h3>
  1083. <h4 id="runcustomscriptbeforeuninstallingtarget">Run custom script before uninstalling target</h4>
  1084. <p>It does not override the default uninstall operation, just add some custom actions before uninstalling.</p>
  1085. <pre><code class="lang-lua">target("test")
  1086. before_uninstall(function (target)
  1087. print("")
  1088. end)
  1089. </code></pre>
  1090. <h3 id="targetbefore_run">target:before_run</h3>
  1091. <h4 id="runcustomscriptbeforerunningtarget">Run custom script before running target</h4>
  1092. <p>It does not override the default run operation, just add some custom actions before running.</p>
  1093. <pre><code class="lang-lua">target("test")
  1094. before_run(function (target)
  1095. print("")
  1096. end)
  1097. </code></pre>
  1098. <h3 id="targetafter_link">target:after_link</h3>
  1099. <h4 id="runcustomscriptafterlinkingtarget">Run custom script after linking target</h4>
  1100. <p>This is a new interface after v2.2.7 to add custom script after linking target.</p>
  1101. <pre><code class="lang-lua">target("test")
  1102. after_link(function (target)
  1103. print("")
  1104. end)
  1105. </code></pre>
  1106. <h3 id="targetafter_build">target:after_build</h3>
  1107. <h4 id="runcustomscriptafterbuildingtarget">Run custom script after building target</h4>
  1108. <p>It does not override the default build operation, just add some custom actions after the build.</p>
  1109. <p>For example, for jailbreak development of ios, after the program is built, you need to use <code>ldid</code> for signature operation.</p>
  1110. <pre><code class="lang-lua">target("test")
  1111. after_build(function (target)
  1112. os.run("ldid -S %s", target:targetfile())
  1113. end)
  1114. </code></pre>
  1115. <h3 id="targetafter_build_file">target:after_build_file</h3>
  1116. <h4 id="runcustomscriptafterbuildingsinglefile">Run custom script after building single file</h4>
  1117. <p>Through this interface, you can use hook to specify the built-in build process of the target, and execute some custom scripts after each source file compilation process:</p>
  1118. <pre><code class="lang-lua">target("test")
  1119. set_kind("binary")
  1120. add_files("src/*.c")
  1121. after_build_file(function (target, sourcefile, opt)
  1122. end)
  1123. </code></pre>
  1124. <h3 id="targetafter_build_files">target:after_build_files</h3>
  1125. <h4 id="runcustomscriptafterbuildingfiles">Run custom script after building files</h4>
  1126. <p>Through this interface, you can use hook to specify the built-in build process of the target, and execute some custom scripts after a batch of source files of the same type:</p>
  1127. <pre><code class="lang-lua">target("test")
  1128. set_kind("binary")
  1129. add_files("src/*.c")
  1130. after_build_files(function (target, sourcebatch, opt)
  1131. end)
  1132. </code></pre>
  1133. <h3 id="targetafter_clean">target:after_clean</h3>
  1134. <h4 id="runcustomscriptaftercleaningtarget">Run custom script after cleaning target</h4>
  1135. <p>It does not override the default cleanup operation, just add some custom actions after cleanup.</p>
  1136. <p>Generally used to clean up some extra temporary files automatically generated by a target. The default cleanup rules of these files may not be cleaned up.<br>To, for example:</p>
  1137. <pre><code class="lang-lua">target("test")
  1138. after_clean(function (target)
  1139. os.rm("$(buildir)/otherfiles")
  1140. end)
  1141. </code></pre>
  1142. <h3 id="targetafter_package">target:after_package</h3>
  1143. <h4 id="runcustomscriptafterpackagingtarget">Run custom script after packaging target</h4>
  1144. <p>It does not override the default packaging operation, just add some custom operations after packaging.</p>
  1145. <pre><code class="lang-lua">target("test")
  1146. after_package(function (target)
  1147. print("")
  1148. end)
  1149. </code></pre>
  1150. <h3 id="targetafter_install">target:after_install</h3>
  1151. <h4 id="runcustomscriptafterinstallingtarget">Run custom script after installing target</h4>
  1152. <p>It does not override the default installation operation, just add some custom actions after installation.</p>
  1153. <pre><code class="lang-lua">target("test")
  1154. after_install(function (target)
  1155. print("")
  1156. end)
  1157. </code></pre>
  1158. <h3 id="targetafter_uninstall">target:after_uninstall</h3>
  1159. <h4 id="runcustomscriptafteruninstallingtarget">Run custom script after uninstalling target</h4>
  1160. <p>It does not override the default uninstall operation, just add some custom actions after uninstalling.</p>
  1161. <pre><code class="lang-lua">target("test")
  1162. after_uninstall(function (target)
  1163. print("")
  1164. end)
  1165. </code></pre>
  1166. <h3 id="targetafter_run">target:after_run</h3>
  1167. <h4 id="runcustomscriptafterrunningtarget">Run custom script after running target</h4>
  1168. <p>It does not override the default run operation, just add some custom actions after the run.</p>
  1169. <pre><code class="lang-lua">target("test")
  1170. after_run(function (target)
  1171. print("")
  1172. end)
  1173. </code></pre>
  1174. <h3 id="targetset_pcheader">target:set_pcheader</h3>
  1175. <h4 id="setprecompiledcheaderfile">Set pre-compiled c header file</h4>
  1176. <p>Xmake supports accelerating c program compilation by precompiling header files. Currently supported compilers are: gcc, clang, and msvc.</p>
  1177. <p>The usage is as follows:</p>
  1178. <pre><code class="lang-lua">target("test")
  1179. set_pcheader("header.h")
  1180. </code></pre>
  1181. <h3 id="targetset_pcxxheader">target:set_pcxxheader</h3>
  1182. <h4 id="setprecompiledcheaderfile">Set pre-compiled c++ header file</h4>
  1183. <p>Xmake supports precompiled header files to speed up C++ program compilation. Currently supported compilers are: gcc, clang, and msvc.</p>
  1184. <p>The usage is as follows:</p>
  1185. <pre><code class="lang-lua">target("test")
  1186. set_pcxxheader("header.h")
  1187. </code></pre>
  1188. <h3 id="targetset_pmheader">target:set_pmheader</h3>
  1189. <h4 id="setprecompiledobjcheaderfile">Set pre-compiled objc header file</h4>
  1190. <p>Xmake supports accelerating objc program compilation by precompiling header files. Currently supported compilers are: gcc, clang, and msvc.</p>
  1191. <p>The usage is as follows:</p>
  1192. <pre><code class="lang-lua">target("test")
  1193. set_pmheader("header.h")
  1194. </code></pre>
  1195. <h3 id="targetset_pmxxheader">target:set_pmxxheader</h3>
  1196. <h4 id="setprecompiledobjcheaderfile">Set pre-compiled objc++ header file</h4>
  1197. <p>Xmake supports precompiled header files to speed up ObjC++ program compilation. Currently supported compilers are: gcc, clang, and msvc.</p>
  1198. <p>The usage is as follows:</p>
  1199. <pre><code class="lang-lua">target("test")
  1200. set_pmxxheader("header.h")
  1201. </code></pre>
  1202. <h3 id="targetadd_deps">target:add_deps</h3>
  1203. <h4 id="addtargetdependencies">Add target dependencies</h4>
  1204. <p>Add the dependency target of the current target. When compiling, it will first compile the target of the dependency and then compile the current target. . .</p>
  1205. <pre><code class="lang-lua">target("test1")
  1206. set_kind("static")
  1207. set_files("*.c")
  1208. target("test2")
  1209. set_kind("static")
  1210. set_files("*.c")
  1211. target("demo")
  1212. add_deps("test1", "test2")
  1213. </code></pre>
  1214. <p>In the above example, when compiling the target demo, you need to compile the test1 and test2 targets first, because the demo will use them.</p>
  1215. <p><p class="tip"><br>The target will automatically inherit the configuration and properties in the dependent target. You don&#39;t need to call the interfaces <code>add_links</code>, <code>add_linkdirs</code> and <code>add_rpathdirs</code> to associate the dependent targets.<br></p>
  1216. </p>
  1217. <p>And the inheritance relationship is to support cascading, for example:</p>
  1218. <pre><code class="lang-lua">target("library1")
  1219. set_kind("static")
  1220. add_files("*.c")
  1221. add_includedirs("inc") -- The default private header file directory will not be inherited
  1222. add_includedirs("inc1", {public = true}) -- The header file related directory here will also be inherited
  1223. target("library2")
  1224. set_kind("static")
  1225. add_deps("library1")
  1226. add_files("*.c")
  1227. target("test")
  1228. set_kind("binary")
  1229. add_deps("library2")
  1230. </code></pre>
  1231. <p>If we don&#39;t want to inherit any configuration that depends on the target, what should we do?</p>
  1232. <pre><code class="lang-lua">add_deps("dep1", "dep2", {inherit = false})
  1233. </code></pre>
  1234. <p>By explicitly setting the inherit configuration, tell xmake whether the two dependent configurations need to be inherited. If not set, the default is to enable inheritance.</p>
  1235. <p>After version 2.2.5, you can set public to true by <code>add_includedirs("inc1", {public = true})</code>, and expose the settings of includers to other dependent child targets.</p>
  1236. <p>At present, for the target compilation link flags related interface settings, support for inheritance properties, you can artificially control whether you need to export to other targets to rely on inheritance, the currently supported properties are:</p>
  1237. <table>
  1238. <thead>
  1239. <tr>
  1240. <th>Attribute</th>
  1241. <th>Description</th>
  1242. </tr>
  1243. </thead>
  1244. <tbody>
  1245. <tr>
  1246. <td>private</td>
  1247. <td>The default setting, as the private configuration of the current target, will not be inherited by other targets that depend on</td>
  1248. </tr>
  1249. </tbody>
  1250. </table>
  1251. <p>Public | public configuration, current target, dependent child targets will be set |<br>Interface | interface settings, only inherited by the dependent child target, the current target does not participate |</p>
  1252. <p>For a detailed description of this, you can look at it: <a href="https://github.com/xmake-io/xmake/issues/368">https://github.com/xmake-io/xmake/issues/368</a></p>
  1253. <h3 id="targetadd_links">target:add_links</h3>
  1254. <h4 id="addlinklibraries">Add link libraries</h4>
  1255. <p>Add a link library for the current target, which is usually paired with <a href="#targetadd_linkdirs">add_linkdirs</a>.</p>
  1256. <pre><code class="lang-lua">target("demo")
  1257. -- Add a link to libtest.a, equivalent to -ltest
  1258. add_links("test")
  1259. -- Add link search directory
  1260. add_linkdirs("$(buildir)/lib")
  1261. </code></pre>
  1262. <p>Starting with version 2.8.1, add_links also supports adding the full path to the library, e.g. <code>add_links("/tmp/libfoo.a")</code>, explicitly specifying the library file.</p>
  1263. <h3 id="targetadd_syslinks">target:add_syslinks</h3>
  1264. <h4 id="addsystemlinklibraries">Add system link libraries</h4>
  1265. <p>This interface is similar to <a href="#targetadd_links">add_links</a>. The only difference is that the link library added through this interface is in the order of all <code>add_links</code>.</p>
  1266. <p>Therefore, it is mainly used to add system library dependencies, because the link order of the system libraries is very backward, for example:</p>
  1267. <pre><code class="lang-lua">add_syslinks("pthread", "m", "dl")
  1268. target("demo")
  1269. add_links("a", "b")
  1270. add_linkdirs("$(buildir)/lib")
  1271. </code></pre>
  1272. <p>The above configuration, even if <code>add_syslinks</code> is set in advance, the final link order is still: <code>-la -lb -lpthread -lm -ldl</code></p>
  1273. <h3 id="targetadd_linkorders">target:add_linkorders</h3>
  1274. <h4 id="adjustlinkorder">Adjust link order</h4>
  1275. <p>This is a feature only supported by xmake 2.8.5 and later, and is mainly used to adjust the link order within the target.</p>
  1276. <p>Since xmake provides <code>add_links</code>, <code>add_deps</code>, <code>add_packages</code>, <code>add_options</code> interfaces, you can configure targets, dependencies, links in packages and options.</p>
  1277. <p>However, the link order between them was previously less controllable and could only be generated in a fixed order, which was a bit overwhelming for some complex projects.</p>
  1278. <p>For more details and background see: <a href="https://github.com/xmake-io/xmake/issues/1452">#1452</a></p>
  1279. <h5 id="sortlinks">Sort links</h5>
  1280. <p>In order to more flexibly adjust the various link orders within the target, we have added the <code>add_linkorders</code> interface, which is used to configure various link orders introduced by the target, dependencies, packages, options, and link groups.</p>
  1281. <p>For example:</p>
  1282. <pre><code class="lang-lua">add_links("a", "b", "c", "d", "e")
  1283. -- e -> b -> a
  1284. add_linkorders("e", "b", "a")
  1285. --e->d
  1286. add_linkorders("e", "d")
  1287. </code></pre>
  1288. <p>add_links is the configured initial link order, and then we configure two local link dependencies <code>e -> b -> a</code> and <code>e -> d</code> through add_linkorders.</p>
  1289. <p>xmake will internally generate a DAG graph based on these configurations, and use topological sorting to generate the final link sequence and provide it to the linker.</p>
  1290. <p>Of course, if there is a circular dependency and a cycle is created, it will also provide warning information.</p>
  1291. <h5 id="sortinglinksandlinkgroups">Sorting links and link groups</h5>
  1292. <p>In addition, we can also solve the problem of circular dependencies by configuring link groups through <code>add_linkgroups</code>.</p>
  1293. <p>And <code>add_linkorders</code> can also sort link groups.</p>
  1294. <pre><code class="lang-lua">add_links("a", "b", "c", "d", "e")
  1295. add_linkgroups("c", "d", {name = "foo", group = true})
  1296. add_linkorders("e", "linkgroup::foo")
  1297. </code></pre>
  1298. <p>If we want to sort link groups, we need to give each link group a name, <code>{name = "foo"}</code>, and then we can reference the configuration through <code>linkgroup::foo</code> in <code>add_linkorders</code>.</p>
  1299. <p>Version 2.9.6 adds the as_needed configuration item, which can be used to disable as_needed. (Not configured by default, that is, enabled.)</p>
  1300. <pre><code class="lang-lua">add_linkgroups("c", "d", {as_needed = false})
  1301. </code></pre>
  1302. <p>The corresponding flags are as follows.</p>
  1303. <pre><code class="lang-bash">-Wl,--no-as-needed c d -Wl,--as-needed
  1304. </code></pre>
  1305. <h5 id="sortlinksandframeworks">Sort links and frameworks</h5>
  1306. <p>We can also sort links and frameworks for macOS/iPhoneOS.</p>
  1307. <pre><code class="lang-lua">add_links("a", "b", "c", "d", "e")
  1308. add_frameworks("Foundation", "CoreFoundation")
  1309. add_linkorders("e", "framework::CoreFoundation")
  1310. </code></pre>
  1311. <h5 id="completeexample">Complete example</h5>
  1312. <p>For a complete example, we can look at:</p>
  1313. <pre><code class="lang-lua">add_rules("mode.debug", "mode.release")
  1314. add_requires("libpng")
  1315. target("bar")
  1316. set_kind("shared")
  1317. add_files("src/foo.cpp")
  1318. add_linkgroups("m", "pthread", {whole = true})
  1319. target("foo")
  1320. set_kind("static")
  1321. add_files("src/foo.cpp")
  1322. add_packages("libpng", {public = true})
  1323. target("demo")
  1324. set_kind("binary")
  1325. add_deps("foo")
  1326. add_files("src/main.cpp")
  1327. if is_plat("linux", "macosx") then
  1328. add_syslinks("pthread", "m", "dl")
  1329. end
  1330. if is_plat("macosx") then
  1331. add_frameworks("Foundation", "CoreFoundation")
  1332. end
  1333. add_linkorders("framework::Foundation", "png16", "foo")
  1334. add_linkorders("dl", "linkgroup::syslib")
  1335. add_linkgroups("m", "pthread", {name = "syslib", group = true})
  1336. </code></pre>
  1337. <p>The complete project is at: <a href="https://github.com/xmake-io/xmake/blob/master/tests/projects/c%2B%2B/linkorders/xmake.lua">linkorders example</a></p>
  1338. <h3 id="targetadd_linkgroups">target:add_linkgroups</h3>
  1339. <h4 id="addlinkgroup">Add link group</h4>
  1340. <p>This is a feature only supported by versions after xmake 2.8.5. This link group feature is currently mainly used for compilation on the Linux platform and only supports the gcc/clang compiler.</p>
  1341. <p>It should be noted that the concept of link group in gcc/clang mainly refers to: <code>-Wl,--start-group</code></p>
  1342. <p>xmake is aligned and encapsulated, further abstracted, and is not only used to process <code>-Wl,--start-group</code>, but also <code>-Wl,--whole-archive</code> and <code>-Wl,-Bstatic</code> .</p>
  1343. <p>Below we will explain them one by one.</p>
  1344. <p>For more details, see: <a href="https://github.com/xmake-io/xmake/issues/1452">#1452</a></p>
  1345. <h5 id="startgroupsupport">--start-group support</h5>
  1346. <p><code>-Wl,--start-group</code> and <code>-Wl,--end-group</code> are linker options for handling complex library dependencies, ensuring that the linker can resolve symbolic dependencies and successfully connect multiple libraries.</p>
  1347. <p>In xmake, we can achieve this in the following way:</p>
  1348. <pre><code class="lang-lua">add_linkgroups("a", "b", {group = true})
  1349. </code></pre>
  1350. <p>It will generate the corresponding <code>-Wl,--start-group -la -lb -Wl,--end-group</code> link options.</p>
  1351. <p>If there is a symbolic circular dependency between libraries a and b, no link error will be reported and the link can be successful.</p>
  1352. <p>For unsupported platforms and compilations, it will fall back to <code>-la -lb</code></p>
  1353. <h5 id="wholearchivesupport">--whole-archive support</h5>
  1354. <p><code>--whole-archive</code> is a linker option commonly used when dealing with static libraries.<br>Its function is to tell the linker to include all object files in the specified static library into the final executable file, not just the object files that satisfy the current symbol dependencies.<br>This can be used to ensure that all code for certain libraries is linked, even if they are not directly referenced in the current symbol dependencies.</p>
  1355. <p>For more information, please refer to the gcc/clang documentation.</p>
  1356. <p>In xmake, we can achieve this in the following way:</p>
  1357. <pre><code class="lang-lua">add_linkgroups("a", "b", {whole = true})
  1358. </code></pre>
  1359. <p>It will generate the corresponding <code>-Wl,--whole-archive -la -lb -Wl,--no-whole-archive</code> link options.</p>
  1360. <p>For unsupported platforms and compilations, it will fall back to <code>-la -lb</code></p>
  1361. <p>Additionally, we can configure group/whole at the same time:</p>
  1362. <pre><code class="lang-lua">add_linkgroups("a", "b", {whole = true, group = true})
  1363. </code></pre>
  1364. <h5 id="bstaticsupport">-Bstatic support</h5>
  1365. <p><code>-Bstatic</code> is also an option for compilers (such as gcc) to instruct the compiler to use only static libraries and not shared libraries when linking.</p>
  1366. <p>For more information, please refer to the gcc/clang documentation.</p>
  1367. <p>In xmake, we can achieve this in the following way:</p>
  1368. <pre><code class="lang-lua">add_linkgroups("a", "b", {static = true})
  1369. </code></pre>
  1370. <p>It will generate the corresponding <code>-Wl,-Bstatic -la -lb -Wl,-Bdynamic</code> linkage options.</p>
  1371. <h3 id="targetadd_files">target:add_files</h3>
  1372. <h4 id="addsourcefiles">Add source files</h4>
  1373. <p>Source files used to add target projects, even library files, some file types currently supported:</p>
  1374. <table>
  1375. <thead>
  1376. <tr>
  1377. <th>Supported source file types</th>
  1378. <th>Description</th>
  1379. </tr>
  1380. </thead>
  1381. <tbody>
  1382. <tr>
  1383. <td>.c/.cpp/.cc/.cxx</td>
  1384. <td>c++ file</td>
  1385. </tr>
  1386. <tr>
  1387. <td>.s/.S/.asm</td>
  1388. <td>assembly files</td>
  1389. </tr>
  1390. <tr>
  1391. <td>.m/.mm</td>
  1392. <td>objc file</td>
  1393. </tr>
  1394. <tr>
  1395. <td>.swift</td>
  1396. <td>swift file</td>
  1397. </tr>
  1398. <tr>
  1399. <td>.go</td>
  1400. <td>golang file</td>
  1401. </tr>
  1402. <tr>
  1403. <td>.o/.obj</td>
  1404. <td>object File</td>
  1405. </tr>
  1406. <tr>
  1407. <td>.a/.lib</td>
  1408. <td>static library files, will automatically merge the library to the target program</td>
  1409. </tr>
  1410. <tr>
  1411. <td>.rc</td>
  1412. <td>msvc resource file</td>
  1413. </tr>
  1414. <tr>
  1415. <td>.manifest</td>
  1416. <td>windows manifest file</td>
  1417. </tr>
  1418. <tr>
  1419. <td>.dll</td>
  1420. <td>windows export file</td>
  1421. </tr>
  1422. <tr>
  1423. <td>.ld/.lds</td>
  1424. <td>linker scripts file for gcc/clang</td>
  1425. </tr>
  1426. <tr>
  1427. <td>.map/.ver</td>
  1428. <td>version script file for gcc/clang</td>
  1429. </tr>
  1430. </tbody>
  1431. </table>
  1432. <p>The wildcard <code>*</code> indicates that the file in the current directory is matched, and <code>**</code> matches the file in the multi-level directory.</p>
  1433. <p>E.g:</p>
  1434. <pre><code class="lang-lua">add_files("src/test_*.c")
  1435. add_files("src/xxx/**.cpp")
  1436. add_files("src/asm/*.S", "src/objc/**/hello.m")
  1437. </code></pre>
  1438. <p>The use of <code>add_files</code> is actually quite flexible and convenient. Its matching mode draws on the style of premake, but it has been improved and enhanced.</p>
  1439. <p>This makes it possible to not only match files, but also to filter out a batch of files in the specified mode while adding files.</p>
  1440. <p>E.g:</p>
  1441. <pre><code class="lang-lua">-- Recursively add all c files under src, but not all c files under src/impl/
  1442. add_files("src/**.c|impl/*.c")
  1443. -- Add all cpp files under src, but not including src/test.cpp, src/hello.cpp, and all cpp files with xx_ prefix under src
  1444. add_files("src/*.cpp|test.cpp|hello.cpp|xx_*.cpp")
  1445. </code></pre>
  1446. <p>The separators after the <code>`</code>are all files that need to be excluded. These files also support the matching mode, and you can add multiple filtering modes at the same time, as long as the middle is separated by <code>|</code>. .</p>
  1447. <p>One of the benefits of supporting the filtering of some files when adding files is that they provide the basis for subsequent file additions based on different switching logic.</p>
  1448. <p><p class="tip"><br>In order to make the description more streamlined, the filter descriptions after <code>|</code> are based on a schema: the directory before <code>*</code> in <code>src/*.cpp</code>.<br>So the above example is filtered after the file under src, this is to pay attention to.<br></p>
  1449. </p>
  1450. <p>After version 2.1.6, <code>add_files</code> has been improved to support more fine-grained compilation option controls based on files, such as:</p>
  1451. <pre><code class="lang-lua">target("test")
  1452. add_defines("TEST1")
  1453. add_files("src/*.c")
  1454. add_files("test/*.c", "test2/test2.c", {defines = "TEST2", languages = "c99", includedirs = ".", cflags = "-O0"})
  1455. </code></pre>
  1456. <p>You can pass a configuration table in the last parameter of <code>add_files</code> to control the compilation options of the specified files. The configuration parameters are consistent with the target, and these files will also inherit the target&#39;s common configuration <code>-DTEST1</code>.</p>
  1457. <p>After version 2.1.9, support for adding unknown code files, by setting rule custom rules, to achieve custom build of these files, for example:</p>
  1458. <pre><code class="lang-lua">target("test")
  1459. -- ...
  1460. add_files("src/test/*.md", {rule = "markdown"})
  1461. </code></pre>
  1462. <p>After version 2.3.1, you can use the sourcekind parameter to force the use of the C or C++ compiler:</p>
  1463. <pre><code class="lang-lua">add_files("*.c", {sourcekind = "cxx"}) -- force to compile as c++
  1464. add_files("*.cpp", {sourcekind = "cc"}) -- force to compile as c
  1465. </code></pre>
  1466. <p>For instructions on using custom build rules, see: <a href="#Building Rules">Building Rules</a>.</p>
  1467. <p>And after the 2.1.9 version, you can use the force parameter to force the automatic detection of cxflags, cflags and other compile options, directly into the compiler, even if the compiler may not support, it will also be set:</p>
  1468. <pre><code class="lang-lua">add_files("src/*.c", {force = {cxflags = "-DTEST", mflags = "-framework xxx"}})
  1469. </code></pre>
  1470. <h3 id="targetremove_files">target:remove_files</h3>
  1471. <h4 id="removesourcefiles">Remove source files</h4>
  1472. <p>Through this interface, you can delete the specified file from the list of files added by the <a href="targetadd_files">add_files</a> interface, for example:</p>
  1473. <pre><code class="lang-lua">target("test")
  1474. add_files("src/*.c")
  1475. remove_files("src/test.c")
  1476. </code></pre>
  1477. <p>In the above example, you can add all files except <code>test.c</code> from the <code>src</code> directory. Of course, this can also be done by `add_files("src/*.c|test.c").To achieve the same purpose, but this way is more flexible.</p>
  1478. <p>For example, we can conditionally determine which files to delete, and this interface also supports the matching mode of <a href="targetadd_files">add_files</a>, filtering mode, and bulk removal.</p>
  1479. <pre><code class="lang-lua">target("test")
  1480. add_files("src/**.c")
  1481. remove_files("src/test*.c")
  1482. remove_files("src/subdir/*.c|xxx.c")
  1483. if is_plat("iphoneos") then
  1484. add_files("xxx.m")
  1485. end
  1486. </code></pre>
  1487. <p>Through the above example, we can see that <code>add_files</code> and <code>remove_files</code> are added and deleted sequentially according to the calling sequence, and deleted by <code>remove_files("src/subdir/*.c|xxx.c")</code> Batch file,<br>And exclude <code>src/subdir/xxx.c</code> (that is, don&#39;t delete this file).</p>
  1488. <p>Note: This interface is only available in version v2.6.3. The previous version was del_files, which has been abandoned.</p>
  1489. <p>If you want to be compatible with the previous version, you can solve it through the following configuration.</p>
  1490. <pre><code class="lang-lua">remove_files = remove_files or del_files
  1491. </code></pre>
  1492. <h3 id="targetremove_headerfiles">target:remove_headerfiles</h3>
  1493. <h4 id="removethespecifiedfilefromtheprecedinglistofheaderfiles">Remove the specified file from the preceding list of header files</h4>
  1494. <p>Mainly used to remove files from the list of header files set by <code>add_headerfiles</code>, similar to <code>remove_files</code>.</p>
  1495. <p>This interface is only provided in v2.6.3 version.</p>
  1496. <h3 id="targetadd_linkdirs">target:add_linkdirs</h3>
  1497. <h4 id="addlinksearchdirectories">Add link search directories</h4>
  1498. <p>Set the search directory of the link library. This interface is used as follows:</p>
  1499. <pre><code class="lang-lua">target("test")
  1500. add_linkdirs("$(buildir)/lib")
  1501. </code></pre>
  1502. <p>This interface is equivalent to gcc&#39;s <code>-Lxxx</code> link option.</p>
  1503. <p>Generally, it is used together with <a href="#targetadd_links">add_links</a>. Of course, it can also be added directly through the <a href="#targetadd_ldflags">add_ldflags</a> or <a href="#targetadd_shflags">add_shflags</a> interface. It is also possible.</p>
  1504. <p><p class="tip"><br>If you don&#39;t want to write to death in the project, you can set it by: <code>xmake f --linkdirs=xxx</code> or <code>xmake f --ldflags="-L/xxx"</code>, of course, this manually set directory search priority. higher.<br></p>
  1505. </p>
  1506. <h3 id="targetadd_rpathdirs">target:add_rpathdirs</h3>
  1507. <h4 id="addloadsearchdirectoriesfordynamiclibraries">Add load search directories for dynamic libraries</h4>
  1508. <p>After <a href="#targetadd_linkdirs">add_linkdirs</a> sets the link search directory of the dynamic library, the program is normally linked, but in the Linux platform, if you want to run the compiled program normally, it will report that the dynamic library fails to be loaded.</p>
  1509. <p>Because the dynamic library&#39;s load directory is not found, if you want to run the program that depends on the dynamic library, you need to set the <code>LD_LIBRARY_PATH</code> environment variable to specify the dynamic library directory to be loaded.</p>
  1510. <p>However, this method is global, and the impact is too wide. The better way is to set the dynamic library search path to be loaded when the linker is set by the linker option of <code>-rpath=xxx</code>, and xmake does it. Encapsulation, better handling cross-platform issues with <code>add_rpathdirs</code>.</p>
  1511. <p>The specific use is as follows:</p>
  1512. <pre><code class="lang-lua">target("test")
  1513. set_kind("binary")
  1514. add_linkdirs("$(buildir)/lib")
  1515. add_rpathdirs("$(buildir)/lib")
  1516. </code></pre>
  1517. <p>Just need to set the rpath directory when linking, although the same purpose can be achieved by <code>add_ldflags("-Wl,-rpath=xxx")</code>, but this interface is more general.</p>
  1518. <p>Internally, different platforms will be processed. For example, under macOS, the <code>-rpath</code> setting is not required, and the running program can be loaded normally. Therefore, for this platform, xmake internally ignores the setting directly to avoid link error.</p>
  1519. <p>When doing dynamic library linking for dlang programs, xmake will automatically process it into <code>-L-rpath=xxx</code> to pass in the linker of dlang, thus avoiding the need to directly use <code>add_ldflags</code> to determine and handle different platforms and compile. Problem.</p>
  1520. <p>The 2.1.7 version has improved this interface, supporting: <code>@loader_path</code>, <code>@executable_path</code> and <code>$ORIGIN</code> built-in variables to specify the program&#39;s load directory. Their effects are basically the same, mainly for Also compatible with macho, elf.</p>
  1521. <p>E.g:</p>
  1522. <pre><code class="lang-lua">target("test")
  1523. set_kind("binary")
  1524. add_linkdirs("$(buildir)/lib")
  1525. add_rpathdirs("@loader_path/lib")
  1526. </code></pre>
  1527. <p>Specify the test program to load the dynamic library file of <code>lib/*.[so|dylib]</code> in the current execution directory, which will help to improve the portability of the program without writing dead absolute paths and relative paths, resulting in program and directory switching. Causes the program to load the dynamic library failed.</p>
  1528. <p>!> It should be noted that under macos, if the add_rpathdirs setting is in effect, you need to do some preprocessing on dylib and add the <code>@rpath/xxx</code> path setting:<br><code>$install_name_tool -add_rpath @rpath/libxxx.dylib xxx/libxxx.dylib</code><br>We can also check if there is a path with @rpath via <code>otool -L libxxx.dylib</code></p>
  1529. <p>In addition, for gcc, <code>add_rpathdirs</code> defaults to runpath. If you want to configure it explicitly, use <code>-Wl,--enable-new-dtags</code>, <code>-Wl,--disable-new-dtags</code> to configure rpath. Or runpath</p>
  1530. <p>We can specify it through additional parameters, <code>add_rpathdirs("xxx", {runpath = true})</code></p>
  1531. <p>For relevant background details, see: <a href="https://github.com/xmake-io/xmake/issues/5109">#5109</a></p>
  1532. <p>After 2.9.4, we added <code>add_rpathdirs("xxx", {install_only = true})</code>, which can configure the installed rpath path separately.</p>
  1533. <h3 id="targetadd_includedirs">target:add_includedirs</h3>
  1534. <h4 id="addincludesearchdirectories">Add include search directories</h4>
  1535. <p>Set the search directory for the header file. This interface is used as follows:</p>
  1536. <pre><code class="lang-lua">target("test")
  1537. add_includedirs("$(buildir)/include")
  1538. </code></pre>
  1539. <p>Of course, it can also be set directly through interfaces such as <a href="#targetadd_cxflags">add_cxflags</a> or <a href="#targetadd_mxflags">add_mxflags</a>, which is also possible.</p>
  1540. <p>After 2.2.5, includedirs can be exported to dependent child targets via the extra <code>{public|interface = true}</code> property setting, for example:</p>
  1541. <pre><code class="lang-lua">target("test")
  1542. set_kind("static")
  1543. add_includedirs("src/include") -- only for the current target
  1544. add_includedirs("$(buildir)/include", {public = true}), the current target and child targets will be set
  1545. target("demo")
  1546. set_kind("binary")
  1547. add_deps("test")
  1548. </code></pre>
  1549. <p>For more on this block, see: <a href="#targetadd_deps">add_deps</a></p>
  1550. <p>!>If you don&#39;t want it to be fixed in the project, you can set it by: xmake f --includedirs=xxx or xmake f --cxflags="-I/xxx". This manual setting has higher directory search priority.</p>
  1551. <p>!> The header file does not support pattern matching by default, and it is not recommended to do so. It is easy to introduce some unnecessary subdirectories, resulting in the interference of various header file reference conflicts, and it is more difficult to check if there is a problem.<br>If the user insists on doing this, it can be achieved by <code>add_includedirs(os.dirs(path.join(os.scriptdir(), "xxx/**")))</code>.</p>
  1552. <h3 id="targetadd_sysincludedirs">target:add_sysincludedirs</h3>
  1553. <h4 id="addsystemheaderfilesearchdirectory">Add system header file search directory</h4>
  1554. <p><code>add_includedirs</code> is usually used to add search directories for project header files. The introduction of some system library header files may trigger some internal warning messages, but these warnings may be unavoidable for users and cannot be fixed.</p>
  1555. <p>Then, every time these warnings are displayed, it will interfere with the user. Therefore, gcc/clang provides <code>-isystem</code> to set the system header file search path. The header files set through this interface will suppress some warning messages to avoid disturbing users .</p>
  1556. <p>msvc also provides the <code>/external:I</code> compilation option to set it, but it needs a higher version of msvc to support it.</p>
  1557. <p>Therefore, xmake provides <code>add_sysincludedirs</code> to abstractly adapt and set the search path of system library header files. If the current compiler does not support it, it will automatically switch back to the <code>-I</code> compilation option.</p>
  1558. <pre><code class="lang-lua">target("test")
  1559. add_sysincludedirs("/usr/include")
  1560. </code></pre>
  1561. <p>The generated compilation options are as follows:</p>
  1562. <pre><code class="lang-console">-isystem /usr/include
  1563. </code></pre>
  1564. <p>In the case of the msvc compiler, it will be:</p>
  1565. <pre><code class="lang-console">/experimental:external /external:W0 /external:I /usr/include
  1566. </code></pre>
  1567. <p>!> In addition, the dependency package introduced with <code>add_requires()</code> will also use <code>-isystem</code> as the external system header file by default.</p>
  1568. <h3 id="targetadd_defines">target:add_defines</h3>
  1569. <h4 id="addmacrodefinition">Add macro definition</h4>
  1570. <pre><code class="lang-lua">add_defines("DEBUG", "TEST=0", "TEST2=\"hello\"")
  1571. </code></pre>
  1572. <p>Equivalent to setting the compile option:</p>
  1573. <pre><code>-DDEBUG -DTEST=0 -DTEST2=\"hello\"
  1574. </code></pre><h3 id="targetadd_undefines">target:add_undefines</h3>
  1575. <h4 id="addmacroundefinition">Add macro undefinition</h4>
  1576. <pre><code class="lang-lua">add_undefines("DEBUG")
  1577. </code></pre>
  1578. <p>Equivalent to setting the compile option: <code>-UDEBUG</code></p>
  1579. <p>In the code is equivalent to: <code>#undef DEBUG</code></p>
  1580. <h3 id="targetadd_cflags">target:add_cflags</h3>
  1581. <h4 id="addccompilationflags">Add c compilation flags</h4>
  1582. <p>Add compilation options only for c code</p>
  1583. <pre><code class="lang-lua">add_cflags("-g", "-O2", "-DDEBUG")
  1584. </code></pre>
  1585. <p><p class="warn"><br>All option values are based on the definition of gcc as standard. If other compilers are not compatible (for example: vc), xmake will automatically convert it internally to the corresponding option values supported by the compiler.<br>Users don&#39;t have to worry about compatibility. If other compilers don&#39;t have matching values, xmake will automatically ignore the settings.<br></p>
  1586. </p>
  1587. <p>After version 2.1.9, the force parameter can be used to force the automatic detection of flags to be disabled and passed directly to the compiler. Even if the compiler may not support it, it will be set:</p>
  1588. <pre><code class="lang-lua">add_cflags("-g", "-O2", {force = true})
  1589. </code></pre>
  1590. <h3 id="targetadd_cxflags">target:add_cxflags</h3>
  1591. <h4 id="addcccompilationflags">Add c/c++ compilation flags</h4>
  1592. <p>Add compilation options to c/c++ code at the same time</p>
  1593. <h3 id="targetadd_cxxflags">target:add_cxxflags</h3>
  1594. <h4 id="addccompilationflags">Add c++ compilation flags</h4>
  1595. <p>Add compilation options only to c++ code</p>
  1596. <h5 id="addcompilerspecificflags">Add compiler-specific flags</h5>
  1597. <p>In version 2.7.3, we have improved all flags adding interfaces to specify flags only for specific compilers, e.g.</p>
  1598. <pre><code class="lang-lua">add_cxxflags("clang::-stdlib=libc++")
  1599. add_cxxflags("gcc::-stdlib=libc++")
  1600. add_cxxflags("cl::/GR-")
  1601. add_cxxflags("clang_cl::/GR-")
  1602. </code></pre>
  1603. <p>Or.</p>
  1604. <pre><code class="lang-lua">add_cxxflags("-stdlib=libc++", {tools = "clang"})
  1605. add_cxxflags("-stdlib=libc++", {tools = "gcc"})
  1606. add_cxxflags("/GR-", {tools = {"clang_cl", "cl"}})
  1607. </code></pre>
  1608. <p>!> Not just for compile flags, but also for link flags such as add_ldflags, which also work. For link flags, the user must specify<br>if they want to target the C or C++ linker, such as "clang" for C and "clangxx" for C++.</p>
  1609. <h3 id="targetadd_mflags">target:add_mflags</h3>
  1610. <h4 id="addobjccompilationflags">Add objc compilation flags</h4>
  1611. <p>Add compilation options only to objc code</p>
  1612. <pre><code class="lang-lua">add_mflags("-g", "-O2", "-DDEBUG")
  1613. </code></pre>
  1614. <p>After version 2.1.9, the force parameter can be used to force the automatic detection of flags to be disabled and passed directly to the compiler. Even if the compiler may not support it, it will be set:</p>
  1615. <pre><code class="lang-lua">add_mflags("-g", "-O2", {force = true})
  1616. </code></pre>
  1617. <h3 id="targetadd_mxflags">target:add_mxflags</h3>
  1618. <h4 id="addobjcobjccompilationflags">Add objc/objc++ compilation flags</h4>
  1619. <p>Also add compile options to objc/objc++ code</p>
  1620. <pre><code class="lang-lua">add_mxflAgs("-framework CoreFoundation")
  1621. </code></pre>
  1622. <h3 id="targetadd_mxxflags">target:add_mxxflags</h3>
  1623. <h4 id="addobjccompilationflags">Add objc++ compilation flags</h4>
  1624. <p>Add compilation options only to objc++ code</p>
  1625. <pre><code class="lang-lua">add_mxxflags("-framework CoreFoundation")
  1626. </code></pre>
  1627. <h3 id="targetadd_scflags">target:add_scflags</h3>
  1628. <h4 id="addswiftcompilationflags">Add swift compilation flags</h4>
  1629. <p>Add compilation options to swift code</p>
  1630. <pre><code class="lang-lua">add_scflags("xxx")
  1631. </code></pre>
  1632. <h3 id="targetadd_asflags">target:add_asflags</h3>
  1633. <h4 id="addasmcompilationflags">Add asm compilation flags</h4>
  1634. <p>Add compilation options to assembly code</p>
  1635. <pre><code class="lang-lua">add_asflags("xxx")
  1636. </code></pre>
  1637. <h3 id="targetadd_gcflags">target:add_gcflags</h3>
  1638. <h4 id="addgocompilationflags">Add go compilation flags</h4>
  1639. <p>Add compile options to golang code</p>
  1640. <pre><code class="lang-lua">add_gcflags("xxx")
  1641. </code></pre>
  1642. <h3 id="targetadd_dcflags">target:add_dcflags</h3>
  1643. <h4 id="adddlangcompilationflags">Add dlang compilation flags</h4>
  1644. <p>Add compilation options to dlang code</p>
  1645. <pre><code class="lang-lua">add_dcflags("xxx")
  1646. </code></pre>
  1647. <h3 id="targetadd_rcflags">target:add_rcflags</h3>
  1648. <h4 id="addrustcompilationflags">Add rust compilation flags</h4>
  1649. <p>Add compilation options to the rust code</p>
  1650. <pre><code class="lang-lua">add_rcflags("xxx")
  1651. </code></pre>
  1652. <h3 id="targetadd_fcflags">target:add_fcflags</h3>
  1653. <h4 id="addfortrancompilationflags">Add fortran compilation flags</h4>
  1654. <p>Add compilation options to the fortran code</p>
  1655. <pre><code class="lang-lua">add_fcflags("xxx")
  1656. </code></pre>
  1657. <h3 id="targetadd_zcflags">target:add_zcflags</h3>
  1658. <h4 id="addzigcompilationflags">Add zig compilation flags</h4>
  1659. <p>Add compilation options to the zig code</p>
  1660. <pre><code class="lang-lua">add_zcflags("xxx")
  1661. </code></pre>
  1662. <h3 id="targetadd_cuflags">target:add_cuflags</h3>
  1663. <h4 id="addcudacompilationflags">Add cuda compilation flags</h4>
  1664. <p>Add compilation options to cuda code</p>
  1665. <pre><code class="lang-lua">add_cuflags("-gencode arch=compute_30,code=sm_30")
  1666. </code></pre>
  1667. <h3 id="targetadd_culdflags">target:add_culdflags</h3>
  1668. <h4 id="addcudadevicelinkflags">Add cuda device link flags</h4>
  1669. <p>After v2.2.7, cuda default build will use device-link. If you want to set some link flags in this stage, you can set it through this interface.<br>The final program link will use ldflags, will not call nvcc, and directly link through c/c++ linker such as gcc/clang.</p>
  1670. <p>For a description of device-link, please refer to: <a href="https://devblogs.nvidia.com/separate-compilation-linking-cuda-device-code/">https://devblogs.nvidia.com/separate-compilation-linking-cuda-device-code/</a></p>
  1671. <pre><code class="lang-lua">add_culdflags("-gencode arch=compute_30,code=sm_30")
  1672. </code></pre>
  1673. <h3 id="targetadd_cugencodes">target:add_cugencodes</h3>
  1674. <h4 id="addgencodesettingsforcudadevices">Add gencode settings for cuda devices</h4>
  1675. <p>The <code>add_cugencodes()</code> interface is actually a simplified encapsulation of <code>add_cuflags("-gencode arch=compute_xx, code=compute_xx")</code> compilation flags settings. The actual flags mapping relationship corresponding to the internal parameter values is as follows:</p>
  1676. <pre><code class="lang-lua">- compute_xx --> `-gencode arch=compute_xx,code=compute_xx`
  1677. - sm_xx --> `-gencode arch=compute_xx,code=sm_xx`
  1678. - sm_xx,sm_yy --> `-gencode arch=compute_xx,code=[sm_xx,sm_yy]`
  1679. - compute_xx,sm_yy --> `-gencode arch=compute_xx,code=sm_yy`
  1680. - compute_xx,sm_yy,sm_zz --> `-gencode arch=compute_xx,code=[sm_yy,sm_zz]`
  1681. - native --> match the fastest cuda device on current host,
  1682. eg. for a Tesla P100, `-gencode arch=compute_60,code=sm_60` will be added,
  1683. if no available device is found, no `-gencode` flags will be added
  1684. </code></pre>
  1685. <p>E.g:</p>
  1686. <pre><code class="lang-lua">add_cugencodes("sm_30")
  1687. </code></pre>
  1688. <p>Is equivalent to</p>
  1689. <pre><code class="lang-lua">add_cuflags("-gencode arch=compute_30,code=sm_30")
  1690. add_culdflags("-gencode arch=compute_30,code=sm_30")
  1691. </code></pre>
  1692. <p>Is it more streamlined? This is actually an auxiliary interface for simplifying the setup.</p>
  1693. <p>And if we set the native value, then xmake will automatically detect the cuda device of the current host, and then quickly match its corresponding gencode setting, and automatically append it to the entire build process.</p>
  1694. <p>For example, if our host&#39;s current GPU is Tesla P100, and it can be automatically detected by xmake, then the following settings:</p>
  1695. <pre><code class="lang-lua">add_cugencodes("native")
  1696. </code></pre>
  1697. <p>Equivalent to:</p>
  1698. <pre><code class="lang-lua">add_cugencodes("sm_60")
  1699. </code></pre>
  1700. <h3 id="targetadd_ldflags">target:add_ldflags</h3>
  1701. <h4 id="addstaticlibrarylinkflags">Add static library link flags</h4>
  1702. <p>Add static link option</p>
  1703. <pre><code class="lang-lua">add_ldflags("-L/xxx", "-lxxx")
  1704. </code></pre>
  1705. <p>While adding flags, argument with space is not allowed defaultly, use expand = false instead.</p>
  1706. <pre><code class="lang-lua">-- add_ldflags("-L/my lib") ERROR: Invalid arguments
  1707. add_ldflags({"-L/my lib"}, {expand = false}) -- OK
  1708. </code></pre>
  1709. <h3 id="targetadd_arflags">target:add_arflags</h3>
  1710. <h4 id="addarchivelibraryflags">Add archive library flags</h4>
  1711. <p>Affect the generation of static libraries</p>
  1712. <pre><code class="lang-lua">add_arflags("xxx")
  1713. </code></pre>
  1714. <h3 id="targetadd_shflags">target:add_shflags</h3>
  1715. <h4 id="adddynamiclibrarylinkflags">Add dynamic library link flags</h4>
  1716. <p>Affect the generation of dynamic libraries</p>
  1717. <pre><code class="lang-lua">add_shflags("xxx")
  1718. </code></pre>
  1719. <h3 id="targetadd_options">target:add_options</h3>
  1720. <h4 id="addoptiondependencies">Add option dependencies</h4>
  1721. <p>This interface is similar to <a href="#targetset_options">set_options</a>, the only difference is that this is an append option, and <a href="#targetset_options">set_options</a> overrides the previous settings each time.</p>
  1722. <h3 id="targetadd_packages">target:add_packages</h3>
  1723. <h4 id="addpackagedependencies">Add package dependencies</h4>
  1724. <p>In the target scope, add integration package dependencies, for example:</p>
  1725. <pre><code class="lang-lua">target("test")
  1726. add_packages("zlib", "polarssl", "pcre", "mysql")
  1727. </code></pre>
  1728. <p>In this way, when compiling the test target, if the package exists, the macro definition, the header file search path, and the link library directory in the package will be automatically appended, and all the libraries in the package will be automatically linked.</p>
  1729. <p>Users no longer need to call the <a href="#targetadd_links">add_links</a>, <a href="#targetadd_includedirs">add_includedirs</a>, <a href="#targetadd_ldflags">add_ldflags</a> interfaces to configure the dependent library links.</p>
  1730. <p>For how to set up the package search directory, please refer to: <a href="/mirror/manual/global_interfaces.html#add_packagedirs">add_packagedirs</a> interface</p>
  1731. <p>After v2.2.2, this interface also supports packages defined by <a href="/mirror/manual/global_interfaces.html#add_requires">add_requires</a> in remote dependency management.</p>
  1732. <pre><code class="lang-lua">add_requires("zlib", "polarssl")
  1733. target("test")
  1734. add_packages("zlib", "polarssl")
  1735. </code></pre>
  1736. <p>After v2.2.3, it also supports overwriting built-in links to control the actual linked libraries:</p>
  1737. <pre><code class="lang-lua">-- By default, there will be links to ncurses, panel, form, etc.
  1738. add_requires("ncurses")
  1739. target("test")
  1740. -- Display specified, only use ncurses a link library
  1741. add_packages("ncurses", {links = "ncurses"})
  1742. </code></pre>
  1743. <p>Or simply disable links and only use header files:</p>
  1744. <pre><code class="lang-lua">add_requires("lua")
  1745. target("test")
  1746. add_packages("lua", {links = {}})
  1747. </code></pre>
  1748. <h3 id="targetadd_languages">target:add_languages</h3>
  1749. <h4 id="addlanguagestandards">Add language standards</h4>
  1750. <p>Similar to <a href="#targetset_languages">set_languages</a>, the only difference is that this interface will not overwrite the previous settings, but append settings.</p>
  1751. <h3 id="targetadd_vectorexts">target:add_vectorexts</h3>
  1752. <h4 id="addvectorextensions">Add vector extensions</h4>
  1753. <p>Add extended instruction optimization options, currently supports the following extended instruction sets:</p>
  1754. <pre><code class="lang-lua">add_vectorexts("mmx")
  1755. add_vectorexts("neon")
  1756. add_vectorexts("avx", "avx2", "avx512")
  1757. add_vectorexts("sse", "sse2", "sse3", "ssse3", "sse4.2")
  1758. </code></pre>
  1759. <p>!> If the currently set instruction set compiler does not support it, xmake will automatically ignore it, so you don&#39;t need the user to manually determine the maintenance. Just set all the instruction sets you need.</p>
  1760. <p>In 2.8.2, we added <code>all</code> configuration item has been added which can be used to turn on all extended directive optimisations where possible.</p>
  1761. <pre><code class="lang-lua">add_vectorexts("all")
  1762. </code></pre>
  1763. <h3 id="targetadd_frameworks">target:add_frameworks</h3>
  1764. <h4 id="addframeworks">Add frameworks</h4>
  1765. <p>Currently used for the <code>objc</code> and <code>swift</code> programs of the <code>ios</code> and <code>macosx</code> platforms, for example:</p>
  1766. <pre><code class="lang-lua">target("test")
  1767. add_frameworks("Foundation", "CoreFoundation")
  1768. </code></pre>
  1769. <p>Of course, you can also use <a href="#targetadd_mxflags">add_mxflags</a> and <a href="#targetadd_ldflags">add_ldflags</a> to set them up, but it is cumbersome and is not recommended.</p>
  1770. <pre><code class="lang-lua">target("test")
  1771. add_mxflags("-framework Foundation", "-framework CoreFoundation")
  1772. add_ldflags("-framework Foundation", "-framework CoreFoundation")
  1773. </code></pre>
  1774. <p>If it is not for both platforms, these settings will be ignored.</p>
  1775. <h3 id="targetadd_frameworkdirs">target:add_frameworkdirs</h3>
  1776. <h4 id="addframeworksearchdirectories">Add framework search directories</h4>
  1777. <p>For some third-party frameworks, it is impossible to find them only through <a href="#targetadd_frameworks">add_frameworks</a>. You also need to add a search directory through this interface.</p>
  1778. <pre><code class="lang-lua">target("test")
  1779. add_frameworks("MyFramework")
  1780. add_frameworkdirs("/tmp/frameworkdir", "/tmp/frameworkdir2")
  1781. </code></pre>
  1782. <h3 id="targetset_toolset">target:set_toolset</h3>
  1783. <h4 id="settoolset">Set toolset</h4>
  1784. <p>Separate settings for a specific target to switch a compiler, linker, but we recommend using <a href="#targetset_toolchains">set_toolchains</a> to switch the overall tool chain of a target.</p>
  1785. <p>Compared with set_toolchains, this interface only switches a specific compiler or linker of the toolchain.</p>
  1786. <p>!> This interface is only supported in versions above 2.3.4. The set_toolchain/set_tool interface before 2.3.4 will be gradually deprecated. The new interface is adopted and the usage is the same.</p>
  1787. <p>For the source files added by <code>add_files("*.c")</code>, the default is to call the system&#39;s best matching compiler to compile, or manually modify it by <code>xmake f --cc=clang</code> command, but these are Globally affects all target targets.</p>
  1788. <p>If there are some special requirements, you need to specify a different compiler, linker or specific version of the compiler for a specific target target under the current project. At this time, the interface can be used for purposes. For example:</p>
  1789. <pre><code class="lang-lua">target("test1")
  1790. add_files("*.c")
  1791. target("test2")
  1792. add_files("*.c")
  1793. set_toolset("cc", "$(projectdir)/tools/bin/clang-5.0")
  1794. </code></pre>
  1795. <p>The above description only makes special settings for the compiler of the test2 target, compiling test2 with a specific clang-5.0 compiler, and test1 still uses the default settings.</p>
  1796. <p><p class="tip"><br>Each setting will override the previous setting under the current target target. Different targets will not be overwritten and independent of each other. If set in the root domain, all child targets will be affected.<br></p>
  1797. </p>
  1798. <p>The previous parameter is key, which is used to specify the tool type. Currently supported (compiler, linker, archiver):</p>
  1799. <table>
  1800. <thead>
  1801. <tr>
  1802. <th>Tool Type</th>
  1803. <th>Description</th>
  1804. </tr>
  1805. </thead>
  1806. <tbody>
  1807. <tr>
  1808. <td>cc</td>
  1809. <td>c compiler</td>
  1810. </tr>
  1811. <tr>
  1812. <td>cxx</td>
  1813. <td>c++ compiler</td>
  1814. </tr>
  1815. <tr>
  1816. <td>mm</td>
  1817. <td>objc compiler</td>
  1818. </tr>
  1819. <tr>
  1820. <td>mxx</td>
  1821. <td>objc++ compiler</td>
  1822. </tr>
  1823. <tr>
  1824. <td>gc</td>
  1825. <td>go compiler</td>
  1826. </tr>
  1827. <tr>
  1828. <td>as</td>
  1829. <td>Assembler</td>
  1830. </tr>
  1831. <tr>
  1832. <td>sc</td>
  1833. <td>swift compiler</td>
  1834. </tr>
  1835. <tr>
  1836. <td>rc</td>
  1837. <td>rust compiler</td>
  1838. </tr>
  1839. <tr>
  1840. <td>dc</td>
  1841. <td>dlang compiler</td>
  1842. </tr>
  1843. <tr>
  1844. <td>fc</td>
  1845. <td>fortran compiler</td>
  1846. </tr>
  1847. <tr>
  1848. <td>sc</td>
  1849. <td>swift compiler</td>
  1850. </tr>
  1851. <tr>
  1852. <td>rust</td>
  1853. <td>rust compiler</td>
  1854. </tr>
  1855. <tr>
  1856. <td>strip</td>
  1857. <td>strip program</td>
  1858. </tr>
  1859. <tr>
  1860. <td>ld</td>
  1861. <td>c/c++/asm/objc and other general executable program linker</td>
  1862. </tr>
  1863. <tr>
  1864. <td>sh</td>
  1865. <td>c/c++/asm/objc and other general dynamic library linkers</td>
  1866. </tr>
  1867. <tr>
  1868. <td>ar</td>
  1869. <td>c/c++/asm/objc and other general static library archivers</td>
  1870. </tr>
  1871. <tr>
  1872. <td>dcld</td>
  1873. <td>dlang executable linker, rcld/gcld and similar</td>
  1874. </tr>
  1875. <tr>
  1876. <td>dcsh</td>
  1877. <td>dlang dynamic library linker, rcsh/gcsh and similar</td>
  1878. </tr>
  1879. </tbody>
  1880. </table>
  1881. <p>For some compiler file names that are irregular, causing xmake to fail to recognize the known compiler name, we can also add a tool name prompt, for example:</p>
  1882. <pre><code class="lang-lua">set_toolset("cc", "gcc@$(projectdir)/tools/bin/Mipscc.exe")
  1883. </code></pre>
  1884. <h3 id="targetset_toolchains">target:set_toolchains</h3>
  1885. <h4 id="setupthetoolchain">Set up the toolchain</h4>
  1886. <p>This sets up different tool chains for a specific target individually. Unlike set_toolset, this interface is an overall switch for a complete tool chain, such as cc/ld/sh and a series of tool sets.</p>
  1887. <p>This is also a recommended practice, because most compiler tool chains like gcc/clang, the compiler and the linker are used together. To cut it, you have to cut it as a whole. Separate and scattered switch settings will be cumbersome.</p>
  1888. <p>For example, we switch the test target to two tool chains of clang+yasm:</p>
  1889. <pre><code class="lang-lua">target("test")
  1890. set_kind("binary")
  1891. add_files("src/*.c")
  1892. set_toolchains("clang", "yasm")
  1893. </code></pre>
  1894. <p>You only need to specify the name of the toolchain. Specific toolchains supported by xmake can be viewed by the following command:</p>
  1895. <pre><code class="lang-bash">$ xmake show -l toolchains
  1896. xcode Xcode IDE
  1897. vs VisualStudio IDE
  1898. yasm The Yasm Modular Assembler
  1899. clang A C language family frontend for LLVM
  1900. go Go Programming Language Compiler
  1901. dlang D Programming Language Compiler
  1902. sdcc Small Device C Compiler
  1903. cuda CUDA Toolkit
  1904. ndk Android NDK
  1905. rust Rust Programming Language Compiler
  1906. llvm A collection of modular and reusable compiler and toolchain technologies
  1907. cross Common cross compilation toolchain
  1908. nasm NASM Assembler
  1909. gcc GNU Compiler Collection
  1910. mingw Minimalist GNU for Windows
  1911. gnu-rm GNU Arm Embedded Toolchain
  1912. envs Environment variables toolchain
  1913. fasm Flat Assembler
  1914. </code></pre>
  1915. <p>Of course, we can also switch to other tool chains globally through the command line:</p>
  1916. <pre><code class="lang-bash">$ xmake f --toolchain=clang
  1917. $ xmake
  1918. </code></pre>
  1919. <p>In addition, we can also customize toolchain in xmake.lua, and then specify it through <code>set_toolchains</code>, for example:</p>
  1920. <pre><code class="lang-lua">toolchain("myclang")
  1921. set_kind("standalone")
  1922. set_toolset("cc", "clang")
  1923. set_toolset("cxx", "clang", "clang++")
  1924. set_toolset("ld", "clang++", "clang")
  1925. set_toolset("sh", "clang++", "clang")
  1926. set_toolset("ar", "ar")
  1927. set_toolset("ex", "ar")
  1928. set_toolset("strip", "strip")
  1929. set_toolset("mm", "clang")
  1930. set_toolset("mxx", "clang", "clang++")
  1931. set_toolset("as", "clang")
  1932. - ...
  1933. </code></pre>
  1934. <p>For details about this piece, you can go to the <a href="/mirror/manual/custom_toolchain.html">Custom Toolchain</a>.</p>
  1935. <p>For more details, please see: <a href="https://github.com/xmake-io/xmake/issues/780">#780</a></p>
  1936. <p>Starting from version 2.3.5, new settings and switches for toolchains platform and architecture have been added, such as:</p>
  1937. <pre><code class="lang-lua">target("test")
  1938. set_toolchains("xcode", {plat = os.host(), arch = os.arch()})
  1939. </code></pre>
  1940. <p>If it is currently in cross-compilation mode, this test will still be forced to switch to the local compilation toolchain of xcode and the corresponding pc platform. This is for those who want to support part of the target using the host toolchain and part of the target using the cross-compilation toolchain. ,very useful.</p>
  1941. <p>However, this is not particularly convenient, especially when cross-platform compilation, pc tool chains of different platforms are different, there are msvc, xcode, clang, etc., you need to judge the platform to specify.</p>
  1942. <p>Therefore, we can directly use the <a href="#targetset_plat">set_plat</a> and <a href="#targetset_arch">set_arch</a> interfaces to directly set a specific target to the host platform, and we can automatically select the host toolchain internally, for example:</p>
  1943. <pre><code class="lang-lua">target("test")
  1944. set_plat(os.host())
  1945. set_arch(os.arch())
  1946. </code></pre>
  1947. <p>The application scenario and example of this piece can be seen: <a href="https://github.com/xmake-io/xmake-repo/blob/dev/packages/l/luajit/port/xmake.lua">https://github.com/xmake-io/xmake-repo/blob/dev/packages/l/luajit/port/xmake.lua</a></p>
  1948. <p>In luajit, you need to compile the minilua/buildvm of the host platform to generate jit related code, and then start compiling luajit itself to different cross tool chains.</p>
  1949. <p>For details of this, you can refer to: <a href="https://github.com/xmake-io/xmake/pull/857">https://github.com/xmake-io/xmake/pull/857</a></p>
  1950. <p>v2.5.1 has made further improvements to set_toolchains to better support independent toolchain switching for specific targets. For example, different targets support switching to different VS versions, for example:</p>
  1951. <pre><code class="lang-lua">target("test")
  1952. set_toolchains("msvc", {vs = "2015"})
  1953. </code></pre>
  1954. <p>By default, xmake will use the global vs tool chain. For example, if vs2019 is currently detected, but the user also installs vs2015 at the same time, you can switch the test target to vs2015 to compile through the above configuration.</p>
  1955. <p>You can even use <code>set_arch</code> to specify a specific architecture to x86 instead of the default x64.</p>
  1956. <pre><code class="lang-lua">target("test")
  1957. set_arch("x86")
  1958. set_toolchains("msvc", {vs = "2015"})
  1959. </code></pre>
  1960. <p>The above effect is similar to <code>set_toolchains("msvc", {vs = "2015", arch = "x86"})</code>, but <code>set_arch</code> is for target granularity, and the arch setting in <code>set_toolchains</code> is only for specific tools Chain granularity.</p>
  1961. <p>Generally, we recommend using <code>set_arch</code> to switch the architecture of the entire target.</p>
  1962. <h3 id="targetset_plat">target:set_plat</h3>
  1963. <h4 id="setthecompilationplatformforthespecifiedtarget">Set the compilation platform for the specified target</h4>
  1964. <p>Usually used with <a href="#targetset_arch">set_arch</a> to switch the compilation platform of the specified target to the specified platform, xmake will automatically select the appropriate tool chain according to the switched platform.</p>
  1965. <p>Generally used in scenarios where the host platform target and cross-compilation target need to be compiled at the same time. For more details, see: <a href="#targetset_toolchains">set_toolchains</a></p>
  1966. <p>E.g:</p>
  1967. <pre><code class="lang-console">$ xmake f -p android --ndk=/xxx
  1968. </code></pre>
  1969. <p>Even if you are using android ndk to compile the android platform target, the host target it depends on will still switch to the host platform and use xcode, msvc and other host tool chains to compile.</p>
  1970. <pre><code class="lang-lua">target("host")
  1971. set_kind("binary")
  1972. set_plat(os.host())
  1973. set_arch(os.arch())
  1974. add_files("src/host/*.c")
  1975. target("test")
  1976. set_kind("binary")
  1977. add_deps("host")
  1978. add_files("src/test/*.c")
  1979. </code></pre>
  1980. <h3 id="targetset_arch">target:set_arch</h3>
  1981. <h4 id="setthecompilationarchitectureofthespecifiedtarget">Set the compilation architecture of the specified target</h4>
  1982. <p>For details, see: <a href="#targetset_plat">set_plat</a></p>
  1983. <h3 id="targetset_values">target:set_values</h3>
  1984. <h4 id="setcustomconfigurationvalues">Set custom configuration values</h4>
  1985. <p>Set some extended configuration values for the target. These configurations do not have a built-in api like <code>set_ldflags</code>. You can extend the configuration by passing in a configuration name with the first argument.<br>Generally used to pass configuration parameters to scripts in custom rules, for example:</p>
  1986. <pre><code class="lang-lua">rule("markdown")
  1987. on_build_file(function (target, sourcefile)
  1988. -- compile .markdown with flags
  1989. local flags = target:values("markdown.flags")
  1990. if flags then
  1991. -- ..
  1992. end
  1993. end)
  1994. target("test")
  1995. add_files("src/*.md", {rule = "markdown"})
  1996. set_values("markdown.flags", "xxx", "xxx")
  1997. </code></pre>
  1998. <p>In the above code example, it can be seen that when the target applies the markdown rule, some flag values are set by set_values and provided to the markdown rule for processing.<br>In the rule script, you can get the extended flag value set in the target by <code>target:values("markdown.flags")</code>.</p>
  1999. <p>!> The specific extension configuration name will be different according to different rules. Currently, you can refer to the description of related rules: <a href="/mirror/manual/custom_rule.html#built-in-rules">built-in rules</a></p>
  2000. <p>The following is a list of some built-in extended configuration items currently supported by xmake.</p>
  2001. <table>
  2002. <thead>
  2003. <tr>
  2004. <th>Extended configuration name</th>
  2005. <th>Configuration description</th>
  2006. </tr>
  2007. </thead>
  2008. <tbody>
  2009. <tr>
  2010. <td>fortran.moduledir</td>
  2011. <td>Set the output directory of the fortran module</td>
  2012. </tr>
  2013. <tr>
  2014. <td>ndk.arm_mode</td>
  2015. <td>Set the arm compilation mode of ndk (arm/thumb)</td>
  2016. </tr>
  2017. <tr>
  2018. <td>objc.build.arc</td>
  2019. <td>Set to enable or disable objc&#39;s arc</td>
  2020. </tr>
  2021. <tr>
  2022. <td>objc++.build.arc</td>
  2023. <td>Set to enable or disable arc of objc++</td>
  2024. </tr>
  2025. <tr>
  2026. <td>xcode.bundle_identifier</td>
  2027. <td>Set the Bundle Identifier of the xcode toolchain</td>
  2028. </tr>
  2029. <tr>
  2030. <td>xcode.mobile_provision</td>
  2031. <td>Set the certificate information of the xcode toolchain</td>
  2032. </tr>
  2033. <tr>
  2034. <td>xcode.codesign_identity</td>
  2035. <td>Set the code signing identity of the xcode toolchain</td>
  2036. </tr>
  2037. <tr>
  2038. <td>wasm.preloadfiles</td>
  2039. <td>Set the preload file (and path mapping) of wasm build</td>
  2040. </tr>
  2041. <tr>
  2042. <td>wdk.env.winver</td>
  2043. <td>Set the win support version of wdk</td>
  2044. </tr>
  2045. <tr>
  2046. <td>wdk.umdf.sdkver</td>
  2047. <td>Set the umdf sdk version of wdk</td>
  2048. </tr>
  2049. <tr>
  2050. <td>wdk.kmdf.sdkver</td>
  2051. <td>Set the kmdf sdk version of wdk</td>
  2052. </tr>
  2053. <tr>
  2054. <td>wdk.sign.mode</td>
  2055. <td>Set the code signing mode of wdk</td>
  2056. </tr>
  2057. <tr>
  2058. <td>wdk.sign.store</td>
  2059. <td>Set wdk code signing store</td>
  2060. </tr>
  2061. <tr>
  2062. <td>wdk.sign.certfile</td>
  2063. <td>Set wdk code signing certificate file</td>
  2064. </tr>
  2065. <tr>
  2066. <td>wdk.sign.thumbprint</td>
  2067. <td>Set wdk code signing fingerprint</td>
  2068. </tr>
  2069. </tbody>
  2070. </table>
  2071. <h3 id="targetadd_values">target:add_values</h3>
  2072. <h4 id="addcustomconfigurationvalues">Add custom configuration values</h4>
  2073. <p>Usage is similar to <a href="#targetset_values">target:set_values</a>, the difference is that this interface is an additional setting, and will not override the settings each time.</p>
  2074. <h3 id="targetset_rundir">target:set_rundir</h3>
  2075. <h4 id="settherunningdirectory">Set the running directory</h4>
  2076. <p>This interface is used to set the current running directory of the default running target program. If not set, by default, the target is loaded and run in the directory where the executable file is located.</p>
  2077. <p>If the user wants to modify the load directory, one is to customize the run logic by <code>on_run()</code>, and to do the switch inside, but just to cut the directory, this is too cumbersome.</p>
  2078. <p>Therefore, you can quickly switch settings to the default directory environment through this interface.</p>
  2079. <pre><code class="lang-lua">target("test")
  2080. set_kind("binary")
  2081. add_files("src/*.c")
  2082. set_rundir("$(projectdir)/xxx")
  2083. </code></pre>
  2084. <h3 id="targetset_runargs">target:set_runargs</h3>
  2085. <h4 id="setthelistofrunparameters">Set the list of run parameters</h4>
  2086. <p>2.6.9 New interface to set default run arguments for <code>xmake run</code>, with which we can avoid typing run arguments every time on the command line, <code>xmake run -x --arg1=val</code></p>
  2087. <pre><code class="lang-lua">set_runargs("-x", "--arg1=val")
  2088. </code></pre>
  2089. <h3 id="targetadd_runenvs">target:add_runenvs</h3>
  2090. <h4 id="addruntimeenvironmentvariables">Add runtime environment variables</h4>
  2091. <p>This interface is used to add an environment variable that sets the default running target program. Unlike <a href="#targetset_runenv">set_runenv</a>, this interface appends the value in the existing system env and does not overwrite it.</p>
  2092. <p>Therefore, for PATH, it is very convenient to append values through this interface, and this interface supports multi-value settings, so it is usually used to set multi-value env with path sep. .</p>
  2093. <pre><code class="lang-lua">target("test")
  2094. set_kind("binary")
  2095. add_files("src/*.c")
  2096. add_runenvs("PATH", "/tmp/bin", "xxx/bin")
  2097. add_runenvs("LD_LIBRARY_PATH", "/tmp/lib", "xxx/lib")
  2098. </code></pre>
  2099. <h3 id="targetset_runenv">target:set_runenv</h3>
  2100. <h4 id="settheruntimeenvironmentvariable">Set the runtime environment variable</h4>
  2101. <p>This interface differs from <a href="#targetadd_runenvs">add_runenvs</a> in that <code>set_runenv</code> is an override setting for an environment variable that overrides the env value of the original system environment, and this interface is singular and cannot pass multiple parameters.</p>
  2102. <p>So, if you want to override the env that sets the multipath in PATH, you need to splicing yourself:</p>
  2103. <pre><code class="lang-lua">target("test")
  2104. set_kind("binary")
  2105. add_files("src/*.c")
  2106. set_runenv("PATH", path.joinenv("/tmp/bin", "xxx/bin"))
  2107. set_runenv("NAME", "value")
  2108. </code></pre>
  2109. <h3 id="targetset_installdir">target:set_installdir</h3>
  2110. <h4 id="settheinstallationdirectory">Set the installation directory</h4>
  2111. <p>By default, <code>xmake install</code> will be installed to the system <code>/usr/local</code> directory. We can specify other installation directories except <code>xmake install -o /usr/local</code>.<br>You can also set a different installation directory for the target in xmake.lua instead of the default directory.</p>
  2112. <h3 id="targetset_prefixdir">target:set_prefixdir</h3>
  2113. <h4 id="settheinstallationprefixsubdirectory">Set the installation prefix subdirectory</h4>
  2114. <p>Although the installation root directory is set by <code>set_installdir</code> and <code>xmake install -o [installdir]</code>, if we still want to further adjust the subpaths of bin, lib and include.</p>
  2115. <p>Then, we can use this interface. By default, the installation directory will follow this structure:</p>
  2116. <pre><code class="lang-bash">installdir
  2117. - bin
  2118. - lib
  2119. - include
  2120. </code></pre>
  2121. <p>If we configure:</p>
  2122. <pre><code class="lang-lua">set_prefix("prefixdir")
  2123. </code></pre>
  2124. <p>It is to add a general subdirectory:</p>
  2125. <pre><code class="lang-bash">installdir
  2126. - prefixdir
  2127. - bin
  2128. - lib
  2129. - include
  2130. </code></pre>
  2131. <p>We can also configure bin, lib and include subdirectories separately, for example:</p>
  2132. <pre><code class="lang-lua">set_prefix("prefixdir", {bindir = "mybin", libdir = "mylib", includedir = "myinc"})
  2133. </code></pre>
  2134. <pre><code class="lang-bash">installdir
  2135. - prefixdir
  2136. - mybin
  2137. - mylib
  2138. - myinc
  2139. </code></pre>
  2140. <p>If we do not configure prefixdir and only modify the bin subdirectory, we can configure prefixdir to <code>/</code>.</p>
  2141. <pre><code class="lang-lua">set_prefix("/", {bindir = "mybin", libdir = "mylib", includedir = "myinc"})
  2142. </code></pre>
  2143. <pre><code class="lang-bash">installdir
  2144. - mybin
  2145. - mylib
  2146. - myinc
  2147. </code></pre>
  2148. <h3 id="targetadd_installfiles">target:add_installfiles</h3>
  2149. <h4 id="addinstallationfiles">Add installation files</h4>
  2150. <p>2.2.5 version of the new interface, used to set the corresponding file for each target, generally used for the <code>xmake install/uninstall</code> command.</p>
  2151. <p>For example, we can specify to install various types of files to the installation directory:</p>
  2152. <pre><code class="lang-lua">target("test")
  2153. add_installfiles("src/*.h")
  2154. add_installfiles("doc/*.md")
  2155. </code></pre>
  2156. <p>By default on Linux and other systems, we will install to <code>/usr/local/*.h, /usr/local/*.md</code>, but we can also specify to install to a specific subdirectory:</p>
  2157. <pre><code class="lang-lua">target("test")
  2158. add_installfiles("src/*.h", {prefixdir = "include"})
  2159. add_installfiles("doc/*.md", {prefixdir = "share/doc"})
  2160. </code></pre>
  2161. <p>The above settings, we will install to <code>/usr/local/include/*.h, /usr/local/share/doc/*.md</code></p>
  2162. <p>We can also install by subdirectory in the source file by <code>()</code>, for example:</p>
  2163. <pre><code class="lang-lua">target("test")
  2164. add_installfiles("src/(tbox/*.h)", {prefixdir = "include"})
  2165. add_installfiles("doc/(tbox/*.md)", {prefixdir = "share/doc"})
  2166. </code></pre>
  2167. <p>We extract the <code>src/*.h</code> subdirectory structure from the files in <code>src/tbox/*.h</code> and install it: <code>/usr/local/include/tbox/*.h, /usr/local /share/doc/tbox/*.md</code></p>
  2168. <p>Of course, users can also use the <a href="#targetset_installdir">set_installdir</a> interface.</p>
  2169. <p>For a detailed description of this interface, see: <a href="https://github.com/xmake-io/xmake/issues/318">https://github.com/xmake-io/xmake/issues/318</a></p>
  2170. <h3 id="targetadd_headerfiles">target:add_headerfiles</h3>
  2171. <h4 id="addheaderfiles">Add header files</h4>
  2172. <p>2.2.5 version of the new interface, used to set the corresponding header file for each target, generally used for the <code>xmake install/uninstall</code> command.</p>
  2173. <p>This interface is used in almost the same way as the <a href="#targetadd_installfiles">add_installfiles</a> interface. But it is provided for installing header files.<br>It is not required to set the <code>prefixdir</code> option. The header files are installed into the corresponding <code>include</code> subdirectory by default.</p>
  2174. <p>And this interface for the <code>xmake project -k vs201x</code> and other plug-in generated IDE files, will also add the corresponding header file into it.</p>
  2175. <p>We can also install by subdirectory in the source file by <code>()</code>, for example:</p>
  2176. <pre><code class="lang-lua">target("test")
  2177. add_headerfiles("src/(tbox/*.h)", {prefixdir = "include"})
  2178. </code></pre>
  2179. <p>After v2.7.1, we can disable the default header file installation behavior through the <code>{install = false}</code> parameter,<br>and only display and edit the set header files for the project generator&#39;s file list, such as vs project.</p>
  2180. <pre><code class="lang-lua">add_headerfiles("src/foo.h")
  2181. add_headerfiles("src/test.h", {install = false})
  2182. </code></pre>
  2183. <p>The above two header files will be displayed in the vs project, but only foo.h will be distributed and installed on the system.</p>
  2184. <h3 id="targetset_configdir">target:set_configdir</h3>
  2185. <h4 id="settheoutputdirectoryofconfigurationfiles">Set the output directory of configuration files</h4>
  2186. <p>Version 2.2.5 adds a new interface, mainly used for the output directory of the template configuration file set by the <a href="#targetadd_configfiles">add_configfiles</a> interface.</p>
  2187. <h3 id="targetset_configvar">target:set_configvar</h3>
  2188. <h4 id="settemplateconfigurationvariables">Set template configuration variables</h4>
  2189. <p>The new interface in version 2.2.5 is used to add some template configuration variables that need to be pre-processed before compilation, generally used in the <a href="#targetadd_configfiles">add_configfiles</a> interface.</p>
  2190. <pre><code class="lang-lua">target("test")
  2191. set_kind("binary")
  2192. add_files("main.c")
  2193. set_configvar("HAS_FOO", 1)
  2194. set_configvar("HAS_BAR", "bar")
  2195. set_configvar("HAS_ZOO", "zoo", {quote = false})
  2196. add_configfiles("config.h.in")
  2197. </code></pre>
  2198. <p>config.h.in</p>
  2199. <pre><code class="lang-c">${define HAS_FOO}
  2200. ${define HAS_BAR}
  2201. ${define HAS_ZOO}
  2202. </code></pre>
  2203. <p>The content of the generated config.h is as follows:</p>
  2204. <pre><code class="lang-c">#define HAS_FOO 1
  2205. #define HAS_BAR "bar"
  2206. #define HAS_ZOO zoo
  2207. </code></pre>
  2208. <p>set_configvar can set number, string and boolean type values. If it is a string value, the macro definition generated by default is enclosed in quotation marks. If you want to remove the quotation marks, you can set <code>{quote = false}</code>.</p>
  2209. <p>For related issues, see: <a href="https://github.com/xmake-io/xmake/issues/1694">#1694</a></p>
  2210. <p>If there is a path in the macro definition, and the path separator needs to be escaped, we can also configure to enable path character escaping.</p>
  2211. <pre><code class="lang-lua">set_configvar("TEST", "C:\\hello", {escape = true})
  2212. </code></pre>
  2213. <p>It will be automatically escaped into <code>#define TEST "C:\\hello"</code>, if escaping is not turned on, it will become: <code>#define TEST "C:\hello"</code></p>
  2214. <p>For related issues, see: <a href="https://github.com/xmake-io/xmake/issues/1872">#1872</a></p>
  2215. <h3 id="targetadd_configfiles">target:add_configfiles</h3>
  2216. <h4 id="addtemplateconfigurationfiles">Add template configuration files</h4>
  2217. <p>2.2.5 version of the new interface, used to add some configuration files that need to be pre-processed before compiling.</p>
  2218. <p>Let&#39;s start with a simple example:</p>
  2219. <pre><code class="lang-lua">target("test")
  2220. set_kind("binary")
  2221. add_files("src/*.c")
  2222. set_configdir("$(buildir)/config")
  2223. add_configfiles("src/config.h.in")
  2224. </code></pre>
  2225. <p>The above settings will automatically configure the <code>config.h.in</code> header file template before compiling. After preprocessing, it will generate the output to the specified <code>build/config/config.h</code>.</p>
  2226. <p>If <code>set_configdir</code> is not set, the default output is in the <code>build</code> directory.</p>
  2227. <p>The <code>.in</code> suffix will be automatically recognized and processed. If you want to store the output as a different file name, you can pass:</p>
  2228. <pre><code class="lang-lua">add_configfiles("src/config.h", {filename = "myconfig.h"})
  2229. </code></pre>
  2230. <p>The way to rename the output, again, this interface is similar to <a href="#targetadd_configfiles">add_installfiles</a>, which also supports prefixdir and subdirectory extraction settings:</p>
  2231. <pre><code class="lang-lua">add_configfiles("src/*.h.in", {prefixdir = "subdir"})
  2232. add_configfiles("src/(tbox/config.h)")
  2233. </code></pre>
  2234. <h5 id="variables">Variables</h5>
  2235. <p>One of the most important features of this interface is that it can be preprocessed and replaced with some of the template variables in the preprocessing, for example:</p>
  2236. <p>Config.h.in</p>
  2237. <pre><code>#define VAR1 "${VAR1}"
  2238. #define VAR2 "${VAR2}"
  2239. #define HELLO "${HELLO}"
  2240. </code></pre><pre><code class="lang-lua">set_configvar("VAR1", "1")
  2241. target("test")
  2242. set_kind("binary")
  2243. add_files("main.c")
  2244. set_configvar("VAR2", 2)
  2245. add_configfiles("config.h.in", {variables = {hello = "xmake"}})
  2246. add_configfiles("*.man", {onlycopy = true})
  2247. </code></pre>
  2248. <p>The template variable is set via the <a href="#targetset_configvar">set_configvar</a> interface, and the substitution is handled by the variable set in <code>{variables = {xxx = ""}}</code>.</p>
  2249. <p>The preprocessed file <code>config.h</code> is:</p>
  2250. <pre><code>#define VAR1 "1"
  2251. #define VAR2 "2"
  2252. #define HELLO "xmake"
  2253. </code></pre><p>The <code>{onlycopy = true}</code> setting will force <code>*.man</code> to be treated as a normal file, copying files only during the preprocessing stage, and not replacing variables.</p>
  2254. <p>The default template variable matching mode is <code>${var}</code>, of course we can also set other matching modes, for example, to <code>@var@</code> matching rules:</p>
  2255. <pre><code class="lang-lua">target("test")
  2256. add_configfiles("config.h.in", {pattern = "@(.-)@"})
  2257. </code></pre>
  2258. <h5 id="builtinvariables">Builtin variables</h5>
  2259. <p>We also have some built-in variables that can be replaced with default variables even if they are not set through this interface:</p>
  2260. <pre><code>${VERSION} -> 1.6.3
  2261. ${VERSION_MAJOR} -> 1
  2262. ${VERSION_MINOR} -> 6
  2263. ${VERSION_ALTER} -> 3
  2264. ${VERSION_BUILD} -> set_version("1.6.3", {build = "%Y%m%d%H%M"}) -> 201902031421
  2265. ${PLAT} and ${plat} -> MACOS and macosx
  2266. ${ARCH} and ${arch} -> ARM and arm
  2267. ${MODE} and ${mode} -> DEBUG/RELEASE and debug/release
  2268. ${DEBUG} and ${debug} -> 1 or 0
  2269. ${OS} and ${os} -> IOS or ios
  2270. </code></pre><p>E.g:</p>
  2271. <p>Config.h.in</p>
  2272. <pre><code class="lang-c">#define CONFIG_VERSION "${VERSION}"
  2273. #define CONFIG_VERSION_MAJOR ${VERSION_MAJOR}
  2274. #define CONFIG_VERSION_MINOR ${VERSION_MINOR}
  2275. #define CONFIG_VERSION_ALTER ${VERSION_ALTER}
  2276. #define CONFIG_VERSION_BUILD ${VERSION_BUILD}
  2277. </code></pre>
  2278. <p>Config.h</p>
  2279. <pre><code class="lang-c">#define CONFIG_VERSION "1.6.3"
  2280. #define CONFIG_VERSION_MAJOR 1
  2281. #define CONFIG_VERSION_MINOR 6
  2282. #define CONFIG_VERSION_ALTER 3
  2283. #define CONFIG_VERSION_BUILD 201902031401
  2284. </code></pre>
  2285. <p>Added git related built-in variables after v2.5.3:</p>
  2286. <pre><code class="lang-c">#define GIT_COMMIT "${GIT_COMMIT}"
  2287. #define GIT_COMMIT_LONG "${GIT_COMMIT_LONG}"
  2288. #define GIT_COMMIT_DATE "${GIT_COMMIT_DATE}"
  2289. #define GIT_BRANCH "${GIT_BRANCH}"
  2290. #define GIT_TAG "${GIT_TAG}"
  2291. #define GIT_TAG_LONG "${GIT_TAG_LONG}"
  2292. #define GIT_CUSTOM "${GIT_TAG}-${GIT_COMMIT}"
  2293. </code></pre>
  2294. <pre><code class="lang-c">#define GIT_COMMIT "8c42b2c2"
  2295. #define GIT_COMMIT_LONG "8c42b2c251793861eb85ffdf7e7c2307b129c7ae"
  2296. #define GIT_COMMIT_DATE "20210121225744"
  2297. #define GIT_BRANCH "dev"
  2298. #define GIT_TAG "v1.6.6"
  2299. #define GIT_TAG_LONG "v1.6.6-0-g8c42b2c2"
  2300. #define GIT_CUSTOM "v1.6.6-8c42b2c2"
  2301. </code></pre>
  2302. <h5 id="macrodefinition">Macro definition</h5>
  2303. <p>We can also perform some variable state control processing on the <code>#define</code> definition:</p>
  2304. <p>Config.h.in</p>
  2305. <pre><code class="lang-c">${define FOO_ENABLE}
  2306. </code></pre>
  2307. <pre><code class="lang-lua">set_configvar("FOO_ENABLE", 1) -- or pass true
  2308. set_configvar("FOO_STRING", "foo")
  2309. </code></pre>
  2310. <p>After setting the above variable, <code>${define xxx}</code> will be replaced with:</p>
  2311. <pre><code class="lang-c">#define FOO_ENABLE 1
  2312. #define FOO_STRING "foo"
  2313. </code></pre>
  2314. <p>Or (when set to 0 disable)</p>
  2315. <pre><code class="lang-c">/* #undef FOO_ENABLE */
  2316. /* #undef FOO_STRING */
  2317. </code></pre>
  2318. <p>This method is very useful for some automatic detection generation config.h, such as with the option to do automatic detection:</p>
  2319. <pre><code class="lang-lua">option("foo")
  2320. set_default(true)
  2321. set_description("Enable Foo")
  2322. set_configvar("FOO_ENABLE", 1) -- or pass true to enable the FOO_ENABLE variable
  2323. set_configvar("FOO_STRING", "foo")
  2324. target("test")
  2325. add_configfiles("config.h.in")
  2326. -- If the foo option is enabled -> Add FOO_ENABLE and FOO_STRING definitions
  2327. add_options("foo")
  2328. </code></pre>
  2329. <p>Config.h.in</p>
  2330. <pre><code class="lang-c">${define FOO_ENABLE}
  2331. ${define FOO_STRING}
  2332. </code></pre>
  2333. <p>Config.h</p>
  2334. <pre><code class="lang-c">#define FOO_ENABLE 1
  2335. #define FOO_STRING "foo"
  2336. </code></pre>
  2337. <p>Regarding the option option detection, and the automatic generation of config.h, there are some helper functions, you can look at it: <a href="https://github.com/xmake-io/xmake/issues/342">https://github.com/xmake-io/xmake/issues/342</a></p>
  2338. <p>In addition to <code>#define</code>, if you want to other non<code>#define xxx</code> also performs state switching processing. You can use the <code>${default xxx 0}</code> mode to set default values, for example:</p>
  2339. <pre><code>HAVE_SSE2 equ ${default VAR_HAVE_SSE2 0}
  2340. </code></pre><p>After <code>set_configvar("HAVE_SSE2", 1)</code> is enabled, it becomes <code>HAVE_SSE2 equ 1</code>. If no variable is set, the default value is used: <code>HAVE_SSE2 equ 0</code></p>
  2341. <p>For a detailed description of this, see: <a href="https://github.com/xmake-io/xmake/issues/320">https://github.com/xmake-io/xmake/issues/320</a></p>
  2342. <h5 id="defineexportmacros">Define export macros</h5>
  2343. <p>A new feature added in v2.9.8 is that it can generate export macro definitions for dynamic libraries, which are usually used for symbol export and import of dll libraries under Windows.</p>
  2344. <p>Define in config.h.in:</p>
  2345. <pre><code class="lang-c">${define_export MYLIB}
  2346. </code></pre>
  2347. <p>It will generate</p>
  2348. <pre><code class="lang-c">#ifdef MYLIB_STATIC
  2349. # define MYLIB_EXPORT
  2350. #else
  2351. # if defined(_WIN32)
  2352. # define MYLIB_EXPORT __declspec(dllexport)
  2353. # elif defined(__GNUC__) &amp;&amp; ((__GNUC__ >= 4) || (__GNUC__ == 3 &amp;&amp; __GNUC_MINOR__ >= 3))
  2354. # define MYLIB_EXPORT __attribute__((visibility("default")))
  2355. # else
  2356. # define MYLIB_EXPORT
  2357. # endif
  2358. #endif
  2359. </code></pre>
  2360. <p>When we define the dynamic library export symbol, we can use this macro to control the import and export.</p>
  2361. <pre><code class="lang-c">MYLIB_EXPORT void foo();
  2362. </code></pre>
  2363. <p>It is similar to CMake&#39;s <a href="https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html">GenerateExportHeader</a>.</p>
  2364. <p>However, it does not generate an independent export header file, but generates it directly in config.h.</p>
  2365. <p>For more details, see: <a href="https://github.com/xmake-io/xmake/issues/6088">#6088</a></p>
  2366. <h5 id="custompreprocessor">Custom preprocessor</h5>
  2367. <p>If the built-in build rules of xmake do not meet your needs, you can also customize the processor to rewrite the build rules, such as rewriting <code>${define_export XXX}</code>:</p>
  2368. <pre><code class="lang-lua">target("test")
  2369. set_kind("binary")
  2370. add_files("main.c")
  2371. add_configfiles("config.h.in", {
  2372. preprocessor = function (preprocessor_name, name, value, opt)
  2373. if preprocessor_name == "define_export" then
  2374. value = ([[#ifdef %s_STATIC
  2375. # define %s_EXPORT
  2376. #else
  2377. # if defined(_WIN32)
  2378. # define %s_EXPORT __declspec(dllexport)
  2379. # elif defined(__GNUC__) &amp;&amp; ((__GNUC__ >= 4) || (__GNUC__ == 3 &amp;&amp; __GNUC_MINOR__ >= 3))
  2380. # define %s_EXPORT __attribute__((visibility("default")))
  2381. # else
  2382. # define %s_EXPORT
  2383. # endif
  2384. #endif
  2385. ]]):format(name, name, name, name, name)
  2386. return value
  2387. end
  2388. end})
  2389. </code></pre>
  2390. <p>We can also override the generation of <code>${define XXX}</code> and <code>${default XXX}</code>, or even customize and extend other preprocessor configurations.</p>
  2391. <p>For example:</p>
  2392. <pre><code class="lang-lua">target("test")
  2393. set_kind("binary")
  2394. add_files("main.c")
  2395. set_configvar("FOO", "foo")
  2396. add_configfiles("config.h.in", {
  2397. preprocessor = function (preprocessor_name, name, value, opt)
  2398. local argv = opt.argv
  2399. if preprocessor_name == "define_custom" then
  2400. return string.format("#define CUSTOM_%s %s", name, value)
  2401. end
  2402. end})
  2403. </code></pre>
  2404. <p>Then we configure in config.h.in:</p>
  2405. <pre><code class="lang-c">${define_custom FOO arg1 arg2}
  2406. </code></pre>
  2407. <p>Where, <code>define_custom</code> is the custom preprocessor name, FOO is the variable name, and the variable value can be obtained from <code>set_configvar</code>.</p>
  2408. <p>arg1 and arg2 are optional preprocessing parameter lists. Whether they are needed depends on actual needs. If you want to use parameters, you can get them through <code>opt.argv</code>, which is a parameter list table.</p>
  2409. <p>After running <code>xmake config</code>, the following configuration will be automatically generated in config.h:</p>
  2410. <pre><code class="lang-c">#define CUSTOM_FOO foo
  2411. </code></pre>
  2412. <h3 id="targetset_policy">target:set_policy</h3>
  2413. <h4 id="setbuildpolicy">Set build policy</h4>
  2414. <p>Xmake has many default behaviors, such as: automatic detection and mapping of flags, cross-target parallel construction, etc. Although it provides a certain amount of intelligent processing, it is difficult to adjust and may not meet all users&#39; habits and needs.</p>
  2415. <p>Therefore, starting with v2.3.4, xmake provides modified settings for the default build strategy, which is open to users to a certain degree of configurability.</p>
  2416. <p>The usage is as follows:</p>
  2417. <pre><code class="lang-lua">set_policy("check.auto_ignore_flags", false)
  2418. </code></pre>
  2419. <p>You only need to set this configuration in the project root domain to disable the automatic detection and ignore mechanism of flags. In addition, set_policy can also take effect locally for a specific target.</p>
  2420. <pre><code class="lang-lua">target ("test")
  2421. set_policy ("check.auto_ignore_flags", false)
  2422. </code></pre>
  2423. <p>For a complete list of policies support and instructions, see: <a href="/mirror/guide/build_policies.html">build policies</a></p>
  2424. <h3 id="targetset_runtimes">target:set_runtimes</h3>
  2425. <h4 id="settheruntimelibraryofthecompilationtarget">Set the runtime library of the compilation target</h4>
  2426. <p>This is a newly added interface since v2.5.1, which is used to abstractly set the runtime library that the compilation target depends on. Currently, only the abstraction of the msvc runtime library is supported, but the mapping to other compiler runtime libraries may be expanded in the future.</p>
  2427. <p>Some of the currently supported configuration values are described as follows:</p>
  2428. <table>
  2429. <thead>
  2430. <tr>
  2431. <th>Value</th>
  2432. <th>Description</th>
  2433. </tr>
  2434. </thead>
  2435. <tbody>
  2436. <tr>
  2437. <td>MT</td>
  2438. <td>msvc runtime library: multithreaded static library</td>
  2439. </tr>
  2440. <tr>
  2441. <td>MTd</td>
  2442. <td>msvc runtime library: multithreaded static library (debug)</td>
  2443. </tr>
  2444. <tr>
  2445. <td>MD</td>
  2446. <td>msvc runtime library: multi-threaded dynamic library</td>
  2447. </tr>
  2448. <tr>
  2449. <td>MDd</td>
  2450. <td>msvc runtime library: multi-threaded dynamic library (debug)</td>
  2451. </tr>
  2452. <tr>
  2453. <td>c++_static</td>
  2454. <td>clang&#39;s c++ runtime library, static library</td>
  2455. </tr>
  2456. <tr>
  2457. <td>c++_shared</td>
  2458. <td>c++ Runtime Library, Dynamic Libraries</td>
  2459. </tr>
  2460. <tr>
  2461. <td>stdc++_static</td>
  2462. <td>c++ runtime library for gcc, static library</td>
  2463. </tr>
  2464. <tr>
  2465. <td>stdc++_shared</td>
  2466. <td>c++ runtime library for gcc, dynamic libraries</td>
  2467. </tr>
  2468. <tr>
  2469. <td>gnustl_static</td>
  2470. <td>c++ runtime library for android, static libraries, deprecated in higher NDK versions</td>
  2471. </tr>
  2472. <tr>
  2473. <td>gnustl_shared</td>
  2474. <td>c++ runtime library, static library for android, deprecated in higher NDK versions</td>
  2475. </tr>
  2476. <tr>
  2477. <td>stlport_static</td>
  2478. <td>c++ runtime library, static library for android, deprecated by NDK</td>
  2479. </tr>
  2480. <tr>
  2481. <td>stlport_static</td>
  2482. <td>c++ Runtime Library for android, static library, deprecated in higher NDK versions</td>
  2483. </tr>
  2484. </tbody>
  2485. </table>
  2486. <p>About vs runtime, you can refer to: <a href="https://docs.microsoft.com/en-us/cpp/build/reference/md-mt-ld-use-run-time-library?view =msvc-160">msvc runtime description</a></p>
  2487. <p>And this interface passes in the MT/MTd parameter configuration, xmake will automatically configure the <code>/MT /nodefaultlib:msvcrt.lib</code> parameter.</p>
  2488. <p>We can set different runtimes for different targets.</p>
  2489. <p>In addition, if we set <code>set_runtimes</code> in the global root domain, then all <code>add_requires("xx")</code> package definitions will also be globally synchronized to the corresponding vs runtime configuration</p>
  2490. <pre><code class="lang-lua">set_runtimes("MD")
  2491. add_requires("libcurl", "fmt")
  2492. target("test")
  2493. set_kind("binary")
  2494. add_files("src/*.c")
  2495. </code></pre>
  2496. <p>Of course, we can also use <code>add_requires("xx", {configs = {vs_runtime = "MD"}})</code> to modify the vs runtime library for specific packages.</p>
  2497. <p>We can also use <code>xmake f --vs_runtime=&#39;MD&#39;</code> to switch it globally through parameter configuration.</p>
  2498. <p>Issues related to this api: <a href="https://github.com/xmake-io/xmake/issues/1071#issuecomment-750817681">#1071</a></p>
  2499. <h3 id="targetset_group">target:set_group</h3>
  2500. <h4 id="settargetgroup">Set target group</h4>
  2501. <h5 id="usedforgroupdisplayofprojectfiles">Used for group display of project files</h5>
  2502. <p>This interface can be used to generate the vs/vsxmake project. The directory tree of the internal subprojects of the vs project is grouped and displayed according to the specified structure. However, grouping support may be added to other modules in the future.</p>
  2503. <p>For example, for the following grouping configuration:</p>
  2504. <pre><code class="lang-lua">add_rules("mode.debug", "mode.release")
  2505. target("test1")
  2506. set_kind("binary")
  2507. add_files("src/*.cpp")
  2508. set_group("group1")
  2509. target("test2")
  2510. set_kind("binary")
  2511. add_files("src/*.cpp")
  2512. set_group("group1")
  2513. target("test3")
  2514. set_kind("binary")
  2515. add_files("src/*.cpp")
  2516. set_group("group1/group2")
  2517. target("test4")
  2518. set_kind("binary")
  2519. add_files("src/*.cpp")
  2520. set_group("group3/group4")
  2521. target("test5")
  2522. set_kind("binary")
  2523. add_files("src/*.cpp")
  2524. target("test6")
  2525. set_kind("binary")
  2526. add_files("src/*.cpp")
  2527. </code></pre>
  2528. <p>The effect of the generated VS project directory structure is as follows:</p>
  2529. <p><img src="assets/img/manual/set_group.png" alt=""></p>
  2530. <p>For more details, please see: <a href="https://github.com/xmake-io/xmake/issues/1026">#1026</a></p>
  2531. <h5 id="compileandspecifyabatchoftargetprograms">Compile and specify a batch of target programs</h5>
  2532. <p>We can use <code>set_group()</code> to mark a given target as <code>test/benchmark/...</code> and use <code>set_default(false)</code> to disable to build it by default.</p>
  2533. <p>Then, through the <code>xmake -g xxx</code> command, you can specify to build a batch of target programs.</p>
  2534. <p>For example, we can use this feature to build all tests.</p>
  2535. <pre><code class="lang-lua">target("test1")
  2536. set_kind("binary")
  2537. set_default(false)
  2538. set_group("test")
  2539. add_files("src/*.cpp")
  2540. target("test2")
  2541. set_kind("binary")
  2542. set_default(false)
  2543. set_group("test")
  2544. add_files("src/*.cpp")
  2545. </code></pre>
  2546. <pre><code class="lang-console">$ xmake -g test
  2547. $ xmake --group=test
  2548. </code></pre>
  2549. <h5 id="runaspecifiedbatchoftargetprograms">Run a specified batch of target programs</h5>
  2550. <p>We can also specify to run all test programs with the <code>test</code> group by setting the group.</p>
  2551. <pre><code class="lang-console">$ xmake run -g test
  2552. $ xmake run --group=test
  2553. </code></pre>
  2554. <p>In addition, we can also support grouped pattern matching:</p>
  2555. <pre><code>$ xmake build -g test_*
  2556. $ xmake run -g test/foo_*
  2557. $ xmake build -g bench*
  2558. $ xmake run -g bench*
  2559. </code></pre><p>For more information: <a href="https://github.com/xmake-io/xmake/issues/1913">#1913</a></p>
  2560. <h3 id="targetadd_filegroups">target:add_filegroups</h3>
  2561. <h4 id="addsourcefilegroups">Add Source file groups</h4>
  2562. <p>This interface is currently used to group the source files generated by the vs/vsxmake/cmakelists generator.</p>
  2563. <p>If you don&#39;t set up grouping, Xmake will also display them in tree mode by default, but in some extreme cases, the directory hierarchy is not very good, e.g.</p>
  2564. <pre><code class="lang-lua">target("test")
  2565. set_kind("binary")
  2566. add_files("... /... /... /... /src/**.cpp")
  2567. </code></pre>
  2568. <p><img src="https://xmake.io/assets/img/manual/filegroup1.png" alt=""></p>
  2569. <p>Two main presentation modes are currently supported.</p>
  2570. <ul>
  2571. <li>plain: flat mode</li>
  2572. <li>tree: tree display, which is also the default mode</li>
  2573. </ul>
  2574. <p>Also, it supports grouping of files added by <code>add_headerfiles</code>.</p>
  2575. <h5 id="setthegroupandspecifiestherootdirectory">Set the group and specifies the root directory</h5>
  2576. <pre><code class="lang-lua">target("test")
  2577. set_kind("binary")
  2578. add_files("... /... /... /... /src/**.cpp")
  2579. add_filegroups("group1/group2", {rootdir = "... /... /... /... /"})
  2580. </code></pre>
  2581. <p><img src="https://xmake.io/assets/img/manual/filegroup2.png" alt=""></p>
  2582. <h5 id="setthegroupandspecifiesthefilematchingpattern">Set the group and specifies the file matching pattern</h5>
  2583. <pre><code class="lang-lua">target("test")
  2584. set_kind("binary")
  2585. add_files("... /... /... /... /src/**.cpp")
  2586. add_filegroups("group1/group2", {rootdir = "... /... /... /... /", files = {"src/**.cpp"}})
  2587. </code></pre>
  2588. <h5 id="showasflatmode">Show as flat mode</h5>
  2589. <p>In this mode, all source files ignore the nested directory hierarchy and are displayed at the same level under grouping.</p>
  2590. <pre><code class="lang-lua">target("test")
  2591. set_kind("binary")
  2592. add_files("... /... /... /... /src/**.cpp")
  2593. add_filegroups("group1/group2", {rootdir = "... /... /... /... /", mode = "plain"})
  2594. </code></pre>
  2595. <p><img src="https://xmake.io/assets/img/manual/filegroup3.png" alt=""></p>
  2596. <h3 id="targetset_exceptions">target:set_exceptions</h3>
  2597. <h4 id="enablingordisablingexceptions">Enabling or disabling exceptions</h4>
  2598. <p>We can configure C++/Objc exceptions to be enabled and disabled via this configuration.</p>
  2599. <p>Normally, if we configure them via the add_cxxflags interface, it would be cumbersome for the compiler to handle them separately, depending on the platform.</p>
  2600. <p>For example</p>
  2601. <pre><code class="lang-lua"> on_config(function (target)
  2602. if (target:has_tool("cxx", "cl")) then
  2603. target:add("cxflags", "/EHsc", {force = true})
  2604. target:add("defines", "_HAS_EXCEPTIONS=1", {force = true})
  2605. elseif(target:has_tool("cxx", "clang") or target:has_tool("cxx", "clang-cl")) then
  2606. target:add("cxflags", "-fexceptions", {force = true})
  2607. target:add("cxflags", "-fcxx-exceptions", {force = true})
  2608. end
  2609. end)
  2610. </code></pre>
  2611. <p>And with this interface, we can abstract to configure them in a compiler-independent way.</p>
  2612. <p>Enabling C++ exceptions:</p>
  2613. <pre><code class="lang-lua">set_exceptions("cxx")
  2614. </code></pre>
  2615. <p>Disable C++ exceptions:</p>
  2616. <pre><code class="lang-lua">set_exceptions("no-cxx")
  2617. </code></pre>
  2618. <p>We can also configure to turn on objc exceptions at the same time.</p>
  2619. <pre><code class="lang-lua">set_exceptions("cxx", "objc")
  2620. </code></pre>
  2621. <p>or disable them.</p>
  2622. <pre><code class="lang-lua">set_exceptions("no-cxx", "no-objc")
  2623. </code></pre>
  2624. <p>Xmake will automatically adapt the flags internally to the different compilers.</p>
  2625. <h3 id="targetset_encodings">target:set_encodings</h3>
  2626. <h4 id="setencodings">Set encodings</h4>
  2627. <p>This is a new interface in version 2.8.2, we can use this interface to set the encoding of source and target files.</p>
  2628. <p>All supported encodings: utf-8, gb2312 (msvc)</p>
  2629. <p>By default, just specifying the encoding will work for both the source and target files.</p>
  2630. <pre><code class="lang-lua">-- for all source/target encodings
  2631. set_encodings("utf-8") -- msvc: /utf-8
  2632. </code></pre>
  2633. <p>It is equivalent to:</p>
  2634. <pre><code class="lang-lua">set_encodings("source:utf-8", "target:utf-8")
  2635. </code></pre>
  2636. <p>And it only supports utf-8 encodings for now, but will be expanded in the future.</p>
  2637. <p>If we just want to set the source file encoding or the target file encoding separately, we can do that too.</p>
  2638. <h5 id="setsourceencoding">Set source encoding</h5>
  2639. <p>Usually this refers to the encoding of the source file of the compiled code, and we can set it like this.</p>
  2640. <pre><code class="lang-lua">-- gcc/clang: -finput-charset=UTF-8, msvc: -source-charset=utf-8
  2641. set_encodings("source:utf-8")
  2642. </code></pre>
  2643. <h5 id="setthetargetfileencoding">Set the target file encoding</h5>
  2644. <p>It usually refers to the runtime output encoding of the target executable.</p>
  2645. <pre><code class="lang-lua">-- gcc/clang: -fexec-charset=UTF-8, msvc: -target-charset=utf-8
  2646. set_encodings("target:utf-8")
  2647. </code></pre>
  2648. <h3 id="targetadd_forceincludes">target:add_forceincludes</h3>
  2649. <h4 id="forceincludes">forceincludes</h4>
  2650. <p>This is a new interface in 2.8.2 for forcing <code>includes</code> headers directly into configuration files.</p>
  2651. <pre><code class="lang-lua">add_forceincludes("config.h")
  2652. </code></pre>
  2653. <p>It works like <code>#include <config.h></code>, but you don&#39;t need to add it explicitly in the source code.</p>
  2654. <p>Also, its search path is controlled by <code>add_includedirs</code> instead of the direct config file path.</p>
  2655. <pre><code class="lang-lua">add_forceincludes("config.h")
  2656. add_includedirs("src")
  2657. </code></pre>
  2658. <p>By default add_forceincludes matches c/c++/objc, if you just want to match c++ you can do so:</p>
  2659. <pre><code class="lang-lua">add_forceincludes("config.h", {sourcekinds = "cxx"})
  2660. </code></pre>
  2661. <p>If you want to match multiple source file types at the same time, that&#39;s also possible:</p>
  2662. <pre><code class="lang-lua">add_forceincludes("config.h", {sourcekinds = {"cxx", "mxx"}})
  2663. </code></pre>
  2664. <h3 id="targetadd_extrafiles">target:add_extrafiles</h3>
  2665. <h4 id="addingextrafiles">Adding Extra Files</h4>
  2666. <p>This interface, also new in 2.8.2, is mainly used in projects generated by the vs/vsxmake project generator to add extra files to the project list, so that users can also quickly click on them to edit them, even though they are not code files.</p>
  2667. <p>In the future, we may use this interface for more other things as well.</p>
  2668. <pre><code class="lang-lua">add_extrafiles("assets/other.txt")
  2669. </code></pre>
  2670. <h3 id="targetadd_tests">target:add_tests</h3>
  2671. <h4 id="addtestcase">Add test case</h4>
  2672. <p>Starting from version 2.8.5, we have added a built-in test command: <code>xmake test</code>. We only need to configure some test cases through add_tests on the target that needs to be tested to automatically execute the test.</p>
  2673. <p>Even if the current target is set to <code>set_default(false)</code>, when executing tests, xmake will still automatically compile them first, and then automatically run all tests.</p>
  2674. <p>We can first look at an overall example to get a rough idea of what it looks like.</p>
  2675. <pre><code class="lang-lua">add_rules("mode.debug", "mode.release")
  2676. for _, file in ipairs(os.files("src/test_*.cpp")) do
  2677. local name = path.basename(file)
  2678. target(name)
  2679. set_kind("binary")
  2680. set_default(false)
  2681. add_files("src/" .. name .. ".cpp")
  2682. add_tests("default")
  2683. add_tests("args", {runargs = {"foo", "bar"}})
  2684. add_tests("pass_output", {trim_output = true, runargs = "foo", pass_outputs = "hello foo"})
  2685. add_tests("fail_output", {fail_outputs = {"hello2 .*", "hello xmake"}})
  2686. end
  2687. </code></pre>
  2688. <p>This example automatically scans the <code>test_*.cpp</code> source files in the source code directory, and then automatically creates a test target for each file. It is set to <code>set_default(false)</code>, which means that under normal circumstances, it will not be compiled by default. they.</p>
  2689. <p>However, if you execute <code>xmake test</code> for testing, they will be automatically compiled and then tested. The running effect is as follows:</p>
  2690. <pre><code class="lang-bash">ruki-2:test ruki$ xmake test
  2691. running tests ...
  2692. [ 2%]: test_1/args .................................... passed 7.000s
  2693. [ 5%]: test_1/default .................................... passed 5.000s
  2694. [ 8%]: test_1/fail_output .................................... passed 5.000s
  2695. [ 11%]: test_1/pass_output .................................... passed 6.000s
  2696. [ 13%]: test_2/args .................................... passed 7.000s
  2697. [ 16%]: test_2/default .................................... passed 6.000s
  2698. [ 19%]: test_2/fail_output .................................... passed 6.000s
  2699. [ 22%]: test_2/pass_output .................................... passed 6.000s
  2700. [ 25%]: test_3/args .................................... passed 7.000s
  2701. [ 27%]: test_3/default .................................... passed 7.000s
  2702. [ 30%]: test_3/fail_output .................................... passed 6.000s
  2703. [ 33%]: test_3/pass_output .................................... passed 6.000s
  2704. [ 36%]: test_4/args .................................... passed 6.000s
  2705. [ 38%]: test_4/default .................................... passed 6.000s
  2706. [ 41%]: test_4/fail_output .................................... passed 5.000s
  2707. [ 44%]: test_4/pass_output .................................... passed 6.000s
  2708. [ 47%]: test_5/args .................................... passed 5.000s
  2709. [ 50%]: test_5/default .................................... passed 6.000s
  2710. [ 52%]: test_5/fail_output .................................... failed 6.000s
  2711. [ 55%]: test_5/pass_output .................................... failed 5.000s
  2712. [ 58%]: test_6/args .................................... passed 7.000s
  2713. [ 61%]: test_6/default .................................... passed 6.000s
  2714. [ 63%]: test_6/fail_output .................................... passed 6.000s
  2715. [ 66%]: test_6/pass_output .................................... passed 6.000s
  2716. [ 69%]: test_7/args .................................... failed 6.000s
  2717. [ 72%]: test_7/default .................................... failed 7.000s
  2718. [ 75%]: test_7/fail_output .................................... failed 6.000s
  2719. [ 77%]: test_7/pass_output .................................... failed 5.000s
  2720. [ 80%]: test_8/args .................................... passed 7.000s
  2721. [ 83%]: test_8/default .................................... passed 6.000s
  2722. [ 86%]: test_8/fail_output .................................... passed 6.000s
  2723. [ 88%]: test_8/pass_output .................................... failed 5.000s
  2724. [ 91%]: test_9/args .................................... passed 6.000s
  2725. [ 94%]: test_9/default .................................... passed 6.000s
  2726. [ 97%]: test_9/fail_output .................................... passed 6.000s
  2727. [100%]: test_9/pass_output .................................... passed 6.000s
  2728. 80% tests passed, 7 tests failed out of 36, spent 0.242s
  2729. </code></pre>
  2730. <p><img src="/assets/img/manual/xmake-test1.png" alt=""></p>
  2731. <p>We can also execute <code>xmake test -vD</code> to view detailed test failure error messages:</p>
  2732. <p><img src="/assets/img/manual/xmake-test2.png" alt=""></p>
  2733. <h5 id="runthespecifiedtesttarget">Run the specified test target</h5>
  2734. <p>We can also specify to run a test with a specified target:</p>
  2735. <pre><code class="lang-bash">$ xmake test targetname/testname
  2736. </code></pre>
  2737. <p>Or run all tests of a target or a batch of tests by pattern matching:</p>
  2738. <pre><code class="lang-bash">$ xmake test targetname/*
  2739. $ xmake test targetname/foo*
  2740. </code></pre>
  2741. <p>You can also run tests with the same name for all targets:</p>
  2742. <pre><code class="lang-bash">$ xmake test */testname
  2743. </code></pre>
  2744. <h5 id="parallelizerunningtests">Parallelize running tests</h5>
  2745. <p>In fact, the default is to run in parallel, but we can adjust the parallelism of the operation through <code>-jN</code>.</p>
  2746. <pre><code class="lang-bash">$ xmake test -jN
  2747. </code></pre>
  2748. <h5 id="runtestsingroups">Run tests in groups</h5>
  2749. <pre><code class="lang-bash">$ xmake test -g "foo"
  2750. $ xmake test -g "foo*"
  2751. </code></pre>
  2752. <h5 id="addtesttotargetnoparameters">Add test to target (no parameters)</h5>
  2753. <p>If no parameters are configured, and only the test name is configured to <code>add_tests</code>, then it is only tested whether the target program will fail to run, and whether the test passes is judged based on the exit code.</p>
  2754. <pre><code>target("test")
  2755. add_tests("testname")
  2756. </code></pre><h5 id="configurerunningparameters">Configure running parameters</h5>
  2757. <p>We can also use <code>{runargs = {"arg1", "arg2"}}</code> to configure <code>add_tests</code> to specify the parameters that the test needs to run.</p>
  2758. <p>In addition, a target can be configured with multiple test cases at the same time, and each test case can be run independently without conflicting with each other.</p>
  2759. <pre><code class="lang-lua">target("test")
  2760. add_tests("testname", {runargs = "arg1"})
  2761. add_tests("testname", {runargs = {"arg1", "arg2"}})
  2762. </code></pre>
  2763. <p>If we do not configure runargs to <code>add_tests</code>, then we will also try to get the running parameters set by <code>set_runargs</code> from the bound target.</p>
  2764. <pre><code class="lang-lua">target("test")
  2765. add_tests("testname")
  2766. set_runargs("arg1", "arg2")
  2767. </code></pre>
  2768. <h5 id="configurerunningdirectory">Configure running directory</h5>
  2769. <p>We can also set the current working directory of the test run through rundir, for example:</p>
  2770. <pre><code class="lang-lua">targett("test")
  2771. add_tests("testname", {rundir = os.projectdir()})
  2772. </code></pre>
  2773. <p>If we do not configure rundir to <code>add_tests</code>, then we will also try to obtain the running directory set by <code>set_rundir</code> from the bound target.</p>
  2774. <pre><code class="lang-lua">target("test")
  2775. add_tests("testname")
  2776. set_rundir("$(projectdir)")
  2777. </code></pre>
  2778. <h5 id="configuretherunningenvironment">Configure the running environment</h5>
  2779. <p>We can also set some runtime environment variables through runenvs, for example:</p>
  2780. <pre><code class="lang-lua">target("test")
  2781. add_tests("testname", {runenvs = {LD_LIBRARY_PATH = "/lib"}})
  2782. </code></pre>
  2783. <p>If we do not configure runenvs to <code>add_tests</code>, then we will also try to obtain the running environment set by <code>add_runenvs</code> from the bound target.</p>
  2784. <pre><code class="lang-lua">target("test")
  2785. add_tests("testname")
  2786. add_runenvs("LD_LIBRARY_PATH", "/lib")
  2787. </code></pre>
  2788. <h5 id="matchingoutputresults">Matching output results</h5>
  2789. <p>By default, <code>xmake test</code> will determine whether the test passed based on whether the exit code of the test run is 0.</p>
  2790. <p>Of course, we can also determine whether the test passes by configuring whether the output result of the test run meets our specified matching pattern.</p>
  2791. <p>Mainly controlled by these two parameters:</p>
  2792. <table>
  2793. <thead>
  2794. <tr>
  2795. <th>Parameters</th>
  2796. <th>Description</th>
  2797. </tr>
  2798. </thead>
  2799. <tbody>
  2800. <tr>
  2801. <td>pass_outputs</td>
  2802. <td>The test passes if the outputs match</td>
  2803. </tr>
  2804. <tr>
  2805. <td>fail_outputs</td>
  2806. <td>If the outputs match, the test fails</td>
  2807. </tr>
  2808. </tbody>
  2809. </table>
  2810. <p>What is passed into <code>pass_outputs</code> and <code>fail_outputs</code> is a list of lua matching patterns, but the patterns are slightly simplified, such as the processing of <code>*</code>.</p>
  2811. <p>If the match is successful, the test passes and can be configured like this:</p>
  2812. <pre><code class="lang-lua">target("test")
  2813. add_tests("testname1", {pass_outputs = "hello"})
  2814. add_tests("testname2", {pass_outputs = "hello *"})
  2815. add_tests("testname3", {pass_outputs = {"hello", "hello *"}})
  2816. </code></pre>
  2817. <p>If the match is successful, the test fails. You can configure it like this:</p>
  2818. <pre><code class="lang-lua">target("test")
  2819. add_tests("testname1", {fail_outputs = "hello"})
  2820. add_tests("testname2", {fail_outputs = "hello *"})
  2821. add_tests("testname3", {fail_outputs = {"hello", "hello *"}})
  2822. </code></pre>
  2823. <p>We can also configure them simultaneously:</p>
  2824. <pre><code class="lang-lua">target("test")
  2825. add_tests("testname", {pass_outputs = "foo", fail_outputs = "hello"})
  2826. </code></pre>
  2827. <p>Since some test output results will have some newline or other blank characters at the end, which interferes with the matching mode, we can configure <code>trim_output = true</code> to truncate the blank characters before matching.</p>
  2828. <pre><code class="lang-lua">target("test")
  2829. add_tests("testname", {trim_output = true, pass_outputs = "foo", fail_outputs = "hello"})
  2830. </code></pre>
  2831. <p>We can also configure <code>{plain = true}</code> to disable lua pattern matching and only do the most basic flat text matching.</p>
  2832. <pre><code class="lang-lua">target("test")
  2833. add_tests("testname", {plain = true, pass_outputs = "foo", fail_outputs = "hello"})
  2834. </code></pre>
  2835. <h5 id="configuretestgroup">Configure test group</h5>
  2836. <p>We can also configure a test group through <code>group = "foo"</code> for group testing:</p>
  2837. <pre><code class="lang-lua">target("test")
  2838. add_tests("testname1", {group = "foo"})
  2839. add_tests("testname2", {group = "foo"})
  2840. add_tests("testname3", {group = "bar"})
  2841. add_tests("testname4", {group = "bae"})
  2842. </code></pre>
  2843. <p>Where testname1/testname2 is a group foo, and the other two are in another group.</p>
  2844. <p>Then, we can use <code>xmake test -g groupname</code> to perform group testing.</p>
  2845. <pre><code class="lang-bash">$ xmake test -g "foo"
  2846. $ xmake test -g "foo*"
  2847. </code></pre>
  2848. <p>!> Running grouping also supports pattern matching.</p>
  2849. <p>In addition, if the <code>group</code> parameter is not set to <code>add_tests</code>, we can also get the group name bound to the target by default.</p>
  2850. <pre><code class="lang-lua">target("test")
  2851. add_tests("testname")
  2852. set_group("foo")
  2853. </code></pre>
  2854. <h5 id="customtestscript">Custom test script</h5>
  2855. <p>We have also added <code>before_test</code>, <code>on_test</code> and <code>after_test</code> configuration scripts. Users can customize them in the rule and target fields to implement customized test execution.</p>
  2856. <pre><code class="lang-lua">target("test")
  2857. on_test(function (target, opt)
  2858. print(opt.name, opt.runenvs, opt.runargs, opt.pass_outputs)
  2859. -- do test
  2860. --...
  2861. -- passed
  2862. return true
  2863. -- failed
  2864. return false, errors
  2865. end)
  2866. </code></pre>
  2867. <p>Among them, all parameters passed into <code>add_tests</code> can be obtained in opt. We customize the test logic in on_test, and then return true to indicate that the test passed, return false to indicate that the test failed, and then continue to return the error message of test failure.</p>
  2868. <h5 id="automatedbuild">Automated build</h5>
  2869. <p>Since the test target usually does not need to be built during the normal development build phase, we will set <code>set_default(false)</code>.</p>
  2870. <pre><code class="lang-lua">target("test")
  2871. add_tests("testname")
  2872. set_default(false)
  2873. </code></pre>
  2874. <p>However, when running <code>xmake test</code> for testing, the targets corresponding to these tests will still be automatically built to ensure that they can be run.</p>
  2875. <pre><code class="lang-bash">$ xmake test
  2876. [25%]: cache compiling.release src/main.cpp
  2877. [50%]: linking.release test
  2878. running tests...
  2879. [100%]: test/testname ............................. passed 6.000s
  2880. 100% tests passed, 0 tests failed out of 1, spent 0.006s
  2881. </code></pre>
  2882. <h5 id="terminateifthefirsttestfails">Terminate if the first test fails</h5>
  2883. <p>By default, <code>xmake test</code> will wait until all tests have been run, no matter how many of them failed.</p>
  2884. <p>Sometimes, we want to interrupt the test directly if the first test fails, then we can enable it through the following configuration:</p>
  2885. <pre><code class="lang-lua">set_policy("test.stop_on_first_failure", true)
  2886. </code></pre>
  2887. <h5 id="ifthetestfailsreturnzero">If the test fails, return zero</h5>
  2888. <p>By default, as long as a test fails, it will return a non-zero exit code when <code>xmake test</code> is completed. This is very useful for some CI environments and can interrupt other CI scripts to continue running.</p>
  2889. <p>Then the trigger signal tells CI that we need to generate test reports and alarms.</p>
  2890. <p>Then, if we want to suppress this behavior, we can force the exit code of <code>xmake test</code> to always be set to 0.</p>
  2891. <pre><code class="lang-lua">set_policy("test.return_zero_on_failure", true)
  2892. </code></pre>
  2893. <h5 id="onlytestcompilation">Only test compilation</h5>
  2894. <p>Sometimes, we just want to test whether the code compiles or fails without running them. This can be achieved by configuring <code>build_should_pass</code> and <code>build_should_fail</code>.</p>
  2895. <pre><code class="lang-lua">target("test_10")
  2896. set_kind("binary")
  2897. set_default(false)
  2898. add_files("src/compile.cpp")
  2899. add_tests("compile_fail", {build_should_fail = true})
  2900. target("test_11")
  2901. set_kind("binary")
  2902. set_default(false)
  2903. add_files("src/compile.cpp")
  2904. add_tests("compile_pass", {build_should_pass = true})
  2905. </code></pre>
  2906. <p>This is usually used in scenarios with <code>static_assert</code> in some test code, for example:</p>
  2907. <pre><code class="lang-c++">template <typename T>
  2908. bool foo(T val) {
  2909. if constexpr (std::is_same_v<T, int>) {
  2910. printf("int!\n");
  2911. } else if constexpr (std::is_same_v<T, float>) {
  2912. printf("float!\n");
  2913. } else {
  2914. static_assert(false, "unsupported type");
  2915. }
  2916. }
  2917. int main(int, char**) {
  2918. foo("BAD");
  2919. return 0;
  2920. }
  2921. </code></pre>
  2922. <h5 id="configureadditionalcodecompilation">Configure additional code compilation</h5>
  2923. <p>When configuring test cases, we can also configure additional code that needs to be compiled for each test, as well as some macro definitions to implement inline testing.</p>
  2924. <p>xmake will compile an independent executable program for each test to run it, but this will not affect the compilation results of the target in the production environment.</p>
  2925. <pre><code class="lang-lua">target("test_13")
  2926. set_kind("binary")
  2927. set_default(false)
  2928. add_files("src/test_1.cpp")
  2929. add_tests("stub_1", {files = "tests/stub_1.cpp", defines = "STUB_1"})
  2930. target("test_14")
  2931. set_kind("binary")
  2932. set_default(false)
  2933. add_files("src/test_2.cpp")
  2934. add_tests("stub_2", {files = "tests/stub_2.cpp", defines = "STUB_2"})
  2935. target("test_15")
  2936. set_kind("binary")
  2937. set_default(false)
  2938. add_files("src/test_1.cpp")
  2939. add_tests("stub_n", {files = "tests/stub_n*.cpp", defines = "STUB_N"})
  2940. </code></pre>
  2941. <p>Taking doctest as an example, we can externally unit test without modifying any main.cpp:</p>
  2942. <pre><code class="lang-lua">add_rules("mode.debug", "mode.release")
  2943. add_requires("doctest")
  2944. target("doctest")
  2945. set_kind("binary")
  2946. add_files("src/*.cpp")
  2947. for _, testfile in ipairs(os.files("tests/*.cpp")) do
  2948. add_tests(path.basename(testfile), {
  2949. files = testfile,
  2950. remove_files = "src/main.cpp",
  2951. languages = "c++11",
  2952. packages = "doctest",
  2953. defines = "DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN"})
  2954. end
  2955. </code></pre>
  2956. <p>Defining DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN will introduce additional main entry function, so we need to configure remove_files to remove the existing main.cpp file.</p>
  2957. <p>The running effect is as follows:</p>
  2958. <pre><code class="lang-bash">ruki-2:doctest ruki$ xmake test
  2959. running tests...
  2960. [50%]: doctest/test_1 ........................ failed 0.009s
  2961. [100%]: doctest/test_2 ........................ passed 0.009s
  2962. 50% tests passed, 1 tests failed out of 2, spent 0.019s
  2963. ruki-2:doctest ruki$ xmake test -v
  2964. running tests...
  2965. [50%]: doctest/test_1 ........................ failed 0.026s
  2966. [doctest] doctest version is "2.4.11"
  2967. [doctest] run with "--help" for options
  2968. ================================================== =============================
  2969. tests/test_1.cpp:7:
  2970. TEST CASE: testing the factorial function
  2971. tests/test_1.cpp:8: ERROR: CHECK( factorial(1) == 10 ) is NOT correct!
  2972. values: CHECK( 1 == 10 )
  2973. ================================================== =============================
  2974. [doctest] test cases: 1 | 0 passed | 1 failed | 0 skipped
  2975. [doctest] assertions: 4 | 3 passed | 1 failed |
  2976. [doctest] Status: FAILURE!
  2977. run failed, exit code: 1
  2978. [100%]: doctest/test_2 ........................ passed 0.010s
  2979. 50% tests passed, 1 tests failed out of 2, spent 0.038s
  2980. </code></pre>
  2981. <h5 id="testdynamiclibrary">Test dynamic library</h5>
  2982. <p>Usually, <code>add_tests</code> is only used to run tests on executable programs. Running dynamic libraries requires an additional main entry, so we need to configure an additional executable program to load it, for example:</p>
  2983. <pre><code class="lang-lua">
  2984. target("doctest_shared")
  2985. set_kind("shared")
  2986. add_files("src/foo.cpp")
  2987. for _, testfile in ipairs(os.files("tests/*.cpp")) do
  2988. add_tests(path.basename(testfile), {
  2989. kind = "binary",
  2990. files = testfile,
  2991. languages = "c++11",
  2992. packages = "doctest",
  2993. defines = "DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN"})
  2994. end
  2995. </code></pre>
  2996. <p>Each unit test can be changed to a binary executable program through <code>kind = "binary"</code>, and the main entry function can be introduced through DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN.</p>
  2997. <p>This enables external runnable unit tests in dynamic library targets.</p>
  2998. <h5 id="configureruntimeout">Configure run timeout</h5>
  2999. <p>If some test programs get stuck if they run for a long time without exiting, we can force them to exit and return failure by configuring a timeout.</p>
  3000. <pre><code class="lang-lua">target("test_timeout")
  3001. set_kind("binary")
  3002. set_default(false)
  3003. add_files("src/run_timeout.cpp")
  3004. add_tests("run_timeout", {run_timeout = 1000})
  3005. ``
  3006. ```bash
  3007. $ xmake test
  3008. [100%]: test_timeout/run_timeout .................................... failed 1.006s
  3009. run failed, exit code: -1, exit error: wait process timeout
  3010. </code></pre>
  3011. </article>
  3012. </body>
  3013. </html>