Repository navigation
Expand file tree
/
Copy pathmake.js
More file actions
681 lines (600 loc) · 25.6 KB
/
Copy pathmake.js
File metadata and controls
681 lines (600 loc) · 25.6 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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
// TerranMAKE -- a MAKE implementation for TSVM / TVDOS
// Copyright (C) CuriousTorvald. Licence: GPL-3.0-or-later
//
// A deliberate subset of GNU Make. It aims to be Makefile-compatible for the
// common case while NOT supporting conditionals, loops, recursion, functions or
// dynamic (pattern/implicit) rule generation. It DOES support explicit rules,
// variables (= := ?= +=), variable expansion incl. automatic variables, and the
// .PHONY / .SILENT / .IGNORE special targets.
//
// -- Why a content hash instead of timestamps -------------------------------
// GNU Make decides whether a target is out of date by comparing file
// modification times. TSVM's disk protocol never exposes a file's mtime to a
// JS program (LISTFILES gives type+name, GETLEN gives size, there is no STAT),
// so a faithful mtime comparison is impossible on this platform. TerranMAKE
// therefore records a content fingerprint of every prerequisite the last time a
// target was successfully built (in a ".tmake.state" sidecar next to the
// Makefile) and rebuilds a target when it is missing, phony, or when any
// prerequisite's fingerprint has changed since then. The observable effect --
// "I edited a source, so its dependents rebuild" -- matches Make; only the
// mechanism differs (and `touch`-without-edit will NOT force a rebuild, unlike
// real make).
const PROGRAM = "make"
const VERSION = "TerranMAKE 1.0.0"
const STATE_FILE = ".tmake.state"
// ---------------------------------------------------------------------------
// small helpers
// ---------------------------------------------------------------------------
function rtrim(s) { return s.replace(/[ \t]+$/, "") }
function ltrim(s) { return s.replace(/^[ \t]+/, "") }
function trim(s) { return s.replace(/^[ \t]+/, "").replace(/[ \t]+$/, "") }
function splitWords(s) {
s = trim(s)
if (s.length === 0) return []
return s.split(/[ \t]+/)
}
function uniq(arr) {
let seen = {}
let out = []
for (let i = 0; i < arr.length; i++) {
if (!Object.prototype.hasOwnProperty.call(seen, arr[i])) {
seen[arr[i]] = true
out.push(arr[i])
}
}
return out
}
// FNV-1a over the full char codes, salted with length -> stable hex fingerprint
function contentHash(str) {
let h = 0x811c9dc5
for (let i = 0; i < str.length; i++) {
h ^= str.charCodeAt(i)
h = Math.imul(h, 0x01000193) >>> 0
}
return str.length.toString(16) + ":" + (h >>> 0).toString(16)
}
// ---------------------------------------------------------------------------
// filesystem shims (all disk access goes through here)
// ---------------------------------------------------------------------------
function resolve(name) { return _G.shell.resolvePathInput(name).full }
function fileExists(name) {
try { return files.open(resolve(name)).exists }
catch (e) { return false }
}
function readFile(name) {
return files.open(resolve(name)).sread()
}
function writeFile(name, text) {
files.open(resolve(name)).swrite(text)
}
// fingerprint of a prerequisite: null when it does not exist, else a content
// hash (falling back to an existence marker if the bytes cannot be read as text)
function fingerprint(name) {
if (!fileExists(name)) return null
try { return contentHash(readFile(name)) }
catch (e) { return "exists" }
}
// ---------------------------------------------------------------------------
// state file (persisted content fingerprints)
// ---------------------------------------------------------------------------
function loadState() {
if (!fileExists(STATE_FILE)) return { version: 1, targets: {} }
try {
let obj = JSON.parse(readFile(STATE_FILE))
if (!obj || !obj.targets) return { version: 1, targets: {} }
return obj
} catch (e) {
return { version: 1, targets: {} }
}
}
function saveState(state) {
try { writeFile(STATE_FILE, JSON.stringify(state)) }
catch (e) { printerrln(`${PROGRAM}: warning: could not write ${STATE_FILE}: ${e.message}`) }
}
// ===========================================================================
// Makefile object model
// ===========================================================================
function makeModel() {
return {
vars: {}, // name -> { value, flavor:'simple'|'recursive' }
overrides: {}, // name -> string (command line, always wins)
rules: {}, // target -> { prereqs, orderOnly, recipe, hasRecipe }
phony: {}, // target -> true
defaultGoal: null,
silentAll: false, // .SILENT with no prereqs
ignoreAll: false // .IGNORE with no prereqs
}
}
// ---- variable lookup + expansion ------------------------------------------
function lookupRaw(model, name) {
if (Object.prototype.hasOwnProperty.call(model.overrides, name))
return { value: model.overrides[name], flavor: "simple" }
if (Object.prototype.hasOwnProperty.call(model.vars, name))
return model.vars[name]
// fall back to the TVDOS environment (lowest priority)
try {
if (typeof _TVDOS !== "undefined" && _TVDOS.variables &&
_TVDOS.variables[name] !== undefined)
return { value: String(_TVDOS.variables[name]), flavor: "simple" }
} catch (e) { /* ignore */ }
return undefined
}
function resolveName(model, name, auto, stack) {
if (auto && Object.prototype.hasOwnProperty.call(auto, name)) return auto[name]
let r = lookupRaw(model, name)
if (r === undefined) return ""
if (r.flavor === "simple") return r.value
if (stack.indexOf(name) >= 0) return "" // recursion guard
return expand(model, r.value, auto, stack.concat([name]))
}
function expand(model, str, auto, stack) {
if (str === undefined || str === null) return ""
if (!stack) stack = []
let out = ""
for (let i = 0; i < str.length; i++) {
let c = str[i]
if (c !== "$") { out += c; continue }
let n = str[i + 1]
if (n === undefined) { out += "$"; break }
if (n === "$") { out += "$"; i++; continue }
if (n === "(" || n === "{") {
let close = (n === "(") ? ")" : "}"
let depth = 1, j = i + 2, inner = ""
while (j < str.length) {
let cj = str[j]
if (cj === n) depth++
else if (cj === close) { depth--; if (depth === 0) break }
inner += cj
j++
}
let nameExpr = expand(model, inner, auto, stack)
out += resolveName(model, nameExpr, auto, stack)
i = j // land on the closer; loop ++ moves past
continue
}
// single-character variable / automatic variable ($@ $< $^ ...)
out += resolveName(model, n, auto, stack)
i++
}
return out
}
// ---------------------------------------------------------------------------
// Makefile parsing
// ---------------------------------------------------------------------------
// Fold physical lines into logical lines, tracking whether each logical line is
// a recipe (its first physical line began with a TAB). Backslash-newline joins.
function foldLines(text) {
let phys = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n")
let logical = []
let i = 0
while (i < phys.length) {
let first = phys[i]
let isRecipe = (first.length > 0 && first[0] === "\t")
let acc = first
// count trailing backslashes: odd => continues onto the next line
while (i < phys.length) {
let m = acc.match(/(\\+)$/)
if (m && (m[1].length % 2) === 1) {
acc = acc.substring(0, acc.length - 1) // drop one trailing backslash
i++
let next = (i < phys.length) ? phys[i] : ""
acc += " " + ltrim(next)
} else break
}
logical.push({ text: acc, isRecipe: isRecipe })
i++
}
return logical
}
// strip an unescaped '#...' comment (non-recipe lines only)
function stripComment(line) {
let out = ""
for (let i = 0; i < line.length; i++) {
if (line[i] === "\\" && line[i + 1] === "#") { out += "#"; i++; continue }
if (line[i] === "#") break
out += line[i]
}
return out
}
const ASSIGN_RE = /^[ \t]*([A-Za-z_][A-Za-z0-9_.\-]*)[ \t]*(\?=|:=|\+=|=)([\s\S]*)$/
function applyAssignment(model, name, op, rhsRaw) {
// command-line overrides win: ignore in-file assignments to the same name
if (Object.prototype.hasOwnProperty.call(model.overrides, name)) return
let rhs = ltrim(rhsRaw)
let existing = Object.prototype.hasOwnProperty.call(model.vars, name) ? model.vars[name] : undefined
if (op === "=") {
model.vars[name] = { value: rhs, flavor: "recursive" }
} else if (op === ":=") {
model.vars[name] = { value: expand(model, rhs, null, []), flavor: "simple" }
} else if (op === "?=") {
if (existing === undefined && lookupRaw(model, name) === undefined)
model.vars[name] = { value: rhs, flavor: "recursive" }
} else if (op === "+=") {
if (existing === undefined) {
model.vars[name] = { value: rhs, flavor: "recursive" }
} else if (existing.flavor === "simple") {
let add = expand(model, rhs, null, [])
model.vars[name] = { value: existing.value + " " + add, flavor: "simple" }
} else {
model.vars[name] = { value: existing.value + " " + rhs, flavor: "recursive" }
}
}
}
function registerRule(model, target, prereqs, orderOnly, inlineRecipe) {
let dotTarget = (target[0] === ".")
// special targets
if (target === ".PHONY") {
prereqs.forEach(function (p) { model.phony[p] = true })
return null
}
if (target === ".SILENT") {
if (prereqs.length === 0) model.silentAll = true
else prereqs.forEach(function (p) {
if (!model.rules[p]) model.rules[p] = newRule()
model.rules[p].silent = true
})
return null
}
if (target === ".IGNORE") {
if (prereqs.length === 0) model.ignoreAll = true
return null
}
if (target === ".DEFAULT" || target === ".SUFFIXES" || target === ".PRECIOUS") {
return null // recognised but inert in this subset
}
let rule = model.rules[target]
if (!rule) { rule = newRule(); model.rules[target] = rule }
// merge prerequisites (make allows adding prereqs across several lines)
rule.prereqs = uniq(rule.prereqs.concat(prereqs))
rule.orderOnly = uniq(rule.orderOnly.concat(orderOnly))
if (inlineRecipe !== null && inlineRecipe !== undefined && trim(inlineRecipe).length > 0) {
if (rule.hasRecipe)
printerrln(`${PROGRAM}: warning: overriding recipe for target '${target}'`)
rule.recipe = [inlineRecipe]
rule.hasRecipe = true
}
if (!dotTarget && model.defaultGoal === null) model.defaultGoal = target
return target
}
function newRule() {
return { prereqs: [], orderOnly: [], recipe: [], hasRecipe: false, silent: false }
}
function parseMakefile(model, text) {
let logical = foldLines(text)
let currentTargets = null // targets awaiting recipe lines
for (let li = 0; li < logical.length; li++) {
let entry = logical[li]
if (entry.isRecipe) {
let body = entry.text.replace(/^\t/, "")
if (trim(body).length === 0) continue // empty command line
if (currentTargets === null) {
throw new Error("recipe commences before first target")
}
currentTargets.forEach(function (t) {
let r = model.rules[t]
if (r) { r.recipe.push(body); r.hasRecipe = true }
})
continue
}
let line = stripComment(entry.text)
if (trim(line).length === 0) { currentTargets = null; continue }
// strip an optional leading "export"/"unexport" keyword
let exp = line.match(/^[ \t]*(export|unexport)[ \t]+([\s\S]*)$/)
if (exp) {
if (exp[2].indexOf("=") < 0) { continue } // bare `export NAME` -> ignore
line = exp[2]
}
let am = line.match(ASSIGN_RE)
if (am) {
applyAssignment(model, am[1], am[2], am[3])
currentTargets = null
continue
}
// otherwise: a rule. find the separating colon (skip a leading "::")
let colon = line.indexOf(":")
if (colon < 0) {
throw new Error(`missing separator: ${trim(line)}`)
}
let targetsPart = line.substring(0, colon)
let rest = line.substring(colon + 1)
if (rest[0] === ":") rest = rest.substring(1) // double-colon rule
// an inline recipe after ';'
let inlineRecipe = null
let semi = rest.indexOf(";")
if (semi >= 0) {
inlineRecipe = rest.substring(semi + 1)
rest = rest.substring(0, semi)
}
// order-only prerequisites after '|'
let normalPart = rest, orderPart = ""
let bar = rest.indexOf("|")
if (bar >= 0) {
normalPart = rest.substring(0, bar)
orderPart = rest.substring(bar + 1)
}
let targets = splitWords(expand(model, targetsPart, null, []))
let prereqs = splitWords(expand(model, normalPart, null, []))
let orderOnly = splitWords(expand(model, orderPart, null, []))
if (targets.length === 0) throw new Error(`missing target: ${trim(line)}`)
let registered = []
targets.forEach(function (t) {
let name = registerRule(model, t, prereqs, orderOnly,
inlineRecipe !== null ? expandInline(model, inlineRecipe) : null)
if (name !== null) registered.push(name)
})
currentTargets = (registered.length > 0) ? registered : null
}
}
// inline recipes (after ';') are stored raw like TAB recipes; keep them raw so
// automatic variables expand at run time
function expandInline(model, s) { return s }
// ===========================================================================
// build engine
// ===========================================================================
function makeBuilder(model, state, opts) {
let built = {} // target -> was it (re)built this run
let inProgress = {} // for cycle detection
let newState = { version: 1, targets: {} }
// carry forward prior fingerprints so untouched targets keep their record
for (let k in state.targets)
if (Object.prototype.hasOwnProperty.call(state.targets, k))
newState.targets[k] = state.targets[k]
let failed = false
let recipesRun = 0 // count of recipe commands processed (real or dry-run)
function isPhony(t) { return model.phony[t] === true }
// build() returns whether the target is considered "changed" -- i.e. whether
// its dependents must be rebuilt. A recipe running counts as changed; a
// recipe-less target inherits change from its prerequisites (and .PHONY is
// always changed). This is distinct from whether any command actually ran,
// which is tracked separately via `recipesRun` for the up-to-date messaging.
function build(target, neededBy) {
if (Object.prototype.hasOwnProperty.call(built, target)) return built[target]
if (inProgress[target]) {
printerrln(`${PROGRAM}: Circular ${target} <- ${neededBy || "?"} dependency dropped.`)
built[target] = false
return false
}
inProgress[target] = true
let rule = model.rules[target]
// no rule at all: must be an existing source file, else it's an error
if (!rule) {
inProgress[target] = false
if (fileExists(target)) { built[target] = false; return false }
let via = neededBy ? `, needed by '${neededBy}'` : ""
printerrln(`${PROGRAM}: *** No rule to make target '${target}'${via}. Stop.`)
failed = true
built[target] = false
return false
}
let prereqs = rule.prereqs
let orderOnly = rule.orderOnly
// build prerequisites first
let anyPrereqChanged = false
for (let i = 0; i < prereqs.length; i++) {
if (failed && !opts.keepGoing) break
if (build(prereqs[i], target)) anyPrereqChanged = true
}
for (let i = 0; i < orderOnly.length; i++) {
if (failed && !opts.keepGoing) break
build(orderOnly[i], target)
}
if (failed && !opts.keepGoing) { inProgress[target] = false; built[target] = false; return false }
let phony = isPhony(target)
let changed = false
if (rule.hasRecipe) {
let exists = fileExists(target)
let record = newState.targets[target]
let changedPrereqs = []
let stateChanged = false
for (let i = 0; i < prereqs.length; i++) {
let p = prereqs[i]
let fp = fingerprint(p)
let prev = (record && record.deps) ? record.deps[p] : undefined
if (prev === undefined || prev !== fp) { stateChanged = true; changedPrereqs.push(p) }
}
let outOfDate = opts.alwaysMake || phony || !exists ||
anyPrereqChanged || stateChanged || (record === undefined)
if (outOfDate) {
let auto = {
"@": target,
"<": prereqs.length ? prereqs[0] : "",
"^": uniq(prereqs).join(" "),
"+": prereqs.join(" "),
"?": (changedPrereqs.length ? changedPrereqs : prereqs).join(" "),
"*": ""
}
let ok = runRecipe(target, rule.recipe, auto, rule)
if (!ok) {
failed = true
inProgress[target] = false
built[target] = false
return false
}
// record fresh prerequisite fingerprints for next time
if (!opts.dryRun && !phony) {
let deps = {}
for (let i = 0; i < prereqs.length; i++) deps[prereqs[i]] = fingerprint(prereqs[i])
for (let i = 0; i < orderOnly.length; i++) deps[orderOnly[i]] = fingerprint(orderOnly[i])
newState.targets[target] = { deps: deps }
}
changed = true
}
} else {
// a recipe-less target does no work itself, but propagates change
// upward from its prerequisites (and .PHONY forces it)
changed = anyPrereqChanged || phony
}
inProgress[target] = false
built[target] = changed
return changed
}
function runRecipe(target, recipe, auto, rule) {
for (let i = 0; i < recipe.length; i++) {
let raw = recipe[i]
let line = expand(model, raw, auto, [])
// parse leading prefixes @ - +
let silent = false, ignore = false, force = false
let k = 0
while (k < line.length) {
let ch = line[k]
if (ch === "@") { silent = true; k++ }
else if (ch === "-") { ignore = true; k++ }
else if (ch === "+") { force = true; k++ }
else break
}
let cmd = line.substring(k)
if (trim(cmd).length === 0) continue
recipesRun++ // there is a real command to run (or to print under -n)
let showEcho = !(silent || model.silentAll || (rule && rule.silent) || opts.silent)
if (opts.dryRun || showEcho) println(cmd)
if (opts.dryRun && !force) continue
let rc = 0
try {
rc = _G.shell.execute(cmd) | 0
} catch (e) {
rc = 1
printerrln(`${PROGRAM}: ${e.message}`)
}
if (rc !== 0 && !(ignore || model.ignoreAll || opts.ignoreErrors)) {
printerrln(`${PROGRAM}: *** [${target}] Error ${rc}`)
return false
}
if (rc !== 0) {
// ignored error, mirror make's note
printerrln(`${PROGRAM}: [${target}] Error ${rc} (ignored)`)
}
}
return true
}
return {
run: function (goals) {
for (let g = 0; g < goals.length; g++) {
let goal = goals[g]
let before = recipesRun
build(goal, null)
if (failed && !opts.keepGoing) break
// if nothing actually ran for this goal, report why
if (recipesRun === before && !failed) {
if (fileExists(goal))
println(`${PROGRAM}: '${goal}' is up to date.`)
else
println(`${PROGRAM}: Nothing to be done for '${goal}'.`)
}
}
if (!opts.dryRun) saveState(newState)
return failed ? 2 : 0
}
}
}
// ===========================================================================
// command-line handling
// ===========================================================================
function printUsage() {
println(`usage: ${PROGRAM} [options] [target ...] [VAR=value ...]`)
println("options:")
println(" -f FILE read FILE as the makefile (default: Makefile, makefile)")
println(" -C DIR change to DIR before doing anything")
println(" -n dry run: print recipes without executing them")
println(" -s silent: do not echo recipes")
println(" -B unconditionally rebuild every target")
println(" -i ignore errors from recipes")
println(" -k keep going after a failed target where possible")
println(" -h show this help")
println(" -v show version")
}
function parseArgs(argv) {
let opts = {
makefile: null, dir: null,
dryRun: false, silent: false, alwaysMake: false,
ignoreErrors: false, keepGoing: false,
help: false, version: false
}
let goals = []
let overrides = {}
let i = 0
while (i < argv.length) {
let a = argv[i]
if (a === "-f" || a === "--file" || a === "--makefile") { opts.makefile = argv[++i] }
else if (a.indexOf("--file=") === 0) { opts.makefile = a.substring(7) }
else if (a === "-C" || a === "--directory") { opts.dir = argv[++i] }
else if (a.indexOf("--directory=") === 0) { opts.dir = a.substring(12) }
else if (a === "-n" || a === "--dry-run" || a === "--just-print") { opts.dryRun = true }
else if (a === "-s" || a === "--silent" || a === "--quiet") { opts.silent = true }
else if (a === "-B" || a === "--always-make") { opts.alwaysMake = true }
else if (a === "-i" || a === "--ignore-errors") { opts.ignoreErrors = true }
else if (a === "-k" || a === "--keep-going") { opts.keepGoing = true }
else if (a === "-h" || a === "--help") { opts.help = true }
else if (a === "-v" || a === "--version") { opts.version = true }
else if (a.length > 1 && a[0] === "-" && a[1] !== "-") {
// bundled short flags, e.g. -sn
let bundle = a.substring(1)
let good = true
for (let c = 0; c < bundle.length; c++) {
let f = bundle[c]
if (f === "n") opts.dryRun = true
else if (f === "s") opts.silent = true
else if (f === "B") opts.alwaysMake = true
else if (f === "i") opts.ignoreErrors = true
else if (f === "k") opts.keepGoing = true
else { good = false; break }
}
if (!good) { printerrln(`${PROGRAM}: unknown option '${a}'`); return null }
}
else {
// VAR=value override, or a goal
let m = a.match(/^([A-Za-z_][A-Za-z0-9_.\-]*)=([\s\S]*)$/)
if (m) overrides[m[1]] = m[2]
else goals.push(a)
}
i++
}
return { opts: opts, goals: goals, overrides: overrides }
}
// ===========================================================================
// entry point
// ===========================================================================
function main() {
let argv = (typeof exec_args !== "undefined" && exec_args) ? exec_args.slice(1) : []
let parsed = parseArgs(argv)
if (parsed === null) return 2
let opts = parsed.opts
if (opts.help) { printUsage(); return 0 }
if (opts.version) { println(VERSION); return 0 }
if (opts.dir) {
try { _G.shell.coreutils.cd(["cd", opts.dir]) }
catch (e) { printerrln(`${PROGRAM}: *** ${opts.dir}: ${e.message}. Stop.`); return 2 }
}
// locate the makefile
let makefile = opts.makefile
if (!makefile) {
if (fileExists("Makefile")) makefile = "Makefile"
else if (fileExists("makefile")) makefile = "makefile"
}
if (!makefile || !fileExists(makefile)) {
printerrln(`${PROGRAM}: *** No targets specified and no makefile found. Stop.`)
return 2
}
let text
try { text = readFile(makefile) }
catch (e) { printerrln(`${PROGRAM}: ${makefile}: ${e.message}`); return 2 }
let model = makeModel()
model.overrides = parsed.overrides
try {
parseMakefile(model, text)
} catch (e) {
printerrln(`${PROGRAM}: ${makefile}: *** ${e.message}. Stop.`)
return 2
}
let goals = parsed.goals
if (goals.length === 0) {
if (model.defaultGoal === null) {
printerrln(`${PROGRAM}: *** No targets. Stop.`)
return 2
}
goals = [model.defaultGoal]
}
let state = loadState()
let builder = makeBuilder(model, state, opts)
return builder.run(goals)
}
return main()