diff options
| author | ruki <[email protected]> | 2022-03-09 23:25:38 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-03-09 23:25:38 +0800 |
| commit | fa2e4a9a5f21a8819beafb99a85652d83b7e5429 (patch) | |
| tree | 8ae328914c4dd54012e0cf6cc013547acaa4caeb | |
| parent | 4eb65435961776fbefcc11ed774b778532c2c8dd (diff) | |
improve path.directory
| -rw-r--r-- | core/src/xmake/engine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 1 | ||||
| -rw-r--r-- | core/src/xmake/path/absolute.c | 4 | ||||
| -rw-r--r-- | core/src/xmake/path/directory.c | 51 | ||||
| -rw-r--r-- | core/src/xmake/path/relative.c | 2 | ||||
| -rw-r--r-- | tests/modules/path/test.lua | 3 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 2 | ||||
| -rw-r--r-- | xmake/core/base/path.lua | 17 | ||||
| -rw-r--r-- | xmake/modules/core/tools/cl.lua | 3 | ||||
| -rw-r--r-- | xmake/modules/core/tools/clang_cl.lua | 3 | ||||
| -rw-r--r-- | xmake/modules/core/tools/gcc.lua | 3 |
11 files changed, 60 insertions, 31 deletions
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/tests/modules/path/test.lua b/tests/modules/path/test.lua index 7f0224b86..05a5a0b40 100644 --- a/tests/modules/path/test.lua +++ b/tests/modules/path/test.lua @@ -35,7 +35,6 @@ function test_extension(t) end function test_directory(t) - --[[ t:are_equal(path.directory(""), nil) t:are_equal(path.directory("."), nil) if is_host("windows") then @@ -49,7 +48,7 @@ function test_directory(t) t:are_equal(path.directory("/tmp/xxx"), "/tmp") t:are_equal(path.directory("/tmp/xxx/"), "/tmp") t:are_equal(path.directory("/"), nil) - end]] + end end function test_absolute(t) diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 19f22e88f..5fefd9ab5 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.translate(rootdir) -- compute the recursion level -- diff --git a/xmake/core/base/path.lua b/xmake/core/base/path.lua index 92b926705..712d8e604 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 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 |
