summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-10-11 09:47:50 +0800
committerGitHub <[email protected]>2024-10-11 09:47:50 +0800
commitfdf11786de46222d61403280e68fdf3cd5d572e3 (patch)
tree2ca27997b3796f5600ace902e4cc63b24283c309
parent687beddbf316b1565a55b68934c3681320a1d51f (diff)
parentbae6c8f0ecbcebe57d77cb88fd8648373193f28b (diff)
Merge pull request #5703 from xmake-io/hash
Improve hash
-rw-r--r--xmake/core/base/hash.lua26
-rw-r--r--xmake/core/package/package.lua2
-rw-r--r--xmake/core/project/target.lua4
-rw-r--r--xmake/core/sandbox/modules/hash.lua56
-rw-r--r--xmake/core/sandbox/modules/import/core/sandbox/module.lua2
-rw-r--r--xmake/core/sandbox/modules/interpreter/hash.lua23
-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/modules/utils/ci/packageskey.lua2
-rw-r--r--xmake/plugins/pack/nsis/main.lua2
-rw-r--r--xmake/rules/qt/qrc/xmake.lua2
12 files changed, 94 insertions, 33 deletions
diff --git a/xmake/core/base/hash.lua b/xmake/core/base/hash.lua
index 74bbfd4cd..f6bd27033 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,5 +96,21 @@ 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()
+end
+
+-- generate hash128 from string, e.g. "91e8ecf1417f4edfa574e22d7d8d204a"
+function hash.strhash128(str)
+ return hash.uuid4(str):replace("-", "", {plain = true}):lower()
+end
+
-- return module: hash
return hash
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua
index 0b1b9879e..5d8d6b111 100644
--- a/xmake/core/package/package.lua
+++ b/xmake/core/package/package.lua
@@ -1701,7 +1701,7 @@ function _instance:buildhash()
table.sort(toolchains)
str = str .. "_" .. table.concat(toolchains, "_")
end
- return hash.uuid4(str):gsub('-', ''):lower()
+ return hash.strhash128(str)
end
local function _get_installdir(...)
local name = self:name():lower():gsub("::", "_")
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index 4ca895d0c..4e908ec6b 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -1496,7 +1496,7 @@ function _instance:autogenfile(sourcefile, opt)
-- @see
-- https://github.com/xmake-io/xmake/issues/3021
-- https://github.com/xmake-io/xmake/issues/3715
- relativedir = hash.uuid4(relativedir):gsub("%-", ""):lower()
+ relativedir = hash.strhash128(relativedir)
end
relativedir = relativedir:gsub("%.%.", "__")
local rootdir = (opt and opt.rootdir) and opt.rootdir or self:autogendir()
@@ -2138,7 +2138,7 @@ function _instance:dependfile(objectfile)
-- @see
-- https://github.com/xmake-io/xmake/issues/3021
-- https://github.com/xmake-io/xmake/issues/3715
- relativedir = hash.uuid4(relativedir):gsub("%-", ""):lower()
+ relativedir = hash.strhash128(relativedir)
end
-- originfile: project/build/.objs/xxxx/../../xxx.c will be out of range for objectdir
diff --git a/xmake/core/sandbox/modules/hash.lua b/xmake/core/sandbox/modules/hash.lua
index 0827f072f..50e2100cc 100644
--- a/xmake/core/sandbox/modules/hash.lua
+++ b/xmake/core/sandbox/modules/hash.lua
@@ -25,65 +25,87 @@ local raise = require("sandbox/modules/raise")
-- define module
local sandbox_hash = sandbox_hash or {}
--- make a new uuid
-function sandbox_hash.uuid(name)
- return sandbox_hash.uuid4(name)
+-- generate a new uuid
+function sandbox_hash.uuid(str)
+ local uuid = hash.uuid(str)
+ if not uuid then
+ raise("cannot generate uuid %s", str)
+ end
+ return uuid
end
--- make a new uuid v4
-function sandbox_hash.uuid4(name)
- local uuid = hash.uuid4(name)
+-- generate a new uuid v4
+function sandbox_hash.uuid4(str)
+ local uuid = hash.uuid4(str)
if not uuid then
- raise("cannot make uuid %s", name)
+ 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.strhash32(str)
+ local hash32, errors = hash.strhash32(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.strhash128(str)
+ local hash128, errors = hash.strhash128(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/core/sandbox/modules/import/core/sandbox/module.lua b/xmake/core/sandbox/modules/import/core/sandbox/module.lua
index bddfb1524..df1d31240 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.uuid4(projectdir):split("-", {plain = true})[1]:lower()
+ local modulehash = hash.strhash32(projectdir)
local buildir
local is_global = false
local modulepath
diff --git a/xmake/core/sandbox/modules/interpreter/hash.lua b/xmake/core/sandbox/modules/interpreter/hash.lua
new file mode 100644
index 000000000..83d7f2fd3
--- /dev/null
+++ b/xmake/core/sandbox/modules/interpreter/hash.lua
@@ -0,0 +1,23 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file hash.lua
+--
+
+-- load module
+return require("sandbox/modules/hash")
+
diff --git a/xmake/includes/xrepo/xmake.lua b/xmake/includes/xrepo/xmake.lua
index 7f16410da..2810dc5a8 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.strhash32(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.strhash32(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..74b97efda 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.strhash128(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..13d31cd3a 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.strhash32(key)
else
return key
end
diff --git a/xmake/modules/utils/ci/packageskey.lua b/xmake/modules/utils/ci/packageskey.lua
index a293009af..c29ddfdfe 100644
--- a/xmake/modules/utils/ci/packageskey.lua
+++ b/xmake/modules/utils/ci/packageskey.lua
@@ -60,7 +60,7 @@ function main(requires_raw)
keys = table.concat(keys, ",")
option.restore()
- print(hash.uuid4(keys):gsub('-', ''):lower())
+ print(hash.strhash128(keys))
end
diff --git a/xmake/plugins/pack/nsis/main.lua b/xmake/plugins/pack/nsis/main.lua
index 4ab5f8d8e..64c002042 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.strhash32(content)
end
-- translate the file path
diff --git a/xmake/rules/qt/qrc/xmake.lua b/xmake/rules/qt/qrc/xmake.lua
index 60653d080..084a7fff0 100644
--- a/xmake/rules/qt/qrc/xmake.lua
+++ b/xmake/rules/qt/qrc/xmake.lua
@@ -44,7 +44,7 @@ rule("qt.qrc")
local rcc = target:data("qt.rcc")
-- get c++ source file for qrc
- local sourcefile_cpp = path.join(target:autogendir(), "rules", "qt", "qrc", path.basename(sourcefile_qrc).. "_" .. hash.uuid4(sourcefile_qrc):split('%-')[1] .. ".cpp")
+ local sourcefile_cpp = path.join(target:autogendir(), "rules", "qt", "qrc", path.basename(sourcefile_qrc).. "_" .. hash.strhash32(sourcefile_qrc) .. ".cpp")
local sourcefile_dir = path.directory(sourcefile_cpp)
-- add objectfile