summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-01-27 22:36:27 +0800
committerruki <[email protected]>2025-01-27 22:36:27 +0800
commitf2ca3ab2561f4789f72d88e0a53528084a172dc3 (patch)
treeea71acecd4c7ee48b96ddcb0169bb3018a57c706
parent65fcd8cbf19e7a0d1330c61831313a1d72e1a327 (diff)
add custom preprocessor
-rw-r--r--tests/apis/add_configfiles/config.h.in1
-rw-r--r--tests/apis/add_configfiles/xmake.lua7
-rw-r--r--xmake/actions/config/configfiles.lua50
3 files changed, 41 insertions, 17 deletions
diff --git a/tests/apis/add_configfiles/config.h.in b/tests/apis/add_configfiles/config.h.in
index bb7e6ec81..c21ebeafb 100644
--- a/tests/apis/add_configfiles/config.h.in
+++ b/tests/apis/add_configfiles/config.h.in
@@ -11,5 +11,6 @@ ${define FOO2_ENABLE2}
${define FOO2_STRING}
${define_export MYLIB}
+${define_custom FOO_STRING arg1 arg2}
#define HAVE_SSE2 ${default HAVE_SSE2 0}
diff --git a/tests/apis/add_configfiles/xmake.lua b/tests/apis/add_configfiles/xmake.lua
index 4f4b75936..22da0dcf6 100644
--- a/tests/apis/add_configfiles/xmake.lua
+++ b/tests/apis/add_configfiles/xmake.lua
@@ -25,7 +25,12 @@ target("test")
set_configvar("module", "test")
set_configdir("$(buildir)/config")
add_configfiles("test.c.in", {filename = "mytest.c"})
- add_configfiles("config.h.in", {variables = {hello = "xmake"}, prefixdir = "header"})
+ add_configfiles("config.h.in", {variables = {hello = "xmake"}, prefixdir = "header",
+ preprocessor = function (preprocessor_name, name, value, opt)
+ if preprocessor_name == "define_custom" then
+ return string.format("#define CUSTOM_%s %s", name, value)
+ end
+ end})
add_configfiles("*.man", {onlycopy = true, prefixdir = "man"})
add_includedirs("$(buildir)/config/header")
diff --git a/xmake/actions/config/configfiles.lua b/xmake/actions/config/configfiles.lua
index b581ce749..189fd51eb 100644
--- a/xmake/actions/config/configfiles.lua
+++ b/xmake/actions/config/configfiles.lua
@@ -72,6 +72,13 @@ function _get_configfiles()
-- save targets
srcinfo.targets = srcinfo.targets or {}
table.insert(srcinfo.targets, target)
+
+ -- save preprocessors
+ local preprocessor = target:extraconf("configfiles", srcfile, "preprocessor")
+ if preprocessor then
+ srcinfo.preprocessors = srcinfo.preprocessors or {}
+ table.insert(srcinfo.preprocessors, preprocessor)
+ end
end
end
end
@@ -231,20 +238,32 @@ function _get_variable_value(variables, name, opt)
local value = variables[name]
local extraconf = variables["__extraconf_" .. name]
if preprocessor_name then
- local preprocessors = _g._preprocessors
- if preprocessors == nil then
- preprocessors = {
- define = _preprocess_define_value,
- default = _preprocess_default_value,
- define_export = _preprocess_define_export_value
- }
- _g._preprocessors = preprocessors
+ local preprocessed = false
+ if opt.preprocessors then
+ for _, preprocessor in ipairs(opt.preprocessors) do
+ value = preprocessor(preprocessor_name, name, value, {argv = preprocessor_argv, extraconf = extraconf})
+ if value ~= nil then
+ preprocessed = true
+ break
+ end
+ end
end
- local preprocessor = preprocessors[preprocessor_name]
- if preprocessor == nil then
- raise("unknown variable keyword, ${%s %s}", preprocessor_name, name)
+ if not preprocessed then
+ local preprocessors = _g._preprocessors
+ if preprocessors == nil then
+ preprocessors = {
+ define = _preprocess_define_value,
+ default = _preprocess_default_value,
+ define_export = _preprocess_define_export_value
+ }
+ _g._preprocessors = preprocessors
+ end
+ local preprocessor = preprocessors[preprocessor_name]
+ if preprocessor == nil then
+ raise("unknown variable keyword, ${%s %s}", preprocessor_name, name)
+ end
+ value = preprocessor(name, value, {argv = preprocessor_argv, extraconf = extraconf})
end
- value = preprocessor(name, value, {argv = preprocessor_argv, extraconf = extraconf})
assert(value ~= nil, "cannot get variable(%s %s) in %s.", preprocessor_name, name, configfile)
else
assert(value ~= nil, "cannot get variable(%s) in %s.", name, configfile)
@@ -257,7 +276,7 @@ function _get_variable_value(variables, name, opt)
end
-- generate the configuration file
-function _generate_configfile(srcfile, dstfile, fileinfo, targets)
+function _generate_configfile(srcfile, dstfile, fileinfo, targets, preprocessors)
-- trace
if option.get("verbose") then
@@ -335,7 +354,7 @@ function _generate_configfile(srcfile, dstfile, fileinfo, targets)
end
return _get_variable_value(variables, variable, {preprocessor_name = preprocessor_name,
- preprocessor_argv = preprocessor_argv, configfile = srcfile})
+ preprocessor_argv = preprocessor_argv, configfile = srcfile, preprocessors = preprocessors})
end)
-- update file if the content is changed
@@ -369,12 +388,11 @@ function main(opt)
opt = opt or {}
local oldir = os.cd(project.directory())
-
-- generate all configuration files
local configfiles = _get_configfiles()
for dstfile, srcinfo in pairs(configfiles) do
depend.on_changed(function ()
- _generate_configfile(srcinfo.srcfile, dstfile, srcinfo.fileinfo, srcinfo.targets)
+ _generate_configfile(srcinfo.srcfile, dstfile, srcinfo.fileinfo, srcinfo.targets, srcinfo.preprocessors)
end, {files = srcinfo.srcfile,
lastmtime = os.mtime(dstfile),
dependfile = srcinfo.dependfile,