forked from dcourtois/premake-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqt.lua
555 lines (447 loc) · 13.8 KB
/
qt.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
--
-- always include _preload so that the module works even when not embedded.
--
if premake.extensions == nil or premake.extensions.qt == nil then
include ( "_preload.lua" )
end
--
-- define the qt extension
--
premake.extensions.qt = {
--
-- these are private, do not touch
--
enabled = false,
defaultPath = os.getenv("QTDIR") or os.getenv("QT_DIR")
}
--
-- include list of modules
--
include ( "qtmodules.lua" )
--
-- Enable Qt for a project. Be carefull, although this is a method, it will enable Qt
-- functionalities only in the current configuration.
--
function premake.extensions.qt.enable()
local qt = premake.extensions.qt
-- enable Qt for the current config
qtenabled ( true )
-- setup our overrides if not already done
if qt.enabled == false then
qt.enabled = true
premake.override(premake.oven, "bakeFiles", qt.customBakeFiles)
premake.override(premake.oven, "bakeConfig", qt.customBakeConfig)
premake.override(premake.fileconfig, "addconfig", qt.customAddFileConfig)
end
end
--
-- Get the include, lib and bin paths
--
function premake.extensions.qt.getPaths(cfg)
-- get the main path
local qtpath = cfg.qtpath or premake.extensions.qt.defaultpath
-- return the paths
return cfg.qtincludepath or qtpath .. "/include",
cfg.qtlibpath or qtpath .. "/lib",
cfg.qtbinpath or qtpath .. "/bin"
end
--
-- A small function which will get the generated directory for a given config.
-- If objdir was specified, it will be used. Else, it's the project's location +
-- obj + configuration + platform
--
-- @param cfg
-- The input configuration
--
function premake.extensions.qt.getGeneratedDir(cfg)
-- check if the user specified a qtgenerateddir
if cfg.qtgenerateddir ~= nil then
return cfg.qtgenerateddir
end
-- try the objdir, if it's already baked
if cfg.objdir ~= nil then
return cfg.objdir
end
-- last resort, revert to the default obj path used by premake.
-- note : this is a bit hacky, but there is no easy "getobjdir(cfg)" method in
-- premake, thus this piece of code
dir = path.join(cfg.project.location, "obj")
if cfg.platform then
dir = path.join(dir, cfg.platform)
end
dir = path.join(dir, cfg.buildcfg)
return path.getabsolute(dir)
end
--
-- Override the premake.oven.bakeConfig method to configure the configuration object
-- with the Qt module (e.g. add the include directories, the links, etc.)
--
-- @param base
-- The original bakeConfig method.
-- @param wks
-- The current workspace.
-- @param prj
-- The current project.
-- @param buildcfg
-- The current configuration.
-- @param platform
-- The current platform.
-- @param extraFilters
-- Optional additional filters.
-- @return
-- The configuration object.
--
function premake.extensions.qt.customBakeConfig(base, wks, prj, buildcfg, platform, extraFilters)
local qt = premake.extensions.qt
local modules = qt.modules
-- bake
local config = base(wks, prj, buildcfg, platform, extraFilters)
-- do nothing if qt is not enabled for this config
if config.qtenabled ~= true then
return config
end
-- get the needed pathes
local qtinclude, qtlib, qtbin = qt.getPaths(config)
if qtinclude == nil or qtlib == nil or qtbin == nil then
error(
"Some Qt paths were not found. Ensure that you set the Qt path using\n" ..
"either 'qtpath' in your project configuration or using the QTDIR or\n" ..
"QT_DIR environment variable. You can also use the 'qtincludepath',\n" ..
"'qtlibpath' and 'qtbinpath' individually."
)
end
-- bake paths in the config (in case they were retrieved from the environment variable, thy
-- will not be in the config objects, and we need them in the other baking methods)
config.qtincludepath = qtinclude
config.qtlibpath = qtlib
config.qtbinpath = qtbin
-- add the includes and libraries directories
table.insert(config.includedirs, qtinclude)
table.insert(config.libdirs, qtlib)
-- add the modules
for _, modulename in ipairs(config.qtmodules) do
if modules[modulename] ~= nil then
local module = modules[modulename]
local prefix = config.qtprefix or ""
local suffix = config.qtsuffix or ""
local libname = prefix .. module.name .. suffix
-- configure the module
table.insert(config.includedirs, qtinclude .. "/" .. module.include)
table.insert(config.links, libname)
if module.defines ~= nil then
qt.mergeDefines(config, module.defines)
end
-- add additional links
if module.links ~= nil then
for _, additionallink in ipairs(module.links) do
table.insert(config.links, additionallink)
end
end
end
end
-- return the modified config
return config
end
--
-- Override the premake.oven.bakeFiles method to be able to add the Qt generated
-- files to the project.
--
-- @param base
-- The original bakeFiles method.
-- @param prj
-- The current project.
-- @return
-- The table of files.
--
function premake.extensions.qt.customBakeFiles(base, prj)
local qt = premake.extensions.qt
local project = premake.project
-- parse the configurations for the project
for cfg in project.eachconfig(prj) do
-- ignore this config if Qt is not enabled
if cfg.qtenabled == true then
local mocs = {}
local qrc = {}
local ui = false
local objdir = qt.getGeneratedDir(cfg)
-- check each file in this configuration
table.foreachi(cfg.files, function(filename)
if qt.isUI(filename) then
ui = true
elseif qt.isQRC(filename) then
table.insert(qrc, filename)
elseif qt.needMOC(filename) then
table.insert(mocs, filename)
end
end)
-- include path for uic generated headers
if ui == true then
table.insert(cfg.includedirs, objdir)
end
-- the moc files
table.foreachi(mocs, function(filename)
table.insert(cfg.files, objdir .. "/moc_" .. path.getbasename(filename) .. ".cpp")
end)
-- the qrc files
table.foreachi(qrc, function(filename)
table.insert(cfg.files, objdir .. "/qrc_" .. path.getbasename(filename) .. ".cpp")
end)
end
end
return base(prj)
end
--
-- Override the base premake.fileconfig.addconfig method in order to add our
-- custom build rules for special Qt files.
--
-- @param base
-- The base method that we must call.
-- @param fcfg
-- The file configuration object.
-- @param cfg
-- The current configuration that we're adding to the file configuration.
--
function premake.extensions.qt.customAddFileConfig(base, fcfg, cfg)
-- call the base method to add the file config
base(fcfg, cfg)
-- do nothing else if Qt is not enabled
if cfg.qtenabled ~= true then
return
end
-- get the current config
local config = premake.fileconfig.getconfig(fcfg, cfg)
-- now filter the files, and depending on their type, add our
-- custom build rules
local qt = premake.extensions.qt
-- ui files
if qt.isUI(config.abspath) then
qt.addUICustomBuildRule(config, cfg)
-- resource files
elseif qt.isQRC(config.abspath) then
qt.addQRCCustomBuildRule(config, cfg)
-- moc files
elseif qt.needMOC(config.abspath) then
qt.addMOCCustomBuildRule(config, cfg)
end
-- the cpp files generated by the qrc tool can't use precompiled header, so
-- if we have pch and the file is a Qt generated one, check if it's generated
-- by qrc to disable pch for it.
if cfg.pchheader and config.abspath:find(qt.getGeneratedDir(cfg), 1, true) then
-- the generated dir path might contain special pattern character, so escape them
local pattern = path.wildcards(qt.getGeneratedDir(cfg))
-- if it's a qrc generated file, disable pch
if config.abspath:find(pattern .. "/qrc_.+%.cpp") then
config.flags.NoPCH = true
end
end
end
--
-- Checks if a file is a ui file.
--
-- @param filename
-- The file name to check.
-- @return
-- true if the file needs to be run through the uic tool, false if not.
--
function premake.extensions.qt.isUI(filename)
return path.hasextension(filename, { ".ui" })
end
--
-- Adds the custom build for ui files.
--
-- @param fcfg
-- The config for a single file.
-- @param cfg
-- The config of the project ?
--
function premake.extensions.qt.addUICustomBuildRule(fcfg, cfg)
local qt = premake.extensions.qt
-- get the output file
local output = qt.getGeneratedDir(cfg) .. "/ui_" .. fcfg.basename .. ".h"
output = path.getrelative(fcfg.project.location, output)
-- build the command
local command = fcfg.config.qtbinpath .. "/uic -o \"" .. output .. "\" \"" .. fcfg.relpath.. "\""
-- if we have custom commands, add them
if fcfg.config.qtrccargs then
table.foreachi(fcfg.config.qtuicargs, function (arg)
command = command .. " \"" .. arg .. "\""
end)
end
-- add the custom build rule
fcfg.buildmessage = "Uic'ing " .. fcfg.name
fcfg.buildcommands = { command }
fcfg.buildoutputs = { output }
end
--
-- Checks if a file is a qrc file.
--
-- @param filename
-- The file name to check.
-- @return
-- true if the file needs to be run through the rcc tool, false if not.
--
function premake.extensions.qt.isQRC(filename)
return path.hasextension(filename, { ".qrc" })
end
--
-- Adds the custom build for ui files.
--
-- @param fcfg
-- The config for a single file.
-- @param cfg
-- The config of the project ?
--
function premake.extensions.qt.addQRCCustomBuildRule(fcfg, cfg)
local qt = premake.extensions.qt
-- get the input and output files
local output = qt.getGeneratedDir(cfg) .. "/qrc_" .. fcfg.basename .. ".cpp"
output = path.getrelative(fcfg.project.location, output)
-- build the command
local command = fcfg.config.qtbinpath .. "/rcc -name \"" .. fcfg.basename .. "\" -no-compress \"" .. fcfg.relpath .. "\" -o \"" .. output .. "\""
-- if we have custom commands, add them
if fcfg.config.qtrccargs then
table.foreachi(fcfg.config.qtrccargs, function (arg)
command = command .. " \"" .. arg .. "\""
end)
end
-- get the files embedded on the qrc, to add them as input dependencies :
-- if we edit a .qml embedded in the qrc, we want the qrc to re-build whenever
-- we edit the qml file
local inputs = qt.getQRCDependencies(fcfg)
-- add the custom build rule
fcfg.buildmessage = "Rcc'ing " .. fcfg.name
fcfg.buildcommands = { command }
fcfg.buildoutputs = { output }
if #inputs > 0 then
fcfg.buildinputs = inputs
end
end
--
-- Get the files referenced by a qrc file.
--
-- @param fcfg
-- The configuration of the file
-- @return
-- The list of project relative file names of the dependencies
--
function premake.extensions.qt.getQRCDependencies(fcfg)
local dependencies = {}
local file = io.open(fcfg.abspath)
local qrcdirectory = path.getdirectory(fcfg.abspath)
local projectdirectory = fcfg.project.location
-- parse the qrc file to find the files it will embed
for line in file:lines() do
-- try to find the <file></file> entries
local match = string.match(line, "<file>(.+)</file>")
if match == nil then
match = string.match(line, "<file%s+[^>]*>(.+)</file>")
end
-- if we have one, compute the path of the file, and add it to the dependencies
-- note : the QRC files are relative to the folder containing the qrc file.
if match ~= nil then
table.insert(dependencies, path.getrelative(projectdirectory, qrcdirectory .. "/" .. match))
end
end
-- close the qrc file
io.close(file)
return dependencies
end
--
-- Checks if a file needs moc'ing.
--
-- @param filename
-- The file name to check.
-- @return
-- true if the header needs to be run through the moc tool, false if not.
--
function premake.extensions.qt.needMOC(filename)
local needmoc = false
-- only handle headers
if path.iscppheader(filename) then
-- open the file
local file = io.open(filename)
-- scan it to find 'Q_OBJECT' or 'Q_GADGET'
for line in file:lines() do
if line:find("Q_OBJECT") or line:find("Q_GADGET") then
needmoc = true
break
end
end
io.close(file)
end
return needmoc
end
--
-- Adds the custom build for a moc'able file.
--
-- @param fcfg
-- The config for a single file.
-- @param cfg
-- The config of the project ?
--
function premake.extensions.qt.addMOCCustomBuildRule(fcfg, cfg)
local qt = premake.extensions.qt
-- get the project's location (to make paths relative to it)
local projectloc = fcfg.project.location
-- create the output file name
local output = qt.getGeneratedDir(cfg) .. "/moc_" .. fcfg.basename .. ".cpp"
output = path.getrelative(projectloc, output)
-- create the moc command
local command = fcfg.config.qtbinpath .. "/moc \"" .. fcfg.relpath .. "\" -o \"" .. output .. "\""
-- if we have a precompiled header, prepend it
if fcfg.config.pchheader then
command = command .. " \"-b" .. fcfg.config.pchheader .. "\""
end
-- append the defines to the command
if #fcfg.config.defines > 0 then
table.foreachi(fcfg.config.defines, function (define)
command = command .. " -D" .. define
end)
end
-- append the include directories to the command
if #fcfg.config.includedirs > 0 then
table.foreachi(fcfg.config.includedirs, function (include)
command = command .. " -I\"" .. path.getrelative(projectloc, include) .. "\""
end)
end
-- if we have custom commands, add them
if fcfg.config.qtmocargs then
table.foreachi(fcfg.config.qtmocargs, function (arg)
command = command .. " \"" .. arg .. "\""
end)
end
-- add the custom build rule
fcfg.buildmessage = "Moc'ing " .. fcfg.name
fcfg.buildcommands = { command }
fcfg.buildoutputs = { output }
end
--
-- Merge defines into the configuration, taking care of not adding the
-- same define twice.
--
-- @param config
-- The configuration object.
-- @param defines
-- The defines to add.
--
function premake.extensions.qt.mergeDefines(config, defines)
-- a function which checks if a value is contained in a table.
local contains = function (t, v)
for _, d in ipairs(t) do
if d == v then
return true
end
end
return false
end
-- ensure defines is a table
if type(defines) ~= "table" then
defines = { defines }
end
-- add each defines
for _, define in ipairs(defines) do
if contains(config.defines, define) == false then
table.insert(config.defines, define)
end
end
end