forked from premake/premake-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_foundation.lua
More file actions
438 lines (361 loc) · 10.8 KB
/
_foundation.lua
File metadata and controls
438 lines (361 loc) · 10.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
---
-- Base definitions required by all the other scripts.
-- @copyright 2002-2015 Jason Perkins and the Premake project
---
premake = premake or {}
premake._VERSION = _PREMAKE_VERSION
package.loaded["premake"] = premake
premake.modules = {}
premake.extensions = premake.modules
local semver = dofile('semver.lua')
local p = premake
-- Keep track of warnings that have been shown, so they don't get shown twice
local _warnings = {}
-- Keep track of aliased functions, so I can resolve to canonical names
local _aliases = {}
--
-- Define some commonly used symbols, for future-proofing.
--
premake.C = "C"
premake.C7 = "c7"
premake.CLANG = "clang"
premake.CONSOLEAPP = "ConsoleApp"
premake.CPP = "C++"
premake.CSHARP = "C#"
premake.GCC = "gcc"
premake.HAIKU = "haiku"
premake.ANDROID = "android"
premake.IOS = "ios"
premake.LINUX = "linux"
premake.MACOSX = "macosx"
premake.MAKEFILE = "Makefile"
premake.MBCS = "MBCS"
premake.NONE = "None"
premake.DEFAULT = "Default"
premake.OBJECTIVEC = "Objective-C"
premake.OBJECTIVECPP = "Objective-C++"
premake.ON = "On"
premake.OFF = "Off"
premake.POSIX = "posix"
premake.PS3 = "ps3"
premake.SHAREDITEMS = "SharedItems"
premake.SHAREDLIB = "SharedLib"
premake.STATICLIB = "StaticLib"
premake.UNICODE = "Unicode"
premake.UNIVERSAL = "universal"
premake.UTILITY = "Utility"
premake.PACKAGING = "Packaging"
premake.WINDOWEDAPP = "WindowedApp"
premake.WINDOWS = "windows"
premake.X86 = "x86"
premake.X86_64 = "x86_64"
premake.ARM = "ARM"
premake.ARM64 = "ARM64"
---
-- Provide an alias for a function in a namespace. Calls to the alias will
-- invoke the canonical function, and attempts to override the alias will
-- instead override the canonical call.
--
-- @param scope
-- The table containing the function to be overridden. Use _G for
-- global functions.
-- @param canonical
-- The name of the function to be aliased (a string value)
-- @param alias
-- The new alias for the function (another string value).
---
function p.alias(scope, canonical, alias)
scope, canonical = p.resolveAlias(scope, canonical)
if not scope[canonical] then
error("unable to alias '" .. canonical .. "'; no such function", 2)
end
_aliases[scope] = _aliases[scope] or {}
_aliases[scope][alias] = canonical
scope[alias] = function(...)
return scope[canonical](...)
end
end
---
-- Call a list of functions.
--
-- @param funcs
-- The list of functions to be called, or a function that can be called
-- to build and return the list. If this is a function, it will be called
-- with all of the additional arguments (below).
-- @param ...
-- An optional set of arguments to be passed to each of the functions as
-- as they are called.
---
function premake.callArray(funcs, ...)
if type(funcs) == "function" then
funcs = funcs(...)
end
if funcs then
for i = 1, #funcs do
funcs[i](...)
end
end
end
-- TODO: THIS IMPLEMENTATION IS GOING AWAY
function premake.callarray(namespace, array, ...)
local n = #array
for i = 1, n do
local fn = namespace[array[i]]
if not fn then
error(string.format("Unable to find function '%s'", array[i]))
end
fn(...)
end
end
---
-- Compare a version string that uses semver semantics against a
-- version comparision string. Comparisions take the form of ">=5.0" (5.0 or
-- later), "5.0" (5.0 or later), ">=5.0 <6.0" (5.0 or later but not 6.0 or
-- later).
--
-- @param version
-- The version to be tested.
-- @param checks
-- The comparision string to be evaluated.
-- @return
-- True if the comparisions pass, false if any fail.
---
function p.checkVersion(version, checks)
if not version then
return false
end
-- try to parse semver, if it fails, it's not semver compatible and we cannot compare, in which case
-- we're going to ignore the checkVersion entirely, but warn.
if not premake.isSemVer(version) then
p.warn("'" .. version .. "' is not semver compatible, and cannot be compared against '" .. checks .. "'.");
return true
end
-- now compare the semver against the checks.
local function eq(a, b) return a == b end
local function le(a, b) return a <= b end
local function lt(a, b) return a < b end
local function ge(a, b) return a >= b end
local function gt(a, b) return a > b end
local function compat(a, b) return a ^ b end
version = semver(version)
checks = string.explode(checks, " ", true)
for i = 1, #checks do
local check = checks[i]
local func
if check:startswith(">=") then
func = ge
check = check:sub(3)
elseif check:startswith(">") then
func = gt
check = check:sub(2)
elseif check:startswith("<=") then
func = le
check = check:sub(3)
elseif check:startswith("<") then
func = lt
check = check:sub(2)
elseif check:startswith("=") then
func = eq
check = check:sub(2)
elseif check:startswith("^") then
func = compat
check = check:sub(2)
else
func = ge
end
check = semver(check)
if not func(version, check) then
return false
end
end
return true
end
function premake.clearWarnings()
_warnings = {}
end
--
-- Raise an error, with a formatted message built from the provided
-- arguments.
--
-- @param message
-- The error message, which may contain string formatting tokens.
-- @param ...
-- Values to fill in the string formatting tokens.
--
function premake.error(message, ...)
error(string.format("** Error: " .. message, ...), 0)
end
--
-- Finds the correct premake script filename to be run.
--
-- @param fname
-- The filename of the script to run.
-- @return
-- The correct location and filename of the script to run.
--
function premake.findProjectScript(fname)
return os.locate(fname, fname .. ".lua", path.join(fname, "premake5.lua"), path.join(fname, "premake4.lua"))
end
---
-- "Immediate If" - returns one of the two values depending on the value
-- of the provided condition. Note that both the true and false expressions
-- will be evaluated regardless of the condition, even if only one result
-- is returned.
--
-- @param condition
-- A boolean condition, determining which value gets returned.
-- @param trueValue
-- The value to return if the condition is true.
-- @param falseValue
-- The value to return if the condition is false.
-- @return
-- One of trueValue or falseValue.
---
function iif(condition, trueValue, falseValue)
if condition then
return trueValue
else
return falseValue
end
end
---
-- Override an existing function with a new implementation; the original
-- function is passed as the first argument to the replacement when called.
--
-- @param scope
-- The table containing the function to be overridden. Use _G for
-- global functions.
-- @param name
-- The name of the function to override (a string value).
-- @param repl
-- The replacement function. The first argument to the function
-- will be the original implementation, followed by the arguments
-- passed to the original call.
---
function premake.override(scope, name, repl)
scope, name = p.resolveAlias(scope, name)
local original = scope[name]
if not original then
error("unable to override '" .. name .. "'; no such function", 2)
end
scope[name] = function(...)
return repl(original, ...)
end
-- Functions from premake.main are special in that they are fetched
-- from an array, which can be modified by system and project scripts,
-- instead of a function which would have already been called before
-- those scripts could have run. Since the array will have already
-- been evaluated by the time override() is called, the new value
-- won't be picked up as it would with the function-fetched call
-- lists. Special case the workaround for that here so everyone else
-- can just override without having to think about the difference.
if scope == premake.main then
table.replace(premake.main.elements, original, scope[name])
end
end
---
-- Find the canonical name and scope of a function, resolving any aliases.
--
-- @param scope
-- The table containing the function to be overridden. Use _G for
-- global functions.
-- @param name
-- The name of the function to resolve.
-- @return
-- The canonical scope and function name (a string value).
---
function p.resolveAlias(scope, name)
local aliases = _aliases[scope]
if aliases then
while aliases[name] do
name = aliases[name]
end
end
return scope, name
end
--
-- Display a warning, with a formatted message built from the provided
-- arguments.
--
-- @param message
-- The warning message, which may contain string formatting tokens.
-- @param ...
-- Values to fill in the string formatting tokens.
--
function premake.warn(message, ...)
message = string.format(message, ...)
if _OPTIONS.fatal then
error(message)
else
term.pushColor(term.warningColor)
io.stderr:write(string.format("** Warning: " .. message .. "\n", ...))
term.popColor();
end
end
--
-- Displays a warning just once per run.
--
-- @param key
-- A unique key to identify this warning. Subsequent warnings messages
-- using the same key will not be shown.
-- @param message
-- The warning message, which may contain string formatting tokens.
-- @param ...
-- Values to fill in the string formatting tokens.
--
function premake.warnOnce(key, message, ...)
if not _warnings[key] then
_warnings[key] = true
premake.warn(message, ...)
end
end
--
-- Display information in the term.infoColor color.
--
-- @param message
-- The info message, which may contain string formatting tokens.
-- @param ...
-- Values to fill in the string formatting tokens.
--
function premake.info(message, ...)
message = string.format(message, ...)
term.pushColor(term.infoColor)
io.stdout:write(string.format("** Info: " .. message .. "\n", ...))
term.popColor();
end
--
-- A shortcut for printing formatted output.
--
function printf(msg, ...)
print(string.format(msg, ...))
end
--
-- A shortcut for printing formatted output in verbose mode.
--
function verbosef(msg, ...)
if _OPTIONS.verbose then
print(string.format(msg, ...))
end
end
--
-- make a string from debug.getinfo information.
--
function filelineinfo(level)
local info = debug.getinfo(level+1, "Sl")
if info == nil then
return nil
end
if info.what == "C" then
return "C function"
else
local sep = iif(os.ishost('windows'), '\\', '/')
return string.format("%s(%d)", path.translate(info.short_src, sep), info.currentline)
end
end
---
-- check if version is semver.
---
function premake.isSemVer(version)
local sMajor, sMinor, sPatch, sPrereleaseAndBuild = version:match("^(%d+)%.?(%d*)%.?(%d*)(.-)$")
return (type(sMajor) == 'string')
end