From d81c6dee77d69e8116a5b6acd1d404f470a2aabb Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 18 Mar 2020 23:05:33 +0800 Subject: improve path.directory --- xmake/core/base/path.lua | 24 ++++++++++++++++++------ xmake/modules/core/tools/cl.lua | 3 ++- xmake/modules/core/tools/gcc.lua | 3 ++- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/xmake/core/base/path.lua b/xmake/core/base/path.lua index f3d3d4f03..f79590423 100644 --- a/xmake/core/base/path.lua +++ b/xmake/core/base/path.lua @@ -25,9 +25,15 @@ local path = path or {} local string = require("base/string") -- get the directory of the path -function path.directory(p) - local i = p:lastof("[/\\]") - if i then +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 @@ -36,9 +42,15 @@ function path.directory(p) end -- get the filename of the path -function path.filename(p) - local i = p:lastof("[/\\]") - if i then +function path.filename(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 return p:sub(i + 1) else return p diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua index fc701d5d8..fb2d69a53 100644 --- a/xmake/modules/core/tools/cl.lua +++ b/xmake/modules/core/tools/cl.lua @@ -330,7 +330,8 @@ end function _compile1(self, sourcefile, objectfile, dependinfo, flags) -- ensure the object directory - local objectdir = path.directory(objectfile) + -- @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()) if not os.isdir(objectdir) then os.mkdir(objectdir) end diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 3912d7031..071a8140a 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -419,7 +419,8 @@ end function _compile1(self, sourcefile, objectfile, dependinfo, flags) -- ensure the object directory - os.mkdir(path.directory(objectfile)) + -- @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())) -- compile it local depfile = dependinfo and os.tmpfile() or nil -- cgit v1.3.1