最近我已经从CMake改为Premake(v5.0.0-alpha8),我不太确定如何在Premake中实现以下功能。
我想包含一些依赖项,所以在CMake中我可以这样做:
target_link_libraries(${PROJECT_NAME}
${YALLA_ABS_PLATFORM}
${YALLA_LIBRARY})
上面会将这些库(dir)的路径添加到编译器中的“Additional Include Directories”中,它还会在链接器中的“Additional Dependencies”中添加一个条目(lib),所以我不需要这样做除了致电target_link_libraries
之外还有什么特别之处。
所以当我在Premake做这样的事情时,我预计会这样做:
links {
YALLA_LIBRARY
}
我会得到相同的结果,但我没有。
我也尝试使用libdirs
,但它并不真正工作,我的意思是我看不到库目录及其子目录作为“附加包含”传递给编译器目录“(/ I)或Yalla.Library.lib作为”附加依赖项“传递给链接器。
以下是我使用的目录结构:
.
|-- src
| |-- launcher
| |-- library
| | `-- utils
| `-- platform
| |-- abstract
| `-- win32
`-- tests
`-- platform
`-- win32
库目录在Premake中定义如下:
project(YALLA_LIBRARY)
kind "SharedLib"
files {
"utils/string-converter.hpp",
"utils/string-converter.cpp",
"defines.hpp"
}
平台目录在Premake中定义如下:
project(YALLA_PLATFORM)
kind "SharedLib"
includedirs "abstract"
links {
YALLA_LIBRARY
}
if os.get() == "windows" then
include "win32"
else
return -- OS NOT SUPPORTED
end
win32 目录在Premake中定义如下:
files {
"event-loop.cpp",
"win32-exception.cpp",
"win32-exception.hpp",
"win32-window.cpp",
"win32-window.hpp",
"window.cpp"
}
最后在 root 目录中,我有以下Premake文件:
PROJECT_NAME = "Yalla"
-- Sets global constants that represents the projects' names
YALLA_LAUNCHER = PROJECT_NAME .. ".Launcher"
YALLA_LIBRARY = PROJECT_NAME .. ".Library"
YALLA_ABS_PLATFORM = PROJECT_NAME .. ".AbstractPlatform"
YALLA_PLATFORM = PROJECT_NAME .. ".Platform"
workspace(PROJECT_NAME)
configurations { "Release", "Debug" }
flags { "Unicode" }
startproject ( YALLA_LAUNCHER )
location ( "../lua_build" )
include "src/launcher"
include "src/library"
include "src/platform"
由于缺乏经验,我可能误解了Premake的工作原理。
答案 0 :(得分:0)
我通过创建一个新的全局函数并将其命名为includedeps
来解决它。
function includedeps(workspace, ...)
local workspace = premake.global.getWorkspace(workspace)
local args = { ... }
local args_count = select("#", ...)
local func = select(args_count, ...)
if type(func) == "function" then
args_count = args_count - 1
args = table.remove(args, args_count)
else
func = nil
end
for i = 1, args_count do
local projectName = select(i, ...)
local project = premake.workspace.findproject(workspace, projectName)
if project then
local topIncludeDir, dirs = path.getdirectory(project.script)
if func then
dirs = func(topIncludeDir)
else
dirs = os.matchdirs(topIncludeDir .. "/**")
table.insert(dirs, topIncludeDir)
end
includedirs(dirs)
if premake.project.iscpp(project) then
libdirs(dirs)
end
links(args)
else
error(string.format("project '%s' does not exist.", projectName), 3)
end
end
end
<强>用法:强>
includedeps(PROJECT_NAME, YALLA_LIBRARY)
或
includedeps(PROJECT_NAME, YALLA_PLATFORM, function(topIncludeDir)
return { path.join(topIncludeDir, "win32") }
end)
<强>更新强>
为了使正确工作,您需要确保当include
依赖项时,它们的依赖顺序包含它们而不是目录结构的顺序。
例如,如果我有以下依赖关系图launcher --> platform --> library
,那么我将按以下顺序include
。
include "src/library"
include "src/platform"
include "src/launcher"
与我的情况下的目录结构相反:
src/launcher
src/library
src/platform
如果你将它们的目录结构包含在内,它将失败并告诉你“项目'Yalla.Platform'不存在。”