This is a mirror page, please see the original page:

https://xmake.io/#/zh-cn/manual/package_instance

此页面描述了 包依赖管理on_load()on_install()on_test() 等函数的 package 接口

package:name

package:get

任何在描述域的 set_xxxadd_xxx 配置值都可以通过这个接口获取到。

-- get the dependencies
package:get("deps")
-- get the links
package:get("links")
-- get the defined macros
package:get("defines")

package:set

-- set the dependencies
package:set("deps", "python")
-- set the links
package:set("links", "sdl2")
-- set the defined macros
package:set("defines", "SDL_MAIN_HANDLED")

package:add

-- add dependencies
package:add("deps", "python")
-- add links
package:add("links", "sdl2")
-- add defined macros
package:add("defines", "SDL_MAIN_HANDLED")

package:license

package:description

package:plat

如果包是二进制的,则会返回 os.host 的值

package:arch

如果包是二进制的,则返回 os.arch

package:targetos

package:targetarch

package:is_plat

-- Is the current platform android?
package:is_plat("android")
-- Is the current platform windows, linux or macosx?
package:is_plat("windows", "linux", "macosx")

package:is_arch

-- Is the current architecture x86
package:is_arch("x86")
-- Is the current architecture x64 or x86_64
package:is_arch("x64", "x86_64")

package:is_targetos

-- Is the currently targeted OS windows?
package:is_targetos("windows")
-- Is the currently targeted OS android or iphoneos?
package:is_targetos("android", "iphoneos")

package:is_targetarch

-- Is the currently targeted architecture x86
package:is_targetarch("x86")
-- Is the currently targeted architecture x64 or x86_64
package:is_targetarch("x64", "x86_64")

package:alias

如果用户像这样设置别名:

add_requires("libsdl", {alias = "sdl"})

那么这个别名可以通过这个接口获取到:

-- returns "sdl"
package:alias()

package:urls

如果我们设置了如下 URLs

add_urls("https://example.com/library-$(version).zip")
-- or so
set_urls("https://example.com/library-$(version).zip")

那么我们可以通过下面的接口来获取

-- returns the table {"https://example.com/library-$(version).zip"}
package:urls()

package:dep

local python = package:dep("python")
-- returns "python"
python:name()

package:deps

-- prints the names of all dependencies
for _,dep in pairs(package:deps()) do
    print(dep:name())
end

package:sourcehash

如果校验和是这样提供的:

add_urls("https://example.com/library-$(version).zip", {alias = "example"})
add_versions("example:2.4.1", "29f9983cc7196e882c4bc3d23d7492f9c47574c7cf658afafe7d00c185429941")

您可以像这样获取它:

-- returns "29f9983cc7196e882c4bc3d23d7492f9c47574c7cf658afafe7d00c185429941"
package:sourcehash("example")
-- or so
package:sourcehash(package:url_alias(package:urls()[1]))

package:kind

package:is_binary

package:is_toolchain

package:is_library

package:is_toplevel

-- 包是否在用户 xmake.lua 里面通过 add_requires 直接引用

package:is_thirdparty

package:is_debug

package:is_supported

package:debug

package:is_cross

package:cachedir

package:installdir

-- returns the installation directory
package:installdir()
-- returns the subdirectory include inside the installation directory
package:installdir("include")
-- returns the subdirectory include/files
package:installdir("include", "files")

package:scriptdir

package:envs

package:getenv

-- returns a table
package:getenv("PATH")

package:setenv

-- sets PATH to {"bin", "lib"}
package:setenv("PATH", "bin", "lib")

package:addenv

-- adds "bin" and "lib" to PATH
package:addenv("PATH", "bin", "lib")

package:versions

package:version

它会返回一个语义版本对象,便于做版本之间的判断。

local version = package:version()
-- get the major version
version:major()
-- get the minor version
version:minor()
-- get the patch version
version:patch()

package:version_str

package:config

-- if configurations are set like so
add_require("example", {config = {enable_x = true, value_y = 6}})
-- these values can be retrieved like so
-- returns true
package:config("enable_x")
-- returns 6
package:config("value_y")

package:config_set

package:config_set("enable_x", true)
package:config_set("value_y", 6)

package:configs

-- returns a table with the configuration names as keys and their values as values
local configs = package:configs()
local enable_x = configs["enable_x"]
local value_y = configs["value_y"]

package:buildhash

它确保每个包,不同的配置安装到唯一的路径下,相互之间不冲突。

package:patches

-- returns a table with all patches
local patches = package:patches()
-- each element contains the keys "url" and "sha256"
local url = patches[1]["url"]
local sha256 = patches[1]["sha256"]

package:has_cfuncs

这应该在 on_test 中使用,如下所示:

on_test(function (package)
  assert(package:has_cfuncs("foo"))
  -- you can also add configs
  assert(package:has_cfuncs("bar", {includes = "foo_bar.h"}))
  assert(package:has_cfuncs("blob", {includes = "blob.h", configs = {defines = "USE_BLOB"}}))
  -- you can even set the language
  assert(package:has_cfuncs("bla", {configs = {languages = "c99"}}))
end)

package:has_cxxfuncs

这应该在 on_test 中使用,如下所示:

on_test(function (package)
  assert(package:has_cxxfuncs("foo"))
  -- you can also add configs
  assert(package:has_cxxfuncs("bar", {includes = "foo_bar.hpp"}))
  assert(package:has_cxxfuncs("blob", {includes = "blob.hpp", configs = {defines = "USE_BLOB"}}))
  -- you can even set the language
  assert(package:has_cxxfuncs("bla", {configs = {languages = "cxx17"}}))
end)

package:has_ctypes

这应该在 on_test 中使用,如下所示:

on_test(function (package)
  assert(package:has_ctypes("foo"))
  -- you can also add configs
  assert(package:has_ctypes("bar", {includes = "foo_bar.h"}))
  assert(package:has_ctypes("blob", {includes = "blob.h", configs = {defines = "USE_BLOB"}}))
  -- you can even set the language
  assert(package:has_ctypes("bla", {configs = {languages = "c99"}}))
end)

package:has_cxxtypes

这应该在 on_test 中使用,如下所示:

on_test(function (package)
  assert(package:has_cxxtypes("foo"))
  -- you can also add configs
  assert(package:has_cxxtypes("bar", {includes = "foo_bar.hpp"}))
  assert(package:has_cxxtypes("blob", {includes = "blob.hpp", configs = {defines = "USE_BLOB"}}))
  -- you can even set the language
  assert(package:has_cxxtypes("bla", {configs = {languages = "cxx17"}}))
end)

package:has_cincludes

这应该在 on_test 中使用,如下所示:

on_test(function (package)
  assert(package:has_cincludes("foo.h"))
end)

package:has_cxxincludes

这应该在 on_test 中使用,如下所示:

on_test(function (package)
  assert(package:has_cincludes("foo.hpp"))
end)

package:check_csnippets

这应该在 on_test 中使用,如下所示:

on_test(function (package)
  assert(package:check_csnippets({test = [[
    #define USE_BLOB
    #include 
    void test(int argc, char** argv) {
      foo bar;
      printf("%s", bar.blob);
    }
  ]]}, {configs = {languages = "c99"}, includes = "foo.h"}))
end)

package:check_cxxsnippets

这应该在 on_test 中使用,如下所示:

on_test(function (package)
  assert(package:check_cxxsnippets({test = [[
    #define USE_BLOB
    #include 
    void test(int argc, char** argv) {
      foo bar();
      std::cout << bar.blob;
    }
  ]]}, {configs = {languages = "cxx11"}, includes = "foo.hpp"}))
end)