target_instance.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>xmake</title>
  6. <link rel="icon" href="/assets/img/favicon.ico">
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  8. <meta name="description" content="Description">
  9. <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  10. <link href="/assets/npm/github-markdown/github-markdown.min.css" rel="stylesheet">
  11. <style>
  12. .markdown-body {
  13. box-sizing: border-box;
  14. min-width: 200px;
  15. max-width: 980px;
  16. margin: 0 auto;
  17. padding: 45px;
  18. }
  19. @media (max-width: 767px) {
  20. .markdown-body {
  21. padding: 15px;
  22. }
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <article class="markdown-body">
  28. <h4>This is a mirror page, please see the original page: </h4><a href="https://xmake.io/#/zh-cn/manual/target_instance">https://xmake.io/#/zh-cn/manual/target_instance</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>此页面描述了 <a href="zh-cn/manual/project_target.md">工程目标</a> 的 <code>on_load()</code>、<code>before_build()</code> 或 <code>after_install()</code> 等函数的 <code>target</code> 接口</p>
  86. <p>!> 此处文档还不完整,你也可以通过赞助或者提 pr 来加速文档的更新</p>
  87. <h4 id="targetname">target:name</h4>
  88. <ul>
  89. <li>获取目标的名字</li>
  90. </ul>
  91. <h4 id="targetget">target:get</h4>
  92. <ul>
  93. <li>获取目标在描述域的配置值</li>
  94. </ul>
  95. <p>任何在描述域的 <code>set_xxx</code> 和 <code>add_xxx</code> 配置值都可以通过这个接口获取到。</p>
  96. <pre><code class="lang-lua">-- get the links
  97. target:get("links")
  98. -- get the defined macros
  99. target:get("defines")
  100. </code></pre>
  101. <h4 id="targetset">target:set</h4>
  102. <ul>
  103. <li>设置目标的配置值,(如果你想添加值可以用 <a href="#targetadd">target:add</a>)。</li>
  104. </ul>
  105. <pre><code class="lang-lua">-- set the links
  106. target:set("links", "sdl2")
  107. -- set the defined macros
  108. target:set("defines", "SDL_MAIN_HANDLED")
  109. </code></pre>
  110. <h4 id="targetadd">target:add</h4>
  111. <ul>
  112. <li>按名称添加到目标的值</li>
  113. </ul>
  114. <pre><code class="lang-lua">-- add links
  115. target:add("links", "sdl2")
  116. -- add defined macros
  117. target:add("defines", "SDL_MAIN_HANDLED")
  118. </code></pre>
  119. <h4 id="targetkind">target:kind</h4>
  120. <ul>
  121. <li>获取目标程序类型</li>
  122. </ul>
  123. <p>对应 <code>set_kind</code> 描述域接口设置。目标类型主要有:binary, static, shared, phony, object, headeronly。</p>
  124. <h4 id="targetis_plat">target:is_plat</h4>
  125. <ul>
  126. <li>当前平台是否是给定平台之一</li>
  127. </ul>
  128. <p>尽管,我们也可以用 <code>is_plat</code> 全局接口直接判断平台,但是 xmake 支持使用 <code>set_plat</code> 针对特定 target 单独设置编译平台。</p>
  129. <p>这个时候,使用全局接口,就不适用了,所以通常我们推荐使用 target 提供的接口,来直接对当前 target 判断编译平台,更加可靠。</p>
  130. <pre><code class="lang-lua">-- Is the current platform android?
  131. target:is_plat("android")
  132. -- Is the current platform windows, linux or macosx?
  133. target:is_plat("windows", "linux", "macosx")
  134. </code></pre>
  135. <h4 id="targetis_arch">target:is_arch</h4>
  136. <ul>
  137. <li>当前架构是否是给定架构之一</li>
  138. </ul>
  139. <p>尽管,我们也可以用 <code>is_arch</code> 全局接口直接判断架构,但是 xmake 支持使用 <code>set_arch</code> 针对特定 target 单独设置编译架构。</p>
  140. <p>这个时候,使用全局接口,就不适用了,所以通常我们推荐使用 target 提供的接口,来直接对当前 target 判断编译架构,更加可靠。</p>
  141. <pre><code class="lang-lua">-- Is the current architecture x86
  142. target:is_arch("x86")
  143. -- Is the current architecture x64 or x86_64
  144. target:is_arch("x64", "x86_64")
  145. </code></pre>
  146. <h4 id="targettargetfile">target:targetfile</h4>
  147. <ul>
  148. <li>获取目标文件路径</li>
  149. </ul>
  150. <p>主要用于获取 static, shared, binary 目标程序文件的输出路径。</p>
  151. <pre><code class="lang-lua">os.cp(target:targetfile(), "/tmp/")
  152. </code></pre>
  153. <h4 id="targetartifactfile">target:artifactfile</h4>
  154. <ul>
  155. <li>获取目标的产物文件</li>
  156. </ul>
  157. <p>目前只能获取 windows DLL 的 implib 文件输出路径。</p>
  158. <pre><code class="lang-lua">target:artifactfile("implib")
  159. </code></pre>
  160. <p>不过,后期有可能会扩展到其他类型的产物文件路径获取。</p>
  161. <h4 id="targettargetdir">target:targetdir</h4>
  162. <ul>
  163. <li>获取目标文件的输出目录</li>
  164. </ul>
  165. <p>也就是 target:targetfile() 对应的存储目录。</p>
  166. <h4 id="targetbasename">target:basename</h4>
  167. <ul>
  168. <li>获取目标文件的 base 名</li>
  169. </ul>
  170. <p>也就是 libfoo.a,foo.dll, foo.exe 中的 <code>foo</code>。</p>
  171. <h4 id="targetfilename">target:filename</h4>
  172. <ul>
  173. <li>获取目标文件名</li>
  174. </ul>
  175. <p>目标文件的完整文件名,等价于 <code>path.filename(target:targetfile())</code>。</p>
  176. <h4 id="targetinstalldir">target:installdir</h4>
  177. <ul>
  178. <li>获取目标文件的安装目录</li>
  179. </ul>
  180. <p>通常用于 <code>xmake install/uninstall</code> 的 after_install 等脚本中获取对应的安装目录路径,可以用于用户自定义安装脚本。</p>
  181. <h4 id="targetautogendir">target:autogendir</h4>
  182. <ul>
  183. <li>获取自动生成目录</li>
  184. </ul>
  185. <p>这个通常用于一些自定义规则脚本中,存放一些特定于 target 的自动生成文件,路径通常在 <code>build/.gens/target</code> 下面。</p>
  186. <p>比如,我们在处理 lex/yacc 自动生成一些源码文件,就可以存放在这个目录下,方便之后去处理它。</p>
  187. <h4 id="targetobjectfile">target:objectfile</h4>
  188. <ul>
  189. <li>获取对象文件路径</li>
  190. </ul>
  191. <p>通常用于自定义脚本中,获取源文件对应的目标文件路径,例如</p>
  192. <pre><code class="lang-lua">local objectfile = target:objectfile(sourcefile)
  193. </code></pre>
  194. <h4 id="targetsourcebatches">target:sourcebatches</h4>
  195. <ul>
  196. <li>获取所有源文件</li>
  197. </ul>
  198. <p>它可以获取到 <code>add_files</code> 添加的所有源文件,并且根据不同源文件类型,分别存储。</p>
  199. <p>大概结构如下:</p>
  200. <pre><code class="lang-lua">{
  201. "c++.build" = {
  202. objectfiles = {
  203. "build/.objs/test/macosx/x86_64/release/src/main.cpp.o"
  204. },
  205. rulename = "c++.build",
  206. sourcekind = "cxx",
  207. sourcefiles = {
  208. "src/main.cpp"
  209. },
  210. dependfiles = {
  211. "build/.deps/test/macosx/x86_64/release/src/main.cpp.o.d"
  212. }
  213. },
  214. "asm.build" = {
  215. objectfiles = {
  216. "build/.objs/test/macosx/x86_64/release/src/test.S.o"
  217. },
  218. rulename = "asm.build",
  219. sourcekind = "as",
  220. sourcefiles = {
  221. "src/test.S"
  222. },
  223. dependfiles = {
  224. "build/.deps/test/macosx/x86_64/release/src/test.S.o.d"
  225. }
  226. }
  227. }
  228. </code></pre>
  229. <p>我们可以通过遍历去获取处理每种类型的源文件。</p>
  230. <pre><code class="lang-lua">for _, sourcebatch in pairs(target:sourcebatches()) do
  231. local sourcekind = sourcebatch.sourcekind
  232. if sourcekind == "cc" or sourcekind == "cxx" or sourcekind == "as" then
  233. for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
  234. -- TODO
  235. end
  236. end
  237. end
  238. </code></pre>
  239. <p>其中 sourcekind 是每种源文件的类型,cc 是 c 文件类型,cxx 是 c++ 源文件,as 是 asm 源文件。</p>
  240. <p>sourcebatch 对应每种类型的源文件 batch,对应一批同类型源文件。</p>
  241. <p>sourcebatch.sourcefiles 是源文件列表,sourcebatch.objectfiles 是对象文件列表,sourcebatch.rulename 是对应的规则名。</p>
  242. <h4 id="targetobjectfiles">target:objectfiles</h4>
  243. <ul>
  244. <li>获取所有对象文件列表</li>
  245. </ul>
  246. <p>尽管 <code>target:sourcebatches()</code> 也可以获取所有对象文件,但是它们是根据源文件类型分类过的,且不直接参与最终链接。</p>
  247. <p>如果我们想动态修改最终链接的对象文件列表,可以修改 <code>target:objectfiles()</code>,它是一个数组列表。</p>
  248. <h4 id="targetheaderfiles">target:headerfiles</h4>
  249. <ul>
  250. <li>获取所有的头文件列表</li>
  251. </ul>
  252. <p>可以获取到 <code>add_headerfiles()</code> 接口设置的所有头文件列表。</p>
  253. <pre><code class="lang-lua">for _, headerfile in ipairs(target:headerfiles()) do
  254. -- TODO
  255. end
  256. </code></pre>
  257. <h4 id="targetscriptdir">target:scriptdir</h4>
  258. <ul>
  259. <li>获取目标定义所在的 xmake.lua 目录</li>
  260. </ul>
  261. <p>这通常在自定义规则中使用的比较多,想获取当前 target 实际被定义在哪个 xmake.lua 所在目录下,方便引用一些资源文件,可以用这个接口。</p>
  262. <h4 id="targethas_cfuncs">target:has_cfuncs</h4>
  263. <ul>
  264. <li>检测目标编译配置能否获取给定的 C 函数</li>
  265. </ul>
  266. <p>这应该在 <code>on_config</code> 中使用,比如可以用它来判断当前目标能否获取到 zlib 依赖包的一些函数接口,然后自动定义 <code>HAVE_INFLATE</code>:</p>
  267. <pre><code class="lang-lua">add_requires("zlib")
  268. target("test")
  269. set_kind("binary")
  270. add_files("src/*.c")
  271. add_packages("zlib")
  272. on_config(function (target)
  273. if target:has_cfuncs("inflate", {includes = "zlib.h"}) then
  274. target:add("defines", "HAVE_INFLATE")
  275. end
  276. end)
  277. </code></pre>
  278. <p>尽管 option 也提供了类似的检测功能,但 option 的检测使用的是全局的平台工具链,它无法附带上 target 相关的一些编译配置,<br>也无法根据 target 设置不同编译工具链来适配检测,并且无法检测包里面的一些接口。</p>
  279. <p>如果我们仅仅是想粗粒度的检测函数接口,并且 target 没有额外设置不同的工具链,那么 option 提供的检测功能已经足够使用了。</p>
  280. <p>如果想要更细粒度控制检测,可以使用 target 实例接口提供的检测特性。</p>
  281. <h4 id="targethas_cxxfuncs">target:has_cxxfuncs</h4>
  282. <ul>
  283. <li>检测目标编译配置能否获取给定的 C++ 函数</li>
  284. </ul>
  285. <p>用法跟 <a href="#targethas_cfuncs">target:has_cfuncs</a> 类似,只是这里主要用于检测 C++ 的函数。</p>
  286. <p>不过,在检测函数的同时,我们还可以额外配置 std languages,来辅助检测。</p>
  287. <pre><code>target:has_cxxfuncs("foo", {includes = "foo.h", configs = {languages = "cxx17"}})
  288. </code></pre><h4 id="targethas_ctypes">target:has_ctypes</h4>
  289. <ul>
  290. <li>检测目标编译配置能否获取给定的 C 类型</li>
  291. </ul>
  292. <p>这应该在 <code>on_config</code> 中使用,如下所示:</p>
  293. <pre><code class="lang-lua">add_requires("zlib")
  294. target("test")
  295. set_kind("binary")
  296. add_files("src/*.c")
  297. add_packages("zlib")
  298. on_config(function (target)
  299. if target:has_ctypes("z_stream", {includes = "zlib.h"}) then
  300. target:add("defines", "HAVE_ZSTEAM_T")
  301. end
  302. end)
  303. </code></pre>
  304. <h4 id="targethas_cxxtypes">target:has_cxxtypes</h4>
  305. <ul>
  306. <li>检测目标编译配置能否获取给定的 C++ 类型</li>
  307. </ul>
  308. <p>用法跟 <a href="#targethas_ctypes">target:has_ctypes</a> 类似,只是这里主要用于检测 C++ 的类型。</p>
  309. <h4 id="targethas_cflags">target:has_cflags</h4>
  310. <ul>
  311. <li>检测目标编译配置能否获取给定的 C 编译 flags</li>
  312. </ul>
  313. <pre><code class="lang-lua">target("test")
  314. set_kind("binary")
  315. add_files("src/*.cpp")
  316. on_config(function (target)
  317. if target:has_cxxflags("-fPIC") then
  318. target:add("defines", "HAS_PIC")
  319. end
  320. end)
  321. </code></pre>
  322. <h4 id="targethas_cxxflags">target:has_cxxflags</h4>
  323. <ul>
  324. <li>检测目标编译配置能否获取给定的 C++ 编译 flags</li>
  325. </ul>
  326. <p>用法跟 <a href="#targethas_cflags">target:has_cflags</a> 类似,只是这里主要用于检测 C++ 的编译 flags。</p>
  327. <h4 id="targethas_cincludes">target:has_cincludes</h4>
  328. <ul>
  329. <li>检测目标编译配置能否获取给定的 C 头文件</li>
  330. </ul>
  331. <p>这应该在 <code>on_config</code> 中使用,比如可以用它来判断当前目标能否获取到 zlib 依赖包的 zlib.h 头文件,然后自动定义 <code>HAVE_INFLATE</code>:</p>
  332. <pre><code class="lang-lua">add_requires("zlib")
  333. target("test")
  334. set_kind("binary")
  335. add_files("src/*.c")
  336. add_packages("zlib")
  337. on_config(function (target)
  338. if target:has_cincludes("zlib.h") then
  339. target:add("defines", "HAVE_ZLIB_H")
  340. end
  341. end)
  342. </code></pre>
  343. <h4 id="targethas_cxxincludes">target:has_cxxincludes</h4>
  344. <ul>
  345. <li>检测目标编译配置能否获取给定的 C++ 头文件</li>
  346. </ul>
  347. <p>用法跟 <a href="#targethas_cincludes">target:has_cincludes</a> 类似,只是这里主要用于检测 C++ 的头文件。</p>
  348. <h4 id="targetcheck_csnippets">target:check_csnippets</h4>
  349. <ul>
  350. <li>检测是否可以编译和链接给定的 C 代码片段</li>
  351. </ul>
  352. <p>用法跟 <a href="#targetcheck_cxxsnippets">target:check_cxxsnippets</a> 类似,只是这里主要用于检测 C 的代码片段。</p>
  353. <h4 id="targetcheck_cxxsnippets">target:check_cxxsnippets</h4>
  354. <ul>
  355. <li>检测是否可以编译和链接给定的 C++ 代码片段</li>
  356. </ul>
  357. <p>这应该在 <code>on_config</code> 中使用,如下所示:</p>
  358. <pre><code class="lang-lua">add_requires("libtins")
  359. target("test")
  360. set_kind("binary")
  361. add_files("src/*.cpp")
  362. add_packages("libtins")
  363. on_config(function (target)
  364. local has_snippet = target:check_cxxsnippets({test = [[
  365. #include <string>
  366. using namespace Tins;
  367. void test() {
  368. std::string name = NetworkInterface::default_interface().name();
  369. printf("%s\n", name.c_str());
  370. }
  371. ]]}, {configs = {languages = "c++11"}, includes = {"tins/tins.h"}}))
  372. if has_snippet then
  373. target:add("defines", "HAS_XXX")
  374. end
  375. end)
  376. </code></pre>
  377. <p>默认仅仅检测编译链接是否通过,如果想要尝试运行时检测,可以再设置 <code>tryrun = true</code>。</p>
  378. <pre><code class="lang-lua">target("test")
  379. set_kind("binary")
  380. add_files("src/*.cpp")
  381. on_config(function (target)
  382. local has_int_4 = target:check_cxxsnippets({test = [[
  383. return (sizeof(int) == 4)? 0 : -1;
  384. ]]}, {configs = {languages = "c++11"}, tryrun = true}))
  385. if has_int_4 then
  386. target:add("defines", "HAS_INT4")
  387. end
  388. end)
  389. </code></pre>
  390. <p>我们也可以继续通过设置 <code>output = true</code> 来捕获检测的运行输出,并且加上自定义的 <code>main</code> 入口,实现完整的测试代码,而不仅仅是代码片段。</p>
  391. <pre><code class="lang-lua">target("test")
  392. set_kind("binary")
  393. add_files("src/*.cpp")
  394. on_config(function (target)
  395. local int_size = target:check_cxxsnippets({test = [[
  396. #include <stdio.h>
  397. int main(int argc, char** argv) {
  398. printf("%d", sizeof(int)); return 0;
  399. return 0;
  400. }
  401. ]]}, {configs = {languages = "c++11"}, tryrun = true, output = true}))
  402. end)
  403. </code></pre>
  404. <h4 id="targetcheck_sizeof">target:check_sizeof</h4>
  405. <ul>
  406. <li>检测类型大小</li>
  407. </ul>
  408. <pre><code class="lang-lua">add_rules("mode.debug", "mode.release")
  409. target("test")
  410. set_kind("binary")
  411. add_files("src/*.cpp")
  412. on_config(function (target)
  413. print("sizeof(long) = %s", target:check_sizeof("long"))
  414. print("sizeof(string) = %s", target:check_sizeof("std::string", {includes = "string"}))
  415. if target:check_size("long") == 8 then
  416. target:add("defines", "LONG64")
  417. end
  418. end)
  419. </code></pre>
  420. <pre><code class="lang-bash">$ xmake
  421. sizeof(long) = 8
  422. sizeof(string) = 24
  423. </code></pre>
  424. <h4 id="targethas_features">target:has_features</h4>
  425. <ul>
  426. <li>检测是否指定的 C/C++ 编译特性</li>
  427. </ul>
  428. <p>它相比使用 <code>check_cxxsnippets</code> 来检测,会更加快一些,因为它仅仅执行一次预处理就能检测所有的编译器特性,而不是每次都去调用编译器尝试编译。</p>
  429. <pre><code>target("test")
  430. set_kind("binary")
  431. add_files("src/*.cpp")
  432. on_config(function (target)
  433. if target:has_features("c_static_assert") then
  434. target:add("defines", "HAS_STATIC_ASSERT")
  435. end
  436. if target:has_features("cxx_constexpr") then
  437. target:add("defines", "HAS_CXX_CONSTEXPR")
  438. end
  439. end)
  440. </code></pre></article>
  441. </body>
  442. </html>