summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-10-06 22:52:07 +0800
committerGitHub <[email protected]>2025-10-06 22:52:07 +0800
commit8bcc3569cba28654540fdca7a3954b814fae6ac6 (patch)
tree25d1efdc2d44d485782936e0e8023fc69e7436ac
parent273d9366486b7407f8d901e5304584d2ae7084d5 (diff)
parent69caeb34da2be2d03a86752b144c75881fcabf8f (diff)
Merge pull request #6896 from xmake-io/hash
add hash test
-rw-r--r--tests/modules/hash/test.lua27
-rw-r--r--xmake/core/base/hash.lua1
-rw-r--r--xmake/core/base/scheduler.lua2
-rw-r--r--xmake/core/sandbox/modules/import/core/sandbox/module.lua2
-rw-r--r--xmake/plugins/pack/nsis/main.lua5
-rw-r--r--xmake/plugins/project/cmake/cmakelists.lua4
-rw-r--r--xmake/rules/c++/modules/scanner.lua2
-rw-r--r--xmake/rules/c++/modules/support.lua2
-rw-r--r--xmake/rules/c++/unity_build/unity_build.lua2
9 files changed, 35 insertions, 12 deletions
diff --git a/tests/modules/hash/test.lua b/tests/modules/hash/test.lua
new file mode 100644
index 000000000..06722fd9d
--- /dev/null
+++ b/tests/modules/hash/test.lua
@@ -0,0 +1,27 @@
+function test_rand32(t)
+ local set = {}
+ for i = 1, 1000 do
+ local r = hash.rand32()
+ t:require(set[r] == nil)
+ set[r] = r
+ end
+end
+
+function test_rand64(t)
+ local set = {}
+ for i = 1, 100000 do
+ local r = hash.rand64()
+ t:require(set[r] == nil)
+ set[r] = r
+ end
+end
+
+function test_rand128(t)
+ local set = {}
+ for i = 1, 100000 do
+ local r = hash.rand128()
+ t:require(set[r] == nil)
+ set[r] = r
+ end
+end
+
diff --git a/xmake/core/base/hash.lua b/xmake/core/base/hash.lua
index 9d7ca40ed..d08a7d93b 100644
--- a/xmake/core/base/hash.lua
+++ b/xmake/core/base/hash.lua
@@ -145,6 +145,7 @@ function hash.strhash128(str)
end
-- generate random32 hash
+-- @note it is easy to trigger hash conflicts
function hash.rand32()
if hash._rand32 then
return hash._rand32()
diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua
index 8327af6e8..b11be7d4e 100644
--- a/xmake/core/base/scheduler.lua
+++ b/xmake/core/base/scheduler.lua
@@ -410,7 +410,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.strhash32(envs_hash)
+ envs_hash = hash.strhash64(envs_hash)
self._CO_CURENVS_HASH = envs_hash
-- save the current directory for each coroutine
diff --git a/xmake/core/sandbox/modules/import/core/sandbox/module.lua b/xmake/core/sandbox/modules/import/core/sandbox/module.lua
index 3a50e862b..dd7dac16e 100644
--- a/xmake/core/sandbox/modules/import/core/sandbox/module.lua
+++ b/xmake/core/sandbox/modules/import/core/sandbox/module.lua
@@ -203,7 +203,7 @@ function core_sandbox_module._native_moduleinfo(module_fullpath, modulekind)
local projectdir = path.normalize(path.absolute(module_fullpath))
local programdir = path.normalize(os.programdir())
local modulename = path.basename(module_fullpath)
- local modulehash = hash.strhash32(projectdir)
+ local modulehash = hash.strhash64(projectdir)
local builddir
local is_global = false
local modulepath
diff --git a/xmake/plugins/pack/nsis/main.lua b/xmake/plugins/pack/nsis/main.lua
index 1c6019d00..793bed1cf 100644
--- a/xmake/plugins/pack/nsis/main.lua
+++ b/xmake/plugins/pack/nsis/main.lua
@@ -81,11 +81,6 @@ function _get_makensis()
return makensis, oldenvs
end
--- get unique tag
-function _get_unique_tag(content)
- return hash.strhash32(content)
-end
-
-- translate the file path
function _translate_filepath(package, filepath)
return filepath:replace(package:install_rootdir(), "$InstDir", {plain = true})
diff --git a/xmake/plugins/project/cmake/cmakelists.lua b/xmake/plugins/project/cmake/cmakelists.lua
index a339a654e..a4d8361e1 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.rand32()
+ local key = target:name() .. "_" .. hash.rand64()
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.rand32()
+ local key = target:name() .. "_" .. hash.rand64()
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/scanner.lua b/xmake/rules/c++/modules/scanner.lua
index fa2c3b98b..818e37aa2 100644
--- a/xmake/rules/c++/modules/scanner.lua
+++ b/xmake/rules/c++/modules/scanner.lua
@@ -418,7 +418,7 @@ function _patch_sourcebatch(target, sourcebatch)
memcache:set2(target:fullname(), "cached_sourcebatch", sourcebatch)
local keys = #sourcebatch.sourcefiles > 0 and table.concat(sourcebatch.sourcefiles) or "_"
- local sum = hash.strhash32(keys)
+ local sum = hash.strhash64(keys)
local cached_sum = localcache:get2(target:fullname(), "sourcebatch_sum")
if not cached_sum or cached_sum ~= sum then
localcache:set2(target:fullname(), "sourcebatch_sum", sum)
diff --git a/xmake/rules/c++/modules/support.lua b/xmake/rules/c++/modules/support.lua
index cee0f1ab8..0f89d4e98 100644
--- a/xmake/rules/c++/modules/support.lua
+++ b/xmake/rules/c++/modules/support.lua
@@ -347,7 +347,7 @@ function modules_cachedir(target, opt)
end
function get_modulehash(sourcefile)
- return hash.strhash32(sourcefile)
+ return hash.strhash64(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 3df2d75b4..d5f5b060c 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.rand32())
+ unityfile:print("#define %s %s", uniqueid, "unity_" .. hash.rand64())
end
unityfile:print("#include \"%s\"", sourcefile)
if uniqueid then