1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
--!The Make-like Build Utility based on Lua
--
-- XMake is free software; you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
--
-- XMake is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public License
-- along with XMake;
-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
--
-- Copyright (C) 2015 - 2016, ruki All rights reserved.
--
-- @author ruki
-- @file tool.lua
--
-- define module
local tool = tool or {}
-- load modules
local os = require("base/os")
local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
local string = require("base/string")
local filter = require("base/filter")
local config = require("project/config")
local global = require("project/global")
local sandbox = require("sandbox/sandbox")
local platform = require("platform/platform")
-- the directories of tools
function tool._directories(name)
-- the directories
return { path.join(config.directory(), "tools")
, path.join(global.directory(), "tools")
, path.join(xmake._PROGRAM_DIR, "tools")
}
end
-- match the tool name
function tool._match(name, toolname)
-- match full? ok
if name == toolname then return 100 end
-- match the last word? ok
if name:find(toolname .. "$") then return 80 end
-- contains it? ok
if name:find(toolname, 1, true) then return 30 end
-- not matched
return 0
end
-- find tool from the given root directory and name
function tool._find(root, name)
-- attempt to get it directly first
local filepath = string.format("%s/%s.lua", root, name)
if os.isfile(filepath) then
return filepath
end
-- make the lower name
name = name:lower()
-- remove arguments: -xxx or --xxx
name = (name:gsub("%s%-+%w+", " "))
-- get the last name by ' ': xxx xxx toolname
local names = name:split("%s")
if #names > 0 then
name = names[#names]
end
-- get the last name by '-': xxx-xxx-toolname
local names = name:split("%-")
if #names > 0 then
name = names[#names]
end
-- remove suffix: ".xxx"
name = (name:gsub("%.%w+", ""))
-- get all tool files
local file_ok = nil
local score_maxn = 0
local files = os.match(string.format("%s/*.lua", root))
for _, file in ipairs(files) do
-- the tool name
local toolname = path.basename(file)
-- found it?
if toolname and toolname ~= "tool" then
-- match score
local score = tool._match(name, toolname:lower())
-- ok?
if score >= 100 then return file end
-- select the file with the max score
if score > score_maxn then
file_ok = file
score_maxn = score
end
end
end
-- ok?
return file_ok
end
-- load the given tool from the given shell name
function tool._load(shellname, kind)
-- calculate the cache key
local key = shellname .. (kind or "")
-- get it directly from cache dirst
tool._TOOLS = tool._TOOLS or {}
if tool._TOOLS[key] then
return tool._TOOLS[key]
end
-- find the tool script path
local toolpath = nil
local toolname = path.filename(shellname)
for _, dir in ipairs(tool._directories()) do
-- find this directory
toolpath = tool._find(dir, toolname)
if toolpath then
break
end
end
-- not exists?
if not toolpath or not os.isfile(toolpath) then
return nil, string.format("%s not found!", shellname)
end
-- load script
local script, errors = loadfile(toolpath)
if script then
-- 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
-- import the tool module
local module, errors = instance:import()
if not module then
return nil, errors
end
-- init the tool module
if module.init then
module.init(shellname, kind)
end
-- save tool to the cache
tool._TOOLS[key] = module
-- ok?
return module
end
-- failed
return nil, errors
end
-- check the shellname
function tool._check(shellname, check)
-- uses the passed checker
if check ~= nil then
-- check it
local ok, errors = sandbox.load(check, shellname)
if not ok then
utils.verror(errors)
end
-- ok?
return ok
end
-- load the tool module
local module, errors = tool._load(shellname)
if not module then
utils.verror(errors)
end
-- no checker? attempt to run it directly
if not module or not module.check then
return 0 == os.exec(shellname, xmake._NULDEV, xmake._NULDEV)
end
-- check it
local ok, errors = sandbox.load(module.check)
if not ok then
utils.verror(errors)
end
-- ok?
return ok
end
-- load the given tool from the given kind
--
-- the kinds:
--
-- .e.g cc, cxx, mm, mxx, as, ar, ld, sh, ..
--
function tool.load(kind)
-- get the shell name
local shellname = platform.tool(kind)
if not shellname then
return nil, string.format("cannot get tool for %s", kind)
end
-- load it
return tool._load(shellname, kind)
end
-- check the tool and return the absolute path if exists
function tool.check(shellname, dirs, check)
-- check
assert(shellname)
-- attempt to check it directly first
if tool._check(shellname, check) then
return shellname
end
-- attempt to check it from the given directories
if not path.is_absolute(shellname) then
for _, dir in ipairs(table.wrap(dirs)) do
-- the tool path
local toolpath = path.join(dir, shellname)
if os.isexec(toolpath) then
-- check it
if tool._check(toolpath, check) then
return toolpath
end
end
end
end
end
-- return module
return tool
|