Skip to content

Commit

Permalink
Merge pull request #488 from OpportunityLiu/vsproj
Browse files Browse the repository at this point in the history
Improve vsxmake
  • Loading branch information
waruqi committed Jul 19, 2019
2 parents ae533cd + ca45f89 commit 660b6f4
Show file tree
Hide file tree
Showing 46 changed files with 821 additions and 616 deletions.
95 changes: 56 additions & 39 deletions xmake/core/base/os.lua
Expand Up @@ -11,7 +11,7 @@
-- 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 - 2019, TBOOX Open Source Group.
--
-- @author ruki
Expand Down Expand Up @@ -39,15 +39,15 @@ os._setenv = os._setenv or os.setenv
os._getenvs = os._getenvs or os.getenvs
os._readlink = os._readlink or os.readlink

-- copy single file or directory
-- copy single file or directory
function os._cp(src, dst)

-- check
assert(src and dst)

-- is file?
if os.isfile(src) then

-- the destination is directory? append the filename
if os.isdir(dst) or path.islastsep(dst) then
dst = path.join(dst, path.filename(src))
Expand All @@ -59,7 +59,7 @@ function os._cp(src, dst)
end
-- is directory?
elseif os.isdir(src) then

-- the destination directory exists? append the filename
if os.isdir(dst) or path.islastsep(dst) then
dst = path.join(dst, path.filename(path.translate(src)))
Expand All @@ -74,20 +74,20 @@ function os._cp(src, dst)
else
return false, string.format("cannot copy file %s, error: not found this file", src)
end

-- ok
return true
end

-- move single file or directory
function os._mv(src, dst)

-- check
assert(src and dst)

-- exists file or directory?
if os.exists(src) then

-- the destination directory exists? append the filename
if os.isdir(dst) or path.islastsep(dst) then
dst = path.join(dst, path.filename(path.translate(src)))
Expand All @@ -101,14 +101,14 @@ function os._mv(src, dst)
else
return false, string.format("cannot move %s to %s, not found this file %s", src, dst, os.strerror())
end

-- ok
return true
end

-- remove single file or directory
-- remove single file or directory
function os._rm(filedir)

-- check
assert(filedir)

Expand Down Expand Up @@ -136,7 +136,7 @@ function os.argw(argv)
-- match all arguments
local results = {}
for _, arg in ipairs(table.wrap(argv)) do

-- exists wildcards?
if arg:find("([%+%-%^%$%*%[%]%%])") then
local pathes = os.match(arg, 'a')
Expand Down Expand Up @@ -185,7 +185,7 @@ end

