Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cosmocc toolchain support #4704

Merged
merged 21 commits into from
Feb 6, 2024
Merged
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 工具链支持

### 改进

Expand Down
93 changes: 46 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

## 支持语言
Expand Down
6 changes: 6 additions & 0 deletions tests/projects/c/cosmocc/console/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

int main(int argc, char** argv) {
printf("hello world\n");
return 0;
}
9 changes: 9 additions & 0 deletions tests/projects/c/cosmocc/console/xmake.lua
Original file line number Diff line number Diff line change
@@ -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")

5 changes: 5 additions & 0 deletions tests/projects/c/cosmocc/static_library/src/foo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "foo.h"

int add(int a, int b) {
return a + b;
}
2 changes: 2 additions & 0 deletions tests/projects/c/cosmocc/static_library/src/foo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
int add(int a, int b);

7 changes: 7 additions & 0 deletions tests/projects/c/cosmocc/static_library/src/main.c
Original file line number Diff line number Diff line change
@@ -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;
}
15 changes: 15 additions & 0 deletions tests/projects/c/cosmocc/static_library/xmake.lua
Original file line number Diff line number Diff line change
@@ -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")


6 changes: 3 additions & 3 deletions xmake/core/platform/platform.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
26 changes: 23 additions & 3 deletions xmake/core/project/target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions xmake/core/sandbox/modules/import/lib/detect/find_program.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions xmake/modules/core/tools/ar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ 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
os.tryrm(targetfile)

-- 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


2 changes: 1 addition & 1 deletion xmake/modules/core/tools/clangxx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file clang++.lua
-- @file clangxx.lua
--

-- inherit clang
Expand Down
38 changes: 38 additions & 0 deletions xmake/modules/core/tools/cosmoar.lua
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading