Skip to content

Commit dbaf525

Browse files
committed
implement cl6x
1 parent 99e74d6 commit dbaf525

File tree

2 files changed

+285
-0
lines changed

2 files changed

+285
-0
lines changed

xmake/modules/core/tools/cl6x.lua

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
--!A cross-platform build utility based on Lua
2+
--
3+
-- Licensed under the Apache License, Version 2.0 (the "License");
4+
-- you may not use this file except in compliance with the License.
5+
-- You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
-- Unless required by applicable law or agreed to in writing, software
10+
-- distributed under the License is distributed on an "AS IS" BASIS,
11+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
-- See the License for the specific language governing permissions and
13+
-- limitations under the License.
14+
--
15+
-- Copyright (C) 2015-present, TBOOX Open Source Group.
16+
--
17+
-- @author ruki
18+
-- @file cl6x.lua
19+
--
20+
21+
-- imports
22+
import("core.base.option")
23+
import("core.base.global")
24+
import("core.project.policy")
25+
import("core.language.language")
26+
import("utils.progress")
27+
28+
-- init it
29+
function init(self)
30+
end
31+
32+
-- make the symbol flag
33+
function nf_symbol(self, level)
34+
-- only for source kind
35+
local kind = self:kind()
36+
if language.sourcekinds()[kind] then
37+
local maps = _g.symbol_maps
38+
if not maps then
39+
maps =
40+
{
41+
debug = "-g"
42+
}
43+
_g.symbol_maps = maps
44+
end
45+
return maps[level .. '_' .. kind] or maps[level]
46+
end
47+
end
48+
49+
-- make the optimize flag
50+
function nf_optimize(self, level)
51+
local maps =
52+
{
53+
none = "-O0"
54+
, fast = "-O1"
55+
, faster = "-O2"
56+
, fastest = "-O3"
57+
, smallest = "-m3"
58+
, aggressive = "-O3"
59+
}
60+
return maps[level]
61+
end
62+
63+
-- make the define flag
64+
function nf_define(self, macro)
65+
return "-D" .. macro
66+
end
67+
68+
-- make the undefine flag
69+
function nf_undefine(self, macro)
70+
return "-U" .. macro
71+
end
72+
73+
-- make the includedir flag
74+
function nf_includedir(self, dir)
75+
return {"-I" .. dir}
76+
end
77+
78+
-- make the sysincludedir flag
79+
function nf_sysincludedir(self, dir)
80+
return nf_includedir(self, dir)
81+
end
82+
83+
-- make the link arguments list
84+
function linkargv(self, objectfiles, targetkind, targetfile, flags, opt)
85+
local argv = table.join("-z", "--output_file=" .. targetfile, objectfiles, flags)
86+
return self:program(), argv
87+
end
88+
89+
-- link the target file
90+
--
91+
-- maybe we need to use os.vrunv() to show link output when enable verbose information
92+
-- @see https://github.com/xmake-io/xmake/discussions/2916
93+
--
94+
function link(self, objectfiles, targetkind, targetfile, flags, opt)
95+
opt = opt or {}
96+
os.mkdir(path.directory(targetfile))
97+
local program, argv = linkargv(self, objectfiles, targetkind, targetfile, flags)
98+
if option.get("verbose") then
99+
os.execv(program, argv, {envs = self:runenvs(), shell = opt.shell})
100+
else
101+
os.vrunv(program, argv, {envs = self:runenvs(), shell = opt.shell})
102+
end
103+
end
104+
105+
-- make the compile arguments list
106+
function compargv(self, sourcefile, objectfile, flags)
107+
return self:program(), table.join("-c", "--preproc_with_compile", flags, "--output_file=" .. objectfile, sourcefile)
108+
end
109+
110+
-- compile the source file
111+
function compile(self, sourcefile, objectfile, dependinfo, flags, opt)
112+
os.mkdir(path.directory(objectfile))
113+
try
114+
{
115+
function ()
116+
117+
local compflags = flags
118+
local outdata, errdata = os.iorunv(compargv(self, sourcefile, objectfile, compflags))
119+
return (outdata or "") .. (errdata or "")
120+
end,
121+
catch
122+
{
123+
function (errors)
124+
125+
-- try removing the old object file for forcing to rebuild this source file
126+
os.tryrm(objectfile)
127+
128+
-- find the start line of error
129+
local lines = tostring(errors):split("\n")
130+
local start = 0
131+
for index, line in ipairs(lines) do
132+
if line:find("error:", 1, true) or line:find("错误:", 1, true) then
133+
start = index
134+
break
135+
end
136+
end
137+
138+
-- get 16 lines of errors
139+
if start > 0 or not option.get("verbose") then
140+
if start == 0 then start = 1 end
141+
errors = table.concat(table.slice(lines, start, start + ((#lines - start > 16) and 16 or (#lines - start))), "\n")
142+
end
143+
144+
-- raise compiling errors
145+
raise(errors)
146+
end
147+
},
148+
finally
149+
{
150+
function (ok, warnings)
151+
152+
-- print some warnings
153+
if warnings and #warnings > 0 and policy.build_warnings(opt) then
154+
if progress.showing_without_scroll() then
155+
print("")
156+
end
157+
cprint("${color.warning}%s", table.concat(table.slice(warnings:split('\n'), 1, 8), '\n'))
158+
end
159+
end
160+
}
161+
}
162+
end
163+
164+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
--!A cross-platform build utility based on Lua
2+
--
3+
-- Licensed under the Apache License, Version 2.0 (the "License");
4+
-- you may not use this file except in compliance with the License.
5+
-- You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
-- Unless required by applicable law or agreed to in writing, software
10+
-- distributed under the License is distributed on an "AS IS" BASIS,
11+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
-- See the License for the specific language governing permissions and
13+
-- limitations under the License.
14+
--
15+
-- Copyright (C) 2015-present, TBOOX Open Source Group.
16+
--
17+
-- @author ruki
18+
-- @file has_flags.lua
19+
--
20+
21+
-- imports
22+
import("core.cache.detectcache")
23+
import("core.language.language")
24+
25+
-- is linker?
26+
function _islinker(flags, opt)
27+
local toolkind = opt.toolkind or ""
28+
return toolkind == "ld" or toolkind == "sh" or toolkind:endswith("ld") or toolkind:endswith("sh")
29+
end
30+
31+
-- try running
32+
function _try_running(program, argv, opt)
33+
local errors = nil
34+
return try { function () os.runv(program, argv, opt); return true end, catch { function (errs) errors = (errs or ""):trim() end }}, errors
35+
end
36+
37+
-- attempt to check it from known flags
38+
function _check_from_knownargs(flags, opt, islinker)
39+
local flag = flags[1]
40+
if not islinker then
41+
if flag:startswith("-D") or
42+
flag:startswith("-U") or
43+
flag:startswith("-I") then
44+
return true
45+
end
46+
end
47+
end
48+
49+
-- attempt to check it from the argument list
50+
function _check_from_arglist(flags, opt, islinker)
51+
local key = "detect.tools.cl6x." .. (islinker and "has_ldflags" or "has_cflags")
52+
local flagskey = opt.program .. "_" .. (opt.programver or "")
53+
local allflags = detectcache:get2(key, flagskey)
54+
if not allflags then
55+
allflags = {}
56+
local arglist = try {function () return os.iorunv(opt.program, {"--help"}, {envs = opt.envs}) end}
57+
if arglist then
58+
for arg in arglist:gmatch("%s+(%-[%-%a%d]+)%s+") do
59+
allflags[arg] = true
60+
end
61+
end
62+
detectcache:set2(key, flagskey, allflags)
63+
detectcache:save()
64+
end
65+
local flag = flags[1]
66+
return allflags[flag]
67+
end
68+
69+
-- get extension
70+
function _get_extension(opt)
71+
-- @note we need to detect extension for ndk/clang++.exe: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]
72+
return (opt.program:endswith("++") or opt.flagkind == "cxxflags") and ".cpp" or (table.wrap(language.sourcekinds()[opt.toolkind or "cc"])[1] or ".c")
73+
end
74+
75+
-- try running to check flags
76+
function _check_try_running(flags, opt, islinker)
77+
78+
-- make an stub source file
79+
local snippet = opt.snippet or "int main(int argc, char** argv)\n{return 0;}\n"
80+
local sourcefile = os.tmpfile("cl6x_has_flags:" .. snippet) .. _get_extension(opt)
81+
if not os.isfile(sourcefile) then
82+
io.writefile(sourcefile, snippet)
83+
end
84+
85+
-- check flags for linker
86+
local tmpfile = os.tmpfile()
87+
if islinker then
88+
return _try_running(opt.program, table.join(flags, "-z", "--output_file=" .. tmpfile, sourcefile), opt)
89+
end
90+
91+
-- check flags for compiler
92+
-- @note we cannot use os.nuldev() as the output file, maybe run failed for some flags, e.g. --coverage
93+
return _try_running(opt.program, table.join(flags, "-c", "--output_file=" .. tmpfile, sourcefile), opt)
94+
end
95+
96+
-- has_flags(flags)?
97+
--
98+
-- @param opt the argument options, e.g. {toolname = "", program = "", programver = "", toolkind = "[cc|cxx|ld|ar|sh|gc|mm|mxx]"}
99+
--
100+
-- @return true or false
101+
--
102+
function main(flags, opt)
103+
104+
-- is linker?
105+
opt = opt or {}
106+
local islinker = _islinker(flags, opt)
107+
108+
-- attempt to check it from the argument list
109+
if not opt.tryrun then
110+
if _check_from_arglist(flags, opt, islinker) then
111+
return true
112+
end
113+
if _check_from_knownargs(flags, opt, islinker) then
114+
return true
115+
end
116+
end
117+
118+
-- try running to check it
119+
return _check_try_running(flags, opt, islinker)
120+
end
121+

0 commit comments

Comments
 (0)