This page describes the option interface for functions like on_load(), on_check() or after_check() of the Configuration option.
In the script scope, you can operate various properties and configurations of the current option through the option parameter.
Get the name of the option (without namespace)
option("test")
on_check(function (option)
print(option:name()) -- output: test
end)
Get the full name of the option (with namespace)
option("mymod::test")
on_check(function (option)
print(option:fullname()) -- output: mymod::test
end)
Get the namespace of the option
option("mymod::test")
on_check(function (option)
print(option:namespace()) -- output: mymod
end)
Get the description of the option
option("test")
set_description("This is a test option")
on_check(function (option)
print(option:description()) -- output: This is a test option
end)
Get the current value of the option
option("demo")
set_default(true)
after_check(function (option)
local value = option:value()
if value then
print("demo option is enabled")
else
print("demo option is disabled")
end
end)
Check if the option is enabled
option("test")
after_check(function (option)
if option:enabled() then
print("Option is enabled")
end
end)
Enable or disable the option
option("float")
after_check(function (option)
if option:dep("micro"):enabled() then
-- If micro option is enabled, disable float option
option:enable(false)
end
end)
Set the value of the option
option("test")
on_check(function (option)
-- Set option value to a specific value
option:set_value("custom_value")
end)
Clear the option status, need to recheck
option("test")
after_check(function (option)
-- Clear status, will recheck on next build
option:clear()
end)
Any configuration values set by set_xxx and add_xxx in the description scope can be obtained through this interface.
option("test")
set_default(false)
set_category("option")
set_description("Test option")
add_defines("TEST_MODE")
add_links("testlib")
on_check(function (option)
-- Get various configurations
local default = option:get("default") -- false
local category = option:get("category") -- "option"
local description = option:get("description") -- "Test option"
local defines = option:get("defines") -- {"TEST_MODE"}
local links = option:get("links") -- {"testlib"}
-- Get type checking related configurations
local ctypes = option:get("ctypes") -- Get C type check list
local cfuncs = option:get("cfuncs") -- Get C function check list
local cincludes = option:get("cincludes") -- Get C header file check list
end)
If you want to add values, you can use option:add.
option("test")
on_check(function (option)
-- Set link libraries
option:set("links", "sdl2")
-- Set predefined macros
option:set("defines", "SDL_MAIN_HANDLED")
-- Set configuration variables
option:set("configvar", option:name(), option:value(), {quote = false})
-- Set compilation flags
option:set("cxflags", "-O2", "-Wall")
-- Set header file search paths
option:set("includedirs", "/usr/include/sdl2")
-- Set library file search paths
option:set("linkdirs", "/usr/lib")
end)
::: tip NOTE
Any script scope configuration using option:set("xxx", ...) is completely consistent with the corresponding set_xxx interface in the description scope. For specific parameter descriptions, you can directly refer to the corresponding set_xxx interface documentation in the description scope.
For example:
set_default(false)option:set("default", false)
:::Add values to the option by name
option("test")
on_check(function (option)
-- Add link libraries
option:add("links", "sdl2", "pthread")
-- Add predefined macros
option:add("defines", "DEBUG", "VERSION=1")
-- Add compilation flags
option:add("cxflags", "-g", "-O0")
-- Add header file search paths
option:add("includedirs", "/usr/local/include")
-- Add library file search paths
option:add("linkdirs", "/usr/local/lib")
-- Add system link libraries
option:add("syslinks", "dl", "m")
-- Add C type checks
option:add("ctypes", "wchar_t")
-- Add C function checks
option:add("cfuncs", "malloc", "free")
-- Add C header file checks
option:add("cincludes", "stdio.h", "stdlib.h")
end)
::: tip NOTE
Any script scope configuration using option:add("xxx", ...) is completely consistent with the corresponding add_xxx interface in the description scope. For specific parameter descriptions, you can directly refer to the corresponding add_xxx interface documentation in the description scope.
For example:
add_defines("DEBUG", "VERSION=1")option:add("defines", "DEBUG", "VERSION=1")
:::Remove specified values from the option
option("test")
on_check(function (option)
-- Remove specific link libraries
option:remove("links", "oldlib")
-- Remove specific predefined macros
option:remove("defines", "OLD_MACRO")
-- Remove specific compilation flags
option:remove("cxflags", "-Wall")
end)
Get all dependencies of the option
option("info")
add_deps("small", "micro")
after_check(function (option)
local deps = option:deps()
if deps then
for name, dep in pairs(deps) do
print("Dependency:", name, "enabled:", dep:enabled())
end
end
end)
Get the specified dependent option
option("float")
add_deps("micro")
after_check(function (option)
local micro_dep = option:dep("micro")
if micro_dep and micro_dep:enabled() then
-- If micro dependency is enabled, disable current option
option:enable(false)
end
end)
Get the ordered dependencies of the option
option("test")
add_deps("dep1", "dep2")
after_check(function (option)
local orderdeps = option:orderdeps()
if orderdeps then
for i, dep in ipairs(orderdeps) do
print("Order dependency", i, ":", dep:name())
end
end
end)
Get extra configuration information
option("test")
add_csnippets("test_snippet", "int test() { return 1; }", {output = true})
on_check(function (option)
-- Check if snippet has output configuration
local has_output = option:extraconf("csnippets", "test_snippet", "output")
if has_output then
print("test_snippet has output configuration")
end
end)
::: tip TIP
on_check, after_check, the option parameter represents the current option instance being processedoption:get()option:set() and option:add()option:dep() to access dependent options and implement complex option logicoption:enabled() and option:enable()
:::