summaryrefslogtreecommitdiff
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
parent8c92537300242e1705e522fe0e6a8087fe1f685c (diff)
parentb74097ad5fa27dd871ef4cdc6e342342eaca6192 (diff)
Merge pull request #4704 from xmake-io/cosmocc
Add cosmocc toolchain support
-rw-r--r--CHANGELOG.md2
-rw-r--r--README.md93
-rw-r--r--README_zh.md1
-rw-r--r--tests/projects/c/cosmocc/console/src/main.c6
-rw-r--r--tests/projects/c/cosmocc/console/xmake.lua9
-rw-r--r--tests/projects/c/cosmocc/static_library/src/foo.c5
-rw-r--r--tests/projects/c/cosmocc/static_library/src/foo.h2
-rw-r--r--tests/projects/c/cosmocc/static_library/src/main.c7
-rw-r--r--tests/projects/c/cosmocc/static_library/xmake.lua15
-rw-r--r--xmake/core/platform/platform.lua6
-rw-r--r--xmake/core/project/target.lua26
-rw-r--r--xmake/core/sandbox/modules/import/lib/detect/find_program.lua6
-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
-rw-r--r--xmake/toolchains/cosmocc/check.lua56
-rw-r--r--xmake/toolchains/cosmocc/xmake.lua48
25 files changed, 666 insertions, 65 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b3e289cb7..db29bdf36 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@
* [#4544](https://github.com/xmake-io/xmake/issues/4544): Support to wait process timeout for `xmake test`
* [#4606](https://github.com/xmake-io/xmake/pull/4606): Add `add_versionfiles` api in package
+* [#4709](https://github.com/xmake-io/xmake/issues/4709): Add cosmocc toolchain support
### Changes
@@ -1727,6 +1728,7 @@
* [#4544](https://github.com/xmake-io/xmake/issues/4544): 改进 `xmake test`,支持等待进程超时
* [#4606](https://github.com/xmake-io/xmake/pull/4606): 为 package 添加 `add_versionfiles` 接口
+* [#4709](https://github.com/xmake-io/xmake/issues/4709): 添加 cosmocc 工具链支持
### 改进
diff --git a/README.md b/README.md
index a5bd49156..57cfb13a8 100644
--- a/README.md
+++ b/README.md
@@ -197,53 +197,52 @@ $ xmake f --menu
## Supported toolchains
-### IDE-Tied
-
-* Xcode
-* MSVC (Microsoft Visual C compiler)
-* Android NDK
-
-### Languages
-
-* Zig
-* Go(lang)
-* Swift
-* Nim
-* Rust
-* GCC (GNU Compiler Collection)
-* Clang
-* TinyCC
-* icc (Intel C Compiler)
-* icpc (Intel C++ Compiler)
-* icx (Intel LLVM C/C++ Compiler)
-* Clang-CL (Clang Compatability with MSVC)
-* DPC++ (Intel LLVM C++ Compiler using SYCL)
-* MinGW (GNU for Windows)
-* C51 (Keil C Compiler for the 8051)
-* GNU-RM (GNU Arm Embedded Toolchain)
-* ArmCC (Keil C Compiler for Keil MKD Version 5)
-* Circle (New C++20 compiler)
-* WASI (C/C++ WebAssembly Toolchain)
-* ArmClang (Version 6 of the Keil MDK)
-* SDCC (Small Device C Compiler)
-* GDC (GNU D Compiler)
-* LDC (LLVM D Compiler)
-* DMD (Dlang)
-* FPC (Free Pascal Programming Language Compiler)
-* GFortran (GNU Fortran Compiler)
-* Ifort (Intel Fortran Compiler)
-* CUDA (nvcc, nvc, nvc++, nvfortran)
-* Emscripten
-* LLVM
-* Icarus Verilog
-* Verilator (SystemVerilog simulator and lint system)
-
-### Assemblers
-
-* FASM
-* NASM
-* YASM
-* MASM32 (Microsoft Macro Assembler 32-bit SDK)
+```bash
+$ xmake show -l toolchains
+xcode Xcode IDE
+msvc Microsoft Visual C/C++ Compiler
+clang-cl LLVM Clang C/C++ Compiler compatible with msvc
+yasm The Yasm Modular Assembler
+clang A C language family frontend for LLVM
+go Go Programming Language Compiler
+dlang D Programming Language Compiler (Auto)
+dmd D Programming Language Compiler
+ldc The LLVM-based D Compiler
+gdc The GNU D Compiler (GDC)
+gfortran GNU Fortran Programming Language Compiler
+zig Zig Programming Language Compiler
+sdcc Small Device C Compiler
+cuda CUDA Toolkit (nvcc, nvc, nvc++, nvfortran)
+ndk Android NDK
+rust Rust Programming Language Compiler
+swift Swift Programming Language Compiler
+llvm A collection of modular and reusable compiler and toolchain technologies
+cross Common cross compilation toolchain
+nasm NASM Assembler
+gcc GNU Compiler Collection
+mingw Minimalist GNU for Windows
+gnu-rm GNU Arm Embedded Toolchain
+envs Environment variables toolchain
+fasm Flat Assembler
+tinycc Tiny C Compiler
+emcc A toolchain for compiling to asm.js and WebAssembly
+icc Intel C/C++ Compiler
+ifort Intel Fortran Compiler
+muslcc The musl-based cross-compilation toolchain
+fpc Free Pascal Programming Language Compiler
+wasi WASI-enabled WebAssembly C/C++ toolchain
+nim Nim Programming Language Compiler
+circle A new C++20 compiler
+armcc ARM Compiler Version 5 of Keil MDK
+armclang ARM Compiler Version 6 of Keil MDK
+c51 Keil development tools for the 8051 Microcontroller Architecture
+icx Intel LLVM C/C++ Compiler
+dpcpp Intel LLVM C++ Compiler for data parallel programming model based on Khronos SYCL
+masm32 The MASM32 SDK
+iverilog Icarus Verilog
+verilator Verilator open-source SystemVerilog simulator and lint system
+cosmocc build-once run-anywhere
+```
## Supported languages
diff --git a/README_zh.md b/README_zh.md
index c5318a5ab..7efb052fa 100644
--- a/README_zh.md
+++ b/README_zh.md
@@ -303,6 +303,7 @@ dpcpp Intel LLVM C++ Compiler for data parallel programming model based
masm32 The MASM32 SDK
iverilog Icarus Verilog
verilator Verilator open-source SystemVerilog simulator and lint system
+cosmocc build-once run-anywhere
```
## 支持语言
diff --git a/tests/projects/c/cosmocc/console/src/main.c b/tests/projects/c/cosmocc/console/src/main.c
new file mode 100644
index 000000000..a1ce06b81
--- /dev/null
+++ b/tests/projects/c/cosmocc/console/src/main.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+int main(int argc, char** argv) {
+ printf("hello world\n");
+ return 0;
+}
diff --git a/tests/projects/c/cosmocc/console/xmake.lua b/tests/projects/c/cosmocc/console/xmake.lua
new file mode 100644
index 000000000..babd520a2
--- /dev/null
+++ b/tests/projects/c/cosmocc/console/xmake.lua
@@ -0,0 +1,9 @@
+add_rules("mode.debug", "mode.release")
+
+add_requires("cosmocc")
+
+target("test")
+ set_kind("binary")
+ add_files("src/*.c")
+ set_toolchains("@cosmocc")
+
diff --git a/tests/projects/c/cosmocc/static_library/src/foo.c b/tests/projects/c/cosmocc/static_library/src/foo.c
new file mode 100644
index 000000000..1a1fb3425
--- /dev/null
+++ b/tests/projects/c/cosmocc/static_library/src/foo.c
@@ -0,0 +1,5 @@
+#include "foo.h"
+
+int add(int a, int b) {
+ return a + b;
+}
diff --git a/tests/projects/c/cosmocc/static_library/src/foo.h b/tests/projects/c/cosmocc/static_library/src/foo.h
new file mode 100644
index 000000000..16d451cbb
--- /dev/null
+++ b/tests/projects/c/cosmocc/static_library/src/foo.h
@@ -0,0 +1,2 @@
+int add(int a, int b);
+
diff --git a/tests/projects/c/cosmocc/static_library/src/main.c b/tests/projects/c/cosmocc/static_library/src/main.c
new file mode 100644
index 000000000..a4c769408
--- /dev/null
+++ b/tests/projects/c/cosmocc/static_library/src/main.c
@@ -0,0 +1,7 @@
+#include "foo.h"
+#include <stdio.h>
+
+int main(int argc, char** argv) {
+ printf("add(1, 2) = %d\n", add(1, 2));
+ return 0;
+}
diff --git a/tests/projects/c/cosmocc/static_library/xmake.lua b/tests/projects/c/cosmocc/static_library/xmake.lua
new file mode 100644
index 000000000..73d0a681d
--- /dev/null
+++ b/tests/projects/c/cosmocc/static_library/xmake.lua
@@ -0,0 +1,15 @@
+add_rules("mode.release", "mode.debug")
+
+add_requires("cosmocc")
+set_toolchains("@cosmocc")
+
+target("foo")
+ set_kind("static")
+ add_files("src/foo.c")
+
+target("demo")
+ set_kind("binary")
+ add_deps("foo")
+ add_files("src/main.c")
+
+
diff --git a/xmake/core/platform/platform.lua b/xmake/core/platform/platform.lua
index f9a8fba77..54ec35747 100644
--- a/xmake/core/platform/platform.lua
+++ b/xmake/core/platform/platform.lua
@@ -552,8 +552,8 @@ function platform.archs(plat, arch)
return platform.get("archs", plat, arch)
end
--- get the format of the given target kind for platform
-function platform.format(targetkind, plat, arch)
+-- get the format of the given kind for platform
+function platform.format(kind, plat, arch)
-- get platform instance
local instance, errors = platform.load(plat, arch)
@@ -564,7 +564,7 @@ function platform.format(targetkind, plat, arch)
-- get formats
local formats = instance:formats()
if formats then
- return formats[targetkind]
+ return formats[kind]
end
end
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index b9266ca35..25685ccb5 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -542,6 +542,23 @@ function _instance:_checked_target()
return checked_target
end
+-- get format
+function _instance:_format(kind)
+ local formats = self._FORMATS
+ if not formats then
+ for _, toolchain_inst in ipairs(self:toolchains()) do
+ formats = toolchain_inst:get("formats")
+ if formats then
+ break
+ end
+ end
+ self._FORMATS = formats
+ end
+ if formats then
+ return formats[kind or self:kind()]
+ end
+end
+
-- clone target, @note we can just call it in after_load()
function _instance:clone()
if not self:_is_loaded() then
@@ -1575,6 +1592,7 @@ function _instance:filename()
local suffixname = self:get("suffixname")
local extension = self:get("extension")
filename = target.filename(self:basename(), targetkind, {
+ format = self:_format(),
plat = self:plat(), arch = self:arch(),
prefixname = prefixname,
suffixname = suffixname,
@@ -1620,6 +1638,7 @@ function _instance:symbolfile()
local suffixname = self:get("suffixname")
local filename = target.filename(self:basename(), "symbol", {
plat = self:plat(), arch = self:arch(),
+ format = self:_format("symbol"),
prefixname = prefixname,
suffixname = suffixname})
assert(filename)
@@ -1924,7 +1943,10 @@ end
-- get object file from source file
function _instance:objectfile(sourcefile)
return self:autogenfile(sourcefile, {rootdir = self:objectdir(),
- filename = target.filename(path.filename(sourcefile), "object", {plat = self:plat(), arch = self:arch()})})
+ filename = target.filename(path.filename(sourcefile), "object", {
+ plat = self:plat(),
+ arch = self:arch(),
+ format = self:_format("object")})})
end
-- get the object files
@@ -2939,8 +2961,6 @@ end
-- get the filename from the given target name and kind
function target.filename(targetname, targetkind, opt)
-
- -- check
opt = opt or {}
assert(targetname and targetkind)
diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_program.lua b/xmake/core/sandbox/modules/import/lib/detect/find_program.lua
index 6b8aeb13a..baa180b4b 100644
--- a/xmake/core/sandbox/modules/import/lib/detect/find_program.lua
+++ b/xmake/core/sandbox/modules/import/lib/detect/find_program.lua
@@ -47,7 +47,7 @@ function sandbox_lib_detect_find_program._do_check(program, opt)
-- no check script? attempt to run it directly
if not opt.check then
- local ok, errors = os.runv(program, {"--version"}, {envs = opt.envs})
+ local ok, errors = os.runv(program, {"--version"}, {envs = opt.envs, shell = opt.shell})
if not ok and option.get("verbose") and option.get("diagnosis") then
utils.cprint("${color.warning}checkinfo: ${clear dim}" .. errors)
end
@@ -58,9 +58,9 @@ function sandbox_lib_detect_find_program._do_check(program, opt)
local ok = false
local errors = nil
if type(opt.check) == "string" then
- ok, errors = os.runv(program, {opt.check}, {envs = opt.envs})
+ ok, errors = os.runv(program, {opt.check}, {envs = opt.envs, shell = opt.shell})
elseif type(opt.check) == "table" then
- ok, errors = os.runv(program, opt.check, {envs = opt.envs})
+ ok, errors = os.runv(program, opt.check, {envs = opt.envs, shell = opt.shell})
else
ok, errors = sandbox.load(opt.check, program)
end
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
diff --git a/xmake/toolchains/cosmocc/check.lua b/xmake/toolchains/cosmocc/check.lua
new file mode 100644
index 000000000..01fa54f8b
--- /dev/null
+++ b/xmake/toolchains/cosmocc/check.lua
@@ -0,0 +1,56 @@
+--!A cross-toolchain 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 check.lua
+--
+
+-- imports
+import("core.project.config")
+import("lib.detect.find_path")
+import("detect.sdks.find_cross_toolchain")
+
+-- check the cross toolchain
+function main(toolchain)
+
+ -- get sdk directory
+ local sdkdir = toolchain:sdkdir()
+ local bindir = toolchain:bindir()
+
+ -- find cross toolchain from external envirnoment
+ local cross_toolchain = find_cross_toolchain(sdkdir, {bindir = bindir})
+ if not cross_toolchain then
+ -- find it from packages
+ for _, package in ipairs(toolchain:packages()) do
+ local installdir = package:installdir()
+ if installdir and os.isdir(installdir) then
+ cross_toolchain = find_cross_toolchain(installdir)
+ if cross_toolchain then
+ break
+ end
+ end
+ end
+ end
+ if cross_toolchain then
+ toolchain:config_set("cross", cross_toolchain.cross)
+ toolchain:config_set("bindir", cross_toolchain.bindir)
+ toolchain:config_set("sdkdir", cross_toolchain.sdkdir)
+ toolchain:configs_save()
+ else
+ raise("cosmocc toolchain not found!")
+ end
+ return cross_toolchain
+end
diff --git a/xmake/toolchains/cosmocc/xmake.lua b/xmake/toolchains/cosmocc/xmake.lua
new file mode 100644
index 000000000..fd82c61e2
--- /dev/null
+++ b/xmake/toolchains/cosmocc/xmake.lua
@@ -0,0 +1,48 @@
+--!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 xmake.lua
+--
+
+toolchain("cosmocc")
+ set_kind("standalone")
+ set_homepage("https://github.com/jart/cosmopolitan")
+ set_description("build-once run-anywhere c library")
+
+ set_formats("static", "lib$(name).a")
+ set_formats("object", "$(name).o")
+ set_formats("symbol", "$(name).sym")
+
+ set_toolset("cc", "cosmocc")
+ set_toolset("cxx", "cosmocc", "cosmoc++")
+ set_toolset("cpp", "cosmocc -E")
+ set_toolset("as", "cosmocc")
+ set_toolset("ld", "cosmoc++", "cosmocc")
+ set_toolset("sh", "cosmoc++", "cosmocc")
+ set_toolset("ar", "cosmoar")
+
+ on_check("check")
+
+ on_load(function (toolchain)
+ if toolchain:is_arch("x86_64", "x64") then
+ toolchain:set("toolset", "ranlib", "x86_64-linux-cosmo-ranlib")
+ toolchain:set("toolset", "strip", "x86_64-linux-cosmo-strip")
+ else
+ toolchain:set("toolset", "ranlib", "aarch64-linux-cosmo-ranlib")
+ toolchain:set("toolset", "strip", "aarch64-linux-cosmo-strip")
+ end
+ end)