summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-07-09 00:40:35 +0800
committerruki <[email protected]>2020-07-08 22:03:34 +0800
commit519535ca82c093b011ccec2ba4c8378c117f72c0 (patch)
tree5e10220f230eb465b11f03564a4e2218a17cb29d
parentc8bb0d3edb89d1ca0d2f4615b949eab952421478 (diff)
add zig support
-rw-r--r--tests/projects/zig/console/src/main.zig6
-rw-r--r--tests/projects/zig/console/xmake.lua6
-rw-r--r--tests/projects/zig/static_library/src/main.zig6
-rw-r--r--tests/projects/zig/static_library/src/test.zig3
-rw-r--r--tests/projects/zig/static_library/xmake.lua11
-rw-r--r--xmake/languages/zig/api.lua78
-rw-r--r--xmake/languages/zig/check_main.lua40
-rw-r--r--xmake/languages/zig/load.lua34
-rw-r--r--xmake/languages/zig/xmake.lua127
-rw-r--r--xmake/modules/core/tools/zig.lua71
-rw-r--r--xmake/modules/detect/tools/find_zig.lua45
-rw-r--r--xmake/platforms/bsd/xmake.lua2
-rw-r--r--xmake/platforms/linux/xmake.lua2
-rw-r--r--xmake/platforms/macosx/xmake.lua2
-rw-r--r--xmake/platforms/windows/xmake.lua2
-rw-r--r--xmake/rules/zig/xmake.lua40
-rw-r--r--xmake/toolchains/zig/xmake.lua36
17 files changed, 507 insertions, 4 deletions
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/static_library/src/main.zig b/tests/projects/zig/static_library/src/main.zig
new file mode 100644
index 000000000..fcfd5e0fa
--- /dev/null
+++ b/tests/projects/zig/static_library/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/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/languages/zig/api.lua b/xmake/languages/zig/api.lua
new file mode 100644
index 000000000..53724dd2a
--- /dev/null
+++ b/xmake/languages/zig/api.lua
@@ -0,0 +1,78 @@
+--!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_dcflags"
+ , "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_dcflags"
+ , "option.add_ldflags"
+ , "option.add_arflags"
+ , "option.add_shflags"
+ , "option.add_rpathdirs"
+ -- package.add_xxx
+ , "package.add_links"
+ , "package.add_syslinks"
+ , "package.add_dcflags"
+ , "package.add_ldflags"
+ , "package.add_arflags"
+ , "package.add_shflags"
+ , "package.add_rpathdirs"
+ , "package.add_linkdirs"
+ , "package.add_includedirs"
+ -- toolchain.add_xxx
+ , "toolchain.add_links"
+ , "toolchain.add_syslinks"
+ , "toolchain.add_dcflags"
+ , "toolchain.add_ldflags"
+ , "toolchain.add_arflags"
+ , "toolchain.add_shflags"
+ , "toolchain.add_rpathdirs"
+ , "toolchain.add_linkdirs"
+ , "toolchain.add_includedirs"
+ }
+ _g.pathes =
+ {
+ -- target.add_xxx
+ "target.add_linkdirs"
+ , "target.add_includedirs"
+ -- option.add_xxx
+ , "option.add_linkdirs"
+ , "option.add_includedirs"
+ }
+
+ -- 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..ea9de1e86
--- /dev/null
+++ b/xmake/languages/zig/xmake.lua
@@ -0,0 +1,127 @@
+--!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 =
+ {
+ "config.includedirs"
+ , "target.symbols"
+ , "target.warnings"
+ , "target.optimize:check"
+ , "target.vectorexts:check"
+ , "target.includedirs"
+ , "toolchain.includedirs"
+ }
+ , 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" }
+ , {nil, "includedirs","kv", nil, "The Include Search Directories" }
+ }
+ }
+
diff --git a/xmake/modules/core/tools/zig.lua b/xmake/modules/core/tools/zig.lua
new file mode 100644
index 000000000..1b41bff17
--- /dev/null
+++ b/xmake/modules/core/tools/zig.lua
@@ -0,0 +1,71 @@
+--!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")
+
+-- init it
+function init(self)
+
+ -- init shflags
+ self:set("zcshflags", "-dynamic")
+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
+ table.join2(argv, flags, "--output-dir", path.directory(targetfile), "--name", path.basename(targetfile), "--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/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/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..2b67569a9
--- /dev/null
+++ b/xmake/toolchains/zig/xmake.lua
@@ -0,0 +1,36 @@
+--!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)
+ end)