-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmake_project.lua
525 lines (481 loc) · 16.9 KB
/
cmake_project.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
local p = premake
local project = p.project
local workspace = p.workspace
local tree = p.tree
local config = p.config
local cmake = p.extensions.cmake
cmake.project = {}
local m = cmake.project
m.props = function(prj)
return {
m.kind,
m.files,
m.configs
}
end
m.nonldflags = {
clang = {
"^%-nostdlib$",
"^%-l%S+$",
"^%-m%S+$",
"^%-target %S+$",
"^%-fuse%-ld=%S+$"
}
}
m.msvcRuntimeLibraries = {
On = {
Debug = "MultiThreadedDebug",
Release = "MultiThreaded"
},
Off = {
Debug = "MultiThreadedDebugDLL",
Release = "MultiThreadedDLL"
}
}
function cmake.generateProject(prj)
prj.__cmake = {}
prj.__cmake.files = {}
prj.__cmake.generatedFiles = {}
prj.__cmake.fileLangs = {}
prj.__cmake.customCommands = {}
local timer = cmake.common.createTimer("p.extensions.cmake.generateProject", { prj.name })
p.indent("\t")
local oldGetDefaultSeparator = path.getDefaultSeparator
path.getDefaultSeparator = function() return "/" end
p.callArray(m.props, prj)
path.getDefaultSeparator = oldGetDefaultSeparator
timer.stop()
end
function m.kind(prj)
local timer = cmake.common.createTimer("p.extensions.cmake.project.kind", { prj.name })
if prj.kind == "StaticLib" then
p.push("add_library(\"%s\" STATIC", prj.name)
elseif prj.kind == "SharedLib" then
p.push("add_library(\"%s\" SHARED", prj.name)
else
if prj.executable_suffix then
p.w("set(CMAKE_EXECUTABLE_SUFFIX \"%s\")", prj.executable_suffix)
end
p.push("add_executable(\"%s\"", prj.name)
end
timer.stop()
end
function m.files(prj)
local timer = cmake.common.createTimer("p.extensions.cmake.project.files", { prj.name })
local tr = project.getsourcetree(prj)
tree.traverse(tr, {
onleaf = function(node, depth)
table.insert(prj.__cmake.files, node.abspath)
if node.configs then
for cfg in project.eachconfig(prj) do
local filecfg = p.fileconfig.getconfig(node, cfg)
local rule = p.global.getRuleForFile(node.name, prj.rules)
if filecfg.compileas then
if not prj.__cmake.fileLangs[filecfg.compileas] then prj.__cmake.fileLangs[filecfg.compileas] = {} end
local fileLang = prj.__cmake.fileLangs[filecfg.compileas]
fileLang[node.abspath] = filecfg
end
if p.fileconfig.hasFileSettings(filecfg) then
for _, output in ipairs(filecfg.buildOutputs) do
table.insert(prj.__cmake.files, output)
table.insert(prj.__cmake.generatedFiles, output)
end
table.insert(prj.__cmake.customCommands, {
cfg = filecfg,
relpath = node.relpath
})
break
elseif rule then
local environ = table.shallowcopy(filecfg.environ)
if rule.propertydefinition then
p.rule.prepareEnvironment(rule, environ, cfg)
p.rule.prepareEnvironment(rule, environ, filecfg)
end
local rulecfg = p.context.extent(rule, environ)
for _, output in ipairs(rulecfg.buildOutputs) do
table.insert(prj.__cmake.files, output)
table.insert(prj.__cmake.generatedFiles, output)
end
table.insert(prj.__cmake.customCommands, {
cfg = rulecfg,
relpath = node.relpath
})
break
end
end
end
end
}, true)
for _, v in ipairs(prj.__cmake.files) do
p.w("\"%s\"", v)
end
p.pop(")")
timer.stop()
end
m.configProps = function(prj, cfg)
return {
m.dependencies,
m.sourceFileProperties,
m.outputDirs,
m.includeDirs,
m.defines,
m.msvcRuntimeLibrary,
m.libDirs,
m.libs,
m.buildOptions,
m.linkOptions,
m.compileOptions,
m.cppStandard,
m.pch,
m.prebuildCommands,
m.postbuildCommands,
m.prelinkCommands,
m.customCommands
}
end
function m.configs(prj)
local timer = cmake.common.createTimer("p.extensions.cmake.project.configs", { prj.name })
for cfg in project.eachconfig(prj) do
p.push("if(CMAKE_BUILD_TYPE STREQUAL \"%s\")", cmake.common.configName(cfg, #prj.workspace.platforms > 1))
p.callArray(m.configProps, prj, cfg)
p.pop("endif()")
end
timer.stop()
end
function m.dependencies(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.dependencies", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
local dependencies = project.getdependencies(prj, cfg)
if #dependencies > 0 then
p.push("add_dependencies(\"%s\"", prj.name)
for _, dependency in ipairs(dependencies) do
p.w("\"%s\"", dependency.name)
end
p.pop(")")
end
timer.stop()
end
function m.sourceFileProperties(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.sourceFileProperties", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
p.push("set_source_files_properties(")
for _, v in ipairs(prj.__cmake.generatedFiles) do
p.w("\"%s\"", v)
end
p.w("PROPERTIES")
p.w("GENERATED true")
p.pop(")")
for lang, files in pairs(prj.__cmake.fileLangs) do
if not cmake.common.compileasLangs[lang] then
error("CMake generator does not support compileas langauge " .. lang .. "!")
end
p.push("set_source_files_properties(")
for path, file in pairs(files) do
p.w("\"%s\"", path)
end
p.w("PROPERTIES")
p.w("LANGUAGE %s", cmake.common.compileasLangs[lang])
p.pop(")")
end
timer.stop()
end
function m.outputDirs(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.outputDirs", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
p.push("set_target_properties(\"%s\" PROPERTIES", prj.name)
p.w("OUTPUT_NAME \"%s\"", cfg.buildtarget.basename)
p.w("ARCHIVE_OUTPUT_DIRECTORY \"%s\"", cfg.buildtarget.directory)
p.w("LIBRARY_OUTPUT_DIRECTORY \"%s\"", cfg.buildtarget.directory)
p.w("RUNTIME_OUTPUT_DIRECTORY \"%s\"", cfg.buildtarget.directory)
p.pop(")")
timer.stop()
end
function m.includeDirs(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.includeDirs", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
if #cfg.sysincludedirs > 0 then
p.push("target_include_directories(\"%s\" SYSTEM PRIVATE", prj.name)
for _, includedir in ipairs(cfg.sysincludedirs) do
p.w("\"%s\"", includedir)
end
p.pop(")")
end
if #cfg.externalincludedirs > 0 then
p.push("target_include_directories(\"%s\" SYSTEM PRIVATE", prj.name)
for _, includedir in ipairs(cfg.externalincludedirs) do
p.w("\"%s\"", includedir)
end
p.pop(")")
end
if #cfg.includedirs > 0 then
p.push("target_include_directories(\"%s\" PRIVATE", prj.name)
for _, includedir in ipairs(cfg.includedirs) do
p.w("\"%s\"", includedir)
end
p.pop(")")
end
timer.stop()
end
function m.defines(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.defines", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
if #cfg.defines > 0 then
p.push("target_compile_definitions(\"%s\" PRIVATE", prj.name)
for _, define in ipairs(cfg.defines) do
p.w("%s", p.esc(define):gsub(" ", "\\ "))
end
p.pop(")")
end
timer.stop()
end
function m.msvcRuntimeLibrary(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.runtimeLibrary", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
local static = m.msvcRuntimeLibraries[cfg.staticruntime]
if static then
local runtime = static[cfg.runtime]
if runtime then
p.w("set_property(TARGET \"%s\" PROPERTY MSVC_RUNTIME_LIBRARY \"%s\")", prj.name, runtime)
end
end
timer.stop()
end
function m.libDirs(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.libDirs", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
if #cfg.libdirs > 0 then
p.push("target_link_directories(\"%s\" PRIVATE", prj.name)
for _, libdir in ipairs(cfg.libdirs) do
p.w("\"%s\"", libdir)
end
p.pop(")")
end
timer.stop()
end
function m.libs(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.libs", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
local uselinkgroups = isclangorgcc and cfg.linkgroups == p.ON
if uselinkgroups or #config.getlinks(cfg, "dependencies", "object") > 0 or #config.getlinks(cfg, "system", "fullpath") > 0 then
p.push("target_link_libraries(\"%s\"", prj.name)
if uselinkgroups then
p.w("-Wl,--start-group")
end
for a, link in ipairs(config.getlinks(cfg, "dependencies", "object")) do
p.w("\"%s\"", link.project.name)
end
if uselinkgroups then
p.w("-Wl,--end-group")
p.w("-Wl,--start-group")
end
for _, link in ipairs(config.getlinks(cfg, "system", "fullpath")) do
if cmake.common.isFramework(link) then
p.w("\"-framework %s\"", cmake.common.getFrameworkName(link))
else
p.w("\"%s\"", link)
end
end
if uselinkgroups then
p.w("-Wl,--end-group")
end
p.pop(")")
end
timer.stop()
end
function m.buildOptions(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.buildOptions", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
local options = ""
for _, option in ipairs(cfg.buildoptions) do
if type(option) ~= "string" then
error("Dude, did you really just pass a " .. type(option) .. " to a build option expecting a string?")
end
options = options .. option .. "\n"
end
if options:len() > 0 then
p.push("target_compile_options(\"%s\" PRIVATE", prj.name)
p.w(options)
p.pop(")")
end
timer.stop()
end
function m.linkOptions(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.linkOptions", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
local toolset = cmake.common.getCompiler(cfg)
local ldflags = toolset.getldflags(cfg)
local options = ""
for _, flag in ipairs(ldflags) do
if type(flag) ~= "string" then
error("Dude, did you really just pass a " .. type(flag) .. " to a linker option expecting a string?")
end
if options:len() > 0 then
options = options .. " "
end
options = options .. flag
end
for _, option in ipairs(cfg.linkoptions) do
if type(option) ~= "string" then
error("Dude, did you really just pass a " .. type(flag) .. " to a linker option expecting a string?")
end
if options:len() > 0 then
options = options .. " "
end
options = options .. option
end
local default = iif(cfg.system == p.WINDOWS, "msc", "clang")
local nonlinkerflags = m.nonldflags[_OPTIONS.cc or cfg.toolset or default]
local gsubcallback = function(w)
w = w:gsub("\"([^\"]*)\"", "%1")
if nonlinkerflags then
local isldflag = true
for _, nonflag in pairs(nonlinkerflags) do
if w:find(nonflag) then
isldflag = false
break
end
end
if isldflag then
return "-Wl," .. w
else
return w
end
else
return "-Wl," .. w
end
end
options = options:gsub("%S+", function(w) if w:sub(1, 1) == "\"" or w:sub(-1, -1) == "\"" then return nil else return gsubcallback(w) end end)
options = options:gsub("%b\"\"", gsubcallback)
if options:len() > 0 then
p.w("set_target_properties(\"%s\" PROPERTIES LINK_FLAGS \"%s\")", prj.name, cmake.common.escapeStrings(options))
end
timer.stop()
end
function m.compileOptions(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.compileOptions", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
local toolset = cmake.common.getCompiler(cfg)
local cflags = toolset.getcflags(cfg)
local cxxflags = toolset.getcxxflags(cfg)
local forceIncludes = toolset.getforceincludes(cfg)
if #cflags > 0 or #cxxflags > 0 then
p.push("target_compile_options(\"%s\" PRIVATE", prj.name)
for _, flag in ipairs(cflags) do
p.w("$<$<COMPILE_LANGUAGE:C>:%s>", flag)
end
for _, flag in ipairs(cxxflags) do
p.w("$<$<COMPILE_LANGUAGE:CXX>:%s>", flag)
end
for _, flag in ipairs(forceIncludes) do
p.w("$<$<COMPILE_LANGUAGE:C>:%s>", flag)
p.w("$<$<COMPILE_LANGUAGE:CXX>:%s>", flag)
end
p.pop(")")
end
timer.stop()
end
function m.cppStandard(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.cppStandard", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
if (cfg.cppdialect and cfg.cppdialect:len() > 0) or cfg.cppdialect == "Default" then
local extensions = iif(cfg.cppdialect:find("^gnu") == nil, "NO", "YES")
local pic = iif(cfg.pic == "On", "True", "False")
local lto = iif(cfg.flags.LinkTimeOptimization, "True", "False")
p.push("set_target_properties(\"%s\" PROPERTIES", prj.name)
p.w("CXX_STANDARD %s", cmake.common.cppStandards[cfg.cppdialect])
p.w("CXX_STANDARD_REQUIRED YES")
p.w("CXX_EXTENSIONS %s", extensions)
p.w("POSITION_INDEPENDENT_CODE %s", pic)
p.w("INTERPROCEDURAL_OPTIMIZATION %s", lto)
p.pop(")")
end
timer.stop()
end
function m.pch(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.pch", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
if not cfg.flags.NoPCH and cfg.pchheader then
local pch = cfg.pchheader
local found = false
local testname = path.join(cfg.workspace.basedir, pch)
if os.isfile(testname) then
pch = project.getrelative(cfg.workspace, testname)
found = true
else
for _, incdir in ipairs(cfg.includedirs) do
testname = path.join(incdir, pch)
if os.isfile(testname) then
pch = project.getrelative(cfg.workspace, testname)
found = true
break
end
end
end
if not found then
pch = project.getrelative(cfg.workspace, path.getabsolute(pch))
end
p.w("target_precompile_headers(\"%s\" PRIVATE \"%s\")", prj.name, path.getabsolute(pch))
end
timer.stop()
end
function m.prebuildCommands(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.prebuildCommands", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
if cfg.prebuildmessage or #cfg.prebuildcommands > 0 then
p.push("add_custom_target(prebuild-%s", prj.name)
if cfg.prebuildmessage then
p.w("COMMAND %s", cmake.common.fixSingleQuotes(os.translateCommandsAndPaths("{ECHO} " .. cfg.prebuildmessage, cfg.project.basedir, cfg.project.location)))
end
for _, command in ipairs(cmake.common.fixSingleQuotes(os.translateCommandsAndPaths(cfg.prebuildcommands, cfg.project.basedir, cfg.project.location))) do
p.w("COMMAND %s", command)
end
p.pop(")")
p.w("add_dependencies(\"%s\" prebuild-%s)", prj.name, prj.name)
end
timer.stop()
end
function m.postbuildCommands(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.postbuildCommands", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
if cfg.postbuildmessage or #cfg.postbuildcommands > 0 then
p.push("add_custom_target(postbuild-%s", prj.name)
if cfg.postbuildmessage then
p.w("COMMAND %s", cmake.common.fixSingleQuotes(os.translateCommandsAndPaths("{ECHO} " .. cfg.postbuildmessage, cfg.project.basedir, cfg.project.location)))
end
for _, command in ipairs(cmake.common.fixSingleQuotes(os.translateCommandsAndPaths(cfg.postbuildcommands, cfg.project.basedir, cfg.project.location))) do
p.w("COMMAND %s", command)
end
p.pop(")")
p.w("add_dependencies(\"%s\" postbuild-%s)", prj.name, prj.name)
end
timer.stop()
end
function m.prelinkCommands(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.prelinkCommands", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
if cfg.prelinkmessage or #cfg.prelinkcommands > 0 then
p.push("add_custom_command(TARGET \"%s\"", prj.name)
p.w("PRE_LINK")
for _, command in ipairs(cmake.common.fixSingleQuotes(os.translateCommandsAndPaths(cfg.prelinkcommands, cfg.project.basedir, cfg.project.location))) do
p.w("COMMAND %s", command)
end
p.pop(")")
end
timer.stop()
end
local function addCustomCommand(cfg, fileconfig, filename)
local timer = cmake.common.createTimer("p.extensions.cmake.project.addCustomCommand")
if #fileconfig.buildcommands == 0 or #fileconfig.buildOutputs == 0 then
return
end
p.push("add_custom_command(TARGET OUTPUT %s", table.implode(fileconfig.buildOutputs, "", "", " "))
if fileconfig.buildmessage then
p.w("COMMAND %s", cmake.common.fixSingleQuotes(os.translateCommandsAndPaths("{ECHO} " .. fileconfig.buildmessage, cfg.project.basedir, cfg.project.location)))
end
for _, command in ipairs(cmake.common.fixSingleQuotes(os.translateCommandsAndPaths(fileconfig.buildCommands, cfg.project.basedir, cfg.project.location))) do
p.w("COMMAND %s", command)
end
if filename:len() > 0 then
filename = cfg.project.location .. "/" .. filename
if #fileconfig.buildInputs > 0 then
filename = filename .. " "
end
end
if filename:len() > 0 or #fileconfig.buildInputs > 0 then
p.w("DEPENDS %s", filename .. table.implode(fileconfig.buildInputs, "", "", " "))
end
p.pop(")")
timer.stop()
end
function m.customCommands(prj, cfg)
local timer = cmake.common.createTimer("p.extensions.cmake.project.customCommands", { prj.name, cmake.common.configName(cfg, #prj.workspace.platforms > 1) })
for _, v in ipairs(prj.__cmake.customCommands) do
addCustomCommand(cfg, v.cfg, v.relpath)
end
addCustomCommand(cfg, cfg, "")
timer.stop()
end