summaryrefslogtreecommitdiff
path: root/xmake/modules/package/tools/msbuild.lua
blob: 633ce4e149b75f841b6327893695e9b8a481c735 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
--!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        msbuild.lua
--

-- imports
import("core.base.option")
import("core.project.config")
import("core.project.project")
import("core.tool.toolchain")
import("lib.detect.find_tool")
import("private.utils.upgrade_vsproj")
import("private.utils.toolchain", {alias = "toolchain_utils"})

-- get the number of parallel jobs
function _get_parallel_njobs(opt)
    return opt.jobs or option.get("jobs") or tostring(os.default_njob())
end

-- get msvc
function _get_msvc(package)
    local msvc = package:toolchain("msvc")
    assert(msvc:check(), "vs not found!") -- we need to check vs envs if it has been not checked yet
    return msvc
end

-- get msvc run environments
function _get_msvc_runenvs(package)
    return os.joinenvs(_get_msvc(package):runenvs())
end

-- get vs arch
function _get_vsarch(package)
    local arch = package:arch()
    if arch == 'x86' or arch == 'i386' then return "Win32" end
    if arch == 'x86_64' then return "x64" end
    if arch:startswith('arm64') then return "ARM64" end
    if arch:startswith('arm') then return "ARM" end
    return arch
end

-- get configs
function _get_configs(package, configs, opt)
    local jobs = _get_parallel_njobs(opt)
    configs = configs or {}
    local configs_str = string.serialize(configs, {indent = false, strip = true})
    table.insert(configs, "-nologo")
    table.insert(configs, (jobs ~= nil and format("-m:%d", jobs) or "-m"))
    if not configs_str:find("p:Configuration=", 1, true) then
        table.insert(configs, "-p:Configuration=" .. (package:is_debug() and "Debug" or "Release"))
    end
    if not configs_str:find("p:Platform=", 1, true) then
        table.insert(configs, "-p:Platform=" .. _get_vsarch(package))
    end
    if not configs_str:find("p:PlatformToolset=", 1, true) then
        local vs_toolset = toolchain_utils.get_vs_toolset_ver(_get_msvc(package):config("vs_toolset") or config.get("vs_toolset"))
        if vs_toolset then
            table.insert(configs, "/p:PlatformToolset=" .. vs_toolset)
        end
    end
    if project.policy("package.msbuild.multi_tool_task") or package:policy("package.msbuild.multi_tool_task") then
        table.insert(configs, "/p:UseMultiToolTask=true")
        table.insert(configs, "/p:EnforceProcessCountAcrossBuilds=true")
        if jobs then
            table.insert(configs, format("/p:MultiProcMaxCount=%d", jobs))
        end
    end
    return configs
end

-- get the build environments
function buildenvs(package, opt)
    return _get_msvc_runenvs(package)
end

-- build package
function build(package, configs, opt)
    opt = opt or {}

    -- pass configurations
    local argv = {}
    for name, value in pairs(_get_configs(package, configs, opt)) do
        value = tostring(value):trim()
        if value ~= "" then
            if type(name) == "number" then
                table.insert(argv, value)
            else
                table.insert(argv, name .. "=" .. value)
            end
        end
    end

    -- upgrade vs solution file?
    -- @see https://github.com/xmake-io/xmake/issues/3871
    if opt.upgrade then
        local msvc = _get_msvc(package)
        for _, value in ipairs(opt.upgrade) do
            upgrade_vsproj.upgrade(value, table.join(opt, {msvc = msvc}))
        end
    end

    -- do build
    local envs = opt.envs or buildenvs(package, opt)
    local msbuild = find_tool("msbuild", {envs = envs})
    os.vrunv(msbuild.program, argv, {envs = envs})
end