预制中配置类型对变量的专门化

时间:2021-11-24 17:05:32

I'm using premake5 with Visual Studio 2013 and try to link a third-party library to my project. Debug version of this library has the "d" suffix as usual. This is the sample script.

我在Visual Studio 2013中使用premake5并尝试将第三方库链接到我的项目。该库的调试版本具有通常的“d”后缀。这是示例脚本。

solution "MySln"
    language "C++"
    configurations { "debug", "release" }

    d = ""

    configuration "debug"
        d = "d"

    configuration "release"
        d = ""

    libicu = { "icuin%{d}", "icuuc%{d}" }

project "core"
    kind "SharedLib"
    location "core"
    links(libicu)
    files { "core/**.h", "core/**.cpp" }

But this script links in both configurations (debug and release) icuin.lib and icuuc.lib. If I remove d = "" assignment in configuration "release" block then the both configurations link icuind.lib and icuucd.lib libraries. It looks as if premake uses the latest seen definition of the variable in resolving of %{d} placeholder. How can I change my script to obtain the correct behavior? Thanks!

但是这个脚本在两个配置(调试和发布)icuin.lib和icuuc.lib中都有链接。如果我在配置“release”块中删除d =“”赋值,则两个配置都链接icuind.lib和icuucd.lib库。看起来premake在解析%{d}占位符时使用了最新的变量定义。如何更改脚本以获取正确的行为?谢谢!

1 个解决方案

#1


You can't mix variable assignments (which are evaluated at script evaluation time) and Premake configurations (which are assembled after all project scripts are run) like that.

您不能混合变量赋值(在脚本评估时评估)和Premake配置(在所有项目脚本运行后汇编)。

One solution could be:

一种解决方案可能是:

solution "MySln"
   language "C++"
   configurations { "debug", "release" }

   filter "configurations:debug"
      targetextension "d"

project "core"
   kind "SharedLib"
   location "core"
   files { "core/**.h", "core/**.cpp" }

   filter "configurations:debug"
      links { "icuind", "icuucd" }
   filter "configurations:release"
      links { "icuin", "icuuc" }

If you have lots of libraries or do this often you can use a function for it:

如果您有很多库或经常这样做,您可以使用它的功能:

project "core"
   links_d { "icuin", "icuuc" }

function links_d(value)
   filter("configurations:debug")
   for i = 1, #value do
       links (value[i] .. "d")
   end
   filter("configurations:release")
   links (value)
   filter("*")
end

#1


You can't mix variable assignments (which are evaluated at script evaluation time) and Premake configurations (which are assembled after all project scripts are run) like that.

您不能混合变量赋值(在脚本评估时评估)和Premake配置(在所有项目脚本运行后汇编)。

One solution could be:

一种解决方案可能是:

solution "MySln"
   language "C++"
   configurations { "debug", "release" }

   filter "configurations:debug"
      targetextension "d"

project "core"
   kind "SharedLib"
   location "core"
   files { "core/**.h", "core/**.cpp" }

   filter "configurations:debug"
      links { "icuind", "icuucd" }
   filter "configurations:release"
      links { "icuin", "icuuc" }

If you have lots of libraries or do this often you can use a function for it:

如果您有很多库或经常这样做,您可以使用它的功能:

project "core"
   links_d { "icuin", "icuuc" }

function links_d(value)
   filter("configurations:debug")
   for i = 1, #value do
       links (value[i] .. "d")
   end
   filter("configurations:release")
   links (value)
   filter("*")
end