summaryrefslogtreecommitdiff
path: root/xmake/toolchains/clang/load.lua
blob: 74b04e8672bee3d1cffd78ea8eb29efe78b9fe14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
--!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, Xmake Open Source Community.
--
-- @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
        if name == "INCLUDE" or name == "LIB" then
            toolchain:add("runenvs", name, table.concat(path.splitenv(new), ";"))
        else
            toolchain:add("runenvs", name, table.unwrap(path.splitenv(new)))
        end
    end
end

function main(toolchain, suffix)
    import("core.project.project")

    local target
    if toolchain:is_arch("x86_64", "x64") then
        target = "x86_64"
        march = "-m64"
    elseif toolchain:is_arch("i386", "x86", "i686") then
        target = "i686"
        march = "-m32"
    elseif toolchain:is_arch("arm64", "aarch64") then
        target = "aarch64"
    elseif toolchain:is_arch("arm") then
        target = "armv7"
    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

        if is_host("linux") or project.policy("build.optimization.lto") then
            toolchain:add("ldflags", "-fuse-ld=lld-link" .. suffix)
            toolchain:add("shflags", "-fuse-ld=lld-link" .. suffix)
        end
    elseif toolchain:is_plat("mingw") then
        target = target .. "-w64-windows-gnu"
    elseif toolchain:is_plat("linux") then
        target = target .. "-linux-gnu"
    end

    if target then
        toolchain:add("cxflags", "--target=" .. target)
        toolchain:add("asflags", "--target=" .. target)
        toolchain:add("ldflags", "--target=" .. target)
        toolchain:add("shflags", "--target=" .. target)
    end
end