summaryrefslogtreecommitdiff
path: root/xmake
diff options
context:
space:
mode:
authorruki <[email protected]>2025-10-02 19:09:25 +0800
committerGitHub <[email protected]>2025-10-02 19:09:25 +0800
commitc62972f4def074c8b0b433e557a48d313bf77a18 (patch)
tree0a24c95b7e5f3b2c0b73c3a8857b77dee04842ea /xmake
parent553eceaccbddb35e8499b3a4332a5b798219f796 (diff)
parent6377f4494967e341150f0e2bb042d3c7d8bd15c9 (diff)
Merge pull request #6872 from xmake-io/opti
Improve hash
Diffstat (limited to 'xmake')
-rw-r--r--xmake/actions/config/configfiles.lua2
-rw-r--r--xmake/core/base/hash.lua70
-rw-r--r--xmake/core/base/os.lua3
-rw-r--r--xmake/core/base/scheduler.lua2
-rw-r--r--xmake/core/package/package.lua8
-rw-r--r--xmake/core/project/project.lua4
-rw-r--r--xmake/core/sandbox/modules/hash.lua79
-rw-r--r--xmake/modules/private/cache/build_cache.lua2
-rw-r--r--xmake/plugins/project/cmake/cmakelists.lua4
-rw-r--r--xmake/rules/c++/modules/support.lua4
-rw-r--r--xmake/rules/c++/unity_build/unity_build.lua2
11 files changed, 144 insertions, 36 deletions
diff --git a/xmake/actions/config/configfiles.lua b/xmake/actions/config/configfiles.lua
index ce435d774..60e5ef378 100644
--- a/xmake/actions/config/configfiles.lua
+++ b/xmake/actions/config/configfiles.lua
@@ -292,7 +292,7 @@ function _generate_configfile(srcfile, dstfile, fileinfo, targets, preprocessors
end
else
-- generate to the temporary file first
- local dstfile_tmp = path.join(os.tmpdir(), hash.uuid4(srcfile))
+ local dstfile_tmp = os.tmpfile(srcfile)
os.tryrm(dstfile_tmp)
os.cp(srcfile, dstfile_tmp)
diff --git a/xmake/core/base/hash.lua b/xmake/core/base/hash.lua
index 362eb67e5..e846ba302 100644
--- a/xmake/core/base/hash.lua
+++ b/xmake/core/base/hash.lua
@@ -25,11 +25,15 @@ local hash = hash or {}
local io = require("base/io")
local utils = require("base/utils")
local bytes = require("base/bytes")
+local libc = require("base/libc")
-- save metatable and builtin functions
hash._md5 = hash._md5 or hash.md5
hash._sha = hash._sha or hash.sha
hash._xxhash = hash._xxhash or hash.xxhash
+hash._rand32 = hash._rand32 or hash.rand32
+hash._rand64 = hash._rand64 or hash.rand64
+hash._rand128 = hash._rand128 or hash.rand128
-- generate md5 from the given file or data
function hash.md5(file_or_data)
@@ -70,6 +74,24 @@ function hash.sha256(file_or_data)
return hashstr, errors
end
+-- generate uuid, e.g "91E8ECF1-417F-4EDF-A574-E22D7D8D204A"
+function hash.uuid(str)
+ return hash.uuid4(str)
+end
+
+-- generate xxhash32 from the given file or data
+function hash.xxhash32(file_or_data)
+ local hashstr, errors
+ if bytes.instance_of(file_or_data) then
+ local datasize = file_or_data:size()
+ local dataaddr = file_or_data:caddr()
+ hashstr, errors = hash._xxhash(32, dataaddr, datasize)
+ else
+ hashstr, errors = hash._xxhash(32, file_or_data)
+ end
+ return hashstr, errors
+end
+
-- generate xxhash64 from the given file or data
function hash.xxhash64(file_or_data)
local hashstr, errors
@@ -96,20 +118,52 @@ function hash.xxhash128(file_or_data)
return hashstr, errors
end
--- generate uuid, e.g "91E8ECF1-417F-4EDF-A574-E22D7D8D204A"
-function hash.uuid(str)
- return hash.uuid4(str)
-end
-
--- TODO, we should optimize it
-- generate hash32 from string, e.g. "91e8ecf1"
function hash.strhash32(str)
- return hash.uuid4(str):split("-", {plain = true})[1]:lower()
+ local data = libc.ptraddr(libc.dataptr(str))
+ local size = #str
+ return hash._xxhash(32, data, size)
+end
+
+-- generate hash64 from string, e.g. "91e8ecf191e8ecf1"
+function hash.strhash64(str)
+ local data = libc.ptraddr(libc.dataptr(str))
+ local size = #str
+ return hash._xxhash(64, data, size)
end
-- generate hash128 from string, e.g. "91e8ecf1417f4edfa574e22d7d8d204a"
function hash.strhash128(str)
- return hash.uuid4(str):replace("-", "", {plain = true}):lower()
+ local data = libc.ptraddr(libc.dataptr(str))
+ local size = #str
+ return hash._xxhash(128, data, size)
+end
+
+-- generate random32 hash
+function hash.rand32()
+ if hash._rand32 then
+ return hash._rand32()
+ else
+ return hash.strhash32(tostring(math.random()))
+ end
+end
+
+-- generate random64 hash
+function hash.rand64()
+ if hash._rand64 then
+ return hash._rand64()
+ else
+ return hash.strhash64(tostring(math.random()))
+ end
+end
+
+-- generate random128 hash
+function hash.rand128()
+ if hash._rand128 then
+ return hash._rand128()
+ else
+ return hash.strhash128(tostring(math.random()))
+ end
end
-- return module: hash
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 41fe07d22..988d9e4bf 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -761,7 +761,8 @@ function os.tmpfile(opt_or_key)
key = opt_or_key.key
opt = opt_or_key
end
- return path.join(os.tmpdir(opt), "_" .. (hash.uuid4(key):gsub("-", "")))
+ local filename = "_" .. (key and hash.strhash128(key) or (hash.rand128()))
+ return path.join(os.tmpdir(opt), filename)
end
-- exit program
diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua
index c96b32441..63c9fe009 100644
--- a/xmake/core/base/scheduler.lua
+++ b/xmake/core/base/scheduler.lua
@@ -299,7 +299,7 @@ function scheduler:_co_curenvs_update(envs)
for _, key in ipairs(table.orderkeys(envs)) do
envs_hash = envs_hash .. key:upper() .. envs[key]
end
- envs_hash = hash.uuid4(envs_hash):sub(1, 8)
+ envs_hash = hash.strhash32(envs_hash)
self._CO_CURENVS_HASH = envs_hash
-- save the current directory for each coroutine
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua
index 7a1d8e24b..db943815a 100644
--- a/xmake/core/package/package.lua
+++ b/xmake/core/package/package.lua
@@ -1691,6 +1691,12 @@ function _instance:_compute_buildhash()
self:buildhash()
end
+-- hash.strhash128 has been switched to xxhash.
+-- For compatibility, the old hash algorithm is still used here.
+function _instance:_strhash128(str)
+ return hash.uuid4(str):replace("-", "", {plain = true}):lower()
+end
+
-- get the build hash
function _instance:buildhash()
local buildhash = self._BUILDHASH
@@ -1752,7 +1758,7 @@ function _instance:buildhash()
table.sort(toolchains)
str = str .. "_" .. table.concat(toolchains, "_")
end
- return hash.strhash128(str)
+ return self:_strhash128(str)
end
local function _get_installdir(...)
local name = self:name():lower():gsub("::", "_")
diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua
index 86050ebec..5af05c21f 100644
--- a/xmake/core/project/project.lua
+++ b/xmake/core/project/project.lua
@@ -25,6 +25,7 @@ local project = project or {}
local os = require("base/os")
local io = require("base/io")
local path = require("base/path")
+local hash = require("base/hash")
local task = require("base/task")
local utils = require("base/utils")
local table = require("base/table")
@@ -1380,7 +1381,8 @@ function project.tmpfile(opt_or_key)
key = opt_or_key.key
opt = opt_or_key
end
- return path.join(project.tmpdir(opt), "_" .. (hash.uuid4(key):gsub("-", "")))
+ local filename = "_" .. (key and hash.strhash128(key) or (hash.rand128()))
+ return path.join(project.tmpdir(opt), "_" .. filename)
end
-- get all modes
diff --git a/xmake/core/sandbox/modules/hash.lua b/xmake/core/sandbox/modules/hash.lua
index 47235bcd6..b5927b029 100644
--- a/xmake/core/sandbox/modules/hash.lua
+++ b/xmake/core/sandbox/modules/hash.lua
@@ -47,7 +47,7 @@ end
function sandbox_hash.sha1(file_or_data)
local sha1, errors = hash.sha1(file_or_data)
if not sha1 then
- raise("cannot generate sha1 for %s, %s", file_or_data, errors or "unknown errors")
+ raise("cannot generate sha1, %s", errors or "unknown errors")
end
return sha1
end
@@ -56,7 +56,7 @@ end
function sandbox_hash.sha256(file_or_data)
local sha256, errors = hash.sha256(file_or_data)
if not sha256 then
- raise("cannot generate sha256 for %s, %s", file_or_data, errors or "unknown errors")
+ raise("cannot generate sha256, %s", errors or "unknown errors")
end
return sha256
end
@@ -65,45 +65,90 @@ end
function sandbox_hash.md5(file_or_data)
local md5, errors = hash.md5(file_or_data)
if not md5 then
- raise("cannot generate md5 for %s, %s", file_or_data, errors or "unknown errors")
+ raise("cannot generate md5, %s", errors or "unknown errors")
end
return md5
end
+-- generate xxhash32 from the given file or data
+function sandbox_hash.xxhash32(file_or_data)
+ local result, errors = hash.xxhash32(file_or_data)
+ if not result then
+ raise("cannot generate xxhash32, %s", errors or "unknown errors")
+ end
+ return result
+end
+
-- generate xxhash64 from the given file or data
function sandbox_hash.xxhash64(file_or_data)
- local xxhash64, errors = hash.xxhash64(file_or_data)
- if not xxhash64 then
- raise("cannot generate xxhash64 for %s, %s", file_or_data, errors or "unknown errors")
+ local result, errors = hash.xxhash64(file_or_data)
+ if not result then
+ raise("cannot generate xxhash64, %s", errors or "unknown errors")
end
- return xxhash64
+ return result
end
-- generate xxhash128 from the given file or data
function sandbox_hash.xxhash128(file_or_data)
- local xxhash128, errors = hash.xxhash128(file_or_data)
- if not xxhash128 then
- raise("cannot generate xxhash128 for %s, %s", file_or_data, errors or "unknown errors")
+ local result, errors = hash.xxhash128(file_or_data)
+ if not result then
+ raise("cannot generate xxhash128, %s", errors or "unknown errors")
end
- return xxhash128
+ return result
end
-- generate hash32 from string
function sandbox_hash.strhash32(str)
- local hash32, errors = hash.strhash32(str)
- if not hash32 then
+ local result, errors = hash.strhash32(str)
+ if not result then
+ raise("cannot generate hash32 for %s, %s", str, errors or "unknown errors")
+ end
+ return result
+end
+
+-- generate hash64 from string
+function sandbox_hash.strhash64(str)
+ local result, errors = hash.strhash64(str)
+ if not result then
raise("cannot generate hash32 for %s, %s", str, errors or "unknown errors")
end
- return hash32
+ return result
end
-- generate hash128 from string
function sandbox_hash.strhash128(str)
- local hash128, errors = hash.strhash128(str)
- if not hash128 then
+ local result, errors = hash.strhash128(str)
+ if not result then
raise("cannot generate hash128 for %s, %s", str, errors or "unknown errors")
end
- return hash128
+ return result
+end
+
+-- generate random32
+function sandbox_hash.rand32()
+ local result, errors = hash.rand32()
+ if not result then
+ raise("cannot generate random32, %s", errors or "unknown errors")
+ end
+ return result
+end
+
+-- generate random64
+function sandbox_hash.rand64()
+ local result, errors = hash.rand64()
+ if not result then
+ raise("cannot generate random64, %s", errors or "unknown errors")
+ end
+ return result
+end
+
+-- generate random128
+function sandbox_hash.rand128()
+ local result, errors = hash.rand128()
+ if not result then
+ raise("cannot generate random128, %s", errors or "unknown errors")
+ end
+ return result
end
-- return module
diff --git a/xmake/modules/private/cache/build_cache.lua b/xmake/modules/private/cache/build_cache.lua
index 3b82578e5..905426ef5 100644
--- a/xmake/modules/private/cache/build_cache.lua
+++ b/xmake/modules/private/cache/build_cache.lua
@@ -128,7 +128,7 @@ function cachekey(program, cppinfo, envs)
end
end
end
- return hash.xxhash128(bytes(table.concat(items, "")))
+ return hash.strhash128(table.concat(items, ""))
end
-- get cache root directory
diff --git a/xmake/plugins/project/cmake/cmakelists.lua b/xmake/plugins/project/cmake/cmakelists.lua
index a5ec312cb..a339a654e 100644
--- a/xmake/plugins/project/cmake/cmakelists.lua
+++ b/xmake/plugins/project/cmake/cmakelists.lua
@@ -1074,7 +1074,7 @@ function _add_target_link_libraries(cmakelists, target, outputdir)
local has_links = #target:objectfiles() > objectfiles_set:size()
- local key = target:name() .. "_" .. hash.uuid():split("-", {plain = true})[1]
+ local key = target:name() .. "_" .. hash.rand32()
if has_links then
cmakelists:print("add_library(target_objectfiles_%s OBJECT IMPORTED GLOBAL)", key)
cmakelists:print("set_property(TARGET target_objectfiles_%s PROPERTY IMPORTED_OBJECTS", key)
@@ -1244,7 +1244,7 @@ function _add_target_custom_commands_for_batchcmds(cmakelists, target, outputdir
--
-- @see https://gitlab.kitware.com/cmake/cmake/-/issues/17802
--
- local key = target:name() .. "_" .. hash.uuid():split("-", {plain = true})[1]
+ local key = target:name() .. "_" .. hash.rand32()
cmakelists:print("add_custom_command(OUTPUT output_%s", key)
for _, cmd in ipairs(cmds) do
local command = _get_command_string(cmd, outputdir)
diff --git a/xmake/rules/c++/modules/support.lua b/xmake/rules/c++/modules/support.lua
index 8d1c39531..cee0f1ab8 100644
--- a/xmake/rules/c++/modules/support.lua
+++ b/xmake/rules/c++/modules/support.lua
@@ -336,7 +336,7 @@ function modules_cachedir(target, opt)
moduletype = "interfaces"
elseif opt.scan then
moduletype = "scans"
- else
+ else
moduletype = "implementation"
end
local cachedir = path.join(target:autogendir(), "rules", "bmi", "cache", moduletype)
@@ -347,7 +347,7 @@ function modules_cachedir(target, opt)
end
function get_modulehash(sourcefile)
- return hash.uuid(sourcefile):split("-", {plain = true})[1]:lower()
+ return hash.strhash32(sourcefile)
end
function get_metafile(target, module)
diff --git a/xmake/rules/c++/unity_build/unity_build.lua b/xmake/rules/c++/unity_build/unity_build.lua
index e8f37f868..3df2d75b4 100644
--- a/xmake/rules/c++/unity_build/unity_build.lua
+++ b/xmake/rules/c++/unity_build/unity_build.lua
@@ -36,7 +36,7 @@ function _merge_unityfile(target, sourcefile_unity, sourcefiles, opt)
sourcefile_unity = path.absolute(sourcefile_unity)
sourcefile = path.relative(sourcefile, path.directory(sourcefile_unity))
if uniqueid then
- unityfile:print("#define %s %s", uniqueid, "unity_" .. hash.uuid():split("-", {plain = true})[1])
+ unityfile:print("#define %s %s", uniqueid, "unity_" .. hash.rand32())
end
unityfile:print("#include \"%s\"", sourcefile)
if uniqueid then