Skip to content

Scopes and Inheritance

Manu Evans edited this page Apr 7, 2015 · 5 revisions

As you may have noticed from the previous samples, Premake uses a pseudo-declarative syntax for specifying project information. You specify a scope (i.e. a solution or project) for the settings, and then the settings to be placed in that scope.

Scopes have a hierarchy: a global scope containing solutions, which in turn contains projects. Values placed into the outer scopes are inherited by the inner ones, so solutions inherit the values stored at the global scope, and projects inherit values stored in solutions.

-- global scope, all solutions will receive these values
defines { "GLOBAL" }

solution "MySolution"
  -- solution scope inherits the global scope; the list value
  -- will now be { "GLOBAL", "SOLUTION" }
  defines { "SOLUTION" }

project "MyProject"
  -- project scope inherits from its solution; the list value
  -- will now be { "GLOBAL", "SOLUTION", "PROJECT" }
  defines { "PROJECT" }

Sometimes it can be helpful to go back and add values to a previously declared scope. You can do this the same way you declared it in the first place: by calling solution() or project(), using the same name.

-- declare my solution
solution "MySolution"
  defines { "SOLUTION1" }

-- declare a project or two
project "MyProject"
  defines { "PROJECT" }

-- re-select my solution to add more settings, which will be inherited
-- by all projects in the solution
solution "MySolution"
  defines { "SOLUTION2" }  -- value is now { "SOLUTION1", "SOLUTION2" }

You can also select the parent or container of the current scope without having to know its name by using the special "*" name.

-- declare my solution
solution "MySolution"
  defines { "SOLUTION1" }

-- declare a project or two
project "MyProject"
  defines { "PROJECT" }

-- re-select my solution to add more settings
project "*"
  defines { "SOLUTION2" }  -- value is now { "SOLUTION1", "SOLUTION2" }

-- re-select the global scope
solution "*"

Think of the "*" as a wilcard meaning "all projects in my parent container" or "all solutions in the global scope".

Clone this wiki locally