summaryrefslogtreecommitdiff
path: root/xmake/modules
diff options
context:
space:
mode:
authorruki <[email protected]>2024-02-06 11:04:20 +0800
committerGitHub <[email protected]>2024-02-06 11:04:20 +0800
commitc2d5a4a059d88134ef426cb58499994ef02508b2 (patch)
tree7bb3a5531812a41ac48895a076e1d14e65ea7ea1 /xmake/modules
parent8c92537300242e1705e522fe0e6a8087fe1f685c (diff)
parentb74097ad5fa27dd871ef4cdc6e342342eaca6192 (diff)
Merge pull request #4704 from xmake-io/cosmocc
Add cosmocc toolchain support
Diffstat (limited to 'xmake/modules')
-rw-r--r--xmake/modules/core/tools/ar.lua5
-rw-r--r--xmake/modules/core/tools/clangxx.lua2
-rw-r--r--xmake/modules/core/tools/cosmoar.lua38
-rw-r--r--xmake/modules/core/tools/cosmocc.lua59
-rw-r--r--xmake/modules/core/tools/cosmocxx.lua24
-rw-r--r--xmake/modules/core/tools/gcc.lua13
-rw-r--r--xmake/modules/detect/tools/cosmocc/has_flags.lua143
-rw-r--r--xmake/modules/detect/tools/cosmocxx/has_flags.lua23
-rw-r--r--xmake/modules/detect/tools/find_cosmoar.lua44
-rw-r--r--xmake/modules/detect/tools/find_cosmocc.lua49
-rw-r--r--xmake/modules/detect/tools/find_cosmocxx.lua49
11 files changed, 440 insertions, 9 deletions
diff --git a/xmake/modules/core/tools/ar.lua b/xmake/modules/core/tools/ar.lua
index 0496238b4..b302504d8 100644
--- a/xmake/modules/core/tools/ar.lua
+++ b/xmake/modules/core/tools/ar.lua
@@ -49,7 +49,8 @@ function linkargv(self, objectfiles, targetkind, targetfile, flags, opt)
end
-- link the library file
-function link(self, objectfiles, targetkind, targetfile, flags)
+function link(self, objectfiles, targetkind, targetfile, flags, opt)
+ opt = opt or {}
os.mkdir(path.directory(targetfile))
-- @note remove the previous archived file first to force recreating a new file
@@ -57,7 +58,7 @@ function link(self, objectfiles, targetkind, targetfile, flags)
-- link it
local program, argv = linkargv(self, objectfiles, targetkind, targetfile, flags)
- os.runv(program, argv, {envs = self:runenvs()})
+ os.runv(program, argv, {envs = self:runenvs(), shell = opt.shell})
end
diff --git a/xmake/modules/core/tools/clangxx.lua b/xmake/modules/core/tools/clangxx.lua
index d167dcd17..3a35fc562 100644
--- a/xmake/modules/core/tools/clangxx.lua
+++ b/xmake/modules/core/tools/clangxx.lua
@@ -15,7 +15,7 @@
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
--- @file clang++.lua
+-- @file clangxx.lua
--
-- inherit clang
diff --git a/xmake/modules/core/tools/cosmoar.lua b/xmake/modules/core/tools/cosmoar.lua
new file mode 100644
index 000000000..2ee1741ae
--- /dev/null
+++ b/xmake/modules/core/tools/cosmoar.lua
@@ -0,0 +1,38 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- 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
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file cosmoar.lua
+--
+
+inherit("ar")
+
+function init(self)
+ _super.init(self)
+end
+
+function link(self, objectfiles, targetkind, targetfile, flags, opt)
+ opt = opt or {}
+ if is_host("windows") then
+ targetfile = targetfile:gsub("\\", "/")
+ local objectfiles_new = {}
+ for idx, objectfile in ipairs(objectfiles) do
+ objectfiles_new[idx] = objectfiles[idx]:gsub("\\", "/")
+ end
+ objectfiles = objectfiles_new
+ end
+ return _super.link(self, objectfiles, targetkind, targetfile, flags, table.join(opt, {shell = true}))
+end
diff --git a/xmake/modules/core/tools/cosmocc.lua b/xmake/modules/core/tools/cosmocc.lua
new file mode 100644
index 000000000..c53f6e1c5
--- /dev/null
+++ b/xmake/modules/core/tools/cosmocc.lua
@@ -0,0 +1,59 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- 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
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file cosmocc.lua
+--
+
+-- inherit gcc
+inherit("gcc")
+
+-- init it
+function init(self)
+ _super.init(self)
+end
+
+-- make the strip flag
+function nf_strip(self, level)
+end
+
+-- link the target file
+function link(self, objectfiles, targetkind, targetfile, flags, opt)
+ opt = opt or {}
+ if is_host("windows") then
+ targetfile = targetfile:gsub("\\", "/")
+ local objectfiles_new = {}
+ for idx, objectfile in ipairs(objectfiles) do
+ objectfiles_new[idx] = objectfiles[idx]:gsub("\\", "/")
+ end
+ objectfiles = objectfiles_new
+ end
+ return _super.link(self, objectfiles, targetkind, targetfile, flags, table.join(opt, {shell = true}))
+end
+
+-- compile the source file
+function compile(self, sourcefile, objectfile, dependinfo, flags, opt)
+ opt = opt or {}
+ if is_host("windows") then
+ sourcefile = sourcefile:gsub("\\", "/")
+ objectfile = objectfile:gsub("\\", "/")
+ local target = opt.target
+ if target then
+ target:set("policy", "build.ccache", false)
+ end
+ end
+ return _super.compile(self, sourcefile, objectfile, dependinfo, flags, table.join(opt, {shell = true}))
+end
diff --git a/xmake/modules/core/tools/cosmocxx.lua b/xmake/modules/core/tools/cosmocxx.lua
new file mode 100644
index 000000000..4f64ad261
--- /dev/null
+++ b/xmake/modules/core/tools/cosmocxx.lua
@@ -0,0 +1,24 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- 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
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file cosmocxx.lua
+--
+
+-- inherit cosmocc
+inherit("cosmocc")
+
+
diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua
index e519b376a..e4e658749 100644
--- a/xmake/modules/core/tools/gcc.lua
+++ b/xmake/modules/core/tools/gcc.lua
@@ -519,13 +519,14 @@ end
-- maybe we need to use os.vrunv() to show link output when enable verbose information
-- @see https://github.com/xmake-io/xmake/discussions/2916
--
-function link(self, objectfiles, targetkind, targetfile, flags)
+function link(self, objectfiles, targetkind, targetfile, flags, opt)
+ opt = opt or {}
os.mkdir(path.directory(targetfile))
local program, argv = linkargv(self, objectfiles, targetkind, targetfile, flags)
if option.get("verbose") then
- os.execv(program, argv, {envs = self:runenvs()})
+ os.execv(program, argv, {envs = self:runenvs(), shell = opt.shell})
else
- os.vrunv(program, argv, {envs = self:runenvs()})
+ os.vrunv(program, argv, {envs = self:runenvs(), shell = opt.shell})
end
end
@@ -705,17 +706,17 @@ function _compile(self, sourcefile, objectfile, compflags, opt)
opt = opt or {}
local program, argv = compargv(self, sourcefile, objectfile, compflags)
local function _compile_fallback()
- return os.iorunv(program, argv, {envs = self:runenvs()})
+ return os.iorunv(program, argv, {envs = self:runenvs(), shell = opt.shell})
end
local cppinfo
if distcc_build_client.is_distccjob() and distcc_build_client.singleton():has_freejobs() then
cppinfo = distcc_build_client.singleton():compile(program, argv, {envs = self:runenvs(),
preprocess = _preprocess, compile = _compile_preprocessed_file, compile_fallback = _compile_fallback,
- tool = self, remote = true})
+ tool = self, remote = true, shell = opt.shell})
elseif build_cache.is_enabled(opt.target) and build_cache.is_supported(self:kind()) then
cppinfo = build_cache.build(program, argv, {envs = self:runenvs(),
preprocess = _preprocess, compile = _compile_preprocessed_file, compile_fallback = _compile_fallback,
- tool = self})
+ tool = self, shell = opt.shell})
end
if cppinfo then
return cppinfo.outdata, cppinfo.errdata
diff --git a/xmake/modules/detect/tools/cosmocc/has_flags.lua b/xmake/modules/detect/tools/cosmocc/has_flags.lua
new file mode 100644
index 000000000..e58be748b
--- /dev/null
+++ b/xmake/modules/detect/tools/cosmocc/has_flags.lua
@@ -0,0 +1,143 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- 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
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file has_flags.lua
+--
+
+-- imports
+import("core.cache.detectcache")
+import("core.language.language")
+
+-- is linker?
+function _islinker(flags, opt)
+
+ -- the flags is "-Wl,<arg>" or "-Xlinker <arg>"?
+ local flags_str = table.concat(flags, " ")
+ if flags_str:startswith("-Wl,") or flags_str:startswith("-Xlinker ") then
+ return true
+ end
+
+ -- the tool kind is ld or sh?
+ local toolkind = opt.toolkind or ""
+ return toolkind == "ld" or toolkind == "sh" or toolkind:endswith("ld") or toolkind:endswith("sh")
+end
+
+-- try running
+function _try_running(program, argv, opt)
+ local errors = nil
+ return try {
+ function () os.runv(program, argv, table.join(opt or {}, {shell = true}))
+ return true
+ end, catch { function (errs) errors = (errs or ""):trim() end }}, errors
+end
+
+-- attempt to check it from known flags
+function _check_from_knownargs(flags, opt, islinker)
+ local flag = flags[1]
+ if not islinker then
+ if flag:startswith("-D") or
+ flag:startswith("-U") or
+ flag:startswith("-I") then
+ return true
+ end
+ end
+end
+
+-- attempt to check it from the argument list
+function _check_from_arglist(flags, opt, islinker)
+ local key = "detect.tools.cosmocc." .. (islinker and "has_ldflags" or "has_cflags")
+ local flagskey = opt.program .. "_" .. (opt.programver or "")
+ local allflags = detectcache:get2(key, flagskey)
+ if not allflags then
+ allflags = {}
+ local arglist = try {function () return os.iorunv(opt.program, {islinker and "-Wl,--help" or "--help"}, {envs = opt.envs, shell = true}) end}
+ if arglist then
+ for arg in arglist:gmatch("%s+(%-[%-%a%d]+)%s+") do
+ allflags[arg] = true
+ end
+ end
+ detectcache:set2(key, flagskey, allflags)
+ detectcache:save()
+ end
+ local flag = flags[1]
+ if islinker and flag then
+ if flag:startswith("-Wl,") then
+ flag = flag:match("-Wl,(.-),") or flag:sub(5)
+ end
+ end
+ return allflags[flag]
+end
+
+-- get extension
+function _get_extension(opt)
+ -- @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]
+ return (opt.program:endswith("++") or opt.flagkind == "cxxflags") and ".cpp" or (table.wrap(language.sourcekinds()[opt.toolkind or "cc"])[1] or ".c")
+end
+
+-- try running to check flags
+function _check_try_running(flags, opt, islinker)
+
+ -- make an stub source file
+ local snippet = opt.snippet or "int main(int argc, char** argv)\n{return 0;}"
+ local sourcefile = os.tmpfile("cosmocc_has_flags:" .. snippet) .. _get_extension(opt)
+ if not os.isfile(sourcefile) then
+ io.writefile(sourcefile, snippet)
+ end
+ if is_host("windows") then
+ sourcefile = sourcefile:gsub("\\", "/")
+ end
+
+ -- check flags for linker
+ local tmpfile = os.tmpfile()
+ if is_host("windows") then
+ tmpfile = tmpfile:gsub("\\", "/")
+ end
+ if islinker then
+ return _try_running(opt.program, table.join(flags, "-o", tmpfile, sourcefile), opt)
+ end
+
+ -- check flags for compiler
+ -- @note we cannot use os.nuldev() as the output file, maybe run failed for some flags, e.g. --coverage
+ return _try_running(opt.program, table.join(flags, "-o", tmpfile, sourcefile), opt)
+end
+
+-- has_flags(flags)?
+--
+-- @param opt the argument options, e.g. {toolname = "", program = "", programver = "", toolkind = "[cc|cxx|ld|ar|sh|gc|mm|mxx]"}
+--
+-- @return true or false
+--
+function main(flags, opt)
+
+ -- is linker?
+ opt = opt or {}
+ local islinker = _islinker(flags, opt)
+
+ -- attempt to check it from the argument list
+ if not opt.tryrun then
+ if _check_from_arglist(flags, opt, islinker) then
+ return true
+ end
+ if _check_from_knownargs(flags, opt, islinker) then
+ return true
+ end
+ end
+
+ -- try running to check it
+ return _check_try_running(flags, opt, islinker)
+end
+
diff --git a/xmake/modules/detect/tools/cosmocxx/has_flags.lua b/xmake/modules/detect/tools/cosmocxx/has_flags.lua
new file mode 100644
index 000000000..3f4e9dfad
--- /dev/null
+++ b/xmake/modules/detect/tools/cosmocxx/has_flags.lua
@@ -0,0 +1,23 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- 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
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file has_flags.lua
+--
+
+-- imports
+inherit("detect.tools.cosmocc.has_flags")
+
diff --git a/xmake/modules/detect/tools/find_cosmoar.lua b/xmake/modules/detect/tools/find_cosmoar.lua
new file mode 100644
index 000000000..01c9232ce
--- /dev/null
+++ b/xmake/modules/detect/tools/find_cosmoar.lua
@@ -0,0 +1,44 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- 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
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file find_cosmoar.lua
+--
+
+-- imports
+import("lib.detect.find_program")
+
+-- find ar
+--
+-- @param opt the argument options, e.g. {version = true}
+--
+-- @return program, version
+--
+-- @code
+--
+-- local ar = find_cosmoar()
+--
+-- @endcode
+--
+function main(opt)
+ opt = opt or {}
+ opt.shell = true
+ local program = find_program(opt.program or "cosmoar", opt)
+ if program and is_host("windows") then
+ program = program:gsub("\\", "/")
+ end
+ return program
+end
diff --git a/xmake/modules/detect/tools/find_cosmocc.lua b/xmake/modules/detect/tools/find_cosmocc.lua
new file mode 100644
index 000000000..964b564c8
--- /dev/null
+++ b/xmake/modules/detect/tools/find_cosmocc.lua
@@ -0,0 +1,49 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- 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
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file find_cosmocc.lua
+--
+
+-- imports
+import("lib.detect.find_program")
+import("lib.detect.find_programver")
+
+-- find cosmocc
+--
+-- @param opt the argument options, e.g. {version = true}
+--
+-- @return program, version
+--
+-- @code
+--
+-- local cosmocc = find_cosmocc()
+--
+-- @endcode
+--
+function main(opt)
+ opt = opt or {}
+ opt.shell = true
+ local program = find_program(opt.program or "cosmocc", opt)
+ if program and is_host("windows") then
+ program = program:gsub("\\", "/")
+ end
+ local version = nil
+ if program and opt and opt.version then
+ version = find_programver(program, opt)
+ end
+ return program, version
+end
diff --git a/xmake/modules/detect/tools/find_cosmocxx.lua b/xmake/modules/detect/tools/find_cosmocxx.lua
new file mode 100644
index 000000000..06ce69aac
--- /dev/null
+++ b/xmake/modules/detect/tools/find_cosmocxx.lua
@@ -0,0 +1,49 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- 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
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file find_cosmocxx.lua
+--
+
+-- imports
+import("lib.detect.find_program")
+import("lib.detect.find_programver")
+
+-- find cosmocxx
+--
+-- @param opt the argument options, e.g. {version = true}
+--
+-- @return program, version
+--
+-- @code
+--
+-- local cosmocxx = find_cosmocxx()
+--
+-- @endcode
+--
+function main(opt)
+ opt = opt or {}
+ opt.shell = true
+ local program = find_program(opt.program or "cosmoc++", opt)
+ if program and is_host("windows") then
+ program = program:gsub("\\", "/")
+ end
+ local version = nil
+ if program and opt and opt.version then
+ version = find_programver(program, opt)
+ end
+ return program, version
+end