Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add windows mfc rule #216

Merged
merged 9 commits into from Sep 6, 2018
30 changes: 30 additions & 0 deletions xmake/rules/winsdk/mfc/env/xmake.lua
@@ -0,0 +1,30 @@
--!A cross-platform build utility based on Lua
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you 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 - 2018, TBOOX Open Source Group.
--
-- @author xigal
-- @file xmake.lua
--

-- define rule: mfc.env
rule("win.sdk.mfc.env")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个改成 win.sdk.mfc.env吧

-- FIXME: before load need check of vs's minverion, if defined
--before_load(function (target)
--end)
107 changes: 107 additions & 0 deletions xmake/rules/winsdk/mfc/mfc.lua
@@ -0,0 +1,107 @@
--!A cross-platform build utility based on Lua
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you 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 - 2018, TBOOX Open Source Group.
--
-- @author xigal
-- @file xmake.lua
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

author 可以标注下你的名字 😄

--

-- remove exists md or mt
function _mfc_remove_mt_md_flags(target, flagsname)
local flags = target:get(flagsname)
for key, flag in pairs(table.wrap(flags)) do
flag = flag:lower():trim()
if flag:find("^[/%-]?mt[d]?$") or flag:find("^[/%-]?md[d]?$") then
flags[key] = nil
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处的flags遍历内部直接改写,也处理下吧,我觉得还是有问题的。。

Copy link
Contributor Author

@xigalto xigalto Sep 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

额知道了,flags并没有被转化,转化的地方弄错了,明天去提交下

Copy link
Member

@waruqi waruqi Sep 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flags还在遍历过程中,去改flags的元素会有隐患,最好通过一个临时table先存储下修改后的结果,再到循环外更新整个flags

改成,

    local tmp = {} -- 新建个tmp数组存储新的结果
    for _, flag in ipairs(table.wrap(flags)) do -- 此处是array数组遍历,得用ipairs
        flag = flag:lower():trim()
        if not flag:find("^[/%-]?mt[d]?$") and not flag:find("^[/%-]?md[d]?$") then
            table.insert(tmp, flag)
        end
    end
    flags = tmp

而且flags是个array不是dictionary, 通过flags[] = nil 方式去设置nil,如果之后的值非nil,就被截断了,你可以验证下:

$ xmake lua
> a = {1, 2, 3, nil, 5, 6}
> #a  -- 这个时候a数组的长度只有3,即使通过ipairs去for循环遍历,也到3就终止了,5跟6就被丢弃了

end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处可通过 table.wrap(flags) 跟table的处理进行合并统一处理

target:set(flagsname, flags)
end

-- remove exists settings
function _mfc_remove_flags(target)
local ldflags = target:get("ldflags")
for key, ldflag in pairs(table.wrap(ldflags)) do
ldflag = ldflag:lower():trim()
if ldflag:find("[/%-]subsystem:") then
ldflags[key] = nil
break
end
end
target:set("ldflags", ldflags)

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

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

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

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

-- get application entry
function mfc_application_entry(target)
local defines = target:get("defines")
for key, define in pairs(defines) do
define = define:lower():trim()
if define:find("^[_]?unicode$") then
return "-entry:wWinMainCRTStartup"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

带unicode字样的macro defines说不定会有误判,还是改成 完整匹配 判断吧, define:trim() strip调用空格后判断就行了

        define = define:lower():trim()
        if define == "unicode" or define == "_unicode" then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已调整,的确不太严谨,其他几个flag也加入了校验

end
end
return "-entry:WinMainCRTStartup"
end

-- apply shared mfc settings
function mfc_shared(target)

-- remove some exists flags
_mfc_remove_flags(target)

-- add flags
target:add("ldflags", "-subsystem:windows", {force = true})

-- set runtimelibrary
target:add("cxflags", ifelse(is_mode("debug"), "-MDd", "-MD"))
target:add("defines", "AFX", "_AFXDLL")
end

-- apply static mfc settings
function mfc_static(target)

-- remove some exists flags
_mfc_remove_flags(target)

-- add flags
target:add("ldflags", "-subsystem:windows", {force = true})
target:add("ldflags", "-force", {force = true})

-- set runtimelibrary
target:add("cxflags", ifelse(is_mode("debug"), "-MTd", "-MT"))
end
81 changes: 81 additions & 0 deletions xmake/rules/winsdk/mfc/xmake.lua
@@ -0,0 +1,81 @@
--!A cross-platform build utility based on Lua
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you 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 - 2018, TBOOX Open Source Group.
--
-- @author xigal
-- @file xmake.lua
--

-- define rule: shared
rule("win.sdk.mfc.shared")

-- add mfc base rule
add_deps("win.sdk.mfc.env")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

通用的环境预加载,可以通过 加个 win.sdk.mfc.env 的依赖规则,优先执行:

rule("win.sdk.mfc.env")
     -- FIXME: before load need check of vs's minverion, if defined
    --before_load(function (target)
    --end)

rule("win.sdk.mfcapp.shared")

    add_deps("win.sdk.mfc.env")

     -- after load
    after_load(function (target)
         -- apply mfc settings
        import("mfc").mfc_shared_app(target)        
    end)

 -- define rule: static mfcapp
rule("win.sdk.mfcapp.static")

    add_deps("win.sdk.mfc.env")

     -- after load
    after_load(function (target)
         -- apply mfc settings
        import("mfc").mfc_static_app(target)
    end)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是想这样弄的,哈哈,就是当时没发现rule怎么继承另外的rule

-- after load
after_load(function (target)

-- apply mfc settings
import("mfc").mfc_shared(target)
end)

-- define rule: static
rule("win.sdk.mfc.static")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

规则名也改下吧,改成 win.sdk.mfc.static_appwin.sdk.mfc.shared_app吧,之后还能扩展支持mfc的静态库,动态库 win.sdk.mfc.static, win.sdk.mfc.shared

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是这样计划的 win.sdk.mfcdll.static,win.sdk.mfcdll.shared,格式比较规整点

-- add mfc base rule
add_deps("win.sdk.mfc.env")

-- after load
after_load(function (target)

-- apply mfc settings
import("mfc").mfc_static(target)
end)

-- define rule: sharedcapp
rule("win.sdk.mfc.shared_app")

-- add mfc base rule
add_deps("win.sdk.mfc.shared")

-- after load
after_load(function (target)

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

-- set entry
target:add("ldflags", import("mfc").mfc_application_entry(target), {force = true})
end)

-- define rule: staticapp
rule("win.sdk.mfc.static_app")

-- add mfc base rule
add_deps("win.sdk.mfc.static")

-- after load
after_load(function (target)

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

-- set entry
target:add("ldflags", import("mfc").mfc_application_entry(target), {force = true})
end)
3 changes: 1 addition & 2 deletions xmake/rules/winsdk/xmake.lua
Expand Up @@ -51,5 +51,4 @@ rule("win.sdk.application")
target:add("links", "kernel32", "user32", "gdi32", "winspool", "comdlg32", "advapi32")
target:add("links", "shell32", "ole32", "oleaut32", "uuid", "odbc32", "odbccp32", "comctl32")
target:add("links", "cfgmgr32", "comdlg32", "setupapi", "strsafe", "shlwapi")
end)

end)