Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Feb 26, 2024
1 parent 50ea48f commit bca8ae3
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 264 deletions.
204 changes: 203 additions & 1 deletion scripts/workspace.lua
Expand Up @@ -2,6 +2,205 @@ local arguments = require "arguments"
local pathutil = require "pathutil"
local globals = require "globals"

local AttributePlatform <const> = 0
local AttributePaths <const> = 1
local AttributeArgs <const> = 2
local AttributeStrings <const> = 3
local AttributeGlobs <const> = 4
local AttributePath <const> = 5

local ATTRIBUTE <const> = {
-- os
windows = AttributePlatform,
linux = AttributePlatform,
macos = AttributePlatform,
ios = AttributePlatform,
android = AttributePlatform,
freebsd = AttributePlatform,
openbsd = AttributePlatform,
netbsd = AttributePlatform,
-- cc
msvc = AttributePlatform,
gcc = AttributePlatform,
clang = AttributePlatform,
clang_cl = AttributePlatform,
mingw = AttributePlatform,
emcc = AttributePlatform,
-- paths
includes = AttributePaths,
sysincludes = AttributePaths,
linkdirs = AttributePaths,
outputs = AttributePaths,
-- path
script = AttributePath,
-- strings
objdeps = AttributeStrings,
defines = AttributeStrings,
flags = AttributeStrings,
ldflags = AttributeStrings,
links = AttributeStrings,
frameworks = AttributeStrings,
deps = AttributeStrings,
-- globs
inputs = AttributeGlobs,
sources = AttributeGlobs,
-- args
args = AttributeArgs,
}

local LINK_ATTRIBUTE <const> = {
ldflags = true,
links = true,
linkdirs = true,
frameworks = true,
}

local function push_globs(t, v)
local vt = type(v)
if vt == "string" then
t[#t+1] = v
elseif vt == "userdata" then
t[#t+1] = v
elseif vt == "table" then
if getmetatable(v) ~= nil then
t[#t+1] = v
else
for i = 1, #v do
push_globs(t, v[i])
end
end
end
end

local function push_strings(t, v)
local vt = type(v)
if vt == "string" then
t[#t+1] = v
elseif vt == "table" then
for i = 1, #v do
push_strings(t, v[i])
end
end
end

local function push_path(v, root)
local vt = type(v)
if vt == "string" then
return pathutil.tostring(root, v)
elseif vt == "userdata" then
return pathutil.tostring(root, v)
elseif vt == "table" then
if getmetatable(v) ~= nil then
return pathutil.tostring(root, v)
end
end
end

local function push_paths(t, v, root)
local vt = type(v)
if vt == "string" then
t[#t+1] = pathutil.tostring(root, v)
elseif vt == "userdata" then
t[#t+1] = pathutil.tostring(root, v)
elseif vt == "table" then
if getmetatable(v) ~= nil then
t[#t+1] = pathutil.tostring(root, v)
else
for i = 1, #v do
push_paths(t, v[i], root)
end
end
end
end

local function push_mix(t, v, root)
local vt = type(v)
if vt == "string" then
if v:sub(1, 1) == "@" then
t[#t+1] = pathutil.tostring(root, v:sub(2))
else
t[#t+1] = v:gsub("@{([^}]*)}", function (s)
return pathutil.tostring(root, s)
end)
end
elseif vt == "userdata" then
t[#t+1] = pathutil.tostring(root, v)
elseif vt == "table" then
if getmetatable(v) ~= nil then
t[#t+1] = pathutil.tostring(root, v)
else
for i = 1, #v do
push_mix(t, v[i], root)
end
end
end
end

local function push_args(t, v, root)
for i = 1, #v do
push_mix(t, v[i], root)
end
end

local function push_table(t, a, root, NOLINK)
for k, v in pairs(a) do
if type(k) ~= "string" then
goto continue
end
if NOLINK and LINK_ATTRIBUTE[k] then
goto continue
end
if ATTRIBUTE[k] == AttributePlatform then
elseif ATTRIBUTE[k] == AttributePaths then
t[k] = t[k] or {}
push_paths(t[k], v, root)
if #t[k] == 0 then
t[k] = nil
end
elseif ATTRIBUTE[k] == AttributePath then
t[k] = push_path(v, root)
elseif ATTRIBUTE[k] == AttributeArgs then
t[k] = t[k] or {}
push_args(t[k], v, root)
if #t[k] == 0 then
t[k] = nil
end
elseif ATTRIBUTE[k] == AttributeStrings then
t[k] = t[k] or {}
push_strings(t[k], v)
if #t[k] == 0 then
t[k] = nil
end
elseif ATTRIBUTE[k] == AttributeGlobs then
t[k] = t[k] or {}
push_globs(t[k], v)
if #t[k] == 0 then
t[k] = nil
end
else
t[k] = v
end
::continue::
end
return t
end

local function push_attributes(t, a, root, NOLINK)
push_table(t, a, root, NOLINK)
if a[globals.os] then
push_table(t, a[globals.os], root, NOLINK)
end
if a[globals.compiler] then
push_table(t, a[globals.compiler], root, NOLINK)
end
if a.mingw and globals.os == "windows" and globals.hostshell == "sh" then
push_table(t, a.mingw, root, NOLINK)
end
if a.clang_cl and globals.cc == "clang-cl" then
push_table(t, a.clang_cl, root, NOLINK)
end
end

local function create(workdir, parent, attri)
local mt = {}
function mt:__index(k)
Expand Down Expand Up @@ -51,5 +250,8 @@ local function create(workdir, parent, attri)
end

return {
create = create
create = create,
push_attributes = push_attributes,
push_strings = push_strings,
push_args = push_args,
}

0 comments on commit bca8ae3

Please sign in to comment.