summaryrefslogtreecommitdiff
path: root/make/msvc_crt.lua
blob: 70523827bb278724dbfe927b08cf34ce12d5e365 (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
require 'bee'
local sp = require 'bee.subprocess'
local fs = require 'bee.filesystem'
local registry = require 'bee.registry'

local vswhere = fs.path(os.getenv('ProgramFiles(x86)')) / 'Microsoft Visual Studio' / 'Installer' / 'vswhere.exe'

local function strtrim(str)
    return str:gsub("^%s*(.-)%s*$", "%1")
end

local InstallDir = (function ()
    local process = assert(sp.spawn {
        vswhere,
        '-latest',
        '-products', '*',
        '-requires', 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64',
        '-property', 'installationPath',
        stdout = true,
    })
    local result = strtrim(process.stdout:read 'a')
    process.stdout:close()
    process:wait()
    assert(result ~= "", "can't find msvc.")
    return fs.path(result)
end)()

local RedistVersion = (function ()
    local verfile = InstallDir / 'VC' / 'Auxiliary' / 'Build' / 'Microsoft.VCRedistVersion.default.txt'
    local f = assert(io.open(verfile:string(), 'r'))
    local r = f:read 'a'
    f:close()
    return strtrim(r)
end)()

local function crtpath(platform)
    return InstallDir / 'VC' / 'Redist' / 'MSVC' / RedistVersion / platform / 'Microsoft.VC141.CRT'
end

local function sdkpath()
    local reg = registry.open [[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Kits\Installed Roots]]
    return fs.path(reg.KitsRoot10)
end

local function ucrtpath(platform)
    return sdkpath() / 'Redist' / 'ucrt' / 'DLLs' / platform
end

return function (platform, target)
    fs.create_directories(target)
    fs.copy_file(crtpath(platform) / 'msvcp140.dll', target / 'msvcp140.dll', true)
    fs.copy_file(crtpath(platform) / 'vcruntime140.dll', target / 'vcruntime140.dll', true)
    for dll in ucrtpath(platform):list_directory() do
        fs.copy_file(dll, target / dll:filename(), true)
    end
end