diff options
| author | ruki <[email protected]> | 2020-07-09 22:06:03 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-07-09 06:36:19 +0800 |
| commit | 57c15ee42343b7ca4c9b5b860aff9a27b266ae3d (patch) | |
| tree | 50ef27e845e86b8e4dcc5154f7aa54f149a3deb6 | |
| parent | 519535ca82c093b011ccec2ba4c8378c117f72c0 (diff) | |
improve zig
| -rw-r--r-- | tests/projects/zig/shared_library/src/main.zig | 8 | ||||
| -rw-r--r-- | tests/projects/zig/shared_library/src/test.zig | 3 | ||||
| -rw-r--r-- | tests/projects/zig/shared_library/xmake.lua | 11 | ||||
| -rw-r--r-- | tests/projects/zig/static_library/src/main.zig | 4 | ||||
| -rw-r--r-- | xmake/core/tool/toolchain.lua | 7 | ||||
| -rw-r--r-- | xmake/modules/core/tools/zig.lua | 76 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/dmd/has_flags.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/go/has_flags.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/zig/has_flags.lua | 120 | ||||
| -rw-r--r-- | xmake/toolchains/zig/xmake.lua | 7 |
10 files changed, 236 insertions, 6 deletions
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 index fcfd5e0fa..cee64b6b7 100644 --- a/tests/projects/zig/static_library/src/main.zig +++ b/tests/projects/zig/static_library/src/main.zig @@ -1,6 +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"}); + try stdout.print("Hello, {} - {}!\n", .{"world", add(1, 1)}); } 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/modules/core/tools/zig.lua b/xmake/modules/core/tools/zig.lua index 1b41bff17..6e6969dbb 100644 --- a/xmake/modules/core/tools/zig.lua +++ b/xmake/modules/core/tools/zig.lua @@ -22,12 +22,83 @@ 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") + 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 @@ -40,7 +111,8 @@ function linkargv(self, objectfiles, targetkind, targetfile, flags) else raise("unknown target kind(%s)!", targetkind) end - table.join2(argv, flags, "--output-dir", path.directory(targetfile), "--name", path.basename(targetfile), "--object", objectfiles) + 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 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/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..3b1150afb --- /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 {\n}") + 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/toolchains/zig/xmake.lua b/xmake/toolchains/zig/xmake.lua index 2b67569a9..bae362a9b 100644 --- a/xmake/toolchains/zig/xmake.lua +++ b/xmake/toolchains/zig/xmake.lua @@ -33,4 +33,11 @@ toolchain("zig") -- on load on_load(function (toolchain) + local march + if is_plat("macosx") then + march = "x86_64-macosx-gnu" + end + if march then + toolchain:add("zcflags", "-target", march) + end end) |