-- match files or directories
--
-- @param pattern the search pattern
-- @param pattern the search pattern
-- uses "*" to match any part of a file or directory name,
-- uses "**" to recurse into subdirectories.
--
Expand All @@ -198,7 +198,7 @@ end
-- @code
-- local dirs, count = os.match("./src/*", true)
-- local files, count = os.match("./src/**.c")
-- local file = os.match("./src/test.c", 'f', function (filepath, isdir)
-- local file = os.match("./src/test.c", 'f', function (filepath, isdir)
-- return true -- continue it
-- return false -- break it
-- end)
Expand Down Expand Up @@ -235,7 +235,7 @@ function os.match(pattern, mode, callback)
assert(mode, "invalid match mode: %s", mode)
elseif mode then
mode = 1
else
else
mode = 0
end

Expand Down Expand Up @@ -314,7 +314,7 @@ end

-- copy files or directories
function os.cp(...)

-- check arguments
local args = {...}
if #args < 2 then
Expand All @@ -341,7 +341,7 @@ end

-- move files or directories
function os.mv(...)

-- check arguments
local args = {...}
if #args < 2 then
Expand All @@ -368,7 +368,7 @@ end

-- remove files or directories
function os.rm(...)

-- check arguments
local args = {...}
if #args < 1 then
Expand Down Expand Up @@ -433,14 +433,14 @@ function os.cd(dir)
else
return nil, string.format("cannot change directory %s, not found this directory %s", dir, os.strerror())
end

-- ok
return oldir
end

-- create directories
function os.mkdir(...)

-- check arguments
local args = {...}
if #args < 1 then
Expand All @@ -460,7 +460,7 @@ end

-- remove directories
function os.rmdir(...)

-- check arguments
local args = {...}
if #args < 1 then
Expand Down Expand Up @@ -534,14 +534,14 @@ function os.runv(program, argv, opt)
opt = opt or {}

-- make temporary log file
local log = os.tmpfile()
local logfile = os.tmpfile()

-- execute it
local ok = os.execv(program, argv, table.join(opt, {stdout = log, stderr = log}))
local ok = os.execv(program, argv, table.join(opt, {stdout = logfile, stderr = logfile}))
if ok ~= 0 then

-- make errors
local errors = io.readfile(log)
local errors = io.readfile(logfile)
if not errors or #errors == 0 then
if argv ~= nil then
errors = string.format("runv(%s %s) failed(%d)!", program, table.concat(argv, ' '), ok)
Expand All @@ -551,20 +551,20 @@ function os.runv(program, argv, opt)
end

-- remove the temporary log file
os.rm(log)
os.rm(logfile)

-- failed
return false, errors
end

-- remove the temporary log file
os.rm(log)
os.rm(logfile)

-- ok
return true
end

-- execute command
-- execute command
function os.exec(cmd, outfile, errfile)

-- parse arguments
Expand All @@ -581,8 +581,8 @@ end
--
-- @param program "clang", "xcrun -sdk macosx clang", "~/dir/test\ xxx/clang"
-- filename "clang", "xcrun"", "~/dir/test\ xxx/clang"
-- @param argv the arguments
-- @param opt the options, .e.g {wildcards = false, stdout = outfile, stderr = errfile,
-- @param argv the arguments
-- @param opt the options, .e.g {wildcards = false, stdout = outfile, stderr = errfile,
-- envs = {PATH = "xxx;xx", CFLAGS = "xx"}}
--
function os.execv(program, argv, opt)
Expand Down Expand Up @@ -631,7 +631,7 @@ function os.execv(program, argv, opt)

-- wait process
local waitok = -1
local status = -1
local status = -1
if coroutine.running() then

-- save the current directory
Expand Down Expand Up @@ -688,7 +688,7 @@ function os.iorunv(program, argv, opt)
local errfile = os.tmpfile()

-- run command
local ok = os.execv(program, argv, table.join(opt, {stdout = outfile, stderr = errfile}))
local ok = os.execv(program, argv, table.join(opt, {stdout = outfile, stderr = errfile}))

-- get output and error data
local outdata = io.readfile(outfile)
Expand Down Expand Up @@ -943,34 +943,51 @@ function os.getenvs()
return envs
end

-- set values to environment variable
-- set values to environment variable
function os.setenv(name, ...)
return os._setenv(name, table.concat({...}, path.envsep()))
local values = {...}
if #values <= 1 then
-- keep compatible with original implementation
return os._setenv(name, values[1] or "")
else
return os._setenv(name, path.joinenv(values))
end
end

-- add values to environment variable
-- add values to environment variable
function os.addenv(name, ...)
local sep = path.envsep()
local values = {...}
if #values > 0 then
return os._setenv(name, table.concat(values, sep) .. sep .. (os.getenv(name) or ""))
local oldenv = os.getenv(name)
local appendenv = path.joinenv(values)
if oldenv == "" or oldenv == nil then
return os._setenv(name, appendenv)
else
return os._setenv(name, appendenv .. path.envsep() .. oldenv)
end
else
return true
end
end

-- set values to environment variable with the given seperator
-- set values to environment variable with the given seperator
function os.setenvp(name, values, sep)
sep = sep or path.envsep()
return os._setenv(name, table.concat(table.wrap(values), sep))
end

-- add values to environment variable with the given seperator
-- add values to environment variable with the given seperator
function os.addenvp(name, values, sep)
sep = sep or path.envsep()
values = table.wrap(values)
if #values > 0 then
return os._setenv(name, table.concat(values, sep) .. sep .. (os.getenv(name) or ""))
local oldenv = os.getenv(name)
local appendenv = table.concat(values, sep)
if oldenv == "" or oldenv == nil then
return os._setenv(name, appendenv)
else
return os._setenv(name, appendenv .. sep .. oldenv)
end
else
return true
end
Expand Down
26 changes: 26 additions & 0 deletions xmake/core/base/path.lua
Expand Up @@ -158,6 +158,32 @@ function path.splitenv(env_path)
return result
end

-- concat environment variable with `path.envsep()`,
-- also handles more speical cases such as posix flags and windows quoted pathes
function path.joinenv(env_table)

-- check
if not env_table or #env_table == 0 then
return ""
end

local envsep = path.envsep()
if xmake._HOST == "windows" then
local tab = {}
for _, v in ipairs(env_table) do
if v ~= "" then
if v:find(envsep, 1, true) then
v = '"' .. v .. '"'
end
table.insert(tab, v)
end
end
return table.concat(tab, envsep)
else
return table.concat(env_table, envsep)
end
end

-- the last character is the path seperator?
function path.islastsep(p)

Expand Down
15 changes: 4 additions & 11 deletions xmake/core/base/table.lua
Expand Up @@ -258,17 +258,10 @@ function table.unique(array, barrier)
end

-- add unique item
if type(v) == "string" then
if not exists[v] then
exists[v] = true
table.insert(unique, v)
end
else
local key = "\"" .. tostring(v) .. "\""
if not exists[key] then
exists[key] = true
table.insert(unique, v)
end
if not exists[v] then
-- v will not be nil
exists[v] = true
table.insert(unique, v)
end
end

Expand Down
2 changes: 2 additions & 0 deletions xmake/core/sandbox/modules/io.lua
Expand Up @@ -53,6 +53,8 @@ if sandbox_io_file.__index ~= sandbox_io_file then
end
end
end
-- file:lines does not use its second return value for error
sandbox_io_file.lines = io.file.lines
end

-- get file size
Expand Down
4 changes: 2 additions & 2 deletions xmake/core/tool/builder.lua
Expand Up @@ -380,8 +380,8 @@ function builder:_add_flags_from_language(flags, target, getters)
{
config = function (name)
local values = config.get(name)
if values and name:endswith("dirs") then
values = values:split(path.envsep(), {plain = true})
if values and name:endswith("dirs") then
values = path.splitenv(values)
end
return values
end
Expand Down
4 changes: 2 additions & 2 deletions xmake/modules/package/tools/autoconf.lua
Expand Up @@ -106,8 +106,8 @@ function buildenvs(package)
table.insert(ACLOCAL_PATH, aclocal)
end
end
envs.ACLOCAL_PATH = table.concat(ACLOCAL_PATH, path.envsep())
envs.PKG_CONFIG_PATH = table.concat(PKG_CONFIG_PATH, path.envsep())
envs.ACLOCAL_PATH = path.joinenv(ACLOCAL_PATH)
envs.PKG_CONFIG_PATH = path.joinenv(PKG_CONFIG_PATH)
return envs
end

Expand Down
6 changes: 3 additions & 3 deletions xmake/modules/package/tools/cmake.lua
Expand Up @@ -71,9 +71,9 @@ function buildenvs(package)
table.join2(CMAKE_PREFIX_PATH, dep:installdir())
end
end
envs.CMAKE_LIBRARY_PATH = table.concat(CMAKE_LIBRARY_PATH, path.envsep())
envs.CMAKE_INCLUDE_PATH = table.concat(CMAKE_INCLUDE_PATH, path.envsep())
envs.CMAKE_PREFIX_PATH = table.concat(CMAKE_PREFIX_PATH, path.envsep())
envs.CMAKE_LIBRARY_PATH = path.joinenv(CMAKE_LIBRARY_PATH)
envs.CMAKE_INCLUDE_PATH = path.joinenv(CMAKE_INCLUDE_PATH)
envs.CMAKE_PREFIX_PATH = path.joinenv(CMAKE_PREFIX_PATH)
return envs
end

Expand Down

0 comments on commit 660b6f4

Please sign in to comment.