-
Notifications
You must be signed in to change notification settings - Fork 4
/
_cmake.lua
292 lines (224 loc) · 6.36 KB
/
_cmake.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
--
-- _cmake.lua
-- Define the makefile action(s).
-- Copyright (c) 2014 Manu Evans and the Premake project
--
premake.extensions.cmake = {}
local cmake = premake.extensions.cmake
local solution = premake.solution
local project = premake.project
---
-- The CMake action, with support for the new platforms API
---
newaction {
trigger = "cmake",
shortname = "CMake",
description = "Generate CMake files",
valid_kinds = { "ConsoleApp", "WindowedApp", "StaticLib", "SharedLib" },
valid_languages = { "C", "C++", "C#", "D" },
valid_tools = {
cc = { "msc", clang", "gcc" },
dc = { "dmd", "gdc", "ldc" },
dotnet = { "mono", "msnet", "pnet" }
},
onsolution = function(sln)
premake.escaper(cmake.esc)
premake.generate(sln, cmake.getmakefilename(sln, false), cmake.generate_solution)
end,
onproject = function(prj)
premake.escaper(cmake.esc)
local cmakefile = cmake.getcmakefilename(prj, true)
if project.isdotnet(prj) then
premake.generate(prj, cmakefile, cmake.cs.generate)
elseif project.isd(prj) then
premake.generate(prj, cmakefile, cmake.d.generate)
elseif project.iscpp(prj) then
premake.generate(prj, cmakefile, cmake.cpp.generate)
end
end,
oncleansolution = function(sln)
premake.clean.file(sln, cmake.getcmakefilename(sln, false))
end,
oncleanproject = function(prj)
premake.clean.file(prj, cmake.getcmakefilename(prj, true))
end
}
--
-- Write out the default configuration rule for a solution or project.
-- @param target
-- The solution or project object for which a cmake file is being generated.
--
function cmake.defaultconfig(target)
-- find the right configuration iterator function for this object
local eachconfig = iif(target.project, project.eachconfig, solution.eachconfig)
local iter = eachconfig(target)
-- grab the first configuration and write the block
local cfg = iter()
if cfg then
_p('ifndef config')
_x(' config=%s', cfg.shortname)
_p('endif')
_p('')
end
end
---
-- Escape a string so it can be written to a cmake file.
---
function cmake.esc(value)
-- result = value:gsub("\\", "\\\\")
-- result = result:gsub(" ", "\\ ")
-- result = result:gsub("%(", "\\%(")
-- result = result:gsub("%)", "\\%)")
-- leave $(...) shell replacement sequences alone
-- result = result:gsub("$\\%((.-)\\%)", "$%(%1%)")
-- return result
return value
end
--
-- Get the cmake file name for a solution or a project. If this object is the
-- only one writing to a location then I can use "cmakelist.txt". If more than one object
-- writes to the same location I use name + ".???" to keep it unique.
--
function cmake.getcmakefilename(this, searchprjs)
local count = 0
for sln in premake.solution.each() do
if sln.location == this.location then
count = count + 1
end
if searchprjs then
for _, prj in ipairs(sln.projects) do
if prj.location == this.location then
count = count + 1
end
end
end
end
if count == 1 then
return "cmakelist.txt"
else
return ".txt"
end
end
--
-- Output a cmake file header.
--
-- @param target
-- The solution or project object for which the cmake file is being generated.
--
function cmake.header(target)
-- find the right configuration iterator function for this object
local kind = iif(target.project, "project", "solution")
_p('# %s %s CMake autogenerated by Premake', premake.action.current().shortname, kind)
_p('')
cmake.defaultconfig(target)
_p('ifndef verbose')
_p(' SILENT = @')
_p('endif')
_p('')
end
--
-- Rules for file ops based on the shell type. Can't use defines and $@ because
-- it screws up the escaping of spaces and parethesis (anyone know a solution?)
--
function cmake.mkdirRules(dirname)
_p('%s:', dirname)
_p('\t@echo Creating %s', dirname)
_p('ifeq (posix,$(SHELLTYPE))')
_p('\t$(SILENT) mkdir -p %s', dirname)
_p('else')
_p('\t$(SILENT) mkdir $(subst /,\\\\,%s)', dirname)
_p('endif')
_p('')
end
--
-- Format a list of values to be safely written as part of a variable assignment.
--
function cmake.list(value)
if #value > 0 then
return " " .. table.concat(value, " ")
else
return ""
end
end
--
-- Convert an arbitrary string (project name) to a cmake variable name.
--
function cmake.tovar(value)
value = value:gsub("[ -]", "_")
value = value:gsub("[()]", "")
return value
end
---------------------------------------------------------------------------
--
-- Handlers for the individual cmake file elements that can be shared
-- between the different language projects.
--
---------------------------------------------------------------------------
function cmake.objdir(cfg)
_x(' OBJDIR = %s', project.getrelative(cfg.project, cfg.objdir))
end
function cmake.objDirRules(prj)
cmake.mkdirRules("$(OBJDIR)")
end
function cmake.phonyRules(prj)
_p('.PHONY: clean prebuild prelink')
_p('')
end
function cmake.buildCmds(cfg, event)
_p(' define %sCMDS', event:upper())
local steps = cfg[event .. "commands"]
local msg = cfg[event .. "message"]
if #steps > 0 then
msg = msg or string.format("Running %s commands", event)
_p('\t@echo %s', msg)
_p('\t%s', table.implode(steps, "", "", "\n\t"))
end
_p(' endef')
end
function cmake.preBuildCmds(cfg, toolset)
cmake.buildCmds(cfg, "prebuild")
end
function cmake.preBuildRules(prj)
_p('prebuild:')
_p('\t$(PREBUILDCMDS)')
_p('')
end
function cmake.preLinkCmds(cfg, toolset)
cmake.buildCmds(cfg, "prelink")
end
function cmake.preLinkRules(prj)
_p('prelink:')
_p('\t$(PRELINKCMDS)')
_p('')
end
function cmake.postBuildCmds(cfg, toolset)
cmake.buildCmds(cfg, "postbuild")
end
function cmake.settings(cfg, toolset)
if #cfg.cmakesettings > 0 then
for _, value in ipairs(cfg.cmakesettings) do
_p(value)
end
end
local value = toolset.getcmakesettings(cfg)
if value then
_p(value)
end
end
function cmake.shellType()
_p('SHELLTYPE := msdos')
_p('ifeq (,$(ComSpec)$(COMSPEC))')
_p(' SHELLTYPE := posix')
_p('endif')
_p('ifeq (/bin,$(findstring /bin,$(SHELL)))')
_p(' SHELLTYPE := posix')
_p('endif')
_p('')
end
function cmake.target(cfg)
_x(' TARGETDIR = %s', project.getrelative(cfg.project, cfg.buildtarget.directory))
_x(' TARGET = $(TARGETDIR)/%s', cfg.buildtarget.name)
end
function cmake.targetDirRules(prj)
cmake.mkdirRules("$(TARGETDIR)")
end