summaryrefslogtreecommitdiff
path: root/xmake/core
diff options
context:
space:
mode:
authorruki <[email protected]>2017-06-21 18:53:20 +0800
committerruki <[email protected]>2017-06-21 18:53:20 +0800
commite7fd0a5c178956eb4801de3780f2bc3c0e7a3e1e (patch)
treec1691a33309a18cf9b5c54dda592f33a8a7abd66 /xmake/core
parent45a54659f7449ead7ed073f9c0cbbe1da9fcdc06 (diff)
move tools dir to core.tools and improve tool module
Diffstat (limited to 'xmake/core')
-rw-r--r--xmake/core/language/language.lua4
-rw-r--r--xmake/core/sandbox/modules/import/core/tool/compiler.lua2
-rw-r--r--xmake/core/tool/builder.lua69
-rw-r--r--xmake/core/tool/compiler.lua10
-rw-r--r--xmake/core/tool/extractor.lua10
-rw-r--r--xmake/core/tool/linker.lua10
-rw-r--r--xmake/core/tool/tool.lua167
7 files changed, 101 insertions, 171 deletions
diff --git a/xmake/core/language/language.lua b/xmake/core/language/language.lua
index c860b1a2a..e76d10b10 100644
--- a/xmake/core/language/language.lua
+++ b/xmake/core/language/language.lua
@@ -82,15 +82,11 @@ end
-- get the language menu
function _instance:menu()
-
- -- get it
return self._INFO.menu
end
-- get the language name
function _instance:name()
-
- -- get it
return self._NAME
end
diff --git a/xmake/core/sandbox/modules/import/core/tool/compiler.lua b/xmake/core/sandbox/modules/import/core/tool/compiler.lua
index fb753aed3..8990f7935 100644
--- a/xmake/core/sandbox/modules/import/core/tool/compiler.lua
+++ b/xmake/core/sandbox/modules/import/core/tool/compiler.lua
@@ -58,7 +58,7 @@ function sandbox_core_tool_compiler.compcmd(sourcefiles, objectfile, target, sou
if not instance then
raise(errors)
end
-
+
-- make command
return instance:compcmd(sourcefiles, objectfile, target)
end
diff --git a/xmake/core/tool/builder.lua b/xmake/core/tool/builder.lua
index 43bed8bfa..d62eb4d1b 100644
--- a/xmake/core/tool/builder.lua
+++ b/xmake/core/tool/builder.lua
@@ -76,8 +76,8 @@ function builder:_mapflag(flag, mapflags)
end
end
- -- check it
- if self:check(flag) then
+ -- has this flag?
+ if self:has_flags(flag) then
return flag
end
end
@@ -103,9 +103,9 @@ function builder:_mapflags(flags)
else
- -- check flags
+ -- has flags?
for _, flag in pairs(flags) do
- if self:check(flag) then
+ if self:has_flags(flag) then
table.insert(results, flag)
end
end
@@ -261,8 +261,8 @@ function builder:_addflags_from_language(flags, target)
for _, flagvalue in ipairs(table.wrap(getter(flagname))) do
-- map and check flag
- local flag = mapper(flagvalue, target, self:_targetkind())
- if flag and flag ~= "" and (not checkstate or self:check(flag)) then
+ local flag = mapper(self:_tool(), flagvalue, target, self:_targetkind())
+ if flag and flag ~= "" and (not checkstate or self:has_flags(flag)) then
table.join2(flags, flag)
end
end
@@ -270,11 +270,29 @@ function builder:_addflags_from_language(flags, target)
end
end
+-- get tool name
+function builder:name()
+ return self:_tool():name()
+end
+
+-- get tool kind
+function builder:kind()
+ return self:_tool():kind()
+end
+
+-- get tool program
+function builder:program()
+ return self:_tool():program()
+end
+
-- get properties of the tool
function builder:get(name)
+ return self:_tool():get(name)
+end
- -- get it
- return self:_tool().get(name)
+-- has flags?
+function builder:has_flags(flags)
+ return self:_tool():has_flags(name)
end
-- get the format of the given target kind
@@ -297,40 +315,5 @@ function builder:feature(name)
end
end
--- check the given flags
-function builder:check(flags)
-
- -- the builder tool
- local ctool = self:_tool()
-
- -- no check?
- if not ctool.check then
- return true
- end
-
- -- have been checked? return it directly
- self._CHECKED = self._CHECKED or {}
- if self._CHECKED[flags] ~= nil then
- return self._CHECKED[flags]
- end
-
- -- check it
- local ok, errors = sandbox.load(ctool.check, flags)
-
- -- trace
- if option.get("verbose") then
- utils.cprint("checking for the flags %s ... %s", flags, utils.ifelse(ok, "${green}ok", "${red}no"))
- if not ok then
- utils.cprint("${red}" .. errors or "")
- end
- end
-
- -- save the checked result
- self._CHECKED[flags] = ok
-
- -- ok?
- return ok
-end
-
-- return module
return builder
diff --git a/xmake/core/tool/compiler.lua b/xmake/core/tool/compiler.lua
index a550564da..d6a1c74ed 100644
--- a/xmake/core/tool/compiler.lua
+++ b/xmake/core/tool/compiler.lua
@@ -50,7 +50,7 @@ end
function compiler:_addflags_from_platform(flags, targetkind)
-- add flags
- local toolkind = self:get("kind")
+ local toolkind = self:kind()
for _, flagkind in ipairs(self:_flagkinds()) do
-- add flags for platform
@@ -128,28 +128,28 @@ end
function compiler:build(sourcefiles, targetkind, targetfile, target)
-- get it
- return sandbox.load(self:_tool().build, sourcefiles, targetkind, targetfile, (self:compflags(target)) .. " " .. (target:linkflags()))
+ return sandbox.load(self:_tool().build, self:_tool(), sourcefiles, targetkind, targetfile, (self:compflags(target)) .. " " .. (target:linkflags()))
end
-- get the build command
function compiler:buildcmd(sourcefiles, targetkind, targetfile, target)
-- get it
- return self:_tool().buildcmd(sourcefiles, targetkind, targetfile, (self:compflags(target) .. " " .. (target:linkflags())))
+ return self:_tool():buildcmd(sourcefiles, targetkind, targetfile, (self:compflags(target) .. " " .. (target:linkflags())))
end
-- compile the source files
function compiler:compile(sourcefiles, objectfile, incdepfile, target)
-- compile it
- return sandbox.load(self:_tool().compile, sourcefiles, objectfile, incdepfile, (self:compflags(target)))
+ return sandbox.load(self:_tool().compile, self:_tool(), sourcefiles, objectfile, incdepfile, (self:compflags(target)))
end
-- get the compile command
function compiler:compcmd(sourcefiles, objectfile, target)
-- get it
- return self:_tool().compcmd(sourcefiles, objectfile, (self:compflags(target)))
+ return self:_tool():compcmd(sourcefiles, objectfile, (self:compflags(target)))
end
-- get the compling flags
diff --git a/xmake/core/tool/extractor.lua b/xmake/core/tool/extractor.lua
index 1149e31aa..480005c68 100644
--- a/xmake/core/tool/extractor.lua
+++ b/xmake/core/tool/extractor.lua
@@ -38,8 +38,6 @@ local tool = require("tool/tool")
-- get the current tool
function extractor:_tool()
-
- -- get it
return self._TOOL
end
@@ -72,16 +70,12 @@ end
-- get properties of the tool
function extractor:get(name)
-
- -- get it
- return self:_tool().get(name)
+ return self:_tool():get(name)
end
-- extract the library file
function extractor:extract(libraryfile, objectdir)
-
- -- extract it
- return sandbox.load(self:_tool().extract, libraryfile, objectdir)
+ return sandbox.load(self:_tool().extract, self:_tool(), libraryfile, objectdir)
end
-- return module
diff --git a/xmake/core/tool/linker.lua b/xmake/core/tool/linker.lua
index 0ddf3165b..7427dbfac 100644
--- a/xmake/core/tool/linker.lua
+++ b/xmake/core/tool/linker.lua
@@ -44,7 +44,7 @@ local compiler = require("tool/compiler")
function linker:_addflags_from_platform(flags, targetkind)
-- add flags
- local toolkind = self:get("kind")
+ local toolkind = self:kind()
for _, flagkind in ipairs(self:_flagkinds()) do
-- attempt to add special lanugage flags first, .e.g gc-ldflags, dc-arflags
@@ -61,7 +61,7 @@ function linker:_addflags_from_compiler(flags, targetkind, sourcekinds)
-- make flags
local flags_of_compiler = {}
- local toolkind = self:get("kind")
+ local toolkind = self:kind()
for _, sourcekind in ipairs(table.wrap(sourcekinds)) do
-- load compiler
@@ -87,7 +87,7 @@ end
function linker:_addflags_from_linker(flags)
-- add flags
- local toolkind = self:get("kind")
+ local toolkind = self:kind()
for _, flagkind in ipairs(self:_flagkinds()) do
-- attempt to add special lanugage flags first, .e.g gc-ldflags, dc-arflags
@@ -176,14 +176,14 @@ end
function linker:link(objectfiles, targetfile, target)
-- link it
- return sandbox.load(self:_tool().link, table.concat(table.wrap(objectfiles), " "), self:_targetkind(), targetfile, (self:linkflags(target)))
+ return sandbox.load(self:_tool().link, self:_tool(), table.concat(table.wrap(objectfiles), " "), self:_targetkind(), targetfile, (self:linkflags(target)))
end
-- get the link command
function linker:linkcmd(objectfiles, targetfile, target)
-- get it
- return self:_tool().linkcmd(table.concat(table.wrap(objectfiles), " "), self:_targetkind(), targetfile, (self:linkflags(target)))
+ return self:_tool():linkcmd(table.concat(table.wrap(objectfiles), " "), self:_targetkind(), targetfile, (self:linkflags(target)))
end
-- get the link flags
diff --git a/xmake/core/tool/tool.lua b/xmake/core/tool/tool.lua
index 5185d7b2f..3be75f4d4 100644
--- a/xmake/core/tool/tool.lua
+++ b/xmake/core/tool/tool.lua
@@ -13,7 +13,7 @@
-- 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
+-- See the License for the specific tool governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015 - 2017, TBOOX Open Source Group.
@@ -24,6 +24,7 @@
-- define module
local tool = tool or {}
+local _instance = _instance or {}
-- load modules
local os = require("base/os")
@@ -35,91 +36,63 @@ local sandbox = require("sandbox/sandbox")
local platform = require("platform/platform")
local import = require("sandbox/modules/import")
--- load the given tool
-function tool._load(kind, name, program)
+-- new an instance
+function _instance.new(kind, name, program)
- -- calculate the cache key
- local key = (kind or "") .. program
-
- -- get it directly from cache dirst
- tool._TOOLS = tool._TOOLS or {}
- if tool._TOOLS[key] then
- return tool._TOOLS[key]
+ -- import "core.tools.xxx"
+ local toolclass = nil
+ if os.isfile(path.join(os.programdir(), "modules", "core", "tools", name .. ".lua")) then
+ toolclass = import("core.tools." .. name)
end
- -- not exists?
- local toolpath = path.join(os.programdir(), "tools", name .. ".lua")
- if not os.isfile(toolpath) then
- return nil, string.format("%s not found!", name)
+ -- not found?
+ if not toolclass then
+ return nil, string.format("cannot import \"core.tool.%s\" module!", name)
end
- -- load script
- local script, errors = loadfile(toolpath)
- if script then
+ -- new an instance
+ local instance = table.inherit(_instance, toolclass)
- -- make sandbox instance with the given script
- local instance, errors = sandbox.new(script, nil, path.directory(toolpath))
- if not instance then
- return nil, errors
- end
+ -- save name, kind and program
+ instance._NAME = name
+ instance._KIND = kind
+ instance._PROGRAM = program
- -- import the tool module
- local module, errors = instance:import()
- if not module then
+ -- init instance
+ if instance.init then
+ local ok, errors = sandbox.load(instance.init, instance)
+ if not ok then
return nil, errors
end
-
- -- init the tool module
- if module.init then
- module.init(program, kind)
- end
-
- -- save tool to the cache
- tool._TOOLS[key] = module
-
- -- ok?
- return module
end
- -- failed
- return nil, errors
+ -- ok
+ return instance
end
--- check the program
-function tool._check(program, check)
+-- get the tool name
+function _instance:name()
+ return self._NAME
+end
- -- uses the passed checker
- if check ~= nil then
+-- get the tool kind
+function _instance:kind()
+ return self._KIND
+end
- -- check it
- local ok, errors = sandbox.load(check, program)
- if not ok then
- utils.verror(errors)
- end
+-- get the tool program
+function _instance:program()
+ return self._PROGRAM
+end
- -- ok?
- return ok
- end
-
- -- load the tool module
- local module, errors = tool._load(program)
- if not module then
- utils.verror(errors)
- end
+-- has the given flag?
+function _instance:has_flags(flag)
- -- no checker? attempt to run it directly
- if not module or not module.check then
- return 0 == os.exec(program, os.nuldev(), os.nuldev())
- end
+ -- import has_flags()
+ self._has_flags = self._has_flags or import("lib.detect.has_flags")
- -- check it
- local ok, errors = sandbox.load(module.check)
- if not ok then
- utils.verror(errors)
- end
-
- -- ok?
- return ok
+ -- has flags?
+ return self._has_flags(self:name(), flag, {program = self:program()})
end
-- load the given tool from the given kind
@@ -130,6 +103,12 @@ end
--
function tool.load(kind)
+ -- get it directly from cache dirst
+ tool._TOOLS = tool._TOOLS or {}
+ if tool._TOOLS[kind] then
+ return tool._TOOLS[kind]
+ end
+
-- get the tool program
local program = platform.tool(kind)
if not program then
@@ -140,50 +119,28 @@ function tool.load(kind)
local find_toolname = import("lib.detect.find_toolname")
-- get the tool name from the program
- local name = find_toolname(program)
- if not name then
- return nil, string.format("cannot find tool name for %s", program)
+ local ok, name_or_errors = sandbox.load(find_toolname, program)
+ if not ok then
+ return nil, name_or_errors
end
- -- load it
- return tool._load(kind, name, program)
-end
-
--- check the tool and return the absolute path if exists
-function tool.check(program, dirs, check)
-
- -- check
- assert(program)
-
- -- attempt to get result from cache first
- tool._CHECKINFO = tool._CHECKINFO or {}
- local result = tool._CHECKINFO[program]
- if result then
- return result
+ -- get name
+ local name = name_or_errors
+ if not name then
+ return nil, string.format("cannot find tool name for %s", program)
end
- -- attempt to check it directly
- if tool._check(program, check) then
- tool._CHECKINFO[program] = program
- return program
+ -- new an instance
+ local instance, errors = _instance.new(kind, name, program)
+ if not instance then
+ return nil, errors
end
- -- attempt to check it from the given directories
- if not path.is_absolute(program) then
- for _, dir in ipairs(table.wrap(dirs)) do
+ -- save instance to the cache
+ tool._TOOLS[kind] = instance
- -- the tool path
- local toolpath = path.join(dir, program)
- if os.isexec(toolpath) then
-
- -- check it
- if tool._check(toolpath, check) then
- tool._CHECKINFO[program] = toolpath
- return toolpath
- end
- end
- end
- end
+ -- ok
+ return instance
end
-- return module