-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmake_workspace.lua
133 lines (121 loc) · 3.92 KB
/
cmake_workspace.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
local p = premake
local project = p.project
local workspace = p.workspace
local tree = p.tree
local cmake = p.extensions.cmake
cmake.workspace = {}
local m = cmake.workspace
m.languageToEnabledLanguage = {
["C"] = "C",
["C++"] = "CXX",
["Objective-C"] = "OBJC",
["Objective-C++"] = "OBJCXX"
}
m.props = function(wks)
return {
m.minimumRequiredVersion,
m.enableLanguages,
m.buildTypes,
m.defaultFlags,
m.name,
m.projects
}
end
function cmake.generateWorkspace(wks)
local timer = cmake.common.createTimer("p.extensions.cmake.generateWorkspace", { wks.name })
p.indent("\t")
p.utf8()
wks.__cmake = {}
wks.__cmake.buildTypes = {}
for cfg in workspace.eachconfig(wks) do
table.insert(wks.__cmake.buildTypes, cmake.common.configName(cfg, #wks.platforms > 1))
end
p.callArray(m.props, wks, buildTypes)
timer.stop()
end
function m.minimumRequiredVersion(wks)
local timer = cmake.common.createTimer("p.extensions.cmake.workspace.minimumRequiredVersion", { wks.name })
p.w("cmake_minimum_required(VERSION 3.16)")
timer.stop()
end
function m.enableLanguages(wks)
local timer = cmake.common.createTimer("p.extensions.cmake.workspace.enableLanguages", { wks.name })
local enabledLanguages = {}
for prj in workspace.eachproject(wks) do
local tr = project.getsourcetree(prj)
for cfg in project.eachconfig(prj) do
tree.traverse(tr, {
onleaf = function(node, depth)
if node.configs then
local filecfg = p.fileconfig.getconfig(node, cfg)
if filecfg.compileas then
local lang = m.languageToEnabledLanguage[filecfg.compileas]
if lang then
enabledLanguages[lang] = true
end
end
end
end
}, true)
local setLang = cfg.language
if setLang then
local lang = m.languageToEnabledLanguage[setLang]
if lang then
enabledLanguages[lang] = true
end
end
end
end
for lang, _ in pairs(enabledLanguages) do
p.w("enable_language(%s)", lang)
end
timer.stop()
end
function m.buildTypes(wks)
local timer = cmake.common.createTimer("p.extensions.cmake.workspace.buildTypes", { wks.name })
p.w("set(PREMAKE_BUILD_TYPES \"%s\")", table.concat(wks.__cmake.buildTypes, "\" \""))
p.w("get_property(multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)")
p.push("if(multi_config)")
p.w("set(CMAKE_CONFIGURATION_TYPES \"${PREMAKE_BUILD_TYPES}\" CACHE STRING \"List of supported configuration types\" FORCE)")
p.pop()
p.push("else()")
p.w("set(CMAKE_BUILD_TYPE \"%s\" CACHE STRING \"Build Type of the project.\")", wks.__cmake.buildTypes[1])
p.w("set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS \"${PREMAKE_BUILD_TYPES}\")")
p.push("if(NOT CMAKE_BUILD_TYPE IN_LIST PREMAKE_BUILD_TYPES)")
p.push("message(FATAL_ERROR")
p.w("\"Invalid build type '${CMAKE_BUILD_TYPE}'.")
p.w("CMAKE_BUILD_TYPE must be any of the possible values:")
p.w("${PREMAKE_BUILD_TYPES}\"")
p.pop(")")
p.pop("endif()")
p.pop("endif()")
timer.stop()
end
function m.defaultFlags(wks)
local timer = cmake.common.createTimer("p.extensions.cmake.workspace.defaultFlags", { wks.name })
p.w("set(CMAKE_MSVC_RUNTIME_LIBRARY \"\")")
p.w("set(CMAKE_C_FLAGS \"\")")
p.w("set(CMAKE_CXX_FLAGS \"\")")
for _, buildType in ipairs(wks.__cmake.buildTypes) do
p.w("set(CMAKE_C_FLAGS_%s \"\")", string.upper(buildType))
p.w("set(CMAKE_CXX_FLAGS_%s \"\")", string.upper(buildType))
end
timer.stop()
end
function m.name(wks)
local timer = cmake.common.createTimer("p.extensions.cmake.workspace.name", { wks.name })
p.w("project(\"%s\")", wks.name)
timer.stop()
end
function m.projects(wks)
local timer = cmake.common.createTimer("p.extensions.cmake.workspace.projects", { wks.name })
local tr = workspace.grouptree(wks)
tree.traverse(tr, {
onleaf = function(n)
local prj = n.project
if prj.kind == "Utility" then return end
p.w("include(\"%s\")", path.getrelative(prj.workspace.location, p.filename(prj, ".cmake")))
end
})
timer.stop()
end