diff options
| author | ruki <[email protected]> | 2020-07-09 12:44:06 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-07-09 12:44:06 +0800 |
| commit | 88ff5e9f82816e8800d3326def6e8e6d1c4f5f4c (patch) | |
| tree | c1058ad22955197d406fb66381e91b30cf19060c | |
| parent | c8bb0d3edb89d1ca0d2f4615b949eab952421478 (diff) | |
| parent | 4fe5f526d685978302ae2d5487a996b052931982 (diff) | |
Merge pull request #887 from xmake-io/zig
Add zig support
28 files changed, 751 insertions, 13 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f49dba9d..a04213208 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ ### New features * Add `xmake project -k xcode` generator (use cmake) -* [870](https://github.com/xmake-io/xmake/issues/870): Support gfortran compiler +* [#870](https://github.com/xmake-io/xmake/issues/870): Support gfortran compiler +* [#887](https://github.com/xmake-io/xmake/pull/887): Support zig compiler ## v2.3.5 @@ -779,7 +780,8 @@ ### 新特性 * 添加xcode工程生成器插件,`xmake project -k cmake` (当前采用cmake生成) -* [870](https://github.com/xmake-io/xmake/issues/870): 支持gfortran编译器 +* [#870](https://github.com/xmake-io/xmake/issues/870): 支持gfortran编译器 +* [#887](https://github.com/xmake-io/xmake/pull/887): 支持zig编译器 ## v2.3.5 @@ -189,6 +189,7 @@ clang A C language family frontend for LLVM go Go Programming Language Compiler dlang D Programming Language Compiler gfortran GNU Fortran Programming Language Compiler +zig Zig Programming Language Compiler sdcc Small Device C Compiler cuda CUDA Toolkit ndk Android NDK @@ -215,6 +216,7 @@ fasm Flat Assembler * Dlang * Fortran * Cuda +* Zig ## Supported Projects diff --git a/README_zh.md b/README_zh.md index 560fb1cc5..7f7c90b29 100644 --- a/README_zh.md +++ b/README_zh.md @@ -196,6 +196,7 @@ clang A C language family frontend for LLVM go Go Programming Language Compiler dlang D Programming Language Compiler gfortran GNU Fortran Programming Language Compiler +zig Zig Programming Language Compiler sdcc Small Device C Compiler cuda CUDA Toolkit ndk Android NDK @@ -221,6 +222,7 @@ fasm Flat Assembler * Dlang * Fortran * Cuda +* Zig ## 工程类型 diff --git a/tests/projects/zig/console/src/main.zig b/tests/projects/zig/console/src/main.zig new file mode 100644 index 000000000..fcfd5e0fa --- /dev/null +++ b/tests/projects/zig/console/src/main.zig @@ -0,0 +1,6 @@ +const std = @import("std"); + +pub fn main() !void { + const stdout = std.io.getStdOut().outStream(); + try stdout.print("Hello, {}!\n", .{"world"}); +} diff --git a/tests/projects/zig/console/xmake.lua b/tests/projects/zig/console/xmake.lua new file mode 100644 index 000000000..0a002cba1 --- /dev/null +++ b/tests/projects/zig/console/xmake.lua @@ -0,0 +1,6 @@ +add_rules("mode.debug", "mode.release") + +target("test") + set_kind("binary") + add_files("src/*.zig") + diff --git a/tests/projects/zig/shared_library/src/main.zig b/tests/projects/zig/shared_library/src/main.zig new file mode 100644 index 000000000..cee64b6b7 --- /dev/null +++ b/tests/projects/zig/shared_library/src/main.zig @@ -0,0 +1,8 @@ +const std = @import("std"); + +extern fn add(a: i32, b: i32) i32; + +pub fn main() !void { + const stdout = std.io.getStdOut().outStream(); + try stdout.print("Hello, {} - {}!\n", .{"world", add(1, 1)}); +} diff --git a/tests/projects/zig/shared_library/src/test.zig b/tests/projects/zig/shared_library/src/test.zig new file mode 100644 index 000000000..a04ec1544 --- /dev/null +++ b/tests/projects/zig/shared_library/src/test.zig @@ -0,0 +1,3 @@ +export fn add(a: i32, b: i32) i32 { + return a + b; +} diff --git a/tests/projects/zig/shared_library/xmake.lua b/tests/projects/zig/shared_library/xmake.lua new file mode 100644 index 000000000..c15479a7d --- /dev/null +++ b/tests/projects/zig/shared_library/xmake.lua @@ -0,0 +1,11 @@ +add_rules("mode.debug", "mode.release") + +target("testlib") + set_kind("shared") + add_files("src/test.zig") + +target("test") + set_kind("binary") + add_deps("testlib") + add_files("src/main.zig") + diff --git a/tests/projects/zig/static_library/src/main.zig b/tests/projects/zig/static_library/src/main.zig new file mode 100644 index 000000000..cee64b6b7 --- /dev/null +++ b/tests/projects/zig/static_library/src/main.zig @@ -0,0 +1,8 @@ +const std = @import("std"); + +extern fn add(a: i32, b: i32) i32; + +pub fn main() !void { + const stdout = std.io.getStdOut().outStream(); + try stdout.print("Hello, {} - {}!\n", .{"world", add(1, 1)}); +} diff --git a/tests/projects/zig/static_library/src/test.zig b/tests/projects/zig/static_library/src/test.zig new file mode 100644 index 000000000..a04ec1544 --- /dev/null +++ b/tests/projects/zig/static_library/src/test.zig @@ -0,0 +1,3 @@ +export fn add(a: i32, b: i32) i32 { + return a + b; +} diff --git a/tests/projects/zig/static_library/xmake.lua b/tests/projects/zig/static_library/xmake.lua new file mode 100644 index 000000000..19ae5cb39 --- /dev/null +++ b/tests/projects/zig/static_library/xmake.lua @@ -0,0 +1,11 @@ +add_rules("mode.debug", "mode.release") + +target("testlib") + set_kind("static") + add_files("src/test.zig") + +target("test") + set_kind("binary") + add_deps("testlib") + add_files("src/main.zig") + diff --git a/xmake/core/tool/toolchain.lua b/xmake/core/tool/toolchain.lua index f6eafa94b..b646e4cd8 100644 --- a/xmake/core/tool/toolchain.lua +++ b/xmake/core/tool/toolchain.lua @@ -247,6 +247,13 @@ function _instance:_description(toolkind) rcld = "the rust linker", rcsh = "the rust shared library linker", rcar = "the rust static library archiver", + fc = "the fortran compiler", + fcld = "the fortran linker", + fcsh = "the fortran shared library linker", + zc = "the zig compiler", + zcld = "the zig linker", + zcsh = "the zig shared library linker", + zcar = "the zig static library archiver", cu = "the cuda compiler", culd = "the cuda linker", cuccbin = "the cuda host c++ compiler", diff --git a/xmake/languages/fortran/api.lua b/xmake/languages/fortran/api.lua index 53724dd2a..9adc41ceb 100644 --- a/xmake/languages/fortran/api.lua +++ b/xmake/languages/fortran/api.lua @@ -27,7 +27,7 @@ function apis() -- target.add_xxx "target.add_links" , "target.add_syslinks" - , "target.add_dcflags" + , "target.add_fcflags" , "target.add_ldflags" , "target.add_arflags" , "target.add_shflags" @@ -35,7 +35,7 @@ function apis() -- option.add_xxx , "option.add_links" , "option.add_syslinks" - , "option.add_dcflags" + , "option.add_fcflags" , "option.add_ldflags" , "option.add_arflags" , "option.add_shflags" @@ -43,7 +43,7 @@ function apis() -- package.add_xxx , "package.add_links" , "package.add_syslinks" - , "package.add_dcflags" + , "package.add_fcflags" , "package.add_ldflags" , "package.add_arflags" , "package.add_shflags" @@ -53,7 +53,7 @@ function apis() -- toolchain.add_xxx , "toolchain.add_links" , "toolchain.add_syslinks" - , "toolchain.add_dcflags" + , "toolchain.add_fcflags" , "toolchain.add_ldflags" , "toolchain.add_arflags" , "toolchain.add_shflags" diff --git a/xmake/languages/zig/api.lua b/xmake/languages/zig/api.lua new file mode 100644 index 000000000..44a339543 --- /dev/null +++ b/xmake/languages/zig/api.lua @@ -0,0 +1,74 @@ +--!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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file api.lua +-- + +-- get apis +function apis() + + -- init apis + _g.values = + { + -- target.add_xxx + "target.add_links" + , "target.add_syslinks" + , "target.add_zcflags" + , "target.add_ldflags" + , "target.add_arflags" + , "target.add_shflags" + , "target.add_rpathdirs" -- @note do not translate path, it's usually an absolute path or contains $ORIGIN/@loader_path + -- option.add_xxx + , "option.add_links" + , "option.add_syslinks" + , "option.add_zcflags" + , "option.add_ldflags" + , "option.add_arflags" + , "option.add_shflags" + , "option.add_rpathdirs" + -- package.add_xxx + , "package.add_links" + , "package.add_syslinks" + , "package.add_zcflags" + , "package.add_ldflags" + , "package.add_arflags" + , "package.add_shflags" + , "package.add_rpathdirs" + , "package.add_linkdirs" + -- toolchain.add_xxx + , "toolchain.add_links" + , "toolchain.add_syslinks" + , "toolchain.add_zcflags" + , "toolchain.add_ldflags" + , "toolchain.add_arflags" + , "toolchain.add_shflags" + , "toolchain.add_rpathdirs" + , "toolchain.add_linkdirs" + } + _g.pathes = + { + -- target.add_xxx + "target.add_linkdirs" + -- option.add_xxx + , "option.add_linkdirs" + } + + -- ok + return _g +end + + diff --git a/xmake/languages/zig/check_main.lua b/xmake/languages/zig/check_main.lua new file mode 100644 index 000000000..1d0fdc8fb --- /dev/null +++ b/xmake/languages/zig/check_main.lua @@ -0,0 +1,40 @@ +--!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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file check_main.lua +-- + +-- check it +function main(sourcefile) + + -- load source code + local sourcecode = io.readfile(sourcefile) + + -- remove comment first + sourcecode = sourcecode:gsub("/%*.-%*/", "") + sourcecode = sourcecode:gsub("//.-\n", "\n") + + -- find func main() { + if sourcecode:find("pub%s+fn%s+main%s*%(.-%)") then + return true + end + + -- no main function + return false +end + + diff --git a/xmake/languages/zig/load.lua b/xmake/languages/zig/load.lua new file mode 100644 index 000000000..400376028 --- /dev/null +++ b/xmake/languages/zig/load.lua @@ -0,0 +1,34 @@ +--!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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file load.lua +-- + +-- imports +import("api") + +-- load it +function main() + + -- init apis + _g.apis = api.apis() + + -- ok + return _g +end + + diff --git a/xmake/languages/zig/xmake.lua b/xmake/languages/zig/xmake.lua new file mode 100644 index 000000000..f45670f16 --- /dev/null +++ b/xmake/languages/zig/xmake.lua @@ -0,0 +1,123 @@ +--!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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +-- define language +language("zig") + + -- set source file kinds + set_sourcekinds {zc = ".zig"} + + -- set source file flags + set_sourceflags {zc = "zcflags"} + + -- set target kinds + set_targetkinds {binary = "zcld", static = "zcar", shared = "zcsh"} + + -- set target flags + set_targetflags {binary = "ldflags", static = "arflags", shared = "shflags"} + + -- set language kinds + set_langkinds {zig = "zc"} + + -- set mixing kinds + set_mixingkinds("zc", "cc", "cxx", "as") + + -- add rules + add_rules("zig") + + -- on load + on_load("load") + + -- on check_main + on_check_main("check_main") + + -- set name flags + set_nameflags + { + object = + { + "target.symbols" + , "target.warnings" + , "target.optimize:check" + , "target.vectorexts:check" + } + , binary = + { + "config.linkdirs" + , "target.linkdirs" + , "target.rpathdirs" + , "target.strip" + , "target.symbols" + , "option.linkdirs" + , "option.rpathdirs" + , "toolchain.linkdirs" + , "toolchain.rpathdirs" + , "config.links" + , "target.links" + , "option.links" + , "toolchain.links" + , "config.syslinks" + , "target.syslinks" + , "option.syslinks" + , "toolchain.syslinks" + } + , shared = + { + "config.linkdirs" + , "target.linkdirs" + , "target.strip" + , "target.symbols" + , "option.linkdirs" + , "toolchain.linkdirs" + , "config.links" + , "target.links" + , "option.links" + , "toolchain.links" + , "config.syslinks" + , "target.syslinks" + , "option.syslinks" + , "toolchain.syslinks" + } + , static = + { + "target.strip" + , "target.symbols" + } + } + + -- set menu + set_menu { + config = + { + {category = "Cross Complation Configuration/Compiler Configuration" } + , {nil, "zc", "kv", nil, "The Zig Compiler" } + + , {category = "Cross Complation Configuration/Linker Configuration" } + , {nil, "zcld", "kv", nil, "The Zig Linker" } + , {nil, "zcar", "kv", nil, "The Zig Static Library Archiver" } + , {nil, "zcsh", "kv", nil, "The Zig Shared Library Linker" } + + , {category = "Cross Complation Configuration/Builtin Flags Configuration" } + , {nil, "links", "kv", nil, "The Link Libraries" } + , {nil, "syslinks", "kv", nil, "The System Link Libraries" } + , {nil, "linkdirs", "kv", nil, "The Link Search Directories" } + } + } + diff --git a/xmake/modules/core/tools/zig.lua b/xmake/modules/core/tools/zig.lua new file mode 100644 index 000000000..6e6969dbb --- /dev/null +++ b/xmake/modules/core/tools/zig.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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file zig.lua +-- + +-- imports +import("core.base.option") +import("core.project.config") +import("core.project.project") +import("core.project.target") + +-- init it +function init(self) + + -- init shflags + self:set("zcshflags", "-dynamic", "-fPIC") + + -- init zcflags for the kind: shared + self:set("shared.zcflags", "-fPIC") +end + +-- make the strip flag +function nf_strip(self, level) + local maps = + { + debug = "--strip" + , all = "--strip" + } + return maps[level] +end + +-- make the define flag +function nf_define(self, macro) + return "-D" .. macro +end + +-- make the optimize flag +function nf_optimize(self, level) + local maps = + { + none = "-O0" + , fast = "--release-safe" + , aggressive = "--release-fast" + , fastest = "--release-fast" + , smallest = "--release-small" + , aggressive = "--release-fast" + } + return maps[level] +end + +-- make the link flag +function nf_link(self, lib) + return "-l" .. lib +end + +-- make the syslink flag +function nf_syslink(self, lib) + return nf_link(self, lib) +end + +-- make the linkdir flag +function nf_linkdir(self, dir) + return "-L" .. os.args(dir) +end + +-- make the framework flag +function nf_framework(self, framework) + return "-framework " .. framework +end + +-- make the frameworkdir flag +function nf_frameworkdir(self, frameworkdir) + return "-F " .. os.args(path.translate(frameworkdir)) +end + +-- make the rpathdir flag +function nf_rpathdir(self, dir) + dir = path.translate(dir) + if is_plat("macosx") then + return "-rpath " .. os.args(dir:gsub("%$ORIGIN", "@loader_path")) + else + return "-rpath " .. os.args(dir:gsub("@[%w_]+", function (name) + local maps = {["@loader_path"] = "$ORIGIN", ["@executable_path"] = "$ORIGIN"} + return maps[name] + end)) + end +end + +-- make the link arguments list +function linkargv(self, objectfiles, targetkind, targetfile, flags) + local argv = {} + if targetkind == "binary" then + table.insert(argv, "build-exe") + elseif targetkind == "static" or targetkind == "shared" then + table.insert(argv, "build-lib") + else + raise("unknown target kind(%s)!", targetkind) + end + local name = targetkind == "binary" and path.basename(targetfile) or target.linkname(path.filename(targetfile)) + table.join2(argv, flags, "--output-dir", path.directory(targetfile), "--name", name, "--object", objectfiles) + return self:program(), argv +end + +-- link the target file +function link(self, objectfiles, targetkind, targetfile, flags) + + -- ensure the target directory + os.mkdir(path.directory(targetfile)) + + -- link it + os.runv(linkargv(self, objectfiles, targetkind, targetfile, flags)) +end + +-- make the compile arguments list +function compargv(self, sourcefile, objectfile, flags) + return self:program(), table.join("build-obj", flags, "--output-dir", path.directory(objectfile), "--name", path.basename(objectfile), sourcefile) +end + +-- compile the source file +function compile(self, sourcefile, objectfile, dependinfo, flags) + + -- ensure the object directory + os.mkdir(path.directory(objectfile)) + + -- compile it + os.runv(compargv(self, sourcefile, objectfile, flags)) +end + diff --git a/xmake/modules/detect/tools/dmd/has_flags.lua b/xmake/modules/detect/tools/dmd/has_flags.lua index 02ece3260..c9b045b68 100644 --- a/xmake/modules/detect/tools/dmd/has_flags.lua +++ b/xmake/modules/detect/tools/dmd/has_flags.lua @@ -32,7 +32,7 @@ function _islinker(flags, opt) -- 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") + return toolkind == "ld" or toolkind == "sh" or toolkind:endswith("ld") or toolkind:endswith("sh") end -- try running diff --git a/xmake/modules/detect/tools/find_zig.lua b/xmake/modules/detect/tools/find_zig.lua new file mode 100644 index 000000000..8fd53eff8 --- /dev/null +++ b/xmake/modules/detect/tools/find_zig.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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file find_zig.lua +-- + +-- imports +import("lib.detect.find_program") +import("lib.detect.find_programver") + +-- find zig +-- +-- @param opt the argument options, e.g. {version = true} +-- +-- @return program, version +-- +-- @code +-- +-- local zig = find_zig() +-- +-- @endcode +-- +function main(opt) + + -- init options + opt = opt or {} + opt.check = opt.check or "--help" + + -- find program + return find_program(opt.program or "zig", opt) +end diff --git a/xmake/modules/detect/tools/go/has_flags.lua b/xmake/modules/detect/tools/go/has_flags.lua index a33fad322..c8ab17ba9 100644 --- a/xmake/modules/detect/tools/go/has_flags.lua +++ b/xmake/modules/detect/tools/go/has_flags.lua @@ -25,9 +25,9 @@ import("core.language.language") -- is linker? function _islinker(flags, opt) - -- the tool kind is go-ld or gosh? + -- the tool kind is gcld or gcsh? local toolkind = opt.toolkind or "" - return toolkind:endswith("-ld") or toolkind:endswith("-sh") + return toolkind:endswith("ld") or toolkind:endswith("sh") end -- try running diff --git a/xmake/modules/detect/tools/zig/has_flags.lua b/xmake/modules/detect/tools/zig/has_flags.lua new file mode 100644 index 000000000..64661e0d2 --- /dev/null +++ b/xmake/modules/detect/tools/zig/has_flags.lua @@ -0,0 +1,120 @@ +--!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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file has_flags.lua +-- + +-- imports +import("lib.detect.cache") + +-- is linker? +function _islinker(flags, opt) + + -- 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(...) + + local argv = {...} + local errors = nil + return try { function () os.runv(unpack(argv)); return true end, catch { function (errs) errors = (errs or ""):trim() end }}, errors +end + +-- attempt to check it from the argument list +function _check_from_arglist(flags, opt, islinker) + + -- only for compiler + if islinker or #flags > 1 then + return + end + + -- make cache key + local key = "detect.tools.zig.has_flags" + + -- make allflags key + local flagskey = opt.program .. "_" .. (opt.programver or "") + + -- load cache + local cacheinfo = cache.load(key) + + -- get all allflags from argument list + local allflags = cacheinfo[flagskey] + if not allflags then + + -- get argument list + allflags = {} + local arglist = os.iorunv(opt.program, {"--help"}) + if arglist then + for arg in arglist:gmatch("%s+(%-[%-%a%d]+)%s+") do + allflags[arg] = true + end + end + + -- save cache + cacheinfo[flagskey] = allflags + cache.save(key, cacheinfo) + end + + -- ok? + return allflags[flags[1]] +end + +-- try running to check flags +function _check_try_running(flags, opt, islinker) + + -- make an stub source file + local sourcefile = path.join(os.tmpdir(), "detect", "zig_has_flags.zig") + local objectdir = path.join(os.tmpdir(), "detect", "zig_has_flags") + if not os.isfile(sourcefile) then + io.writefile(sourcefile, "pub fn main() !void {}") + end + + -- init argv + local argv = table.join(flags, "--output-dir", objectdir, sourcefile) + if islinker then + table.insert(argv, 1, "build-exe") + else + table.insert(argv, 1, "build-obj") + end + + -- check it + return _try_running(opt.program, argv) +end + +-- has_flags(flags)? +-- +-- @param opt the argument options, e.g. {toolname = "", program = "", programver = "", toolkind = "[cc|cxx|ld|ar|sh|gc|rc|dc|mm|mxx]"} +-- +-- @return true or false +-- +function main(flags, opt) + + -- is linker? + local islinker = _islinker(flags, opt) + + -- attempt to check it from the argument list + if _check_from_arglist(flags, opt, islinker) then + return true + end + + -- try running to check it + return _check_try_running(flags, opt, islinker) +end + diff --git a/xmake/platforms/bsd/xmake.lua b/xmake/platforms/bsd/xmake.lua index abc7b3766..ab474b725 100644 --- a/xmake/platforms/bsd/xmake.lua +++ b/xmake/platforms/bsd/xmake.lua @@ -50,7 +50,7 @@ platform("bsd") end) -- set toolchains - set_toolchains("envs", "gcc", "clang", "yasm", "nasm", "fasm", "cuda", "dlang", "go", "rust", "gfortran") + set_toolchains("envs", "gcc", "clang", "yasm", "nasm", "fasm", "cuda", "dlang", "go", "rust", "gfortran", "zig") -- set menu set_menu { diff --git a/xmake/platforms/linux/xmake.lua b/xmake/platforms/linux/xmake.lua index 874bcac7c..0d5e433d7 100644 --- a/xmake/platforms/linux/xmake.lua +++ b/xmake/platforms/linux/xmake.lua @@ -50,7 +50,7 @@ platform("linux") end) -- set toolchains - set_toolchains("envs", "cross", "gcc", "clang", "yasm", "nasm", "fasm", "cuda", "dlang", "go", "rust", "gfortran") + set_toolchains("envs", "cross", "gcc", "clang", "yasm", "nasm", "fasm", "cuda", "dlang", "go", "rust", "gfortran", "zig") -- set menu set_menu { diff --git a/xmake/platforms/macosx/xmake.lua b/xmake/platforms/macosx/xmake.lua index 9df9aaab2..a6a60fef6 100644 --- a/xmake/platforms/macosx/xmake.lua +++ b/xmake/platforms/macosx/xmake.lua @@ -50,7 +50,7 @@ platform("macosx") end) -- set toolchains - set_toolchains("envs", "xcode", "clang", "gcc", "yasm", "nasm", "cuda", "dlang", "rust", "go", "gfortran") + set_toolchains("envs", "xcode", "clang", "gcc", "yasm", "nasm", "cuda", "dlang", "rust", "go", "gfortran", "zig") -- set menu set_menu { diff --git a/xmake/platforms/windows/xmake.lua b/xmake/platforms/windows/xmake.lua index c53613d71..38ad3746e 100644 --- a/xmake/platforms/windows/xmake.lua +++ b/xmake/platforms/windows/xmake.lua @@ -48,7 +48,7 @@ platform("windows") end) -- set toolchains - set_toolchains("msvc", "clang", "yasm", "nasm", "cuda", "dlang", "rust", "go", "gfortran") + set_toolchains("msvc", "clang", "yasm", "nasm", "cuda", "dlang", "rust", "go", "gfortran", "zig") -- set menu set_menu { diff --git a/xmake/rules/zig/xmake.lua b/xmake/rules/zig/xmake.lua new file mode 100644 index 000000000..a33feef06 --- /dev/null +++ b/xmake/rules/zig/xmake.lua @@ -0,0 +1,40 @@ +--!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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +-- define rule: zig.build +rule("zig.build") + set_sourcekinds("zc") + on_build_files("private.action.build.object", {batch = true}) + +-- define rule: zig +rule("zig") + + -- add build rules + add_deps("zig.build") + + -- inherit links and linkdirs of all dependent targets by default + add_deps("utils.inherit.links") + + -- support `add_files("src/*.o")` and `add_files("src/*.a")` to merge object and archive files to target + add_deps("utils.merge.object", "utils.merge.archive") + + -- we attempt to extract symbols to the independent file and + -- strip self-target binary if `set_symbols("debug")` and `set_strip("all")` are enabled + add_deps("utils.symbols.extract") diff --git a/xmake/toolchains/zig/xmake.lua b/xmake/toolchains/zig/xmake.lua new file mode 100644 index 000000000..3c902352b --- /dev/null +++ b/xmake/toolchains/zig/xmake.lua @@ -0,0 +1,50 @@ +--!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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +-- define toolchain +toolchain("zig") + + -- set homepage + set_homepage("https://ziglang.org/") + set_description("Zig Programming Language Compiler") + + -- set toolset + set_toolset("zc", "$(env ZC)", "zig") + set_toolset("zcar", "$(env ZC)", "zig") + set_toolset("zcld", "$(env ZC)", "zig") + set_toolset("zcsh", "$(env ZC)", "zig") + + -- on load + on_load(function (toolchain) + local march + if toolchain:is_plat("macosx") then + -- FIXME + --march = toolchain:is_arch("x86") and "i386-macosx-gnu" or "x86_64-macosx-gnu" + elseif toolchain:is_plat("linux") then + march = toolchain:is_arch("x86") and "i386-linux-gnu" or "x86_64-linux-gnu" + elseif toolchain:is_plat("windows") then + march = toolchain:is_arch("x86") and "i386-windows-msvc" or "x86_64-windows-msvc" + end + if march then + toolchain:add("zcflags", "-target", march) + toolchain:add("zcldflags", "-target", march) + toolchain:add("zcshflags", "-target", march) + end + end) |
