package_instance.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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/package_instance">https://xmake.io/#/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>This page describes the interface for <code>package</code> of functions like <code>on_load()</code>, <code>on_install()</code> or <code>on_test()</code> of the <a href="manual/package_dependencies.md">Package Dependencies</a></p>
  86. <h4 id="packagename">package:name</h4>
  87. <ul>
  88. <li>Get the name of the package</li>
  89. </ul>
  90. <h4 id="packageget">package:get</h4>
  91. <ul>
  92. <li>Get the values of the package by name</li>
  93. </ul>
  94. <pre><code class="lang-lua">-- get the dependencies
  95. package:get("deps")
  96. -- get the links
  97. package:get("links")
  98. -- get the defined macros
  99. package:get("defines")
  100. </code></pre>
  101. <h4 id="packageset">package:set</h4>
  102. <ul>
  103. <li>Set the values of the package by name (If you just want to add values use <a href="#packageadd">package:add</a>)</li>
  104. </ul>
  105. <pre><code class="lang-lua">-- set the dependencies
  106. package:set("deps", "python")
  107. -- set the links
  108. package:set("links", "sdl2")
  109. -- set the defined macros
  110. package:set("defines", "SDL_MAIN_HANDLED")
  111. </code></pre>
  112. <h4 id="packageadd">package:add</h4>
  113. <ul>
  114. <li>Add to the values of the package by name</li>
  115. </ul>
  116. <pre><code class="lang-lua">-- add dependencies
  117. package:add("deps", "python")
  118. -- add links
  119. package:add("links", "sdl2")
  120. -- add defined macros
  121. package:add("defines", "SDL_MAIN_HANDLED")
  122. </code></pre>
  123. <h4 id="packagelicense">package:license</h4>
  124. <ul>
  125. <li>Get the license of the package (Same as <code>package:get("license")</code>)</li>
  126. </ul>
  127. <h4 id="packagedescription">package:description</h4>
  128. <ul>
  129. <li>Get the description of the package (Same as <code>package:get("description")</code>)</li>
  130. </ul>
  131. <h4 id="packageplat">package:plat</h4>
  132. <ul>
  133. <li>Get the platform of the package. Can be any of:<ul>
  134. <li>windows</li>
  135. <li>linux</li>
  136. <li>macosx</li>
  137. <li>android</li>
  138. <li>iphoneos</li>
  139. <li>watchos</li>
  140. <li>mingw</li>
  141. <li>cygwin</li>
  142. <li>bsd</li>
  143. </ul>
  144. </li>
  145. </ul>
  146. <p>If the package is binary <a href="manual/builtin_modules.md#oshost"><code>os.host</code></a> is returned</p>
  147. <h4 id="packagearch">package:arch</h4>
  148. <ul>
  149. <li>Get the architecture of the package (e.g. x86, x64, x86_64)</li>
  150. </ul>
  151. <p>If the package is binary <a href="manual/builtin_modules.md#osarch"><code>os.arch</code></a> is returned</p>
  152. <h4 id="packagetargetos">package:targetos</h4>
  153. <ul>
  154. <li>Get the targeted OS of the package. Can have the same values as <a href="#packageplat">package:plat</a></li>
  155. </ul>
  156. <h4 id="packagetargetarch">package:targetarch</h4>
  157. <ul>
  158. <li>Get the targeted architecture of the package. Can have the same values as <a href="#packagearch">package:arch</a></li>
  159. </ul>
  160. <h4 id="packageis_plat">package:is_plat</h4>
  161. <ul>
  162. <li>Wether the current platform is one of the given platforms</li>
  163. </ul>
  164. <pre><code class="lang-lua">-- Is the current platform android?
  165. package:is_plat("android")
  166. -- Is the current platform windows, linux or macosx?
  167. package:is_plat("windows", "linux", "macosx")
  168. </code></pre>
  169. <h4 id="packageis_arch">package:is_arch</h4>
  170. <ul>
  171. <li>Wether the current platform is one of the given platforms</li>
  172. </ul>
  173. <pre><code class="lang-lua">-- Is the current architecture x86
  174. package:is_arch("x86")
  175. -- Is the current architecture x64 or x86_64
  176. package:is_arch("x64", "x86_64")
  177. </code></pre>
  178. <h4 id="packageis_targetos">package:is_targetos</h4>
  179. <ul>
  180. <li>Wether the currently targeted OS is one of the given OS</li>
  181. </ul>
  182. <pre><code class="lang-lua">-- Is the currently targeted OS windows?
  183. package:is_targetos("windows")
  184. -- Is the currently targeted OS android or iphoneos?
  185. package:is_targetos("android", "iphoneos")
  186. </code></pre>
  187. <h4 id="packageis_targetarch">package:is_targetarch</h4>
  188. <ul>
  189. <li>Wether the currently targeted architecture is one of the given architectures</li>
  190. </ul>
  191. <pre><code class="lang-lua">-- Is the currently targeted architecture x86
  192. package:is_targetarch("x86")
  193. -- Is the currently targeted architecture x64 or x86_64
  194. package:is_targetarch("x64", "x86_64")
  195. </code></pre>
  196. <h4 id="packagealias">package:alias</h4>
  197. <ul>
  198. <li>Get the alias of the package</li>
  199. </ul>
  200. <p>If the user sets an alias like so:</p>
  201. <pre><code class="lang-lua">add_requires("libsdl", {alias = "sdl"})
  202. </code></pre>
  203. <p>This alias can be retrieved by</p>
  204. <pre><code class="lang-lua">-- returns "sdl"
  205. package:alias()
  206. </code></pre>
  207. <h4 id="packageurls">package:urls</h4>
  208. <ul>
  209. <li>Get the URLs of the package</li>
  210. </ul>
  211. <p>Retrieve the URLs set by:</p>
  212. <pre><code class="lang-lua">add_urls("https://example.com/library-$(version).zip")
  213. -- or so
  214. set_urls("https://example.com/library-$(version).zip")
  215. </code></pre>
  216. <p>Then write this:</p>
  217. <pre><code class="lang-lua">-- returns the table {"https://example.com/library-$(version).zip"}
  218. package:urls()
  219. </code></pre>
  220. <h4 id="packagedep">package:dep</h4>
  221. <ul>
  222. <li>Get a dependency of the package by name. The name needs to be a dependency of the package.</li>
  223. </ul>
  224. <pre><code class="lang-lua">local python = package:dep("python")
  225. -- returns "python"
  226. python:name()
  227. </code></pre>
  228. <h4 id="packagedeps">package:deps</h4>
  229. <ul>
  230. <li>Get all dependencies of the package</li>
  231. </ul>
  232. <pre><code class="lang-lua">-- prints the names of all dependencies
  233. for _,dep in pairs(package:deps()) do
  234. print(dep:name())
  235. end
  236. </code></pre>
  237. <h4 id="packagesourcehash">package:sourcehash</h4>
  238. <ul>
  239. <li>Get the sha256 checksum of an URL alias</li>
  240. </ul>
  241. <p>If the checksum is provided like so:</p>
  242. <pre><code class="lang-lua">add_urls("https://example.com/library-$(version).zip", {alias = "example"})
  243. add_versions("example:2.4.1", "29f9983cc7196e882c4bc3d23d7492f9c47574c7cf658afafe7d00c185429941")
  244. </code></pre>
  245. <p>You can retrieve the checksum like so:</p>
  246. <pre><code class="lang-lua">-- returns "29f9983cc7196e882c4bc3d23d7492f9c47574c7cf658afafe7d00c185429941"
  247. package:sourcehash("example")
  248. -- or so
  249. package:sourcehash(package:url_alias(package:urls()[1]))
  250. </code></pre>
  251. <h4 id="packagekind">package:kind</h4>
  252. <ul>
  253. <li>Get the kind of the package. Can be any of:<ul>
  254. <li>binary</li>
  255. <li>toolchain (is also binary)</li>
  256. <li>library (default)</li>
  257. <li>template <a href="https://github.com/xmake-io/xmake/issues/2138">#2138</a></li>
  258. <li>headeronly</li>
  259. </ul>
  260. </li>
  261. </ul>
  262. <h4 id="packageis_binary">package:is_binary</h4>
  263. <ul>
  264. <li>Wether the package is of kind binary</li>
  265. </ul>
  266. <h4 id="packageis_toolchain">package:is_toolchain</h4>
  267. <ul>
  268. <li>Wether the package is of kind toolchain</li>
  269. </ul>
  270. <h4 id="packageis_library">package:is_library</h4>
  271. <ul>
  272. <li>Wether the package is of kind library</li>
  273. </ul>
  274. <h4 id="packageis_toplevel">package:is_toplevel</h4>
  275. <ul>
  276. <li>Wether the package is directly required by the user (e.g. xmake.lua)</li>
  277. </ul>
  278. <h4 id="packageis_thirdparty">package:is_thirdparty</h4>
  279. <ul>
  280. <li>Wether the package is provided by a thirdparty package manager (e.g. brew, conan, vcpkg)</li>
  281. </ul>
  282. <h4 id="packageis_debug">package:is_debug</h4>
  283. <ul>
  284. <li>Wether the package is build with debug mode (Same as <code>package:config("debug")</code>)</li>
  285. </ul>
  286. <h4 id="packageis_supported">package:is_supported</h4>
  287. <ul>
  288. <li>Wether the package is supported by the current platform and architecture</li>
  289. </ul>
  290. <h4 id="packagedebug">package:debug</h4>
  291. <ul>
  292. <li>Wether the the package gets built with debug mode (deprecated: use <a href="#packageis_debug"><code>package:is_debug</code></a> instead)</li>
  293. </ul>
  294. <h4 id="packageis_cross">package:is_cross</h4>
  295. <ul>
  296. <li>Wether the package is getting cross-compiled</li>
  297. </ul>
  298. <h4 id="packagecachedir">package:cachedir</h4>
  299. <ul>
  300. <li>Get the cache directory of the package</li>
  301. </ul>
  302. <h4 id="packageinstalldir">package:installdir</h4>
  303. <ul>
  304. <li>Get the installation directory of the package. Can also be used to get a subdirectory. If the given directory tree does not exist it will be created.</li>
  305. </ul>
  306. <pre><code class="lang-lua">-- returns the installation directory
  307. package:installdir()
  308. -- returns the subdirectory include inside the installation directory
  309. package:installdir("include")
  310. -- returns the subdirectory include/files
  311. package:installdir("include", "files")
  312. </code></pre>
  313. <h4 id="packagescriptdir">package:scriptdir</h4>
  314. <ul>
  315. <li>Get the directory where the xmake.lua of the package lies</li>
  316. </ul>
  317. <h4 id="packageenvs">package:envs</h4>
  318. <ul>
  319. <li>Get the exported environment variables of the package</li>
  320. </ul>
  321. <h4 id="packagegetenv">package:getenv</h4>
  322. <ul>
  323. <li>Get the given environment variable</li>
  324. </ul>
  325. <pre><code class="lang-lua">-- returns a table
  326. package:getenv("PATH")
  327. </code></pre>
  328. <h4 id="packagesetenv">package:setenv</h4>
  329. <ul>
  330. <li>Set the given environment variable. Overwrites the variable</li>
  331. </ul>
  332. <pre><code class="lang-lua">-- sets PATH to {"bin", "lib"}
  333. package:setenv("PATH", "bin", "lib")
  334. </code></pre>
  335. <h4 id="packageaddenv">package:addenv</h4>
  336. <ul>
  337. <li>Add the given values to the environment variable</li>
  338. </ul>
  339. <pre><code class="lang-lua">-- adds "bin" and "lib" to PATH
  340. package:addenv("PATH", "bin", "lib")
  341. </code></pre>
  342. <h4 id="packageversions">package:versions</h4>
  343. <ul>
  344. <li>Get all version strings of the package. Returns a table containing all versions as strings</li>
  345. </ul>
  346. <h4 id="packageversion">package:version</h4>
  347. <ul>
  348. <li>Get the version of the package</li>
  349. </ul>
  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>Get the version of the package as string</li>
  361. </ul>
  362. <h4 id="packageconfig">package:config</h4>
  363. <ul>
  364. <li>Get the given configuration value of the package</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>Set the given configuration value of the package</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>Get all configurations of the package</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>Get the build hash of the package</li>
  393. </ul>
  394. <h4 id="packagepatches">package:patches</h4>
  395. <ul>
  396. <li>Get all patches of the current version</li>
  397. </ul>
  398. <pre><code class="lang-lua">-- returns a table with all patches
  399. local patches = package:patches()
  400. -- each element contains the keys "url" and "sha256"
  401. local url = patches[1]["url"]
  402. local sha256 = patches[1]["sha256"]
  403. </code></pre>
  404. <h4 id="packagehas_cfuncs">package:has_cfuncs</h4>
  405. <ul>
  406. <li>Wether the package has the given C functions</li>
  407. </ul>
  408. <p>This should be used inside <code>on_test</code> like so:</p>
  409. <pre><code class="lang-lua">on_test(function (package)
  410. assert(package:has_cfuncs("foo"))
  411. -- you can also add configs
  412. assert(package:has_cfuncs("bar", {includes = "foo_bar.h"}))
  413. assert(package:has_cfuncs("blob", {includes = "blob.h", configs = {defines = "USE_BLOB"}}))
  414. -- you can even set the language
  415. assert(package:has_cfuncs("bla", {configs = {languages = "c99"}}))
  416. end)
  417. </code></pre>
  418. <h4 id="packagehas_cxxfuncs">package:has_cxxfuncs</h4>
  419. <ul>
  420. <li>Wether the package has the given C++ functions</li>
  421. </ul>
  422. <p>This should be used inside <code>on_test</code> like so:</p>
  423. <pre><code class="lang-lua">on_test(function (package)
  424. assert(package:has_cxxfuncs("foo"))
  425. -- you can also add configs
  426. assert(package:has_cxxfuncs("bar", {includes = "foo_bar.hpp"}))
  427. assert(package:has_cxxfuncs("blob", {includes = "blob.hpp", configs = {defines = "USE_BLOB"}}))
  428. -- you can even set the language
  429. assert(package:has_cxxfuncs("bla", {configs = {languages = "cxx17"}}))
  430. end)
  431. </code></pre>
  432. <h4 id="packagehas_ctypes">package:has_ctypes</h4>
  433. <ul>
  434. <li>Wether the package has the given C types</li>
  435. </ul>
  436. <p>This should be used inside <code>on_test</code> like so:</p>
  437. <pre><code class="lang-lua">on_test(function (package)
  438. assert(package:has_ctypes("foo"))
  439. -- you can also add configs
  440. assert(package:has_ctypes("bar", {includes = "foo_bar.h"}))
  441. assert(package:has_ctypes("blob", {includes = "blob.h", configs = {defines = "USE_BLOB"}}))
  442. -- you can even set the language
  443. assert(package:has_ctypes("bla", {configs = {languages = "c99"}}))
  444. end)
  445. </code></pre>
  446. <h4 id="packagehas_cxxtypes">package:has_cxxtypes</h4>
  447. <ul>
  448. <li>Wether the package has the given C++ types</li>
  449. </ul>
  450. <p>This should be used inside <code>on_test</code> like so:</p>
  451. <pre><code class="lang-lua">on_test(function (package)
  452. assert(package:has_cxxtypes("foo"))
  453. -- you can also add configs
  454. assert(package:has_cxxtypes("bar", {includes = "foo_bar.hpp"}))
  455. assert(package:has_cxxtypes("blob", {includes = "blob.hpp", configs = {defines = "USE_BLOB"}}))
  456. -- you can even set the language
  457. assert(package:has_cxxtypes("bla", {configs = {languages = "cxx17"}}))
  458. end)
  459. </code></pre>
  460. <h4 id="packagehas_cincludes">package:has_cincludes</h4>
  461. <ul>
  462. <li>Wether the package has the given C header files</li>
  463. </ul>
  464. <p>This should be used in <code>on_test</code> like so:</p>
  465. <pre><code class="lang-lua">on_test(function (package)
  466. assert(package:has_cincludes("foo.h"))
  467. end)
  468. </code></pre>
  469. <h4 id="packagehas_cxxincludes">package:has_cxxincludes</h4>
  470. <ul>
  471. <li>Wether the package has the given C++ header files</li>
  472. </ul>
  473. <p>This should be used in <code>on_test</code> like so:</p>
  474. <pre><code class="lang-lua">on_test(function (package)
  475. assert(package:has_cxxincludes("foo.hpp"))
  476. end)
  477. </code></pre>
  478. <h4 id="packagecheck_csnippets">package:check_csnippets</h4>
  479. <ul>
  480. <li>Wether the given C snippet can be compiled and linked</li>
  481. </ul>
  482. <p>This should be used in <code>on_test</code> like so:</p>
  483. <pre><code class="lang-lua">on_test(function (package)
  484. assert(package:check_csnippets({test = [[
  485. #define USE_BLOB
  486. #include <blob.h>
  487. void test(int argc, char** argv) {
  488. foo bar;
  489. printf("%s", bar.blob);
  490. }
  491. ]]}, {configs = {languages = "c99"}, includes = "foo.h"}))
  492. end)
  493. </code></pre>
  494. <h4 id="packagecheck_cxxsnippets">package:check_cxxsnippets</h4>
  495. <ul>
  496. <li>Wether the given C++ snippet can be compiled and linked</li>
  497. </ul>
  498. <p>This should be used in <code>on_test</code> like so:</p>
  499. <pre><code class="lang-lua">on_test(function (package)
  500. assert(package:check_cxxsnippets({test = [[
  501. #define USE_BLOB
  502. #include <blob.hpp>
  503. void test(int argc, char** argv) {
  504. foo bar();
  505. std::cout << bar.blob;
  506. }
  507. ]]}, {configs = {languages = "cxx11"}, includes = "foo.hpp"}))
  508. end)
  509. </code></pre>
  510. <h4 id="packagecheck_fcsnippets">package:check_fcsnippets</h4>
  511. <ul>
  512. <li>Wether the given Fortran snippet can be compiled and linked</li>
  513. </ul>
  514. <p>see above</p>
  515. </article>
  516. </body>
  517. </html>