summaryrefslogtreecommitdiff
path: root/xmake/rules/winsdk/mfc/mfc.lua
blob: 5bcd41072fcef9fe76ed07e3d172854bd36fa461 (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
--!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      xigal, ruki
-- @file        xmake.lua
--

-- remove exists md or mt
function _remove_mt_md_flags(target, flagsname)
    local flags = table.wrap(target:get(flagsname))
    for i = #flags, 1, -1 do
        flag = flags[i]:lower():trim()
        if flag:find("^[/%-]?mt[d]?$") or flag:find("^[/%-]?md[d]?$") then
            table.remove(flags, i)
        end
    end
    target:set(flagsname, flags)
end

-- remove exists settings
function _remove_flags(target)
    local ldflags = table.wrap(target:get("ldflags"))
    for i = #ldflags, 1, -1 do
        ldflag = ldflags[i]:lower():trim()
        if ldflag:find("[/%-]subsystem:") then
            table.remove(ldflags, i)
            break
        end
    end
    target:set("ldflags", ldflags)

    -- remove defines MT MTd MD MDd
    local defines = table.wrap(target:get("defines"))
    for i = #defines, 1, -1 do
        define = defines[i]:lower():trim()
        if define:find("^[/%-]?mt[d]?$") or define:find("^[/%-]?md[d]?$") then
            table.remove(defines, i)
        end
    end
    target:set("defines", defines)

    -- remove c /MD,/MT
    _remove_mt_md_flags(target, "cflags")

    -- remove c,cpp /MD,/MT
    _remove_mt_md_flags(target, "cxflags")

    -- remove cpp /MD,/MT
    _remove_mt_md_flags(target, "cxxflags")
end

-- apply mfc library settings
function library(target, kind)

    -- set kind: static/shared
    target:set("kind", kind)

    -- set runtime library
    if kind == "static" then
        target:add("cxflags", is_mode("debug") and "-MTd" or "-MT")
    else
        target:add("cxflags", is_mode("debug") and "-MDd" or "-MD")
        target:add("defines", "AFX", "_AFXDLL")
    end
end

-- apply mfc application settings
function application(target, mfc_kind)

    -- set kind: binary
    target:set("kind", "binary")

    -- remove some exists flags
    _remove_flags(target)

    -- set windows subsystem
    if not target:values("windows.subsystem") then
        target:values_set("windows.subsystem", "windows")
    end

    -- forces a link to complete even with unresolved symbols
    if mfc_kind == "static" then
        target:add("ldflags", "-force", {force = true})
    end

    -- set runtime library
    if mfc_kind == "static" then
        target:add("cxflags", is_mode("debug") and "-MTd" or "-MT")
    else
        target:add("cxflags", is_mode("debug") and "-MDd" or "-MD")
        target:add("defines", "AFX", "_AFXDLL")
    end

    -- set startup entry
    local unicode = false
    for _, define in ipairs(target:get("defines")) do
        local define = define:lower():trim()
        if define:find("^[_]?unicode$") then
            unicode = true
            break
        end
    end
    target:add("ldflags", unicode and "-entry:wWinMainCRTStartup" or "-entry:WinMainCRTStartup", {force = true})
end