Premake

警告

此功能是实验性的,可能会发生重大更改。有关更多信息,请参阅 Conan 稳定性 部分。

Premake 构建助手是 Premake 命令行调用的包装器。它将抽象项目配置和构建命令。

该助手旨在在 conanfile.py build() 方法中使用,以便在 Conan 直接构建包(create、install)时自动调用 Premake 命令

使用示例

from conan.tools.premake import Premake

class Pkg(ConanFile):
    settings = "os", "compiler", "build_type", "arch"

    # The PremakeToolchain generator is always needed to use premake helper
    generators = "PremakeToolchain"

    def build(self):
        p = Premake(self)

        # Set the main Lua configuration file (default: premake5.lua)
        p.luafile = "myproject.lua"

        # Pass custom arguments to Premake (translates to --{key}={value})
        p.arguments["myarg"] = "myvalue"

        # Automatically determines the correct action:
        # - For MSVC, selects vs<version> based on the compiler version
        # - Defaults to "gmake" for other compilers
        # p.configure() will run: premake5 --file=myproject.lua <action> --{key}={value} ...
        p.configure()
        # p.build() will invoke proper compiler depending on action (automatically detected by profile)
        p.build("HelloWorld.sln")

参考

class Premake(conanfile)

当构建包时,此类会调用 Premake 命令。请注意,此工具应与 PremakeToolchain 生成器一起使用。

此 premake 生成器仅与 premake5 兼容。

参数:

conanfile< ConanFile 对象 > 当前配方对象。始终使用 self

luafile

指向根 premake5 lua 文件(默认值为 premake5.lua)的路径

arguments

键值对。将转换为“–{key}={value}”

configure()

运行 premake5 <action> [FILE],这将根据 action 生成相应的构建脚本。

build(workspace, targets=None, configuration=None, msbuild_platform=None)

根据操作,此方法将运行 msbuildmake,并使用 N_JOBS。您可以通过配置行 tools.build:jobs=N_JOBS 在您的 profile [conf] 部分中指定 N_JOBS

参数:
  • workspacestr 指定要编译的解决方案(仅由 MSBuild 使用)。

  • targetsList[str] 声明要构建的项目(None 构建所有项目)。

  • configurationstr 指定构建类型配置,默认为 build_type(“Release”或“Debug”),但允许设置自定义配置类型。

  • msbuild_platformstr 指定内部 MSBuild 生成器的平台(仅由 MSBuild 使用)。

conf

Premake 构建助手受这些 [conf] 变量影响

  • tools.build:verbosity,它接受 quietverbose 中的一个,并在 Premake.configure() 中设置 --quiet 标志

  • tools.compilation:verbosity,它接受 quietverbose 中的一个,并在 Premake.build() 中设置 --verbose 标志

额外配置

默认情况下,典型的 Premake 配置是 ReleaseDebug。这些配置可能因使用的 Premake 脚本而异。

例如,

workspace "MyProject"
    configurations { "Debug", "Release", "DebugDLL", "ReleaseDLL" }

如果您希望使用不同于 ReleaseDebug 的配置,您可以从 Premake 生成器覆盖配置。

如果项目也有依赖项,您还需要相应地覆盖 PremakeDeps 生成器的 configuration 属性,并使用相同的值。

class MyRecipe(Conanfile):
    ...
    def _premake_configuration(self):
        return str(self.settings.build_type) + ("DLL" if self.options.shared else "")

    def generate(self):
        deps = PremakeDeps(self)
        deps.configuration = self._premake_configuration
        deps.generate()
        tc = PremakeToolchain(self)
        tc.generate()

    def build(self):
        premake = Premake(self)
        premake.configure()
        premake.build(workspace="MyProject", configuration=self._premake_configuration)