summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-03-09 18:59:46 +0800
committerGitHub <[email protected]>2022-03-09 18:59:46 +0800
commit50c63bf3d9d04a358600d6ec66cdcc74d48ad492 (patch)
tree4dbe07ba15c4f2119b6bcded86b5bd1ef38085db
parent96a0a4860880a87ac9f6503df28a4c0bcb291bf1 (diff)
parent8485c3591a80f2b53610adcc2efae9a9e1bf5599 (diff)
Merge pull request #2137 from xmake-io/path
Improve path
m---------core/src/tbox/tbox0
-rw-r--r--core/src/xmake/engine.c2
-rw-r--r--core/src/xmake/makefile1
-rw-r--r--core/src/xmake/path/absolute.c4
-rw-r--r--core/src/xmake/path/directory.c51
-rw-r--r--core/src/xmake/path/relative.c2
-rw-r--r--core/src/xmake/path/translate.c19
-rw-r--r--tests/modules/path/test.lua88
-rw-r--r--xmake/core/base/os.lua2
-rw-r--r--xmake/core/base/path.lua22
-rw-r--r--xmake/core/theme/theme.lua10
-rw-r--r--xmake/modules/core/tools/cl.lua3
-rw-r--r--xmake/modules/core/tools/clang_cl.lua3
-rw-r--r--xmake/modules/core/tools/gcc.lua3
14 files changed, 168 insertions, 42 deletions
diff --git a/core/src/tbox/tbox b/core/src/tbox/tbox
-Subproject cfb72e0c568537c61f672e492013af0d96cb9ae
+Subproject 19a1e565216e9797c3c145d2db176d54c27991c
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index efc89977c..2f4efd60c 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -173,6 +173,7 @@ tb_int_t xm_io_poller_wait(lua_State* lua);
tb_int_t xm_path_relative(lua_State* lua);
tb_int_t xm_path_absolute(lua_State* lua);
tb_int_t xm_path_translate(lua_State* lua);
+tb_int_t xm_path_directory(lua_State* lua);
tb_int_t xm_path_is_absolute(lua_State* lua);
// the hash functions
@@ -366,6 +367,7 @@ static luaL_Reg const g_path_functions[] =
{ "relative", xm_path_relative }
, { "absolute", xm_path_absolute }
, { "translate", xm_path_translate }
+, { "directory", xm_path_directory }
, { "is_absolute", xm_path_is_absolute }
, { tb_null, tb_null }
};
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index c85696f1e..d25a12a9b 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -92,6 +92,7 @@ xmake_C_FILES += \
path/relative \
path/absolute \
path/translate \
+ path/directory \
path/is_absolute \
hash/uuid4 \
hash/sha256 \
diff --git a/core/src/xmake/path/absolute.c b/core/src/xmake/path/absolute.c
index b4baac03d..d8489d25f 100644
--- a/core/src/xmake/path/absolute.c
+++ b/core/src/xmake/path/absolute.c
@@ -45,10 +45,8 @@ tb_int_t xm_path_absolute(lua_State* lua)
// get the root
tb_char_t const* root = luaL_optstring(lua, 2, tb_null);
- // done path:absolute(root)
+ // do path:absolute(root)
tb_char_t data[TB_PATH_MAXN];
lua_pushstring(lua, tb_path_absolute_to(root, path, data, sizeof(data) - 1));
-
- // ok
return 1;
}
diff --git a/core/src/xmake/path/directory.c b/core/src/xmake/path/directory.c
new file mode 100644
index 000000000..b8ec7476d
--- /dev/null
+++ b/core/src/xmake/path/directory.c
@@ -0,0 +1,51 @@
+/*!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 directory.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "directory"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_path_directory(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the path
+ tb_char_t const* path = luaL_checkstring(lua, 1);
+ tb_check_return_val(path, 0);
+
+ // do path:directory()
+ tb_char_t data[TB_PATH_MAXN];
+ tb_char_t const* dir = tb_path_directory(path, data, sizeof(data));
+ if (dir) lua_pushstring(lua, dir);
+ else lua_pushnil(lua);
+ return 1;
+}
diff --git a/core/src/xmake/path/relative.c b/core/src/xmake/path/relative.c
index 782f0f635..f64667747 100644
--- a/core/src/xmake/path/relative.c
+++ b/core/src/xmake/path/relative.c
@@ -48,7 +48,5 @@ tb_int_t xm_path_relative(lua_State* lua)
// done path:relative(root)
tb_char_t data[TB_PATH_MAXN];
lua_pushstring(lua, tb_path_relative_to(root, path, data, sizeof(data) - 1));
-
- // ok
return 1;
}
diff --git a/core/src/xmake/path/translate.c b/core/src/xmake/path/translate.c
index 4a9733781..87059c85c 100644
--- a/core/src/xmake/path/translate.c
+++ b/core/src/xmake/path/translate.c
@@ -39,15 +39,24 @@ tb_int_t xm_path_translate(lua_State* lua)
tb_assert_and_check_return_val(lua, 0);
// get the path
- tb_char_t const* path = luaL_checkstring(lua, 1);
+ size_t path_size = 0;
+ tb_char_t const* path = luaL_checklstring(lua, 1, &path_size);
tb_check_return_val(path, 0);
- // copy path
- tb_char_t data[TB_PATH_MAXN];
- tb_strlcpy(data, path, sizeof(data));
+ // get the option argument, e.g. {reduce_dot2 = true}
+ tb_bool_t reduce_dot2 = tb_false;
+ if (lua_istable(lua, 2))
+ {
+ lua_pushstring(lua, "reduce_dot2");
+ lua_gettable(lua, 2);
+ if (lua_toboolean(lua, -1))
+ reduce_dot2 = tb_true;
+ lua_pop(lua, 1);
+ }
// do path:translate()
- tb_size_t size = tb_path_translate(data, 0, sizeof(data) - 1);
+ tb_char_t data[TB_PATH_MAXN];
+ tb_size_t size = tb_path_translate_to(path, (tb_size_t)path_size, data, sizeof(data), reduce_dot2);
if (size) lua_pushlstring(lua, data, (size_t)size);
else lua_pushnil(lua);
return 1;
diff --git a/tests/modules/path/test.lua b/tests/modules/path/test.lua
index 80e87c5aa..8954e64a3 100644
--- a/tests/modules/path/test.lua
+++ b/tests/modules/path/test.lua
@@ -33,3 +33,91 @@ function test_extension(t)
t:are_equal(path.extension("/home/foo.so"), ".so")
t:are_equal(path.extension("\\home\\foo.so"), ".so")
end
+
+function test_directory(t)
+ t:are_equal(path.directory(""), nil)
+ t:are_equal(path.directory("."), nil)
+ t:are_equal(path.directory("foo"), ".")
+ if is_host("windows") then
+ t:are_equal(path.directory("c:"), nil)
+ t:are_equal(path.directory("c:\\"), nil)
+ t:are_equal(path.directory("c:\\xxx"), "c:")
+ t:are_equal(path.directory("c:\\xxx\\yyy"), "c:\\xxx")
+ else
+ t:are_equal(path.directory("/tmp"), "/")
+ t:are_equal(path.directory("/tmp/"), "/")
+ t:are_equal(path.directory("/tmp/xxx"), "/tmp")
+ t:are_equal(path.directory("/tmp/xxx/"), "/tmp")
+ t:are_equal(path.directory("/"), nil)
+ end
+end
+
+function test_absolute(t)
+ t:are_equal(path.absolute("", ""), nil)
+ t:are_equal(path.absolute(".", "."), ".")
+ if is_host("windows") then
+ t:are_equal(path.absolute("foo", "c:"), "c:\\foo")
+ t:are_equal(path.absolute("foo", "c:\\"), "c:\\foo")
+ t:are_equal(path.absolute("foo", "c:\\tmp"), "c:\\tmp\\foo")
+ t:are_equal(path.absolute("foo", "c:\\tmp\\"), "c:\\tmp\\foo")
+ else
+ t:are_equal(path.absolute("", "/"), nil)
+ t:are_equal(path.absolute("/", "/"), "/")
+ t:are_equal(path.absolute(".", "/"), "/")
+ t:are_equal(path.absolute("foo", "/tmp/"), "/tmp/foo")
+ t:are_equal(path.absolute("foo", "/tmp"), "/tmp/foo")
+ end
+end
+
+function test_relative(t)
+ t:are_equal(path.relative("", ""), nil)
+ t:are_equal(path.relative(".", "."), ".")
+ if is_host("windows") then
+ t:are_equal(path.relative("c:", "c:\\"), ".")
+ t:are_equal(path.relative("c:\\foo", "c:\\foo"), ".")
+ t:are_equal(path.relative("c:\\foo", "c:\\"), "foo")
+ t:are_equal(path.relative("c:\\tmp\\foo", "c:\\tmp"), "foo")
+ t:are_equal(path.relative("c:\\tmp\\foo", "c:\\tmp\\"), "foo")
+ else
+ t:are_equal(path.relative("", "/"), nil)
+ t:are_equal(path.relative("/", "/"), ".")
+ t:are_equal(path.relative("/tmp/foo", "/tmp/"), "foo")
+ t:are_equal(path.relative("/tmp/foo", "/tmp"), "foo")
+ end
+end
+
+function test_translate(t)
+ t:are_equal(path.translate(""), nil)
+ t:are_equal(path.translate("."), ".")
+ t:are_equal(path.translate(".."), "..")
+ t:are_equal(path.translate("././."), ".")
+ t:are_equal(path.translate("../foo/..", {reduce_dot2 = true}), "..")
+ t:are_equal(path.translate("../foo/bar/../..", {reduce_dot2 = true}), "..")
+ if is_host("windows") then
+ t:are_equal(path.translate("c:"), "c:")
+ t:are_equal(path.translate("c:\\"), "c:")
+ t:are_equal(path.translate("c:\\foo\\.\\.\\"), "c:\\foo")
+ t:are_equal(path.translate("c:\\foo\\\\\\"), "c:\\foo")
+ t:are_equal(path.translate("c:\\foo\\..\\.."), "c:\\foo\\..\\..")
+ t:are_equal(path.translate("c:\\foo\\bar\\.\\..\\xyz", {reduce_dot2 = true}), "c:\\foo\\xyz")
+ t:are_equal(path.translate("c:\\foo\\.\\..", {reduce_dot2 = true}), "c:")
+ t:are_equal(path.translate("../..", {reduce_dot2 = true}), "..\\..")
+ t:are_equal(path.translate("../foo/bar/..", {reduce_dot2 = true}), "..\\foo")
+ t:are_equal(path.translate("../foo/bar/../../..", {reduce_dot2 = true}), "..\\..")
+ else
+ t:are_equal(path.translate("/"), "/");
+ t:are_equal(path.translate("////"), "/");
+ t:are_equal(path.translate("/./././"), "/");
+ t:are_equal(path.translate("/foo/././"), "/foo");
+ t:are_equal(path.translate("/foo//////"), "/foo");
+ t:are_equal(path.translate("/foo/../.."), "/foo/../..");
+ t:are_equal(path.translate("/foo/../../"), "/foo/../..");
+ t:are_equal(path.translate("/foo/bar/.//..//xyz", {reduce_dot2 = true}), "/foo/xyz");
+ t:are_equal(path.translate("/foo/../..", {reduce_dot2 = true}), "/");
+ t:are_equal(path.translate("/foo/bar../..", {reduce_dot2 = true}), "/foo");
+ t:are_equal(path.translate("../..", {reduce_dot2 = true}), "../..");
+ t:are_equal(path.translate("../foo/bar/..", {reduce_dot2 = true}), "../foo");
+ t:are_equal(path.translate("../foo/bar/../../..", {reduce_dot2 = true}), "../..");
+ end
+end
+
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 19f22e88f..cb6821d3d 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -364,7 +364,7 @@ function os.match(pattern, mode, callback)
if startpos then
rootdir = rootdir:sub(1, startpos - 1)
end
- rootdir = path.directory(rootdir)
+ rootdir = path.directory(rootdir .. "_") -- patch '_' to avoid getting incorrect directory for `/foo/*`
-- compute the recursion level
--
diff --git a/xmake/core/base/path.lua b/xmake/core/base/path.lua
index 92b926705..ede2c36eb 100644
--- a/xmake/core/base/path.lua
+++ b/xmake/core/base/path.lua
@@ -24,23 +24,6 @@ local path = path or {}
-- load modules
local string = require("base/string")
--- get the directory of the path
-function path.directory(p, sep)
- local i = 0
- if sep then
- -- if the path has been normalized, we can quickly find it with a unique path separator prompt
- i = p:lastof(sep, true) or 0
- else
- i = math.max(p:lastof('/', true) or 0, p:lastof('\\', true) or 0)
- end
- if i > 0 then
- if i > 1 then i = i - 1 end
- return p:sub(1, i)
- else
- return "."
- end
-end
-
-- get the filename of the path
function path.filename(p, sep)
local i = 0
@@ -87,10 +70,7 @@ end
-- join path
function path.join(p, ...)
- for _, name in ipairs({...}) do
- p = p .. "/" .. name
- end
- return path.translate(p)
+ return path.translate(p .. path.sep() .. table.concat({...}, path.sep()))
end
-- split path by the separator
diff --git a/xmake/core/theme/theme.lua b/xmake/core/theme/theme.lua
index 60c01ce1c..6ab9c592b 100644
--- a/xmake/core/theme/theme.lua
+++ b/xmake/core/theme/theme.lua
@@ -115,10 +115,12 @@ function theme.load(name)
-- find the theme script path
local scriptpath = nil
- for _, dir in ipairs(theme.directories()) do
- scriptpath = path.join(dir, name, "xmake.lua")
- if os.isfile(scriptpath) then
- break
+ if name then
+ for _, dir in ipairs(theme.directories()) do
+ scriptpath = path.join(dir, name, "xmake.lua")
+ if os.isfile(scriptpath) then
+ break
+ end
end
end
diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua
index b499542e3..2526536c4 100644
--- a/xmake/modules/core/tools/cl.lua
+++ b/xmake/modules/core/tools/cl.lua
@@ -409,8 +409,7 @@ end
function compile(self, sourcefile, objectfile, dependinfo, flags, opt)
-- ensure the object directory
- -- @note this path here has been normalized, we can quickly find it by the unique path separator prompt
- local objectdir = path.directory(objectfile, path.sep())
+ local objectdir = path.directory(objectfile)
if not os.isdir(objectdir) then
os.mkdir(objectdir)
end
diff --git a/xmake/modules/core/tools/clang_cl.lua b/xmake/modules/core/tools/clang_cl.lua
index 9c4236343..1602b0ac8 100644
--- a/xmake/modules/core/tools/clang_cl.lua
+++ b/xmake/modules/core/tools/clang_cl.lua
@@ -166,8 +166,7 @@ end
function compile(self, sourcefile, objectfile, dependinfo, flags)
-- ensure the object directory
- -- @note this path here has been normalized, we can quickly find it by the unique path separator prompt
- os.mkdir(path.directory(objectfile, path.sep()))
+ os.mkdir(path.directory(objectfile))
-- compile it
local outdata = try
diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua
index a1dc82c9d..0d729692a 100644
--- a/xmake/modules/core/tools/gcc.lua
+++ b/xmake/modules/core/tools/gcc.lua
@@ -427,8 +427,7 @@ end
function compile(self, sourcefile, objectfile, dependinfo, flags)
-- ensure the object directory
- -- @note this path here has been normalized, we can quickly find it by the unique path separator prompt
- os.mkdir(path.directory(objectfile, path.sep()))
+ os.mkdir(path.directory(objectfile))
-- compile it
local depfile = dependinfo and os.tmpfile() or nil