一、脚本例子
1、如下脚本,保存为premake5.lua
-- premake5.lua
workspace "HelloWorld" --解决方案名称
configurations { "Debug", "Release" } --解决方案配置项
project "HelloWorld" --项目名称
kind "ConsoleApp" --项目类型
language "C" --使用语言
targetdir "bin/%{cfg.buildcfg}" --目标文件
files { "**.h", "**.c" } --指定加载哪些类型文件
filter "configurations:Debug" --Debug配置项属性
defines { "DEBUG" } --定义Debug宏
symbols "On" --开启调试符号
filter "configurations:Release" --Release配置项属性
defines { "NDEBUG" }
optimize "On" --开启优化参数
2、通过下面命令运行项目文件
$ premake5 vs2013
这个特别的命令将为Visual Studio 2013生成HelloWorld.sln和HelloWorld.vcxproj文件
3、不是默认名称的加载,靠file参数指定
$ premake5 --file=MyProjectScript.lua vs2013
**:lua语法学习网址Lua: about
5、脚本中的每一行都是一个函数的调用,通常可以省略括号,在需要参数时括号必须要加,如下
-- 可以省略括号
workspace("HelloWorld")
configurations({ "Debug", "Release" })
--不可以省略括号
local lang = "C++"
language (lang) -- using a variable, needs parenthesis
workspace("HelloWorld" .. _ACTION) -- using string concatenation, needs parenthesis
6、如果有相同变量有多个值,后面的值会覆盖前面的值,如
language "C++" -- the value is now "C++"
language "C" -- the value is now "C"
7、对于参数列表可以使用大括号。如
defines { "DEBUG", "TRACE" } -- defines multiple values using list syntax
defines { "NDEBUG" } -- defines a single value using list syntax
defines "NDEBUG" -- defines a single value as a simple string
8、删除定义的值
defines { "DEBUG", "TRACE" } -- value is now { "DEBUG", "TRACE" }
removedefines { "TRACE" } -- value is now { "DEBUG" }
二、配置
- workspace工作空间:每个项目的顶层都是工作空间,如vs中间叫做解决方案,工作空间定义了一套通用的构建配置和平台,用于所有包含的项目。你也可以在这一层指定额外的构建设置(定义、包含路径等),这些设置将同样被项目所继承。
1、一般是一个工作区,如果需要多个工作区,用configurations指定,如
workspace "HelloWorld"
configurations { "Debug", "Release" }
2、为工作区指定不同名称
workspace "Hello World"
filename "Hello"
configurations { "Debug", "Release" }
项目:一个工作空间可以存放多个项目, 可以把项目看成一个库或者可执行文件
1、指定项目名称
workspace "MyWorkspace"
configurations { "Debug", "Release" }
project "MyProject"
2、指定项目种类
project "MyProject"
kind "ConsoleApp"
language "C++"
3、项目文件和脚本文件在相同的目录中
workspace "MyWorkspace"
configurations { "Debug", "Release" }
location "build"
project "MyProject"
location "build/MyProject"
defines:用来给一个project添加预处理或者编译符号
1、作用域定义
-- global scope, all workspaces will receive these values
defines { "GLOBAL" }
workspace "MyWorkspaces"
-- workspace scope inherits the global scope; the list value
-- will now be { "GLOBAL", "WORKSPACE" }
defines { "WORKSPACE" }
project "MyProject"
-- project scope inherits from its workspace; the list value
-- will now be { "GLOBAL", "WORKSPACE", "PROJECT" }
defines { "PROJECT" }
2、你也可以通过使用特殊的 "*"名称来选择当前作用域的父级或容器,而不必知道其名称
-- declare my workspace
workspace "MyWorkspace"
defines { "WORKSPACE1" }
-- declare a project or two
project "MyProject"
defines { "PROJECT" }
-- re-select my workspace to add more settings
project "*"
defines { "WORKSPACE2" } -- value is now { "WORKSPACE1", "WORKSPACE2" }
-- re-select the global scope
workspace "*"
添加文件:使用files函数向你的项目添加文件--源代码、资源等等。
1、可以在文件模式中使用通配符来匹配一组文件。通配符*将匹配一个目录中的文件
files {
"hello.h", -- you can specify exact names
"*.c", -- or use a wildcard...
"**.cpp" -- ...and recurse into subdirectories
}
2、位于其他目录下的文件应该相对于脚本文件来指定。例如,如果脚本位于MyProject/build,而源文件位于MyProject/src,那么这些文件应该被指定为
files { "../src/*.cpp" }
3、排除文件
files { "**.c" }
removefiles { "tests/*.c" }
linking链接,链接库
链接器:指定一个要链接的库和项目的列表
1、链接到一个同级项目(同一工作区的项目),请使用项目名称
workspace "MyWorkspace"
project "MyLibraryProject"
-- ...project settings here...
project "MyExecutableProject"
-- ...project settings here...
links { "MyLibraryProject" }
2、指定链接库位置
libdirs { "libs", "../mylibs" }
3、查找链接库
libdirs { os.findlib("X11") }
配置:配置是应用于构建的设置的集合,包括标志和开关、头文件和库搜索目录等等
1、使用静态库或共享库时名字可以取个有意义的即可
workspace "MyWorkspace"
configurations { "Debug", "DebugDLL", "Release", "ReleaseDLL" }
2、在某个平台上构建项目
configurations { "Debug", "Release" }
platforms { "Win32", "Win64", "Xbox360" }
3、配置和平台列表现在可以按项目指定
workspace "MyWorkspace"
configurations { "Debug", "Release" }
platforms { "Windows", "PS3" }
project "MyProject"
removeplatforms { "PS3" }
4、工作区级的配置转化为项目及的配置并被项目引用
project "UnitTest"
configurations { "Debug", "Release" }
过滤器:过滤器总是由两部分组成:一个指定被过滤的字段的前缀,和一个指定该字段的哪些值应该被接受的模式
1、过滤器的使用
-- All of these settings will appear in the Debug configuration
filter "configurations:Debug"
defines { "DEBUG" }
flags { "Symbols" }
-- All of these settings will appear in the Release configuration
filter "configurations:Release"
defines { "NDEBUG" }
optimize "On"
-- This is a sneaky bug (assuming you always want to link against these lib files).
-- Because the last filter set was Release. These libraries will only be linked for release.
-- To fix this place this after the "Deactivate" filter call below. Or before any filter calls.
links { "png", "zlib" }
-- "Deactivate" the current filter; these settings will apply
-- to the entire workspace or project (whichever is active)
filter {}
files { "**.cpp" }
官方网址: Home | Premake