Are you an LLM? You can read better optimized documentation at /zh/examples/configuration/custom_scope_api.md for this page in Markdown format
自定义描述域 API
我们可以使用 interp_add_scopeapis 注册自定义描述域 API,来支持自定义配置域和接口。
基础示例
EXPLORER
src
main.cpp
xmake.lua
Lua xmake.lua
123456789101112131415161718192021222324252627282930313233
add_rules("mode.debug", "mode.release")
-- register scope apis
interp_add_scopeapis("myscope.set_name", "myscope.add_list", {kind = "values"})
interp_add_scopeapis("myscope.on_script", {kind = "script"})
-- use myscope
myscope("hello")
set_name("foo")
add_list("value1", "value2")
on_script(function ()
print("hello")
end)
target("test")
set_kind("binary")
add_files("src/*.cpp")
on_config(function (target)
import("core.project.project")
-- get scope data
local myscope = project.scope("myscope")
for name, scope in pairs(myscope) do
print("myscope(%s)", name)
print(" name: %s", scope:get("name"))
print(" list: %s", table.concat(scope:get("list"), ", "))
print(" script:")
local script = scope:get("script")
if script then
script()
end
end
end)
编译运行
bash
$ xmake
$ xmake run