mirror of https://github.com/natnat-mc/moonbuild
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
197 lines
4.8 KiB
197 lines
4.8 KiB
5 years ago
|
#!/usr/bin/env moon
|
||
|
|
||
|
argparse=require 'argparse'
|
||
|
|
||
|
require 'moonscript'
|
||
|
import loadfile from require 'moonscript.base'
|
||
|
import truncate_traceback, rewrite_traceback from require 'moonscript.errors'
|
||
|
import trim from require 'moonscript.util'
|
||
|
|
||
4 years ago
|
util=require 'moonbuild.util'
|
||
4 years ago
|
import exists, mtime, run, min, max, first, flatten, match, patsubst, sortedpairs from util
|
||
5 years ago
|
|
||
|
import insert, concat from table
|
||
|
|
||
|
parser=argparse 'moonbuild'
|
||
|
parser\argument('targets', "Targets to run")\args '*'
|
||
|
parser\flag '-a --noskip', "Always run targets"
|
||
|
parser\flag '-l --list', "List available targets"
|
||
4 years ago
|
parser\flag '-d --deps', "List targets and their dependancies"
|
||
5 years ago
|
args=parser\parse!
|
||
|
|
||
|
-- util functions
|
||
|
loadwithscope= (file, scope) ->
|
||
4 years ago
|
fn, err=loadfile file
|
||
|
error err or "failed to load code" unless fn
|
||
|
dumped, err=string.dump fn
|
||
|
error err or "failed to dump function" unless dumped
|
||
5 years ago
|
load dumped, file, 'b', scope
|
||
|
pcall= (fn, ...) ->
|
||
|
rewrite=(err) ->
|
||
|
trace=debug.traceback '', 2
|
||
|
trunc=truncate_traceback trim trace
|
||
|
rewrite_traceback trunc, err
|
||
|
xpcall fn, rewrite, ...
|
||
|
|
||
|
-- command object
|
||
|
-- represents a command that can be called
|
||
|
class Command
|
||
|
new: (@cmd, ...) =>
|
||
|
@args={...}
|
||
|
|
||
|
__unm: => @run error: true, print: true
|
||
|
__len: => @run error: true
|
||
|
__tostring: => @cmd
|
||
|
|
||
|
run: (params) => run @cmd, @args, params
|
||
|
@run: (...) => -@ ...
|
||
|
|
||
|
-- build object
|
||
|
-- represents a target
|
||
|
class BuildObject
|
||
|
all={}
|
||
5 years ago
|
skip={}
|
||
|
|
||
|
@find: (name) =>
|
||
|
target=all[name]
|
||
|
return target if target
|
||
|
for glob, tgt in pairs all
|
||
|
return tgt if match name, glob
|
||
|
nil
|
||
|
|
||
4 years ago
|
@list: =>
|
||
|
{target, {dep, @find dep for dep in *target.deps} for name, target in pairs all}
|
||
5 years ago
|
|
||
4 years ago
|
@build: (name, upper) =>
|
||
4 years ago
|
target=(@find name) or error "No such target: #{name}"
|
||
4 years ago
|
target\build name, upper
|
||
5 years ago
|
|
||
|
__tostring: =>
|
||
|
"Target #{@name} (#{concat @deps, ', '})"
|
||
5 years ago
|
|
||
|
new: (@name, @outs={}, @ins={}, @deps={}, @fn= =>) =>
|
||
|
@skip=false
|
||
|
error "Duplicate build name #{@name}" if all[@name]
|
||
|
all[@name]=@
|
||
|
|
||
4 years ago
|
build: (name, upper={}) =>
|
||
5 years ago
|
return if skip[name]
|
||
4 years ago
|
error "Cycle detected on #{@name}" if upper[@]
|
||
|
upper = setmetatable {[@]: true}, __index: upper
|
||
5 years ago
|
if @name!=name
|
||
4 years ago
|
@@build (patsubst name, @name, dep), upper for dep in *@deps
|
||
5 years ago
|
else
|
||
4 years ago
|
@@build dep, upper for dep in *@deps
|
||
5 years ago
|
return unless @shouldbuild name
|
||
|
|
||
|
ins=@ins
|
||
|
outs=@outs
|
||
|
if @name!=name
|
||
|
ins=[patsubst name, @name, elem for elem in *@ins]
|
||
|
outs=[patsubst name, @name, elem for elem in *@outs]
|
||
|
print "Building #{@name} as #{name}"
|
||
|
else
|
||
|
print "Building #{name}"
|
||
5 years ago
|
ok, err=pcall ->
|
||
5 years ago
|
@.fn
|
||
|
ins: ins
|
||
|
outs: outs
|
||
|
infile: ins[1]
|
||
|
outfile: outs[1]
|
||
|
name: name
|
||
5 years ago
|
error "Can't build #{@name}: lua error\n#{err}" unless ok
|
||
5 years ago
|
for f in *outs
|
||
5 years ago
|
error "Can't build #{@name}: output file #{f} not created" unless exists f
|
||
5 years ago
|
skip[name]=true
|
||
5 years ago
|
|
||
5 years ago
|
shouldbuild: (name) =>
|
||
5 years ago
|
return true if args.noskip
|
||
|
return true if #@ins==0 or #@outs==0
|
||
|
|
||
5 years ago
|
ins=if @name!=name
|
||
|
[patsubst name, @name, elem for elem in *@ins]
|
||
|
else
|
||
|
@ins
|
||
|
itimes=[mtime f for f in *ins]
|
||
5 years ago
|
for i=1, #@ins
|
||
|
error "Can't build #{@name}: missing inputs" unless itimes[i]
|
||
|
|
||
5 years ago
|
outs=if @name!=name
|
||
|
[patsubst name, @name, elem for elem in *@outs]
|
||
|
else
|
||
|
@outs
|
||
|
otimes=[mtime f for f in *outs]
|
||
5 years ago
|
for i=1, #@outs
|
||
|
return true if not otimes[i]
|
||
|
|
||
|
(max itimes)>(min otimes)
|
||
|
|
||
|
error "Need Lua >=5.2" if setfenv
|
||
|
|
||
|
targets={}
|
||
|
defaulttarget='all'
|
||
|
|
||
|
buildscope=
|
||
|
default: (target) ->
|
||
|
defaulttarget=target.name
|
||
|
target
|
||
|
public: (target) ->
|
||
|
insert targets, target.name
|
||
|
target
|
||
|
target: (name, params) ->
|
||
5 years ago
|
tout=flatten params.out
|
||
|
tin=flatten params.in
|
||
|
tdeps=flatten params.deps
|
||
|
for f in *flatten params.from
|
||
|
insert tin, f
|
||
|
insert tdeps, f
|
||
|
BuildObject name, tout, tin, tdeps, params.fn
|
||
5 years ago
|
buildscope[k]=fn for k, fn in pairs util
|
||
|
|
||
|
setmetatable buildscope,
|
||
|
__index: (k) =>
|
||
|
global=rawget _G, k
|
||
|
return global if global
|
||
|
(...) -> Command k, ...
|
||
|
|
||
|
file=first {'Build.moon', 'Buildfile.moon', 'Build', 'Buildfile'}, exists
|
||
|
error "No Build.moon or Buildfile found" unless file
|
||
|
buildfn=loadwithscope file, buildscope
|
||
4 years ago
|
error "Failed to load build function" unless buildfn
|
||
5 years ago
|
ok, err=pcall buildfn
|
||
|
unless ok
|
||
|
if err
|
||
|
io.stderr\write err, '\n'
|
||
|
else
|
||
|
io.stderr\write "Unknown error\n"
|
||
|
os.exit 1
|
||
|
|
||
|
if args.list
|
||
|
io.write "Available targets:\n"
|
||
|
io.write "\t#{concat targets, ', '}\n"
|
||
|
os.exit 0
|
||
|
|
||
4 years ago
|
if args.deps
|
||
|
io.write "Targets:\n"
|
||
|
for target, deps in sortedpairs BuildObject\list!, (a, b) -> a.name<b.name
|
||
|
io.write "\t#{target.name} "
|
||
|
if #target.ins==0
|
||
|
if #target.outs==0
|
||
|
io.write "[no in/out]"
|
||
|
else
|
||
|
io.write "[spontaneous generation]"
|
||
|
else
|
||
|
if #target.outs==0
|
||
|
io.write "[consumer]"
|
||
|
else
|
||
|
io.write "(#{concat target.ins, ', '} -> #{concat target.outs, ', '})"
|
||
|
io.write "\n"
|
||
|
for name, dep in sortedpairs deps
|
||
|
io.write "\t\t#{name} (#{dep.name})\n"
|
||
|
os.exit 0
|
||
|
|
||
5 years ago
|
if #args.targets==0
|
||
|
BuildObject\build defaulttarget
|
||
|
for target in *args.targets
|
||
|
BuildObject\build target
|