diff options
| author | ruki <[email protected]> | 2019-02-02 17:34:22 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-02-02 17:34:22 +0800 |
| commit | 4dbcbd611628ac3534000071c13e9f25ed86c92c (patch) | |
| tree | bb83d4d7bea83686acfe1e9dde99ad9cb7b19dd5 | |
| parent | f1954564a6857bf876341061a43aa05e1861154c (diff) | |
replace variables for configuration files
| -rw-r--r-- | tests/apis/add_configfiles/config.h.in | 1 | ||||
| -rw-r--r-- | tests/apis/add_configfiles/main2.c | 9 | ||||
| -rw-r--r-- | tests/apis/add_configfiles/xmake.lua | 10 | ||||
| -rw-r--r-- | xmake/actions/config/configfiles.lua | 36 | ||||
| -rw-r--r-- | xmake/core/project/target.lua | 2 |
5 files changed, 51 insertions, 7 deletions
diff --git a/tests/apis/add_configfiles/config.h.in b/tests/apis/add_configfiles/config.h.in index 4b1417ad3..bfc3542bb 100644 --- a/tests/apis/add_configfiles/config.h.in +++ b/tests/apis/add_configfiles/config.h.in @@ -1,2 +1,3 @@ #define HAVE_${module}_H #define HELLO "${hello}" +#define HELLO2 "@hello2@" diff --git a/tests/apis/add_configfiles/main2.c b/tests/apis/add_configfiles/main2.c new file mode 100644 index 000000000..16f779261 --- /dev/null +++ b/tests/apis/add_configfiles/main2.c @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <stdlib.h> +#include "config.h" + +int main(int argc, char** argv) +{ + printf("hello2 %s\n", HELLO2); + return 0; +} diff --git a/tests/apis/add_configfiles/xmake.lua b/tests/apis/add_configfiles/xmake.lua index d62153d27..db7b82138 100644 --- a/tests/apis/add_configfiles/xmake.lua +++ b/tests/apis/add_configfiles/xmake.lua @@ -1,21 +1,21 @@ target("test") set_kind("binary") - add_files("*.c") + add_files("main.c") set_configdir("$(buildir)/config") add_configfiles("test.c.in", {filename = "mytest.c"}) - add_configfiles("config.h.in", {hello = "xmake", prefixdir = "header"}) + add_configfiles("config.h.in", {variables = {hello = "xmake"}, prefixdir = "header"}) add_configfiles("*.man", {copyonly = true, prefixdir = "man"}) add_includedirs("$(buildir)/config/header") target("test2") set_kind("binary") - add_files("*.c") + add_files("main2.c") set_configdir("$(buildir)/config2") add_configfiles("test.c.in", {filename = "mytest.c"}) - add_configfiles("config.h.in", {hello = "xmake", prefixdir = "header"}) - add_configfiles("*.man", {copyonly = true, prefixdir = "man"}) + add_configfiles("config.h.in", {variables = {hello2 = "xmake2"}, pattern = "@(.-)@", prefixdir = "header"}) + add_configfiles("*.man", {onlycopy = true, prefixdir = "man"}) add_includedirs("$(buildir)/config2/header") diff --git a/xmake/actions/config/configfiles.lua b/xmake/actions/config/configfiles.lua index 917e4c040..98773149c 100644 --- a/xmake/actions/config/configfiles.lua +++ b/xmake/actions/config/configfiles.lua @@ -73,8 +73,42 @@ function _generate_configfile(srcfile, dstfile, fileinfo, targets) cprint("${dim}generating %s to %s ..", srcfile, dstfile) end + -- only copy it? + local generated = false + if fileinfo.onlycopy then + if os.mtime(srcfile) > os.mtime(dstfile) then + os.cp(srcfile, dstfile) + generated = true + end + else + -- generate to the temporary file first + local dstfile_tmp = path.join(os.tmpdir(), hash.uuid(srcfile)) + os.tryrm(dstfile_tmp) + os.cp(srcfile, dstfile_tmp) + + -- replace all variables + local variables = fileinfo.variables or {} + local pattern = fileinfo.pattern or "%${(.-)}" + io.gsub(dstfile_tmp, "(" .. pattern .. ")", function(_, variable) + return variables[variable] + end) + + -- update file if the content is changed + if os.isfile(dstfile_tmp) then + if os.isfile(dstfile) then + if io.readfile(dstfile_tmp) ~= io.readfile(dstfile) then + os.mv(dstfile_tmp, dstfile) + generated = true + end + else + os.mv(dstfile_tmp, dstfile) + generated = true + end + end + end + -- trace - cprint("generating %s ... ${color.success}${text.success}", srcfile) + cprint("generating %s ... %s", srcfile, generated and "${color.success}${text.success}" or "${color.success}cache") end -- the main entry function diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 58a214215..de1cd6f47 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -306,7 +306,7 @@ function target:_copiedfiles(filetype, outputdir, pathfilter) -- modify filename if fileinfo.filename then - dstfile = path.join(path.directory(dstfile), filename) + dstfile = path.join(path.directory(dstfile), fileinfo.filename) end -- filter the destinate file path |
