|
| 1 | +--- |
| 2 | +title: Usages & Uses |
| 3 | +--- |
| 4 | + |
| 5 | +Defining how libraries of code are to be used is a crucial piece of any build system. Premake exposes this functionality via the `usage` and `uses` APIs. A `usage` specifies a reusable configuration scope which can be later consumed by `uses`. |
| 6 | + |
| 7 | +Unlike other scopes, usages do not inherit their properties from their parent scopes. This is to ensure that consumers only receive the configuration explicitly specified by the usage block. |
| 8 | + |
| 9 | +```lua |
| 10 | +project "A" |
| 11 | + defines { "A_PRIVATE_DEF" } |
| 12 | + usage "PUBLIC" |
| 13 | + defines { "A_PUBLIC_DEF" } |
| 14 | + |
| 15 | +project "B" |
| 16 | + uses { "A" } |
| 17 | + -- B will now contain the defines { "A_PUBLIC_DEF" } |
| 18 | +``` |
| 19 | + |
| 20 | +Usage containers can be provided with any name, however Premake provides three names of power. These words of power allow specifying the name of a project in the `uses` field rather than having to specify the exact name of the usage block. |
| 21 | + |
| 22 | +* `PRIVATE` specifies a usage block that will be applied only to the project defining the usage. This is the default behavior of properties defined outside of usage blocks in Premake. |
| 23 | +* `INTERFACE` specifies a usage block that will be applied only to any projects consuming the usage. |
| 24 | +* `PUBLIC` specifies a usage that will be applied to both the project defining the usage and any projects consuming the usage. |
| 25 | + |
| 26 | +Usages are not applied recursively by default. This is to match the existing Premake behaviors where everything is private by default. In order to apply usages recursively, usage blocks can be utilized to specify which usages should be propagated to the children. This chain can be applied indefinitely by specifying the usages that should be applied in each project's `PUBLIC` block. |
| 27 | + |
| 28 | +```lua |
| 29 | +project "A" |
| 30 | + usage "PUBLIC" |
| 31 | + defines { "A_PUBLIC_DEF" } |
| 32 | + |
| 33 | +project "B" |
| 34 | + usage "PUBLIC" |
| 35 | + uses { "A" } |
| 36 | + defines { "B_PUBLIC_DEF" } |
| 37 | + |
| 38 | +project "C" |
| 39 | + -- C will now contain the defines { "A_PUBLIC_DEF", "B_PUBLIC_DEF" } |
| 40 | + uses { "B" } |
| 41 | +``` |
| 42 | + |
| 43 | +Similarly, for forcing consumers to link or depend on your project, we can specify a usage block as follows: |
| 44 | + |
| 45 | +```lua |
| 46 | +project "MyLib" |
| 47 | + -- Force consumers to link MyLib, but do not force MyLib to link against it |
| 48 | + usage "INTERFACE" |
| 49 | + links { "MyLib" } |
| 50 | + |
| 51 | +project "MyExe" |
| 52 | + -- MyExe will now link against MyLib |
| 53 | + uses { "MyLib" } |
| 54 | +``` |
| 55 | + |
| 56 | +It's worth noting that `usage` blocks do not need to be named with a name of power. They can be specified and used as follows: |
| 57 | + |
| 58 | +```lua |
| 59 | +project "A" |
| 60 | + usage "MyCustomUsageName" |
| 61 | + defines { "HelloWorld" } |
| 62 | + |
| 63 | +project "B" |
| 64 | + uses { "MyCustomUsageName" } |
| 65 | +``` |
| 66 | + |
| 67 | +### See Also ### |
| 68 | + |
| 69 | +* [usage](usage.md) |
| 70 | +* [uses](uses.md) |
0 commit comments