Search code examples
premake

How to get current state in premake5?


In my Premake5 script I'm implementing a function that will return a name of a boost library depending on the current setup (you'll have -gd if it's a debug configuration, -mt if you want multithreading and so on). On my first try I got this:

name = "boost_" .. name
...
filter "configurations:Debug*"
    name = name .. "-gd"
...
links { name }

which is obviously incorrect: -gd will be appended to the name whether we're evaluating a debug configuration or not. Something akin to:

name = "boost_" .. name
...
if (CONFIGURATION.MATCHES_FILTER("Debug*"))
    name = name .. "-gd"
...
links { name }

would make it work, but I can't find a means to easily access the current configuration. There is configuration().current but it's undocumented and doesn't seem to be "the way it should be done", hence could stop working in future premake releases.

I could do:

name = "boost_" .. name
...
filter "configurations:Debug*"
    links { name .. "-gd" }
filter "configurations:Release*"
    links { name }

but this approach would make it problematic if the name could contain multiple variables accessible only through "filter".

Is it at all possible to access the current premake state in a standard (i.e. non-hacky) manner? Or is the latter (more declarative, I guess) way preferred?


EDIT FOR CLARITY:

  1. The main question is: is it possible to use premake's state (e.g. the current configuration name) "in lua" (e.g. in an if expression)? I.e. what do I put as EXPRESSION_HERE to make the code below work:

    if (EXPRESSION_HERE) then
        print("Executed only in Debug* configurations");
    end
    
  2. Rationale:

Boost libraries are named differently depending on the configuration they were built with. Additionally, they have different names under windows and linux.

  • boost_atomic-vc141-mt-1_64.lib is Boost Atomic with multithreading built with Visual Studio 1.41 toolset from Boost 1.64 dll's .lib companion,
  • libboost_prg_exec_monitor-vc141-mt-gd-1_64.lib is Boost PrgExecMonitor with multithreading and debug symbols built with Visual Studio 1.41 toolset from Boost 1.64 static lib,
  • libboostt_prg_exec_monitor-mt-gd.lib would be the same as above under Linux (AFAIR)

It feels natural for me to construct the final library name as a series of ifs adding to the name under specific circumstances (e.g. adding -gd if we want debug symbols). I'm aware this is possible using tokens and via string concatenation under the filter but is it the only way? If it's the best way then why?


Solution

  • Tokens are the way to apply this sort of logic.

    filter "configurations:Debug*"
       links { "boost_%{cfg.name}-gd" }
    

    As to your comment:

    I want a string containing the current configuration name, or the current project's output file path etc so that I could then call a lua function with it (e.g. os.copyfile)

    That is not possible, as there is no such thing as a "current" configuration while your script is being run. That only becomes possible after your script has completed and the target file(s) are being generated.

    I'll try to put together an example, but to copy files you'll want to look at postbuildcommands and command tokens.