forked from premake/premake-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_premake_main.lua
More file actions
398 lines (314 loc) · 8.28 KB
/
_premake_main.lua
File metadata and controls
398 lines (314 loc) · 8.28 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
--
-- _premake_main.lua
-- Script-side entry point for the main program logic.
-- Copyright (c) 2002-2015 Jason Perkins and the Premake project
--
local shorthelp = "Type 'premake5 --help' for help"
local versionhelp = "premake5 (Premake Build Script Generator) %s"
local startTime = os.clock()
-- set a global.
_PREMAKE_STARTTIME = startTime
-- Load the collection of core scripts, required for everything else to work
local modules = dofile("_modules.lua")
local manifest = dofile("_manifest.lua")
for i = 1, #manifest do
dofile(manifest[i])
end
-- Create namespaces for myself
local p = premake
p.main = {}
local m = p.main
-- Keep a table of modules that have been preloaded, and their associated
-- "should load" test functions.
m._preloaded = {}
---
-- Add a new module loader that knows how to use the Premake paths like
-- PREMAKE_PATH and the --scripts option, and follows the module/module.lua
-- naming convention.
---
function m.installModuleLoader()
if not os.ishost('windows') then
local premakeDir = path.getdirectory(_PREMAKE_COMMAND)
package.cpath = package.cpath .. ';' .. premakeDir .. '/?.so'
end
table.insert(package.searchers, 2, m.moduleLoader)
end
function m.moduleLoader(name)
local dir = path.getdirectory(name)
local base = path.getname(name)
if dir ~= "." then
dir = dir .. "/" .. base
else
dir = base
end
local full = dir .. "/" .. base .. ".lua"
-- list of paths where to look for the module
local paths = {
".modules/" .. full,
"modules/" .. full,
full,
name .. ".lua"
}
-- If this module is being requested by an embedded script, favor embedded modules.
-- This helps prevent local scripts from interfering with release build bootstrapping.
if string.startswith(_SCRIPT_DIR, '$/') then
table.insert(paths, 1, '$/' .. full)
end
-- try to locate the module
for _, p in ipairs(paths) do
local file = os.locate(p)
if file then
local chunk, err = loadfile(file)
if chunk then
return chunk
end
if err then
return "\n\tload error " .. err
end
end
end
-- didn't find the module in supported paths, try the embedded scripts
for _, p in ipairs(paths) do
local chunk, err = loadfile("$/" .. p)
if chunk then
return chunk
end
end
end
---
-- Prepare the script environment; anything that should be done
-- before the system script gets a chance to run.
---
function m.prepareEnvironment()
math.randomseed(os.time())
_PREMAKE_DIR = path.getdirectory(_PREMAKE_COMMAND)
p.path = p.path .. ";" .. _PREMAKE_DIR .. ";" .. _MAIN_SCRIPT_DIR
end
---
-- Load the required core modules that are shipped as part of Premake and
-- expected to be present at startup. If a _preload.lua script is present,
-- that script is run and the return value (a "should load" test) is cached
-- to be called after baking is complete. Otherwise the module's main script
-- is run immediately.
---
function m.preloadModules()
for i = 1, #modules do
local name = modules[i]
local preloader = name .. "/_preload.lua"
preloader = os.locate("modules/" .. preloader) or os.locate(preloader)
if preloader then
local modulePath = path.getdirectory(preloader)
m._preloaded[modulePath] = include(preloader)
if not m._preloaded[modulePath] then
p.warn("module '%s' should return function from _preload.lua", name)
end
else
require(name)
end
end
end
---
-- Look for and run the system-wide configuration script; make sure any
-- configuration scoping gets cleared before continuing.
---
function m.runSystemScript()
dofileopt(_OPTIONS["systemscript"] or { "premake5-system.lua", "premake-system.lua" })
filter {}
end
---
-- Look for a user project script, and set up the related global
-- variables if I can find one.
---
function m.locateUserScript()
local defaults = { "premake5.lua", "premake4.lua" }
for i = 1, #defaults do
if os.isfile(defaults[i]) then
_MAIN_SCRIPT = defaults[i]
break
end
end
if not _MAIN_SCRIPT then
_MAIN_SCRIPT = defaults[1]
end
if _OPTIONS.file then
_MAIN_SCRIPT = _OPTIONS.file
end
_MAIN_SCRIPT = path.getabsolute(_MAIN_SCRIPT)
_MAIN_SCRIPT_DIR = path.getdirectory(_MAIN_SCRIPT)
end
---
-- Set the action to be performed from the command line arguments.
---
function m.prepareAction()
p.action.set(_ACTION)
-- Allow the action to initialize stuff.
local action = p.action.current()
if action then
p.action.initialize(action.trigger)
end
end
---
-- If there is a project script available, run it to get the
-- project information, available options and actions, etc.
---
function m.runUserScript()
if os.isfile(_MAIN_SCRIPT) then
dofile(_MAIN_SCRIPT)
end
end
---
-- Run the interactive prompt, if requested.
---
function m.checkInteractive()
if _OPTIONS.interactive then
debug.prompt()
end
end
---
-- Validate and process the command line options and arguments.
---
function m.processCommandLine()
-- Process special options
if (_OPTIONS["version"]) then
printf(versionhelp, _PREMAKE_VERSION)
os.exit(0)
end
if (_OPTIONS["help"]) then
p.showhelp()
os.exit(1)
end
-- Validate the command-line arguments. This has to happen after the
-- script has run to allow for project-specific options
ok, err = p.option.validate(_OPTIONS)
if not ok then
print("Error: " .. err)
os.exit(1)
end
-- If no further action is possible, show a short help message
if not _OPTIONS.interactive then
if not _ACTION then
print(shorthelp)
os.exit(1)
end
local action = p.action.current()
if not action then
print("Error: no such action '" .. _ACTION .. "'")
os.exit(1)
end
if p.action.isConfigurable() and not os.isfile(_MAIN_SCRIPT) then
print(string.format("No Premake script (%s) found!", path.getname(_MAIN_SCRIPT)))
os.exit(1)
end
end
end
---
-- Start up MobDebug and try to hook up with ZeroBrane
---
function m.tryHookDebugger()
if (_OPTIONS["debugger"]) then
print("Loading luasocket...")
require('luasocket')
print("Starting debugger...")
local mobdebug = require('mobdebug')
mobdebug.start()
end
end
---
-- Override point, for logic that should run before baking.
---
function m.preBake()
if p.action.isConfigurable() then
print("Building configurations...")
end
end
---
-- "Bake" the project information, preparing it for use by the action.
---
function m.bake()
if p.action.isConfigurable() then
p.oven.bake()
end
end
---
-- Override point, for logic that should run after baking but before
-- the configurations are validated.
---
function m.postBake()
local function shouldLoad(func)
for wks in p.global.eachWorkspace() do
for prj in p.workspace.eachproject(wks) do
for cfg in p.project.eachconfig(prj) do
if func(cfg) then
return true
end
end
end
end
end
-- any modules need to load to support this project?
for modulePath, func in pairs(m._preloaded) do
local moduleName = path.getbasename(modulePath)
if not package.loaded[moduleName] and shouldLoad(func) then
_SCRIPT_DIR = modulePath
require(moduleName)
end
end
end
---
-- Sanity check the current project setup.
---
function m.validate()
if p.action.isConfigurable() then
p.container.validate(p.api.rootContainer())
end
end
---
-- Override point, for logic that should run after validation and
-- before the action takes control.
---
function m.preAction()
local action = p.action.current()
printf("Running action '%s'...", action.trigger)
end
---
-- Hand over control to the action.
---
function m.callAction()
local action = p.action.current()
p.action.call(action.trigger)
end
---
-- Processing is complete.
---
function m.postAction()
if p.action.isConfigurable() then
local duration = math.floor((os.clock() - startTime) * 1000);
printf("Done (%dms).", duration)
end
end
--
-- Script-side program entry point.
--
m.elements = {
m.tryHookDebugger,
m.installModuleLoader,
m.locateUserScript,
m.prepareEnvironment,
m.preloadModules,
m.runSystemScript,
m.prepareAction,
m.runUserScript,
m.checkInteractive,
m.processCommandLine,
m.preBake,
m.bake,
m.postBake,
m.validate,
m.preAction,
m.callAction,
m.postAction,
}
function _premake_main()
p.callArray(m.elements)
return 0
end