forked from premake/premake-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.lua
More file actions
executable file
·614 lines (496 loc) · 16.8 KB
/
config.lua
File metadata and controls
executable file
·614 lines (496 loc) · 16.8 KB
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
--
-- config.lua
-- Premake configuration object API
-- Copyright (c) 2011-2015 Jason Perkins and the Premake project
--
local p = premake
p.config = {}
local project = p.project
local config = p.config
---
-- Helper function for getlinkinfo() and gettargetinfo(); builds the
-- name parts for a configuration, for building or linking.
--
-- @param cfg
-- The configuration object being queried.
-- @param kind
-- The target kind (SharedLib, StaticLib).
-- @param field
-- One of "target" or "implib", used to locate the naming information
-- in the configuration object (i.e. targetdir, targetname, etc.)
-- @return
-- A target info object; see one of getlinkinfo() or gettargetinfo()
-- for more information.
---
function config.buildtargetinfo(cfg, kind, field)
local basedir = cfg.project.location
local targetdir
if cfg.platform then
targetdir = path.join(basedir, 'bin', cfg.platform, cfg.buildcfg)
else
targetdir = path.join(basedir, 'bin', cfg.buildcfg)
end
local directory = cfg[field.."dir"] or cfg.targetdir or targetdir
local basename = cfg[field.."name"] or cfg.targetname or cfg.project.name
local prefix = cfg[field.."prefix"] or cfg.targetprefix or ""
local suffix = cfg[field.."suffix"] or cfg.targetsuffix or ""
local extension = cfg[field.."extension"] or cfg.targetextension or ""
local bundlename = ""
local bundlepath = ""
if table.contains(os.getSystemTags(cfg.system), "darwin") and (kind == p.WINDOWEDAPP or (kind == p.SHAREDLIB and cfg.sharedlibtype)) then
bundlename = basename .. extension
bundlepath = path.join(bundlename, iif(kind == p.SHAREDLIB and cfg.sharedlibtype == "OSXFramework", "Versions/A", "Contents/MacOS"))
end
local info = {}
info.directory = directory
info.basename = basename .. suffix
info.name = prefix .. info.basename .. extension
info.extension = extension
info.abspath = path.join(directory, info.name)
info.fullpath = info.abspath
info.bundlename = bundlename
info.bundlepath = path.join(directory, bundlepath)
info.prefix = prefix
info.suffix = suffix
return info
end
---
-- Determine whether the given configuration can meaningfully link
-- against the target object.
--
-- @param cfg
-- The configuration to be tested.
-- @param target
-- The object to test against. This can be a library file name, or a
-- configuration from another project.
-- @param linkage
-- Optional. For languages or environments that support different kinds of
-- linking (i.e. Managed/CLR C++, which can link both managed and unmanaged
-- libs), which one to return. One of "unmanaged", "managed". If not
-- specified, the default for the configuration will be used.
-- @return
-- True if linking the target into the configuration makes sense.
---
function config.canLink(cfg, target, linkage)
-- Have I got a project configuration? If so, I've got some checks
-- I can do with the extra information
if type(target) ~= "string" then
-- Can't link against executables
if target.kind ~= "SharedLib" and target.kind ~= "StaticLib" then
return false
end
-- Can link mixed C++ with native projects
if cfg.language == "C++" then
if cfg.clr == p.ON then
return true
end
end
if target.language == "C++" then
if target.clr == p.ON then
return true
end
end
-- Can't link managed and unmanaged projects
local cfgManaged = project.isdotnet(cfg.project) or (cfg.clr ~= p.OFF)
local tgtManaged = project.isdotnet(target.project) or (target.clr ~= p.OFF)
return (cfgManaged == tgtManaged)
end
-- For now, I assume that everything listed in a .NET project can be
-- linked; unmanaged code is simply not supported
if project.isdotnet(cfg.project) then
return true
end
-- In C++ projects, managed dependencies must explicitly include
-- the ".dll" extension, to distinguish from unmanaged libraries
local isManaged = (path.getextension(target) == ".dll")
-- Unmanaged projects can never link managed assemblies
if isManaged and cfg.clr == p.OFF then
return false
end
-- Only allow this link it matches the requested linkage
return (isManaged) == (linkage == "managed")
end
--
-- Determines if this configuration can be linked incrementally.
--
function config.canLinkIncremental(cfg)
if cfg.kind == "StaticLib"
or config.isOptimizedBuild(cfg)
or cfg.flags.NoIncrementalLink then
return false
end
return true
end
--
-- Check a configuration for a source code file with the specified
-- extension. Used for locating special files, such as Windows
-- ".def" module definition files.
--
-- @param cfg
-- The configuration object to query.
-- @param ext
-- The file extension for which to search.
-- @return
-- The full file name if found, nil otherwise.
--
function config.findfile(cfg, ext)
for _, fname in ipairs(cfg.files) do
if fname:endswith(ext) then
return project.getrelative(cfg.project, fname)
end
end
end
---
-- Retrieve linking information for a specific configuration. That is,
-- the path information that is required to link against the library
-- built by this configuration.
--
-- @param cfg
-- The configuration object to query.
-- @return
-- A table with these values:
-- basename - the target with no directory or file extension
-- name - the target name and extension, with no directory
-- directory - relative path to the target, with no file name
-- extension - the file extension
-- prefix - the file name prefix
-- suffix - the file name suffix
-- fullpath - directory, name, and extension relative to project
-- abspath - absolute directory, name, and extension
---
function config.getlinkinfo(cfg)
-- if the configuration target is a DLL, and an import library
-- is provided, change the kind as import libraries are static.
local kind = cfg.kind
if project.isnative(cfg.project) then
if cfg.system == p.WINDOWS and kind == p.SHAREDLIB and not cfg.flags.NoImportLib then
kind = p.STATICLIB
end
end
return config.buildtargetinfo(cfg, kind, "implib")
end
--
-- Retrieve a list of link targets from a configuration.
--
-- @param cfg
-- The configuration object to query.
-- @param kind
-- The type of links to retrieve; one of:
-- siblings - linkable sibling projects
-- system - system (non-sibling) libraries
-- dependencies - all sibling dependencies, including non-linkable
-- all - return everything
-- @param part
-- How the link target should be expressed; one of:
-- name - the decorated library name with no directory
-- basename - the undecorated library name
-- directory - just the directory, no name
-- fullpath - full path with decorated name
-- object - return the project object of the dependency
-- Or, a function(original, decorated) can be supplied, in which case it
-- will be called for each matching link, providing the original value as
-- it was specified in links(), and the decorated value.
-- @param linkage
-- Optional. For languages or environments that support different kinds of
-- linking (i.e. Managed/CLR C++, which can link both managed and unmanaged
-- libs), which one to return. One of "unmanaged", "managed". If not
-- specified, the default for the configuration will be used.
-- @return
-- An array containing the requested link target information.
--
function config.getlinks(cfg, kind, part, linkage)
local result = {}
-- If I'm building a list of link directories, include libdirs
if part == "directory" then
table.foreachi(cfg.libdirs, function(dir)
table.insert(result, project.getrelative(cfg.project, dir))
end)
end
-- Iterate all of the links listed in the configuration and boil
-- them down to the requested data set
for i = 1, #cfg.links do
local link = cfg.links[i]
local item
-- Sort the links into "sibling" (is another project in this same
-- workspace) and "system" (is not part of this workspace) libraries.
local prj = p.workspace.findproject(cfg.workspace, link)
if prj and kind ~= "system" then
-- Sibling; is there a matching configuration in this project that
-- is compatible with linking to me?
local prjcfg = project.getconfig(prj, cfg.buildcfg, cfg.platform)
if prjcfg and (kind == "dependencies" or config.canLink(cfg, prjcfg)) then
-- Yes; does the caller want the whole project config or only part?
if part == "object" then
item = prjcfg
else
item = project.getrelative(cfg.project, prjcfg.linktarget.fullpath)
end
end
elseif not prj and (kind == "system" or kind == "all") then
-- Make sure this library makes sense for the requested linkage; don't
-- link managed .DLLs into unmanaged code, etc.
if config.canLink(cfg, link, linkage) then
-- if the target is listed via an explicit path (i.e. not a
-- system library or assembly), make it project-relative
item = link
if item:find("/", nil, true) then
item = project.getrelative(cfg.project, item)
end
end
end
-- If this is something I can link against, pull out the requested part
-- dont link against my self
if item and item ~= cfg then
if part == "directory" then
item = path.getdirectory(item)
if item == "." then
item = nil
end
elseif part == "name" then
item = path.getname(item)
elseif part == "basename" then
item = path.getbasename(item)
elseif type(part) == "function" then
part(link, item)
end
end
-- Add it to the list, skipping duplicates
if item and not table.contains(result, item) then
table.insert(result, item)
end
end
return result
end
--
-- Returns the list of sibling target directories
--
-- @param cfg
-- The configuration object to query.
-- @return
-- Absolute path list
--
function config.getsiblingtargetdirs(cfg)
local paths = {}
for _, sibling in ipairs(config.getlinks(cfg, "siblings", "object")) do
if (sibling.kind == p.SHAREDLIB) then
local p = sibling.linktarget.directory
if not (table.contains(paths, p)) then
table.insert(paths, p)
end
end
end
return paths
end
--
-- Determines the correct runtime library for a configuration.
--
-- @param cfg
-- The configuration object to query.
-- @return
-- A string identifying the runtime library, one of
-- StaticDebug, StaticRelease, SharedDebug, SharedRelease.
--
function config.getruntime(cfg)
if (not cfg.staticruntime or cfg.staticruntime == "Default") and not cfg.runtime then
return nil -- indicate that no runtime was explicitly selected
end
local linkage = iif(cfg.staticruntime == "On", "Static", "Shared") -- assume 'Shared' is default?
if not cfg.runtime then
return linkage .. iif(config.isDebugBuild(cfg), "Debug", "Release")
else
return linkage .. cfg.runtime
end
end
--
-- Retrieve information about a configuration's build target.
--
-- @param cfg
-- The configuration object to query.
-- @return
-- A table with these values:
-- basename - the target with no directory or file extension
-- name - the target name and extension, with no directory
-- directory - relative path to the target, with no file name
-- extension - the file extension
-- prefix - the file name prefix
-- suffix - the file name suffix
-- fullpath - directory, name, and extension, relative to project
-- abspath - absolute directory, name, and extension
-- bundlepath - the relative path and file name of the bundle
--
function config.gettargetinfo(cfg)
return config.buildtargetinfo(cfg, cfg.kind, "target")
end
---
-- Returns true if any of the files in the provided container pass the
-- provided test function.
---
function config.hasFile(self, testfn)
local files = self.files
for i = 1, #files do
if testfn(files[i]) then
return true
end
end
return false
end
--
-- Determine if the specified library or assembly reference should be copied
-- to the build's target directory. "Copy Local" is the terminology used by
-- Visual Studio C# projects for this feature.
--
-- @param cfg
-- The configuration to query. Can be a project (and will be for C#
-- projects).
-- @param linkname
-- The name of the library or assembly reference to check. This should
-- match the name as it was provided in the call to links().
-- @param default
-- The value to return if the library is not mentioned in any settings.
-- @return
-- True if the library should be copied local, false otherwise.
--
function config.isCopyLocal(cfg, linkname, default)
if cfg.flags.NoCopyLocal then
return false
end
if #cfg.copylocal > 0 then
return table.contains(cfg.copylocal, linkname)
end
return default
end
--
-- Determine if a configuration represents a "debug" or "release" build.
-- This controls the runtime library selected for Visual Studio builds
-- (and might also be useful elsewhere).
--
function config.isDebugBuild(cfg)
return cfg.symbols ~= nil and
cfg.symbols ~= p.OFF and
cfg.symbols ~= "Default" and
not config.isOptimizedBuild(cfg)
end
--
-- Determine if this configuration uses one of the optimize flags.
-- Optimized builds get different treatment, such as full linking
-- instead of incremental.
--
function config.isOptimizedBuild(cfg)
return cfg.optimize ~= nil and cfg.optimize ~= p.OFF and cfg.optimize ~= "Debug"
end
--
-- Does this configuration's list of links contain the specified
-- project? Performs a case-insensitive search for the project's
-- name in the configuration's link array.
--
-- @param cfg
-- The configuration to query.
-- @param prjName
-- The name of the project for which to search.
-- @return
-- True if the project name is found in the configuration's
-- list of links; nil otherwise.
--
function config.linksToProject(cfg, prjName)
prjName = prjName:lower()
local n = #cfg.links
for i = 1,n do
if cfg.links[i]:lower() == prjName then
return true
end
end
end
--
-- Map the values contained in the configuration to an array of flags.
--
-- @param cfg
-- The configuration to map.
-- @param mappings
-- A mapping from configuration fields and values to flags. See
-- the GCC tool interface for examples of these mappings.
-- @return
-- An array containing the translated flags.
--
function config.mapFlags(cfg, mappings)
local flags = {}
-- Helper function to append replacement values to the result
local function add(replacement)
if type(replacement) == "function" then
replacement = replacement(cfg)
end
table.insertflat(flags, replacement)
end
-- To ensure we get deterministic results that don't change as more keys
-- are added to the map, and to open the possibility to controlling the
-- application order of flags, use a prioritized list of fields to order
-- the mapping, even though it takes a little longer.
for field in p.field.eachOrdered() do
local map = mappings[field.name]
if type(map) == "function" then
map = map(cfg, mappings)
end
if map then
-- Pass each cfg value in the list through the map and append the
-- replacement, if any, to the result
local values = cfg[field.name]
if type(values) == "boolean" then
values = iif(values, "On", "Off")
end
if type(values) ~= "table" then
values = { values }
end
local foundValue = false
table.foreachi(values, function(value)
local replacement = map[value]
if replacement ~= nil then
foundValue = true
add(replacement)
end
end)
-- If no value was mapped, check to see if the map specifies a
-- default value and, if so, push that into the result
if not foundValue then
add(map._)
end
-- Finally, check for "not values", which should be added to the
-- result if the corresponding value is not present
for key, replacement in pairs(map) do
if #key > 1 and key:startswith("_") then
key = key:sub(2)
if values[key] == nil then
add(replacement)
end
end
end
end
end
return flags
end
---
-- Returns both a project configuration and a file configuration from a
-- configuration argument that could be either.
--
-- @param cfg
-- A project or file configuration object.
-- @return
-- Both a project configuration and a file configuration. If the input
-- argument is a project configuration, the file configuration value is
-- returned as nil.
---
function config.normalize(cfg)
if cfg and cfg.config ~= nil then
return cfg.config, cfg
else
return cfg, nil
end
end
---
-- Return the appropriate toolset adapter for the provided configuration,
-- or nil if no toolset is specified. If a specific version was provided,
-- returns that as a second argument.
---
function config.toolset(cfg)
if cfg.toolset then
return p.tools.canonical(cfg.toolset)
end
end