diff options
| author | ruki <[email protected]> | 2019-02-03 11:06:55 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-02-03 11:06:55 +0800 |
| commit | e7c2f6900a1aee4c065422ab5a252897f4c90b64 (patch) | |
| tree | 6993ad1be3108cfaeb2a813e3355b23499112a83 | |
| parent | aba74b0697dcd717fff35ec09d7aaffe07e992f3 (diff) | |
support define VAR for configfiles
| -rw-r--r-- | tests/apis/add_configfiles/config.h.in | 4 | ||||
| -rw-r--r-- | tests/apis/add_configfiles/xmake.lua | 11 | ||||
| -rw-r--r-- | xmake/actions/config/configfiles.lua | 34 |
3 files changed, 47 insertions, 2 deletions
diff --git a/tests/apis/add_configfiles/config.h.in b/tests/apis/add_configfiles/config.h.in index dcd698871..e84840f70 100644 --- a/tests/apis/add_configfiles/config.h.in +++ b/tests/apis/add_configfiles/config.h.in @@ -1,2 +1,6 @@ #define HAVE_${module}_H #define HELLO "${hello} ${ARCH} ${PLAT}" + +${define FOO_ENABLE} +${define FOO_ENABLE2} +${define FOO_STRING} diff --git a/tests/apis/add_configfiles/xmake.lua b/tests/apis/add_configfiles/xmake.lua index 070488198..2a5c091ec 100644 --- a/tests/apis/add_configfiles/xmake.lua +++ b/tests/apis/add_configfiles/xmake.lua @@ -2,6 +2,17 @@ set_configvar("ARCH", get_config("arch")) set_configvar("PLAT", get_config("plat")) +option("foo") + set_default("foo") + set_description("The Foo Info") +option_end() + +if has_config("foo") then + set_configvar("FOO_ENABLE", 1) + set_configvar("FOO_ENABLE2", false) + set_configvar("FOO_STRING", get_config("foo")) +end + target("test") set_kind("binary") add_files("main.c") diff --git a/xmake/actions/config/configfiles.lua b/xmake/actions/config/configfiles.lua index afa153a07..94f3c1bd5 100644 --- a/xmake/actions/config/configfiles.lua +++ b/xmake/actions/config/configfiles.lua @@ -99,8 +99,38 @@ function _generate_configfile(srcfile, dstfile, fileinfo, targets) -- replace all variables local pattern = fileinfo.pattern or "%${(.-)}" io.gsub(dstfile_tmp, "(" .. pattern .. ")", function(_, variable) - local value = variables[variable:trim()] - assert(value ~= nil, "cannot get variable(%s) in %s.", variable, srcfile) + + -- get variable name + variable = variable:trim() + + -- is ${define variable}? + local isdefine = false + if variable:startswith("define") then + variable = variable:split("%s+")[2] + isdefine = true + end + + -- get variable value + local value = variables[variable] + if isdefine then + if value == nil then + value = ("/* #undef %s */"):format(variable) + elseif type(value) == "boolean" then + if value then + value = ("#define %s 1"):format(variable) + else + value = ("/* #define %s 0 */"):format(variable) + end + elseif type(value) == "number" then + value = ("#define %s %d"):format(variable, value) + elseif type(value) == "string" then + value = ("#define %s \"%s\""):format(variable, value) + else + raise("unknown variable(%s) type: %s", variable, type(value)) + end + else + assert(value ~= nil, "cannot get variable(%s) in %s.", variable, srcfile) + end return value end) |
