diff options
| author | star9029 <[email protected]> | 2024-11-18 15:42:41 +0800 |
|---|---|---|
| committer | star9029 <[email protected]> | 2024-11-18 15:42:41 +0800 |
| commit | 830e6580042a1a84dae0a6b7ebb1a988814630db (patch) | |
| tree | f5ac7bd5e79ea6d9691e45aa3dee8aef85bebabe | |
| parent | 968471fe0c02ba62bb4dc7b27bb72397090e7697 (diff) | |
support clang
| -rw-r--r-- | xmake/toolchains/clang/check.lua | 69 | ||||
| -rw-r--r-- | xmake/toolchains/clang/load.lua | 107 | ||||
| -rw-r--r-- | xmake/toolchains/clang/xmake.lua | 10 |
3 files changed, 186 insertions, 0 deletions
diff --git a/xmake/toolchains/clang/check.lua b/xmake/toolchains/clang/check.lua new file mode 100644 index 000000000..6b242f469 --- /dev/null +++ b/xmake/toolchains/clang/check.lua @@ -0,0 +1,69 @@ +--!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 +-- + +import("detect.sdks.find_vstudio") +import("lib.detect.find_tool") +import("core.project.config") + +function _check_vc_build_tools(toolchain, sdkdir) + local opt = {} + opt.sdkdir = sdkdir + opt.vs_toolset = toolchain:config("vs_toolset") or config.get("vs_toolset") + opt.vs_sdkver = toolchain:config("vs_sdkver") or config.get("vs_sdkver") + + local vcvarsall = find_vstudio.find_build_tools(opt) + if not vcvarsall then + return + end + + local vcvars = vcvarsall[toolchain:arch()] + if vcvars and vcvars.PATH and vcvars.INCLUDE and vcvars.LIB then + -- save vcvars + toolchain:config_set("vcvars", vcvars) + toolchain:config_set("vcarchs", table.orderkeys(vcvarsall)) + toolchain:config_set("vs_toolset", vcvars.VCToolsVersion) + toolchain:config_set("vs_sdkver", vcvars.WindowsSDKVersion) + + -- check compiler + local paths + local pathenv = os.getenv("PATH") + if pathenv then + paths = path.splitenv(pathenv) + end + local clang = find_tool("clang", {version = true, force = true, paths = paths, envs = vcvars}) + if clang and clang.version then + cprint("checking for LLVM Clang C/C++ Compiler (%s) version ... ${color.success}%s", toolchain:arch(), clang.version) + end + return vcvars + end +end + +function main(toolchain) + + -- only for windows or linux (msvc-wine) + if not is_host("windows", "linux") then + return + end + + local sdkdir = toolchain:sdkdir() + if sdkdir then + return _check_vc_build_tools(toolchain, sdkdir) + end +end diff --git a/xmake/toolchains/clang/load.lua b/xmake/toolchains/clang/load.lua new file mode 100644 index 000000000..c2585f98e --- /dev/null +++ b/xmake/toolchains/clang/load.lua @@ -0,0 +1,107 @@ +--!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 +-- + +import("detect.sdks.find_vstudio") +import("detect.sdks.find_mingw") +import("core.project.config") + +-- add the given vs environment +function _add_vsenv(toolchain, name, curenvs) + + -- get vcvars + local vcvars = toolchain:config("vcvars") + if not vcvars then + return + end + + -- get the paths for the vs environment + local new = vcvars[name] + if new then + -- fix case naming conflict for cmake/msbuild between the new msvc envs and current environment, if we are running xmake in vs prompt. + -- @see https://github.com/xmake-io/xmake/issues/4751 + for k, c in pairs(curenvs) do + if name:lower() == k:lower() and name ~= k then + name = k + break + end + end + toolchain:add("runenvs", name, table.unwrap(path.splitenv(new))) + end +end + +function main(toolchain) + local target + if toolchain:is_arch("x86_64", "x64") then + target = "x86_64" + march = "-m64" + elseif toolchain:is_arch("i386", "x86") then + target = "i386" + march = "-m32" + elseif toolchain:is_arch("arm64") then + target = "arm64" + elseif toolchain:is_arch("arm") then + target = "arm" + end + + if toolchain:is_plat("windows") then + target = target .. "-windows-msvc" + + -- add vs environments + local expect_vars = {"PATH", "LIB", "INCLUDE", "LIBPATH"} + local curenvs = os.getenvs() + for _, name in ipairs(expect_vars) do + _add_vsenv(toolchain, name, curenvs) + end + for _, name in ipairs(find_vstudio.get_vcvars()) do + if not table.contains(expect_vars, name:upper()) then + _add_vsenv(toolchain, name, curenvs) + end + end + elseif toolchain:is_plat("mingw") then + target = target .. "-windows-gnu" + + local mingw + for _, package in ipairs(toolchain:packages()) do + local installdir = package:installdir() + if installdir and os.isdir(installdir) then + mingw = find_mingw(installdir, {verbose = true, cross = toolchain:cross()}) + if mingw then + break + end + end + end + if not mingw then + mingw = find_mingw(toolchain:config("mingw") or config.get("mingw"), {verbose = true, bindir = toolchain:bindir(), cross = toolchain:cross()}) + end + + if mingw and mingw.sdkdir then + local sysroot = "--sysroot=" .. mingw.sdkdir + toolchain:add("cxflags", sysroot) + toolchain:add("ldflags", sysroot) + toolchain:add("shflags", sysroot) + end + end + + if target then + toolchain:add("cxflags", "-target", target) + toolchain:add("ldflags", "-target", target) + toolchain:add("shflags", "-target", target) + end +end diff --git a/xmake/toolchains/clang/xmake.lua b/xmake/toolchains/clang/xmake.lua index cc8da1d26..6add600ef 100644 --- a/xmake/toolchains/clang/xmake.lua +++ b/xmake/toolchains/clang/xmake.lua @@ -44,6 +44,13 @@ toolchain("clang" .. suffix) set_toolset("mrc", "llvm-rc") on_check(function (toolchain) + if toolchain:is_plat("windows") then + local result = import("check")(toolchain) + if result then + return result + end + end + return import("lib.detect.find_tool")("clang" .. suffix) end) @@ -64,6 +71,9 @@ toolchain("clang" .. suffix) if toolchain:is_plat("windows") then toolchain:add("runtimes", "MT", "MTd", "MD", "MDd") end + if toolchain:is_plat("windows", "mingw") then + import("load")(toolchain) + end end) end toolchain_clang() |
