summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-10-11 00:54:08 +0800
committerruki <[email protected]>2024-10-11 00:54:08 +0800
commitc189bb54bc616f219d7881c032a207fdaa385a17 (patch)
tree558bb98806ae93e5bd085a392578371628a5d807
parent09ee92711986cb729d89c23c1802156bd335a602 (diff)
improve hash
-rw-r--r--xmake/core/base/hash.lua22
-rw-r--r--xmake/core/sandbox/modules/hash.lua46
-rw-r--r--xmake/includes/xrepo/xmake.lua4
-rw-r--r--xmake/modules/private/action/require/impl/repository.lua2
-rw-r--r--xmake/modules/private/action/require/impl/utils/requirekey.lua2
-rw-r--r--xmake/plugins/pack/nsis/main.lua2
6 files changed, 53 insertions, 25 deletions
diff --git a/xmake/core/base/hash.lua b/xmake/core/base/hash.lua
index d5fa685b5..be831c205 100644
--- a/xmake/core/base/hash.lua
+++ b/xmake/core/base/hash.lua
@@ -31,7 +31,7 @@ hash._md5 = hash._md5 or hash.md5
hash._sha = hash._sha or hash.sha
hash._xxhash = hash._xxhash or hash.xxhash
--- make md5 from the given file or data
+-- generate md5 from the given file or data
function hash.md5(file_or_data)
local hashstr, errors
if bytes.instance_of(file_or_data) then
@@ -44,7 +44,7 @@ function hash.md5(file_or_data)
return hashstr, errors
end
--- make sha1 from the given file or data
+-- generate sha1 from the given file or data
function hash.sha1(file_or_data)
local hashstr, errors
if bytes.instance_of(file_or_data) then
@@ -57,7 +57,7 @@ function hash.sha1(file_or_data)
return hashstr, errors
end
--- make sha256 from the given file or data
+-- generate sha256 from the given file or data
function hash.sha256(file_or_data)
local hashstr, errors
if bytes.instance_of(file_or_data) then
@@ -70,7 +70,7 @@ function hash.sha256(file_or_data)
return hashstr, errors
end
--- make xxhash64 from the given file or data
+-- generate xxhash64 from the given file or data
function hash.xxhash64(file_or_data)
local hashstr, errors
if bytes.instance_of(file_or_data) then
@@ -83,7 +83,7 @@ function hash.xxhash64(file_or_data)
return hashstr, errors
end
--- make xxhash128 from the given file or data
+-- generate xxhash128 from the given file or data
function hash.xxhash128(file_or_data)
local hashstr, errors
if bytes.instance_of(file_or_data) then
@@ -96,10 +96,20 @@ function hash.xxhash128(file_or_data)
return hashstr, errors
end
--- make uuid
+-- generate uuid, e.g "91E8ECF1-417F-4EDF-A574-E22D7D8D204A"
function hash.uuid(str)
return hash.uuid4(str)
end
+-- generate hash32, e.g. "91e8ecf1"
+function hash.hash32(str)
+ return hash.uuid4(str):split("-", {plain = true})[1]:lower()
+end
+
+-- generate hash128, e.g. "91e8ecf1417f4edfa574e22d7d8d204a"
+function hash.hash128(str)
+ return hash.uuid4(str):replace("-", "", {plain = true}):lower()
+end
+
-- return module: hash
return hash
diff --git a/xmake/core/sandbox/modules/hash.lua b/xmake/core/sandbox/modules/hash.lua
index 6b91f9f25..45fc5fb43 100644
--- a/xmake/core/sandbox/modules/hash.lua
+++ b/xmake/core/sandbox/modules/hash.lua
@@ -25,69 +25,87 @@ local raise = require("sandbox/modules/raise")
-- define module
local sandbox_hash = sandbox_hash or {}
--- make a new uuid
+-- generate a new uuid
function sandbox_hash.uuid(str)
local uuid = hash.uuid(str)
if not uuid then
- raise("cannot make uuid %s", str)
+ raise("cannot generate uuid %s", str)
end
return uuid
end
--- make a new uuid v4
+-- generate a new uuid v4
function sandbox_hash.uuid4(str)
local uuid = hash.uuid4(str)
if not uuid then
- raise("cannot make uuid4 %s", str)
+ raise("cannot generate uuid4 %s", str)
end
return uuid
end
--- make sha1 from the given file or data
+-- generate sha1 from the given file or data
function sandbox_hash.sha1(file_or_data)
local sha1, errors = hash.sha1(file_or_data)
if not sha1 then
- raise("cannot make sha1 for %s, %s", file_or_data, errors or "unknown errors")
+ raise("cannot generate sha1 for %s, %s", file_or_data, errors or "unknown errors")
end
return sha1
end
--- make sha256 from the given file or data
+-- generate sha256 from the given file or data
function sandbox_hash.sha256(file_or_data)
local sha256, errors = hash.sha256(file_or_data)
if not sha256 then
- raise("cannot make sha256 for %s, %s", file_or_data, errors or "unknown errors")
+ raise("cannot generate sha256 for %s, %s", file_or_data, errors or "unknown errors")
end
return sha256
end
--- make md5 from the given file or data
+-- generate md5 from the given file or data
function sandbox_hash.md5(file_or_data)
local md5, errors = hash.md5(file_or_data)
if not md5 then
- raise("cannot make md5 for %s, %s", file_or_data, errors or "unknown errors")
+ raise("cannot generate md5 for %s, %s", file_or_data, errors or "unknown errors")
end
return md5
end
--- make xxhash64 from the given file or data
+-- 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 make xxhash64 for %s, %s", file_or_data, errors or "unknown errors")
+ raise("cannot generate xxhash64 for %s, %s", file_or_data, errors or "unknown errors")
end
return xxhash64
end
--- make xxhash128 from the given file or data
+-- 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 make xxhash128 for %s, %s", file_or_data, errors or "unknown errors")
+ raise("cannot generate xxhash128 for %s, %s", file_or_data, errors or "unknown errors")
end
return xxhash128
end
+-- generate hash32 from string
+function sandbox_hash.hash32(str)
+ local hash32, errors = hash.hash32(str)
+ if not hash32 then
+ raise("cannot generate hash32 for %s, %s", str, errors or "unknown errors")
+ end
+ return hash32
+end
+
+-- generate hash128 from string
+function sandbox_hash.hash128(str)
+ local hash128, errors = hash.hash128(str)
+ if not hash128 then
+ raise("cannot generate hash128 for %s, %s", str, errors or "unknown errors")
+ end
+ return hash128
+end
+
-- return module
return sandbox_hash
diff --git a/xmake/includes/xrepo/xmake.lua b/xmake/includes/xrepo/xmake.lua
index 7f16410da..9b3e0abaa 100644
--- a/xmake/includes/xrepo/xmake.lua
+++ b/xmake/includes/xrepo/xmake.lua
@@ -32,7 +32,7 @@
-- @endcode
--
function xrepo_addenvs(envs)
- local packagename = "__xrepo_addenvs_" .. (tostring(envs):gsub("%s", "_"))
+ local packagename = "__xrepo_addenvs_" .. hash.hash32(tostring(envs))
package(packagename)
on_load(function (package)
if type(envs) == "function" then
@@ -61,7 +61,7 @@ end
--
function xrepo_addenv(name, ...)
local args = table.pack(...)
- local packagename = "__xrepo_addenv_" .. name .. (table.concat(args):gsub("%s", "_"))
+ local packagename = "__xrepo_addenv_" .. name .. hash.hash32(table.concat(args))
package(packagename)
on_load(function (package)
package:addenv(name, table.unpack(args))
diff --git a/xmake/modules/private/action/require/impl/repository.lua b/xmake/modules/private/action/require/impl/repository.lua
index 68f4ccb6d..001d2ec0a 100644
--- a/xmake/modules/private/action/require/impl/repository.lua
+++ b/xmake/modules/private/action/require/impl/repository.lua
@@ -38,7 +38,7 @@ function _get_packagedir_from_locked_repo(packagename, locked_repo)
break
end
end
- local reponame = hash.uuid(locked_repo.url .. (locked_repo.commit or "")):gsub("%-", ""):lower() .. ".lock"
+ local reponame = hash.hash128(locked_repo.url .. (locked_repo.commit or "")) .. ".lock"
-- get local repodir
local repodir_local
diff --git a/xmake/modules/private/action/require/impl/utils/requirekey.lua b/xmake/modules/private/action/require/impl/utils/requirekey.lua
index 633abb0d1..a8f435cdf 100644
--- a/xmake/modules/private/action/require/impl/utils/requirekey.lua
+++ b/xmake/modules/private/action/require/impl/utils/requirekey.lua
@@ -78,7 +78,7 @@ function main(requireinfo, opt)
if key == "" then
key = "_" -- we need to generate a fixed hash value
end
- return hash.uuid(key):split("-", {plain = true})[1]:lower()
+ return hash.hash32(key)
else
return key
end
diff --git a/xmake/plugins/pack/nsis/main.lua b/xmake/plugins/pack/nsis/main.lua
index 4ab5f8d8e..1b49da3b5 100644
--- a/xmake/plugins/pack/nsis/main.lua
+++ b/xmake/plugins/pack/nsis/main.lua
@@ -83,7 +83,7 @@ end
-- get unique tag
function _get_unique_tag(content)
- return hash.uuid(content):split("-", {plain = true})[1]:lower()
+ return hash.hash32(content)
end
-- translate the file path