Premake :https://github.com/premake/premake-core
Premake 是一个基于 Lua 脚本语言的构建系统工具,用于生成项目文件和构建脚本,能够帮助开发者快速创建和配置跨平台的项目。
使用 Premake,开发者可以通过编写简单的 Lua 脚本来描述项目的结构和构建选项,Premake 会根据这些脚本生成特定平台(如 Windows、Linux、Mac 等)的项目文件和构建脚本,例如 Visual Studio 的 .sln 文件、Makefile 或 Xcode 的 .xcodeproj 文件等。
下载最新的windows release版本,不需要自己编译
解压后,只需要其中的premake.exe文件,放在项目路径中
完整的使用教学可以参考wiki
-
tokens,列出了所有预定义的变量,供我们使用,用法类似于vs中项目设置里的宏(ProjectDir 、SolutionDir、ProjecName等等),不同的地方是vs中取值用
$()
,premake中用%{}
-
postbuildcommants,可以使用编译后命令来复制文件(如.dll)到.exe文件目录下
这是wiki的第一个premake使用示例
/* hello.c */
#include <stdio.h>
int main(void) {
puts("Hello, world!");
return 0;
}
在项目中创建一个文件 premake5.lua
workspace "HelloWorld" -- 解决方案名称
configurations {
"Debug", "Release" }
project "HelloWorld"
kind "ConsoleApp" -- 项目类型为可执行程序
language "C"
targetdir "bin/%{cfg.buildcfg}" -- 编译输出路径
files {
"**.h", "**.c" } -- 所有子文件夹中的所有.h .c文件,递归抓取
filter "configurations:Debug" -- 针对debug和release配置下的一些特定的设置
defines {
"DEBUG" }
symbols "On"
filter "configurations:Release"
defines {
"NDEBUG" }
optimize "On"
稍微复杂点的使用(用于根据源代码生成VS的.sln),编写lua脚本
- 假设解决方案名称为MySolution,其中有两个项目,第一个MyProject1 是生成动态链接库供第二个项目MyApp使用
workspace "MySolution" -- 解决方案名称
architecture "x64"
configurations
{
"Debug",
"Release",
"Dist"
}
-- 输出路径变量:类似于 debug-windows-x64
outputdir = "%{cfg.buildcfg}-%{cfg.system}-%{cfg.arcchitecture}"
project "MyProject1" -- 项目名称
location "MyProject1"
kind "SharedLib" -- DLL
language "C++"
targetdir ("bin/" .. outputdir .. "/%{prj.name}") -- lua的字符串拼接方式 ..
objdir ("bin-int/" .. outputdir .. "/%{prj.name}")
files -- 包含项目下所有的.h .cpp文件
{
"%{prj.name}/src/**.h",
"%{prj.name}/src/**.cpp"
}
include
{
"%{prj.name}/3rd/glfw/include"
}
filter "system:windows" --对特定的系统(windows\OS..)、配置(Debug/Release)、平台(x64 x86)的项目属性
cppdialect "C++17" --C++特性版本
staticruntime "On"
systemversion "10.0.22000.0" -- windowsSKD版本
defines
{
"PLATFORM_WINDOWS",
"BUILD_DLL"
}
postbuildcommands
{
-- 拷贝本项目的输出文件中的dll到指定文件夹中
("{COPY} %{cfg.buildtarget.relpath} ../bin/" .. outputdir .. "/MyApp")
}
filter "configurations:Debug"
defines "_DEBUG"
symbols "On"
filter "configurations:Release"
defines "_RELEASE"
optimize "On"
filter "configurations:Dist"
defines "_DIST"
optimize "On"
-- 可以过滤多个选项,一起配置
-- filter {"system:windows", "configurations:Release"}
-- buildoptions "/MT"
project "MyApp"
location "Sandbox"
kind "ConsoleApp"
language "C++"
targetdir ("bin/" .. outputdir .. "/%{prj.name}")
objdir ("bin-int/" .. outputdir .. "/%{prj.name}")
files -- 包含项目下所有的.h .cpp文件
{
"%{prj.name}/src/**.h",
"%{prj.name}/src/**.cpp"
}
include
{
"%{prj.name}/vendor/spdlog/include",
"MyProject1/src"
}
links --链接项目
{
"MyProject1"
}
filter "system:windows"
cppdialect "C++17"
staticruntime "On"
systemversion "10.0.22000.0"
defines
{
"PLATFORM_WINDOWS"
}
filter "configurations:Debug"
defines "_DEBUG"
symbols "On"
filter "configurations:Release"
defines "_RELEASE"
optimize "On"
filter "configurations:Dist"
defines "_DIST"
optimize "On"
之后在lua所在路径打开CMD,输入 call premake5.exe vs2022
即可。
- premake5.exe如果不在同一路径下,则在前面加上它的相对路径如:
call premake/premake.exe vs2022;
- 目标vs版本可以自己选择