A compromise between the speed of make and the ease of use of a build script
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.
moonbuild/util.moon

96 lines
2.2 KiB

import wildcard, exists, isdir, mtime from require 'fsutil'
import foreach, first, flatten, exclude, sortedpairs, min, max from require 'tableutil'
import patsubst, splitsp from require 'stringutil'
5 years ago
import insert, concat, sort, pairs from require 'tableutil'
import upper, lower from require 'stringutil'
5 years ago
GLOB_PATT='^([^%%]*)%%([^%%]*)$'
5 years ago
-- command functions
escapecmdpart= (p) ->
if (type p)=='table'
return p.raw if p.raw
return concat [escapecmdpart part for part in *p], ' '
return p if p\match '^[a-zA-Z0-9_./-]+$'
'"'..p\gsub('\\', '\\\\')\gsub('"', '\\"')..'"'
escapecmd= (c, args={}) ->
c=escapecmdpart c
for a in *flatten args
c..=' '..escapecmdpart a if a
5 years ago
c
run= (c, args, params={}) ->
escaped=escapecmd c, args
print escaped if params.print
ret, _, code=os.execute escaped
ret, code=ret==0, ret if (type ret)=='number'
error "#{c} failed with code #{code}" if params.error and not ret
ret, code
popen= (c, args, mode='r', params={}) ->
escaped=escapecmd c, args
print escaped if params.print
io.popen escaped, mode
4 years ago
calccdeps= (infile, includesys=false) ->
data=(popen 'cc', {includesys and '-M' or '-MM', infile})\read '*a'
rawdeps=data\gsub('\\\n', '')\match ':(.+)'
[dep for dep in rawdeps\gmatch '%S+' when dep!=infile]
findclib= (name, mode='all') ->
args={name}
insert args, '--cflags' if mode=='all' or mode=='cc'
insert args, '--libs' if mode=='all' or mode=='ld'
[arg for arg in (popen 'pkg-config', args)\read('*a')\gmatch '%S+']
-- glob match
match= (str, glob) ->
prefix, suffix=glob\match GLOB_PATT
return str==glob unless prefix
return str\sub #prefix+1, -#suffix-1 if (str\sub 1, #prefix)==prefix and (str\sub -#suffix)==suffix
false
-- is a valid glob
isglob= (glob) ->
return if glob\match GLOB_PATT
true
else
false
-- getenv
5 years ago
env= (key, def) ->
(os.getenv key) or def
{
-- table function
5 years ago
:min, :max
:foreach
:first
:exclude
:flatten
:sortedpairs
5 years ago
:insert, :remove, :concat, :sort
:unpack
5 years ago
-- file functions
:wildcard
:mtime
:exists, :isdir
5 years ago
-- command functions
:run, :popen
:calccdeps, :findclib
5 years ago
-- string functions
:patsubst
:splitsp
:upper, :lower
-- glob functions
:match, :isglob
-- env functions
:env
5 years ago
}