Skip to content

Commit

Permalink
add custom preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Jan 27, 2025
1 parent 65fcd8c commit f2ca3ab
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
1 change: 1 addition & 0 deletions tests/apis/add_configfiles/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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}
7 changes: 6 additions & 1 deletion tests/apis/add_configfiles/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
50 changes: 34 additions & 16 deletions xmake/actions/config/configfiles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f2ca3ab

Please sign in to comment.