summaryrefslogtreecommitdiff
path: root/xmake/modules/utils/platform/gnu2mslib.lua
blob: 183db7248addc4225587cad9c3a0e17ba506568e (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
--!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        gnu2mslib.lua
--

-- imports
import("core.base.option")
import("core.base.hashset")
import("core.project.config")
import("core.tool.toolchain")
import("lib.detect.find_tool")

-- get .def file path from gnulib
function _get_defpath_from_gnulib(gnulib, msvc)

    -- check
    assert(os.isfile(gnulib), "%s not found!", gnulib)

    -- get dumpbin.exe
    local dumpbin = assert(find_tool("dumpbin", {envs = msvc:runenvs()}), "gnu2mslib(): dumpbin.exe not found!")

    -- get symbols
    local symbols_exported = hashset.new()
    local symbols = try {function() return os.iorunv(dumpbin.program, {"/linkermember", gnulib}) end}
    if symbols then
        local symbols_count
        local symbols_index = 0
        for _, line in ipairs(symbols:split('\n', {plain = true})) do
            if symbols_count == nil then
                symbols_count = line:match("(%d+) public symbols")
                if symbols_count then
                    symbols_count = tonumber(symbols_count)
                end
            else
                local symbol = line:match("%s+[%dABCDEF]+%s+(.+)")
                if symbol then
                    if not symbol:startswith("__") and not symbol:startswith("?") then
                        symbols_exported:insert(symbol)
                    end
                    symbols_index = symbols_index + 1
                    if symbols_index >= symbols_count then
                        break
                    end
                end
            end
        end
    end

    -- generate .def file
    if symbols_exported:size() > 0 then
        local defpath = os.tmpfile() .. ".def"
        local file = io.open(defpath, "w")
        file:print("EXPORTS")
        for _, symbol in ipairs(symbols_exported) do
            file:print("%s", symbol)
        end
        file:close()
        return defpath
    end
end

-- convert mingw/gnu xxx.dll.a to msvc xxx.lib
--
-- gnu2mslib(mslib, gnulib_or_defpath, {arch = "x64", dllname = "foo.dll"}
--
-- @see https://github.com/xmake-io/xmake/issues/1181
--
function main(mslib, gnulib_or_defpath, opt)

    -- check
    opt = opt or {}
    assert(is_host("windows"), "we can only run gnu2mslib() on windows!")
    assert(mslib and gnulib_or_defpath, "invalid input parameters, usage: gnu2mslib(mslib, gnulib_or_defpath, {arch = \"x64\", dllname = \"foo.dll\"})")

    -- get msvc toolchain
    local msvc = toolchain.load("msvc", {plat = opt.plat, arch = opt.arch})
    if not msvc:check() then
        raise("we can not get msvc toolchain!")
    end

    -- get lib.exe
    local libtool = assert(find_tool("lib", {envs = msvc:runenvs()}), "gnu2mslib(): lib.exe not found!")

    -- get dll name
    local dllname = opt.dllname or path.basename(mslib) .. ".dll"

    -- get def file path
    local defpath = gnulib_or_defpath:endswith(".def") and gnulib_or_defpath or _get_defpath_from_gnulib(gnulib_or_defpath, msvc)
    assert(defpath and os.isfile(defpath), "gnu2mslib(): convert failed, cannot get .def file!")

    -- generate mslib from gnulib
    os.vrunv(libtool.program, {"/machine:" .. opt.arch, "/def:" .. defpath, "/name:" .. path.filename(dllname), "/out:" .. mslib}, {envs = msvc:runenvs()})

    -- remove temporary .def file
    if not gnulib_or_defpath:endswith(".def") then
        os.rm(defpath)
    end
end