From 8020014e06e7a43c9cd6cd3724f41521d8131723 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 5 Feb 2024 23:51:36 +0800 Subject: [PATCH 01/21] add cosmocc toolchain and test --- tests/projects/c/cosmocc/src/main.c | 6 ++++ tests/projects/c/cosmocc/xmake.lua | 9 +++++ xmake/toolchains/cosmocc/check.lua | 55 +++++++++++++++++++++++++++++ xmake/toolchains/cosmocc/xmake.lua | 44 +++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 tests/projects/c/cosmocc/src/main.c create mode 100644 tests/projects/c/cosmocc/xmake.lua create mode 100644 xmake/toolchains/cosmocc/check.lua create mode 100644 xmake/toolchains/cosmocc/xmake.lua diff --git a/tests/projects/c/cosmocc/src/main.c b/tests/projects/c/cosmocc/src/main.c new file mode 100644 index 00000000000..a1ce06b818d --- /dev/null +++ b/tests/projects/c/cosmocc/src/main.c @@ -0,0 +1,6 @@ +#include + +int main(int argc, char** argv) { + printf("hello world\n"); + return 0; +} diff --git a/tests/projects/c/cosmocc/xmake.lua b/tests/projects/c/cosmocc/xmake.lua new file mode 100644 index 00000000000..f625f7500c5 --- /dev/null +++ b/tests/projects/c/cosmocc/xmake.lua @@ -0,0 +1,9 @@ +add_rules("mode.debug", "mode.release") + +add_requires("cosmocc") + +target("test") + set_kind("binary") + add_files("src/*.c") + add_packages("cosmocc") + diff --git a/xmake/toolchains/cosmocc/check.lua b/xmake/toolchains/cosmocc/check.lua new file mode 100644 index 00000000000..a1abb7daf2c --- /dev/null +++ b/xmake/toolchains/cosmocc/check.lua @@ -0,0 +1,55 @@ +--!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") + +-- 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 00000000000..d2637be9803 --- /dev/null +++ b/xmake/toolchains/cosmocc/xmake.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 xmake.lua +-- + +toolchain("cosmocc") + set_kind("standalone") + set_homepage("https://github.com/jart/cosmopolitan") + set_description("build-once run-anywhere c library") + + 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 + target:set("toolset", "ranlib", "x86_64-linux-cosmo-ranlib") + target:set("toolset", "strip", "x86_64-linux-cosmo-strip") + else + target:set("toolset", "ranlib", "aarch64-linux-cosmo-ranlib") + target:set("toolset", "strip", "aarch64-linux-cosmo-strip") + end + end) From 968d7dc6f05661c381a49e965f56f397cdbb7868 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 5 Feb 2024 23:53:22 +0800 Subject: [PATCH 02/21] find cosmocc --- tests/projects/c/cosmocc/xmake.lua | 2 +- xmake/modules/core/tools/clangxx.lua | 2 +- xmake/modules/core/tools/cosmocc.lua | 32 +++++++++++++++ xmake/modules/core/tools/cosmocxx.lua | 24 +++++++++++ xmake/modules/detect/tools/find_cosmocc.lua | 45 +++++++++++++++++++++ xmake/toolchains/cosmocc/check.lua | 1 + xmake/toolchains/cosmocc/xmake.lua | 8 ++-- 7 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 xmake/modules/core/tools/cosmocc.lua create mode 100644 xmake/modules/core/tools/cosmocxx.lua create mode 100644 xmake/modules/detect/tools/find_cosmocc.lua diff --git a/tests/projects/c/cosmocc/xmake.lua b/tests/projects/c/cosmocc/xmake.lua index f625f7500c5..babd520a25e 100644 --- a/tests/projects/c/cosmocc/xmake.lua +++ b/tests/projects/c/cosmocc/xmake.lua @@ -5,5 +5,5 @@ add_requires("cosmocc") target("test") set_kind("binary") add_files("src/*.c") - add_packages("cosmocc") + set_toolchains("@cosmocc") diff --git a/xmake/modules/core/tools/clangxx.lua b/xmake/modules/core/tools/clangxx.lua index d167dcd17dd..3a35fc562c4 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/cosmocc.lua b/xmake/modules/core/tools/cosmocc.lua new file mode 100644 index 00000000000..978b03395b5 --- /dev/null +++ b/xmake/modules/core/tools/cosmocc.lua @@ -0,0 +1,32 @@ +--!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 + diff --git a/xmake/modules/core/tools/cosmocxx.lua b/xmake/modules/core/tools/cosmocxx.lua new file mode 100644 index 00000000000..4f64ad26166 --- /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/detect/tools/find_cosmocc.lua b/xmake/modules/detect/tools/find_cosmocc.lua new file mode 100644 index 00000000000..b892449941e --- /dev/null +++ b/xmake/modules/detect/tools/find_cosmocc.lua @@ -0,0 +1,45 @@ +--!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 {} + local program = find_program(opt.program or "cosmocc", opt) + 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 index a1abb7daf2c..01fa54f8b35 100644 --- a/xmake/toolchains/cosmocc/check.lua +++ b/xmake/toolchains/cosmocc/check.lua @@ -21,6 +21,7 @@ -- imports import("core.project.config") import("lib.detect.find_path") +import("detect.sdks.find_cross_toolchain") -- check the cross toolchain function main(toolchain) diff --git a/xmake/toolchains/cosmocc/xmake.lua b/xmake/toolchains/cosmocc/xmake.lua index d2637be9803..f09eb836b57 100644 --- a/xmake/toolchains/cosmocc/xmake.lua +++ b/xmake/toolchains/cosmocc/xmake.lua @@ -35,10 +35,10 @@ toolchain("cosmocc") on_load(function (toolchain) if toolchain:is_arch("x86_64", "x64") then - target:set("toolset", "ranlib", "x86_64-linux-cosmo-ranlib") - target:set("toolset", "strip", "x86_64-linux-cosmo-strip") + toolchain:set("toolset", "ranlib", "x86_64-linux-cosmo-ranlib") + toolchain:set("toolset", "strip", "x86_64-linux-cosmo-strip") else - target:set("toolset", "ranlib", "aarch64-linux-cosmo-ranlib") - target:set("toolset", "strip", "aarch64-linux-cosmo-strip") + toolchain:set("toolset", "ranlib", "aarch64-linux-cosmo-ranlib") + toolchain:set("toolset", "strip", "aarch64-linux-cosmo-strip") end end) From 62f498ded882ce0f43bc7ee287d91c59827a5fa2 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 5 Feb 2024 23:54:08 +0800 Subject: [PATCH 03/21] impl cosmocc tool --- xmake/modules/core/tools/cosmocc.lua | 4 -- .../detect/tools/cosmocc/has_flags.lua | 23 ++++++++++ .../detect/tools/cosmocxx/has_flags.lua | 23 ++++++++++ xmake/modules/detect/tools/find_cosmocxx.lua | 45 +++++++++++++++++++ 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 xmake/modules/detect/tools/cosmocc/has_flags.lua create mode 100644 xmake/modules/detect/tools/cosmocxx/has_flags.lua create mode 100644 xmake/modules/detect/tools/find_cosmocxx.lua diff --git a/xmake/modules/core/tools/cosmocc.lua b/xmake/modules/core/tools/cosmocc.lua index 978b03395b5..00363499585 100644 --- a/xmake/modules/core/tools/cosmocc.lua +++ b/xmake/modules/core/tools/cosmocc.lua @@ -26,7 +26,3 @@ function init(self) _super.init(self) end --- make the strip flag -function nf_strip(self, level) -end - 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 00000000000..6464e10b633 --- /dev/null +++ b/xmake/modules/detect/tools/cosmocc/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.gcc.has_flags") + 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 00000000000..6464e10b633 --- /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.gcc.has_flags") + diff --git a/xmake/modules/detect/tools/find_cosmocxx.lua b/xmake/modules/detect/tools/find_cosmocxx.lua new file mode 100644 index 00000000000..c9a06fb0da2 --- /dev/null +++ b/xmake/modules/detect/tools/find_cosmocxx.lua @@ -0,0 +1,45 @@ +--!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 {} + local program = find_program(opt.program or "cosmoc++", opt) + local version = nil + if program and opt and opt.version then + version = find_programver(program, opt) + end + return program, version +end From 7da1f4565975341780560026fe0e28a51091ce70 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 5 Feb 2024 23:54:11 +0800 Subject: [PATCH 04/21] disable strip --- xmake/modules/core/tools/cosmocc.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/xmake/modules/core/tools/cosmocc.lua b/xmake/modules/core/tools/cosmocc.lua index 00363499585..978b03395b5 100644 --- a/xmake/modules/core/tools/cosmocc.lua +++ b/xmake/modules/core/tools/cosmocc.lua @@ -26,3 +26,7 @@ function init(self) _super.init(self) end +-- make the strip flag +function nf_strip(self, level) +end + From 832fc8ded7cbe98ab0244b6975d45215615221b0 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 5 Feb 2024 23:55:44 +0800 Subject: [PATCH 05/21] improve to find cosmocc --- .../core/sandbox/modules/import/lib/detect/find_program.lua | 6 +++--- xmake/modules/detect/tools/find_cosmocc.lua | 1 + xmake/modules/detect/tools/find_cosmocxx.lua | 1 + 3 files changed, 5 insertions(+), 3 deletions(-) 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 6b8aeb13a92..baa180b4b3c 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/detect/tools/find_cosmocc.lua b/xmake/modules/detect/tools/find_cosmocc.lua index b892449941e..be7c873f898 100644 --- a/xmake/modules/detect/tools/find_cosmocc.lua +++ b/xmake/modules/detect/tools/find_cosmocc.lua @@ -36,6 +36,7 @@ import("lib.detect.find_programver") -- function main(opt) opt = opt or {} + opt.shell = true local program = find_program(opt.program or "cosmocc", opt) local version = nil if program and opt and opt.version then diff --git a/xmake/modules/detect/tools/find_cosmocxx.lua b/xmake/modules/detect/tools/find_cosmocxx.lua index c9a06fb0da2..535cea6ab0a 100644 --- a/xmake/modules/detect/tools/find_cosmocxx.lua +++ b/xmake/modules/detect/tools/find_cosmocxx.lua @@ -36,6 +36,7 @@ import("lib.detect.find_programver") -- function main(opt) opt = opt or {} + opt.shell = true local program = find_program(opt.program or "cosmoc++", opt) local version = nil if program and opt and opt.version then From b78f608ae74c5c0398e9160891fc6bbef08e358f Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 5 Feb 2024 23:56:46 +0800 Subject: [PATCH 06/21] pass shell to cosmocc --- xmake/modules/core/tools/cosmocc.lua | 9 +++++++++ xmake/modules/core/tools/gcc.lua | 9 +++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/xmake/modules/core/tools/cosmocc.lua b/xmake/modules/core/tools/cosmocc.lua index 978b03395b5..c412d287bfb 100644 --- a/xmake/modules/core/tools/cosmocc.lua +++ b/xmake/modules/core/tools/cosmocc.lua @@ -30,3 +30,12 @@ end function nf_strip(self, level) end +-- link the target file +function link(self, objectfiles, targetkind, targetfile, flags, opt) + 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) + return _super.compile(self, sourcefile, objectfile, dependinfo, flags, table.join(opt, {shell = true})) +end diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index e519b376a1d..d5ae314b77b 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,7 +706,7 @@ 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 From 9c41c4b806e46bd460bec410870930c785b3aa54 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 00:37:14 +0800 Subject: [PATCH 07/21] add test.lua --- tests/projects/c/cosmocc/test.lua | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 tests/projects/c/cosmocc/test.lua diff --git a/tests/projects/c/cosmocc/test.lua b/tests/projects/c/cosmocc/test.lua new file mode 100644 index 00000000000..53e3c9e74c2 --- /dev/null +++ b/tests/projects/c/cosmocc/test.lua @@ -0,0 +1,5 @@ +function main(t) + if is_host("windows", "macosx", "linux") then + os.exec("xmake -vD") + end +end From a2d3c06fc509f749afbaedb90c1cbec7920e8207 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 00:37:53 +0800 Subject: [PATCH 08/21] improve test.lua --- tests/projects/c/cosmocc/test.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/projects/c/cosmocc/test.lua b/tests/projects/c/cosmocc/test.lua index 53e3c9e74c2..2dce5e048b0 100644 --- a/tests/projects/c/cosmocc/test.lua +++ b/tests/projects/c/cosmocc/test.lua @@ -1,5 +1,8 @@ function main(t) - if is_host("windows", "macosx", "linux") then + if is_subhost("msys", "cygwin") then + os.exec("xmake f -p windows -vD") + os.exec("xmake -vD") + elseif is_host("macosx", "linux") then os.exec("xmake -vD") end end From 8fa5433f3f8136c627dd594f1249edf175896cd8 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 00:42:12 +0800 Subject: [PATCH 09/21] fix test --- tests/projects/c/cosmocc/test.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/projects/c/cosmocc/test.lua b/tests/projects/c/cosmocc/test.lua index 2dce5e048b0..d6ef2e8daf5 100644 --- a/tests/projects/c/cosmocc/test.lua +++ b/tests/projects/c/cosmocc/test.lua @@ -1,8 +1,8 @@ function main(t) if is_subhost("msys", "cygwin") then - os.exec("xmake f -p windows -vD") + os.exec("xmake f -y -p windows -vD") os.exec("xmake -vD") elseif is_host("macosx", "linux") then - os.exec("xmake -vD") + os.exec("xmake -y -vD") end end From b5aa7bfa552889527db96a45a4cef91559f7aede Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 00:54:21 +0800 Subject: [PATCH 10/21] fix toolchain format --- xmake/core/platform/platform.lua | 6 +++--- xmake/core/project/target.lua | 26 +++++++++++++++++++++++--- xmake/toolchains/cosmocc/xmake.lua | 5 +++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/xmake/core/platform/platform.lua b/xmake/core/platform/platform.lua index f9a8fba77eb..54ec3574736 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 b9266ca3517..25685ccb59c 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/toolchains/cosmocc/xmake.lua b/xmake/toolchains/cosmocc/xmake.lua index f09eb836b57..56788e9028a 100644 --- a/xmake/toolchains/cosmocc/xmake.lua +++ b/xmake/toolchains/cosmocc/xmake.lua @@ -23,6 +23,11 @@ toolchain("cosmocc") 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("binary", "$(name).com") + set_formats("symbol", "$(name).sym") + set_toolset("cc", "cosmocc") set_toolset("cxx", "cosmocc", "cosmoc++") set_toolset("cpp", "cosmocc -E") From 4c6ea8fc3681cab9cedadca221263f300726b232 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 00:59:22 +0800 Subject: [PATCH 11/21] improve to find cosmocc --- xmake/modules/detect/tools/find_cosmocc.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xmake/modules/detect/tools/find_cosmocc.lua b/xmake/modules/detect/tools/find_cosmocc.lua index be7c873f898..4b0f689e4f1 100644 --- a/xmake/modules/detect/tools/find_cosmocc.lua +++ b/xmake/modules/detect/tools/find_cosmocc.lua @@ -42,5 +42,8 @@ function main(opt) if program and opt and opt.version then version = find_programver(program, opt) end + if program then + program = program:gsub("\\", "/") + end return program, version end From 1dd66f29e0923d994fc37fcee2db328367c46e7b Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 22:32:37 +0800 Subject: [PATCH 12/21] pass shell --- xmake/modules/core/tools/gcc.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index d5ae314b77b..e4e658749c0 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -712,11 +712,11 @@ function _compile(self, sourcefile, objectfile, compflags, opt) 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 From 619db25f2c85fa771643b3924632915f332b9cce Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 22:34:42 +0800 Subject: [PATCH 13/21] fix formats --- xmake/toolchains/cosmocc/xmake.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/xmake/toolchains/cosmocc/xmake.lua b/xmake/toolchains/cosmocc/xmake.lua index 56788e9028a..fd82c61e2cf 100644 --- a/xmake/toolchains/cosmocc/xmake.lua +++ b/xmake/toolchains/cosmocc/xmake.lua @@ -25,7 +25,6 @@ toolchain("cosmocc") set_formats("static", "lib$(name).a") set_formats("object", "$(name).o") - set_formats("binary", "$(name).com") set_formats("symbol", "$(name).sym") set_toolset("cc", "cosmocc") From 1328e9a90cbc4be7d3019539226c61f3d7eccac6 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 22:34:55 +0800 Subject: [PATCH 14/21] improve to find cosmocc --- xmake/modules/detect/tools/find_cosmocc.lua | 2 +- xmake/modules/detect/tools/find_cosmocxx.lua | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/xmake/modules/detect/tools/find_cosmocc.lua b/xmake/modules/detect/tools/find_cosmocc.lua index 4b0f689e4f1..102c452840c 100644 --- a/xmake/modules/detect/tools/find_cosmocc.lua +++ b/xmake/modules/detect/tools/find_cosmocc.lua @@ -42,7 +42,7 @@ function main(opt) if program and opt and opt.version then version = find_programver(program, opt) end - if program then + if program and is_host("windows") then program = program:gsub("\\", "/") end return program, version diff --git a/xmake/modules/detect/tools/find_cosmocxx.lua b/xmake/modules/detect/tools/find_cosmocxx.lua index 535cea6ab0a..06ce69aac21 100644 --- a/xmake/modules/detect/tools/find_cosmocxx.lua +++ b/xmake/modules/detect/tools/find_cosmocxx.lua @@ -38,6 +38,9 @@ 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) From 7dd4614a455be4d24ef6d8796f053f250bf92c39 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 22:35:17 +0800 Subject: [PATCH 15/21] fix windows files path --- xmake/modules/core/tools/cosmocc.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/xmake/modules/core/tools/cosmocc.lua b/xmake/modules/core/tools/cosmocc.lua index c412d287bfb..68b531a2a43 100644 --- a/xmake/modules/core/tools/cosmocc.lua +++ b/xmake/modules/core/tools/cosmocc.lua @@ -32,10 +32,22 @@ end -- link the target file function link(self, objectfiles, targetkind, targetfile, flags, opt) + 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) + if is_host("windows") then + sourcefile = sourcefile:gsub("\\", "/") + objectfile = objectfile:gsub("\\", "/") + end return _super.compile(self, sourcefile, objectfile, dependinfo, flags, table.join(opt, {shell = true})) end From 61de5394ae8d322c0f1f89e1ee322d8adaaa07d8 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 22:37:12 +0800 Subject: [PATCH 16/21] disable ccache for windows --- xmake/modules/core/tools/cosmocc.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/xmake/modules/core/tools/cosmocc.lua b/xmake/modules/core/tools/cosmocc.lua index 68b531a2a43..60f25a3f0be 100644 --- a/xmake/modules/core/tools/cosmocc.lua +++ b/xmake/modules/core/tools/cosmocc.lua @@ -45,9 +45,14 @@ 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 From 141b1ba725ea1d7e0f6a87a673db6b97b335d718 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 22:39:32 +0800 Subject: [PATCH 17/21] improve has_flags --- .../detect/tools/cosmocc/has_flags.lua | 122 +++++++++++++++++- .../detect/tools/cosmocxx/has_flags.lua | 2 +- 2 files changed, 122 insertions(+), 2 deletions(-) diff --git a/xmake/modules/detect/tools/cosmocc/has_flags.lua b/xmake/modules/detect/tools/cosmocc/has_flags.lua index 6464e10b633..8f389508099 100644 --- a/xmake/modules/detect/tools/cosmocc/has_flags.lua +++ b/xmake/modules/detect/tools/cosmocc/has_flags.lua @@ -19,5 +19,125 @@ -- -- imports -inherit("detect.tools.gcc.has_flags") +import("core.cache.detectcache") +import("core.language.language") + +-- is linker? +function _islinker(flags, opt) + + -- the flags is "-Wl," or "-Xlinker "? + 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}) 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, "-S", "-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 index 6464e10b633..3f4e9dfad07 100644 --- a/xmake/modules/detect/tools/cosmocxx/has_flags.lua +++ b/xmake/modules/detect/tools/cosmocxx/has_flags.lua @@ -19,5 +19,5 @@ -- -- imports -inherit("detect.tools.gcc.has_flags") +inherit("detect.tools.cosmocc.has_flags") From b8c2e1315d46944e72fdaa0b23eb96c34361bfa3 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 22:40:26 +0800 Subject: [PATCH 18/21] improve has_flags --- xmake/modules/detect/tools/cosmocc/has_flags.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/detect/tools/cosmocc/has_flags.lua b/xmake/modules/detect/tools/cosmocc/has_flags.lua index 8f389508099..8ff8fa1ff24 100644 --- a/xmake/modules/detect/tools/cosmocc/has_flags.lua +++ b/xmake/modules/detect/tools/cosmocc/has_flags.lua @@ -64,7 +64,7 @@ function _check_from_arglist(flags, opt, islinker) 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}) end} + 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 From 469f941f5680273b5b734996677b055a776f0056 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 22:41:11 +0800 Subject: [PATCH 19/21] remove -S --- xmake/modules/detect/tools/cosmocc/has_flags.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/detect/tools/cosmocc/has_flags.lua b/xmake/modules/detect/tools/cosmocc/has_flags.lua index 8ff8fa1ff24..e58be748bed 100644 --- a/xmake/modules/detect/tools/cosmocc/has_flags.lua +++ b/xmake/modules/detect/tools/cosmocc/has_flags.lua @@ -112,7 +112,7 @@ function _check_try_running(flags, opt, islinker) -- 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, "-S", "-o", tmpfile, sourcefile), opt) + return _try_running(opt.program, table.join(flags, "-o", tmpfile, sourcefile), opt) end -- has_flags(flags)? From 78a1a52f425010571eda84ab1cefeb11d78c20c8 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 22:44:21 +0800 Subject: [PATCH 20/21] add cosmoar support --- .../c/cosmocc/{ => console}/src/main.c | 0 .../projects/c/cosmocc/{ => console}/test.lua | 0 .../c/cosmocc/{ => console}/xmake.lua | 0 .../c/cosmocc/static_library/src/foo.c | 5 +++ .../c/cosmocc/static_library/src/foo.h | 2 + .../c/cosmocc/static_library/src/main.c | 7 +++ .../c/cosmocc/static_library/xmake.lua | 15 +++++++ xmake/modules/core/tools/ar.lua | 5 ++- xmake/modules/core/tools/cosmoar.lua | 38 ++++++++++++++++ xmake/modules/core/tools/cosmocc.lua | 1 + xmake/modules/detect/tools/find_cosmoar.lua | 44 +++++++++++++++++++ xmake/modules/detect/tools/find_cosmocc.lua | 6 +-- 12 files changed, 118 insertions(+), 5 deletions(-) rename tests/projects/c/cosmocc/{ => console}/src/main.c (100%) rename tests/projects/c/cosmocc/{ => console}/test.lua (100%) rename tests/projects/c/cosmocc/{ => console}/xmake.lua (100%) create mode 100644 tests/projects/c/cosmocc/static_library/src/foo.c create mode 100644 tests/projects/c/cosmocc/static_library/src/foo.h create mode 100644 tests/projects/c/cosmocc/static_library/src/main.c create mode 100644 tests/projects/c/cosmocc/static_library/xmake.lua create mode 100644 xmake/modules/core/tools/cosmoar.lua create mode 100644 xmake/modules/detect/tools/find_cosmoar.lua diff --git a/tests/projects/c/cosmocc/src/main.c b/tests/projects/c/cosmocc/console/src/main.c similarity index 100% rename from tests/projects/c/cosmocc/src/main.c rename to tests/projects/c/cosmocc/console/src/main.c diff --git a/tests/projects/c/cosmocc/test.lua b/tests/projects/c/cosmocc/console/test.lua similarity index 100% rename from tests/projects/c/cosmocc/test.lua rename to tests/projects/c/cosmocc/console/test.lua diff --git a/tests/projects/c/cosmocc/xmake.lua b/tests/projects/c/cosmocc/console/xmake.lua similarity index 100% rename from tests/projects/c/cosmocc/xmake.lua rename to tests/projects/c/cosmocc/console/xmake.lua 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 00000000000..1a1fb3425c5 --- /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 00000000000..16d451cbb6b --- /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 00000000000..a4c76940856 --- /dev/null +++ b/tests/projects/c/cosmocc/static_library/src/main.c @@ -0,0 +1,7 @@ +#include "foo.h" +#include + +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 00000000000..73d0a681df0 --- /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/modules/core/tools/ar.lua b/xmake/modules/core/tools/ar.lua index 0496238b4dd..b302504d8d9 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/cosmoar.lua b/xmake/modules/core/tools/cosmoar.lua new file mode 100644 index 00000000000..2ee1741ae46 --- /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 index 60f25a3f0be..c53f6e1c528 100644 --- a/xmake/modules/core/tools/cosmocc.lua +++ b/xmake/modules/core/tools/cosmocc.lua @@ -32,6 +32,7 @@ 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 = {} diff --git a/xmake/modules/detect/tools/find_cosmoar.lua b/xmake/modules/detect/tools/find_cosmoar.lua new file mode 100644 index 00000000000..01c9232cecb --- /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 index 102c452840c..964b564c8fb 100644 --- a/xmake/modules/detect/tools/find_cosmocc.lua +++ b/xmake/modules/detect/tools/find_cosmocc.lua @@ -38,12 +38,12 @@ 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 - if program and is_host("windows") then - program = program:gsub("\\", "/") - end return program, version end From b74097ad5fa27dd871ef4cdc6e342342eaca6192 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 6 Feb 2024 22:50:38 +0800 Subject: [PATCH 21/21] update changelog and readme --- CHANGELOG.md | 2 + README.md | 93 +++++++++++------------ README_zh.md | 1 + tests/projects/c/cosmocc/console/test.lua | 8 -- 4 files changed, 49 insertions(+), 55 deletions(-) delete mode 100644 tests/projects/c/cosmocc/console/test.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index b3e289cb739..db29bdf3630 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 a5bd49156e1..57cfb13a843 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 c5318a5ab51..7efb052fa94 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/test.lua b/tests/projects/c/cosmocc/console/test.lua deleted file mode 100644 index d6ef2e8daf5..00000000000 --- a/tests/projects/c/cosmocc/console/test.lua +++ /dev/null @@ -1,8 +0,0 @@ -function main(t) - if is_subhost("msys", "cygwin") then - os.exec("xmake f -y -p windows -vD") - os.exec("xmake -vD") - elseif is_host("macosx", "linux") then - os.exec("xmake -y -vD") - end -end