package_instance.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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/package_instance">https://xmake.io/#/zh-cn/manual/package_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/package_dependencies.md">包依赖管理</a> 的 <code>on_load()</code>、<code>on_install()</code> 或 <code>on_test()</code> 等函数的 <code>package</code> 接口</p>
  86. <h4 id="packagename">package:name</h4>
  87. <ul>
  88. <li>获取包的名字</li>
  89. </ul>
  90. <h4 id="packageget">package:get</h4>
  91. <ul>
  92. <li>获取包在描述域的配置值</li>
  93. </ul>
  94. <p>任何在描述域的 <code>set_xxx</code> 和 <code>add_xxx</code> 配置值都可以通过这个接口获取到。</p>
  95. <pre><code class="lang-lua">-- get the dependencies
  96. package:get("deps")
  97. -- get the links
  98. package:get("links")
  99. -- get the defined macros
  100. package:get("defines")
  101. </code></pre>
  102. <h4 id="packageset">package:set</h4>
  103. <ul>
  104. <li>设置包的配置值,(如果你想添加值可以用 <a href="#packageadd">package:add</a>)。</li>
  105. </ul>
  106. <pre><code class="lang-lua">-- set the dependencies
  107. package:set("deps", "python")
  108. -- set the links
  109. package:set("links", "sdl2")
  110. -- set the defined macros
  111. package:set("defines", "SDL_MAIN_HANDLED")
  112. </code></pre>
  113. <h4 id="packageadd">package:add</h4>
  114. <ul>
  115. <li>按名称添加到包的值</li>
  116. </ul>
  117. <pre><code class="lang-lua">-- add dependencies
  118. package:add("deps", "python")
  119. -- add links
  120. package:add("links", "sdl2")
  121. -- add defined macros
  122. package:add("defines", "SDL_MAIN_HANDLED")
  123. </code></pre>
  124. <h4 id="packagelicense">package:license</h4>
  125. <ul>
  126. <li>获取包的许可证(同<code>package:get("license")</code>)</li>
  127. </ul>
  128. <h4 id="packagedescription">package:description</h4>
  129. <ul>
  130. <li>获取包的描述(同<code>package:get("description")</code>)</li>
  131. </ul>
  132. <h4 id="packageplat">package:plat</h4>
  133. <ul>
  134. <li>获取包的平台。 可以是以下任何一种:<ul>
  135. <li>windows</li>
  136. <li>linux</li>
  137. <li>macosx</li>
  138. <li>android</li>
  139. <li>iphoneos</li>
  140. <li>watchos</li>
  141. <li>mingw</li>
  142. <li>cygwin</li>
  143. <li>bsd</li>
  144. </ul>
  145. </li>
  146. </ul>
  147. <p>如果包是二进制的,则会返回 <a href="zh-cn/manual/builtin_modules.md#oshost"><code>os.host</code></a> 的值</p>
  148. <h4 id="packagearch">package:arch</h4>
  149. <ul>
  150. <li>获取包的架构(例如 x86、x64、x86_64)</li>
  151. </ul>
  152. <p>如果包是二进制的,则返回 <a href="zh-cn/manual/builtin_modules.md#osarch"><code>os.arch</code></a></p>
  153. <h4 id="packagetargetos">package:targetos</h4>
  154. <ul>
  155. <li>获取包的目标操作系统。 可以具有与 <a href="#packageplat">package:plat</a> 相同的值</li>
  156. </ul>
  157. <h4 id="packagetargetarch">package:targetarch</h4>
  158. <ul>
  159. <li>获取包的目标架构。 可以具有与 <a href="#packagearch">package:arch</a> 相同的值</li>
  160. </ul>
  161. <h4 id="packageis_plat">package:is_plat</h4>
  162. <ul>
  163. <li>当前平台是否是给定平台之一</li>
  164. </ul>
  165. <pre><code class="lang-lua">-- Is the current platform android?
  166. package:is_plat("android")
  167. -- Is the current platform windows, linux or macosx?
  168. package:is_plat("windows", "linux", "macosx")
  169. </code></pre>
  170. <h4 id="packageis_arch">package:is_arch</h4>
  171. <ul>
  172. <li>当前架构是否是给定架构之一</li>
  173. </ul>
  174. <pre><code class="lang-lua">-- Is the current architecture x86
  175. package:is_arch("x86")
  176. -- Is the current architecture x64 or x86_64
  177. package:is_arch("x64", "x86_64")
  178. </code></pre>
  179. <h4 id="packageis_targetos">package:is_targetos</h4>
  180. <ul>
  181. <li>当前目标操作系统是否是给定操作系统之一</li>
  182. </ul>
  183. <pre><code class="lang-lua">-- Is the currently targeted OS windows?
  184. package:is_targetos("windows")
  185. -- Is the currently targeted OS android or iphoneos?
  186. package:is_targetos("android", "iphoneos")
  187. </code></pre>
  188. <h4 id="packageis_targetarch">package:is_targetarch</h4>
  189. <ul>
  190. <li>当前目标架构是否是给定架构之一</li>
  191. </ul>
  192. <pre><code class="lang-lua">-- Is the currently targeted architecture x86
  193. package:is_targetarch("x86")
  194. -- Is the currently targeted architecture x64 or x86_64
  195. package:is_targetarch("x64", "x86_64")
  196. </code></pre>
  197. <h4 id="packagealias">package:alias</h4>
  198. <ul>
  199. <li>获取包的别名</li>
  200. </ul>
  201. <p>如果用户像这样设置别名:</p>
  202. <pre><code class="lang-lua">add_requires("libsdl", {alias = "sdl"})
  203. </code></pre>
  204. <p>那么这个别名可以通过这个接口获取到:</p>
  205. <pre><code class="lang-lua">-- returns "sdl"
  206. package:alias()
  207. </code></pre>
  208. <h4 id="packageurls">package:urls</h4>
  209. <ul>
  210. <li>获取包的 urls 列表</li>
  211. </ul>
  212. <p>如果我们设置了如下 URLs</p>
  213. <pre><code class="lang-lua">add_urls("https://example.com/library-$(version).zip")
  214. -- or so
  215. set_urls("https://example.com/library-$(version).zip")
  216. </code></pre>
  217. <p>那么我们可以通过下面的接口来获取</p>
  218. <pre><code class="lang-lua">-- returns the table {"https://example.com/library-$(version).zip"}
  219. package:urls()
  220. </code></pre>
  221. <h4 id="packagedep">package:dep</h4>
  222. <ul>
  223. <li>通过名称获取包的依赖项。 该名称需要是包的依赖项。</li>
  224. </ul>
  225. <pre><code class="lang-lua">local python = package:dep("python")
  226. -- returns "python"
  227. python:name()
  228. </code></pre>
  229. <h4 id="packagedeps">package:deps</h4>
  230. <ul>
  231. <li>获取包的所有依赖项</li>
  232. </ul>
  233. <pre><code class="lang-lua">-- prints the names of all dependencies
  234. for _,dep in pairs(package:deps()) do
  235. print(dep:name())
  236. end
  237. </code></pre>
  238. <h4 id="packagesourcehash">package:sourcehash</h4>
  239. <ul>
  240. <li>获取 URL 别名的 sha256 校验和</li>
  241. </ul>
  242. <p>如果校验和是这样提供的:</p>
  243. <pre><code class="lang-lua">add_urls("https://example.com/library-$(version).zip", {alias = "example"})
  244. add_versions("example:2.4.1", "29f9983cc7196e882c4bc3d23d7492f9c47574c7cf658afafe7d00c185429941")
  245. </code></pre>
  246. <p>您可以像这样获取它:</p>
  247. <pre><code class="lang-lua">-- returns "29f9983cc7196e882c4bc3d23d7492f9c47574c7cf658afafe7d00c185429941"
  248. package:sourcehash("example")
  249. -- or so
  250. package:sourcehash(package:url_alias(package:urls()[1]))
  251. </code></pre>
  252. <h4 id="packagekind">package:kind</h4>
  253. <ul>
  254. <li>获取包的类型。 可以是以下任何一种:<ul>
  255. <li>binary</li>
  256. <li>toolchain (is also binary)</li>
  257. <li>library (default)</li>
  258. <li>template <a href="https://github.com/xmake-io/xmake/issues/2138">#2138</a></li>
  259. <li>headeronly</li>
  260. </ul>
  261. </li>
  262. </ul>
  263. <h4 id="packageis_binary">package:is_binary</h4>
  264. <ul>
  265. <li>包是否为二进制类型</li>
  266. </ul>
  267. <h4 id="packageis_toolchain">package:is_toolchain</h4>
  268. <ul>
  269. <li>报是否为工具链类型</li>
  270. </ul>
  271. <h4 id="packageis_library">package:is_library</h4>
  272. <ul>
  273. <li>包是否为库类型</li>
  274. </ul>
  275. <h4 id="packageis_toplevel">package:is_toplevel</h4>
  276. <p>-- 包是否在用户 xmake.lua 里面通过 add_requires 直接引用</p>
  277. <h4 id="packageis_thirdparty">package:is_thirdparty</h4>
  278. <ul>
  279. <li>包是否由第三方包管理器提供(例如 brew、conan、vcpkg)</li>
  280. </ul>
  281. <h4 id="packageis_debug">package:is_debug</h4>
  282. <ul>
  283. <li>包是否以调试模式构建(同<code>package:config("debug")</code>)</li>
  284. </ul>
  285. <h4 id="packageis_supported">package:is_supported</h4>
  286. <ul>
  287. <li>当前平台和架构是否支持该包</li>
  288. </ul>
  289. <h4 id="packagedebug">package:debug</h4>
  290. <ul>
  291. <li>包是否使用调试模式构建(不推荐使用:使用 <a href="#packageis_debug"><code>package:is_debug</code></a> 代替)</li>
  292. </ul>
  293. <h4 id="packageis_cross">package:is_cross</h4>
  294. <ul>
  295. <li>包是否正在交叉编译</li>
  296. </ul>
  297. <h4 id="packagecachedir">package:cachedir</h4>
  298. <ul>
  299. <li>获取包的缓存目录</li>
  300. </ul>
  301. <h4 id="packageinstalldir">package:installdir</h4>
  302. <ul>
  303. <li>获取包的安装目录。 也可用于获取子目录。 如果给定的目录树不存在,它将被创建。</li>
  304. </ul>
  305. <pre><code class="lang-lua">-- returns the installation directory
  306. package:installdir()
  307. -- returns the subdirectory include inside the installation directory
  308. package:installdir("include")
  309. -- returns the subdirectory include/files
  310. package:installdir("include", "files")
  311. </code></pre>
  312. <h4 id="packagescriptdir">package:scriptdir</h4>
  313. <ul>
  314. <li>获取包的xmake.lua所在目录</li>
  315. </ul>
  316. <h4 id="packageenvs">package:envs</h4>
  317. <ul>
  318. <li>获取包导出的环境变量</li>
  319. </ul>
  320. <h4 id="packagegetenv">package:getenv</h4>
  321. <ul>
  322. <li>获取给定的环境变量</li>
  323. </ul>
  324. <pre><code class="lang-lua">-- returns a table
  325. package:getenv("PATH")
  326. </code></pre>
  327. <h4 id="packagesetenv">package:setenv</h4>
  328. <ul>
  329. <li>设置给定的环境变量。 覆盖变量</li>
  330. </ul>
  331. <pre><code class="lang-lua">-- sets PATH to {"bin", "lib"}
  332. package:setenv("PATH", "bin", "lib")
  333. </code></pre>
  334. <h4 id="packageaddenv">package:addenv</h4>
  335. <ul>
  336. <li>将给定的值添加到环境变量</li>
  337. </ul>
  338. <pre><code class="lang-lua">-- adds "bin" and "lib" to PATH
  339. package:addenv("PATH", "bin", "lib")
  340. </code></pre>
  341. <h4 id="packageversions">package:versions</h4>
  342. <ul>
  343. <li>获取包的所有版本列表。</li>
  344. </ul>
  345. <h4 id="packageversion">package:version</h4>
  346. <ul>
  347. <li>获取包的版本</li>
  348. </ul>
  349. <p>它会返回一个语义版本对象,便于做版本之间的判断。</p>
  350. <pre><code class="lang-lua">local version = package:version()
  351. -- get the major version
  352. version:major()
  353. -- get the minor version
  354. version:minor()
  355. -- get the patch version
  356. version:patch()
  357. </code></pre>
  358. <h4 id="packageversion_str">package:version_str</h4>
  359. <ul>
  360. <li>以字符串形式获取包的版本</li>
  361. </ul>
  362. <h4 id="packageconfig">package:config</h4>
  363. <ul>
  364. <li>获取包的给定配置值</li>
  365. </ul>
  366. <pre><code class="lang-lua">-- if configurations are set like so
  367. add_require("example", {configs = {enable_x = true, value_y = 6}})
  368. -- these values can be retrieved like so
  369. -- returns true
  370. package:config("enable_x")
  371. -- returns 6
  372. package:config("value_y")
  373. </code></pre>
  374. <h4 id="packageconfig_set">package:config_set</h4>
  375. <ul>
  376. <li>设置包的给定配置值</li>
  377. </ul>
  378. <pre><code class="lang-lua">package:config_set("enable_x", true)
  379. package:config_set("value_y", 6)
  380. </code></pre>
  381. <h4 id="packageconfigs">package:configs</h4>
  382. <ul>
  383. <li>获取包的所有配置</li>
  384. </ul>
  385. <pre><code class="lang-lua">-- returns a table with the configuration names as keys and their values as values
  386. local configs = package:configs()
  387. local enable_x = configs["enable_x"]
  388. local value_y = configs["value_y"]
  389. </code></pre>
  390. <h4 id="packagebuildhash">package:buildhash</h4>
  391. <ul>
  392. <li>获取包的构建哈希</li>
  393. </ul>
  394. <p>它确保每个包,不同的配置安装到唯一的路径下,相互之间不冲突。</p>
  395. <h4 id="packagepatches">package:patches</h4>
  396. <ul>
  397. <li>获取当前版本的所有补丁</li>
  398. </ul>
  399. <pre><code class="lang-lua">-- returns a table with all patches
  400. local patches = package:patches()
  401. -- each element contains the keys "url" and "sha256"
  402. local url = patches[1]["url"]
  403. local sha256 = patches[1]["sha256"]
  404. </code></pre>
  405. <h4 id="packagehas_cfuncs">package:has_cfuncs</h4>
  406. <ul>
  407. <li>检测包是否具有给定的 C 函数</li>
  408. </ul>
  409. <p>这应该在 <code>on_test</code> 中使用,如下所示:</p>
  410. <pre><code class="lang-lua">on_test(function (package)
  411. assert(package:has_cfuncs("foo"))
  412. -- you can also add configs
  413. assert(package:has_cfuncs("bar", {includes = "foo_bar.h"}))
  414. assert(package:has_cfuncs("blob", {includes = "blob.h", configs = {defines = "USE_BLOB"}}))
  415. -- you can even set the language
  416. assert(package:has_cfuncs("bla", {configs = {languages = "c99"}}))
  417. end)
  418. </code></pre>
  419. <h4 id="packagehas_cxxfuncs">package:has_cxxfuncs</h4>
  420. <ul>
  421. <li>检测包是否具有给定的 C++ 函数</li>
  422. </ul>
  423. <p>这应该在 <code>on_test</code> 中使用,如下所示:</p>
  424. <pre><code class="lang-lua">on_test(function (package)
  425. assert(package:has_cxxfuncs("foo"))
  426. -- you can also add configs
  427. assert(package:has_cxxfuncs("bar", {includes = "foo_bar.hpp"}))
  428. assert(package:has_cxxfuncs("blob", {includes = "blob.hpp", configs = {defines = "USE_BLOB"}}))
  429. -- you can even set the language
  430. assert(package:has_cxxfuncs("bla", {configs = {languages = "cxx17"}}))
  431. end)
  432. </code></pre>
  433. <h4 id="packagehas_ctypes">package:has_ctypes</h4>
  434. <ul>
  435. <li>检测包是否具有给定的 C 类型</li>
  436. </ul>
  437. <p>这应该在 <code>on_test</code> 中使用,如下所示:</p>
  438. <pre><code class="lang-lua">on_test(function (package)
  439. assert(package:has_ctypes("foo"))
  440. -- you can also add configs
  441. assert(package:has_ctypes("bar", {includes = "foo_bar.h"}))
  442. assert(package:has_ctypes("blob", {includes = "blob.h", configs = {defines = "USE_BLOB"}}))
  443. -- you can even set the language
  444. assert(package:has_ctypes("bla", {configs = {languages = "c99"}}))
  445. end)
  446. </code></pre>
  447. <h4 id="packagehas_cxxtypes">package:has_cxxtypes</h4>
  448. <ul>
  449. <li>检测包是否具有给定的 C++ 类型</li>
  450. </ul>
  451. <p>这应该在 <code>on_test</code> 中使用,如下所示:</p>
  452. <pre><code class="lang-lua">on_test(function (package)
  453. assert(package:has_cxxtypes("foo"))
  454. -- you can also add configs
  455. assert(package:has_cxxtypes("bar", {includes = "foo_bar.hpp"}))
  456. assert(package:has_cxxtypes("blob", {includes = "blob.hpp", configs = {defines = "USE_BLOB"}}))
  457. -- you can even set the language
  458. assert(package:has_cxxtypes("bla", {configs = {languages = "cxx17"}}))
  459. end)
  460. </code></pre>
  461. <h4 id="packagehas_cincludes">package:has_cincludes</h4>
  462. <ul>
  463. <li>检测包是否具有给定的 C 头文件</li>
  464. </ul>
  465. <p>这应该在 <code>on_test</code> 中使用,如下所示:</p>
  466. <pre><code class="lang-lua">on_test(function (package)
  467. assert(package:has_cincludes("foo.h"))
  468. end)
  469. </code></pre>
  470. <h4 id="packagehas_cxxincludes">package:has_cxxincludes</h4>
  471. <ul>
  472. <li>检测包是否具有给定的 C++ 头文件</li>
  473. </ul>
  474. <p>这应该在 <code>on_test</code> 中使用,如下所示:</p>
  475. <pre><code class="lang-lua">on_test(function (package)
  476. assert(package:has_cincludes("foo.hpp"))
  477. end)
  478. </code></pre>
  479. <h4 id="packagecheck_csnippets">package:check_csnippets</h4>
  480. <ul>
  481. <li>检测是否可以编译和链接给定的 C 代码片段</li>
  482. </ul>
  483. <p>这应该在 <code>on_test</code> 中使用,如下所示:</p>
  484. <pre><code class="lang-lua">on_test(function (package)
  485. assert(package:check_csnippets({test = [[
  486. #define USE_BLOB
  487. #include <blob.h>
  488. void test(int argc, char** argv) {
  489. foo bar;
  490. printf("%s", bar.blob);
  491. }
  492. ]]}, {configs = {languages = "c99"}, includes = "foo.h"}))
  493. end)
  494. </code></pre>
  495. <h4 id="packagecheck_cxxsnippets">package:check_cxxsnippets</h4>
  496. <ul>
  497. <li>检测是否可以编译和链接给定的 C++ 代码片段</li>
  498. </ul>
  499. <p>这应该在 <code>on_test</code> 中使用,如下所示:</p>
  500. <pre><code class="lang-lua">on_test(function (package)
  501. assert(package:check_cxxsnippets({test = [[
  502. #define USE_BLOB
  503. #include <blob.hpp>
  504. void test(int argc, char** argv) {
  505. foo bar();
  506. std::cout << bar.blob;
  507. }
  508. ]]}, {configs = {languages = "cxx11"}, includes = "foo.hpp"}))
  509. end)
  510. </code></pre>
  511. <h4 id="packagecheck_fcsnippets">package:check_fcsnippets</h4>
  512. <ul>
  513. <li>检测是否可以编译和链接给定的 Fortran 代码片段</li>
  514. </ul>
  515. <p>用法如上</p>
  516. </article>
  517. </body>
  518. </html>