diff options
| author | ruki <[email protected]> | 2025-10-11 15:55:56 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-10-11 15:55:56 +0800 |
| commit | c666f0560bbb68d062c57923821607c2d893340e (patch) | |
| tree | c1b4e3a19ce1291769834aa84853e0228151580f | |
| parent | b3bc1f49a705b8b1a15d14194a239f43be17a95d (diff) | |
add copy_if_different for os.cp
| -rw-r--r-- | core/src/xmake/os/cpdir.c | 4 | ||||
| -rw-r--r-- | core/src/xmake/os/cpfile.c | 4 | ||||
| -rw-r--r-- | xmake/actions/config/configfiles.lua | 20 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 10 | ||||
| -rw-r--r-- | xmake/rules/utils/symbols/export_list/xmake.lua | 8 |
5 files changed, 19 insertions, 27 deletions
diff --git a/core/src/xmake/os/cpdir.c b/core/src/xmake/os/cpdir.c index 4fee30903..cdae6f173 100644 --- a/core/src/xmake/os/cpdir.c +++ b/core/src/xmake/os/cpdir.c @@ -49,6 +49,10 @@ tb_int_t xm_os_cpdir(lua_State* lua) if (is_symlink) flags |= TB_FILE_COPY_LINK; + tb_bool_t copy_if_different = lua_toboolean(lua, 4); + if (copy_if_different) + flags |= TB_FILE_COPY_IF_DIFFERENT; + // do copy lua_pushboolean(lua, tb_directory_copy(src, dst, flags)); return 1; diff --git a/core/src/xmake/os/cpfile.c b/core/src/xmake/os/cpfile.c index 75b09ba28..7c2c312ce 100644 --- a/core/src/xmake/os/cpfile.c +++ b/core/src/xmake/os/cpfile.c @@ -53,6 +53,10 @@ tb_int_t xm_os_cpfile(lua_State* lua) if (is_writeable) flags |= TB_FILE_COPY_WRITEABLE; + tb_bool_t copy_if_different = lua_toboolean(lua, 5); + if (copy_if_different) + flags |= TB_FILE_COPY_IF_DIFFERENT; + // do copy lua_pushboolean(lua, tb_file_copy(src, dst, flags)); return 1; diff --git a/xmake/actions/config/configfiles.lua b/xmake/actions/config/configfiles.lua index 60e5ef378..2262cdb35 100644 --- a/xmake/actions/config/configfiles.lua +++ b/xmake/actions/config/configfiles.lua @@ -284,11 +284,9 @@ function _generate_configfile(srcfile, dstfile, fileinfo, targets, preprocessors 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 @@ -359,26 +357,12 @@ function _generate_configfile(srcfile, dstfile, fileinfo, targets, preprocessors -- 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.cp(dstfile_tmp, dstfile) - generated = true - else - -- I forget why I added it here, but if we switch the option, mode, - -- this will cause the whole project to be rebuilt, - -- even if nothing in config.h has been changed. - -- - --os.touch(dstfile, {mtime = os.time()}) - end - else - os.cp(dstfile_tmp, dstfile) - generated = true - end + os.cp(dstfile_tmp, dstfile, {copy_if_different = true}) end end -- trace - cprint("generating %s ... %s", srcfile, generated and "${color.success}${text.success}" or "${color.success}cache") + cprint("generating %s ... ${color.success}${text.success}", srcfile) end -- the main entry function diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index e6422c549..647c76c50 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -72,6 +72,7 @@ function os._cp(src, dst, rootdir, opt) -- is file or link? local symlink = opt.symlink local writeable = opt.writeable + local copy_if_different = opt.copy_if_different if os.isfile(src) or (symlink and os.islink(src)) then -- the destination is directory? append the filename @@ -87,7 +88,7 @@ function os._cp(src, dst, rootdir, opt) if opt.force and os.isfile(dst) then os.rmfile(dst) end - if not os.cpfile(src, dst, symlink, writeable) then + if not os.cpfile(src, dst, symlink, writeable, copy_if_different) then local errors = os.strerror() if symlink and os.islink(src) then local reallink = os.readlink(src) @@ -109,7 +110,7 @@ function os._cp(src, dst, rootdir, opt) end -- copy directory - if not os.cpdir(src, dst, symlink) then + if not os.cpdir(src, dst, symlink, copy_if_different) then return false, string.format("cannot copy directory %s to %s, %s", src, dst, os.strerror()) end else @@ -497,6 +498,11 @@ function os.filedirs(pattern, callback) end -- copy files or directories and we can reserve the source directory structure +-- +-- @param srcpath the source file path +-- @param dstpath the destination file path +-- @param opt the copy option. e.g. {rootdir, symlink, writeable, force, copy_if_different} +-- -- e.g. os.cp("src/**.h", "/tmp/", {rootdir = "src", symlink = true}) function os.cp(srcpath, dstpath, opt) diff --git a/xmake/rules/utils/symbols/export_list/xmake.lua b/xmake/rules/utils/symbols/export_list/xmake.lua index 5069274e2..2aa732af6 100644 --- a/xmake/rules/utils/symbols/export_list/xmake.lua +++ b/xmake/rules/utils/symbols/export_list/xmake.lua @@ -127,13 +127,7 @@ rule("utils.symbols.export_list") -- update file if the content is changed target:data_add("linkdepfiles", exportfile) if os.isfile(exportfile_tmp) then - if os.isfile(exportfile) then - if io.readfile(exportfile_tmp) ~= io.readfile(exportfile) then - os.cp(exportfile_tmp, exportfile) - end - else - os.cp(exportfile_tmp, exportfile) - end + os.cp(exportfile_tmp, exportfile, {copy_if_different = true}) end target:data_add("linkdepfiles", exportfile) end |
