forked from jcosborn/qex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.nims.in
163 lines (142 loc) · 3.96 KB
/
Makefile.nims.in
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
# -*- mode: nim -*-
import strUtils
import osPaths
var script = ""
var args = newSeq[string](0)
var nim = paramStr(0)
var nimargs = newSeq[string](0)
var qexdir = "@@QEXDIR"
var paths = [ "", qexdir/"src", qexdir ]
for i in 1..paramCount():
let p = paramStr(i)
if p[0]=='-':
nimargs.add p
else:
if script=="": script = p
else: args.add p
echo "Using Nim: ", nim
echo "Nim args: ", nimargs
echo "Makefile script: ", script
echo "Script args: ", args
var iarg = 0
var configTasks = newSeq[tuple[cmd:string,desc:string,f:proc(){.nimcall.}]](0)
template configTask(name: untyped; description: string; body: untyped) =
proc `name Task`*() = body
configTasks.add((cmd: astToStr(name), desc: description, f: `name Task`))
var buildTasks = newSeq[tuple[cmd:string,desc:string,f:proc(){.nimcall.}]](0)
template buildTask(name: untyped; description: string; body: untyped) =
proc `name Task`*() = body
buildTasks.add((cmd: astToStr(name), desc: description, f: `name Task`))
proc getInt(): int =
let t = split(args[iarg],":")
if t.len>=2: result = parseInt(t[1])
proc nimRun(f: string, outfile="")
var debug = false
var run = false
var verbosity = -1
configTask debug, "set debug build":
debug = true
configTask run, "run executable after building":
run = true
configTask verb, "set build verbosity":
verbosity = getInt()
buildTask clean, "remove nimcache dir":
exec "rm -rf nimcache"
buildTask tests, "build unit tests":
var runscript = @["#!/bin/sh","$SETUPJOBS"]
var dorun = run
run = false
if not existsDir("tests"):
mkDir("tests")
for d in listDirs(qexdir/"tests"):
let outdir = "tests"/splitPath(d)[1]
if not existsDir(outdir):
mkDir(outdir)
for f in listFiles(d):
#echo f
let (dir, name, ext) = splitFile(f)
#echo dir, " ", name, " ", ext
if name[0]=='t' and ext==".nim":
nimRun f, outdir/name
runscript.add("$RUNJOB "&(outdir/name))
#echo runscript.join("\n")
runscript.add("$CLEANUPJOBS")
writeFile("testscript.sh", runscript.join("\n"))
exec("chmod 755 testscript.sh")
if dorun:
exec "./testscript.sh"
proc help =
let s = repeatChar(70, '-')
echo s
echo "QEX build script usage:"
echo " make [config commands] [build commands]"
echo s
echo "config commands:"
for t in configTasks:
echo " ", t.cmd, ": ", t.desc
echo s
echo "build commands:"
for t in buildTasks:
echo " ", t.cmd, ": ", t.desc
echo s
quit 0
if args.len>0 and args[0] == "help": help()
while iarg<args.len:
var found = false
for t in configTasks:
if args[iarg][0..(t.cmd.len-1)] == t.cmd:
found = true
t.f()
if not found: break
inc iarg
if iarg>=args.len:
echo "error: no build command given"
help()
when existsFile("config.nims"):
include "config.nims"
proc nimRun(f: string, outfile="") =
var nimcmd = nim & " " & join(nimargs," ") & " " & join(defargs," ")
if run: nimcmd &= " -r "
var (dir, name, ext) = splitFile(f)
if outfile!="": name = outfile
else:
if not dirExists("bin"):
mkDir("bin")
name = "bin" / name
let s = nimcmd & " c -o:" & name & " " & f
echo "running: ", s
exec s
for t in buildTasks:
if args[iarg][0..(t.cmd.len-1)] == t.cmd:
t.f()
quit 0
proc findFile(f: string): string =
for p in paths:
let t = (if p == "": f else: p / f)
echo "searching for file: ", t
if fileExists(t): return t
proc findDir(f: string): string =
for p in paths:
let t = (if p == "": f else: p / f)
echo "searching for directory: ", t
if dirExists(t): return t
# default, treat as file or directory and compile
var cmd = args[iarg]
echo "command: ", cmd
var f: string
block:
var t = cmd
if t[^4..^1] != ".nim": t &= ".nim"
f = findFile(t)
if not f.isNil:
nimRun(f)
else:
f = findDir(cmd)
if not f.isNil and dirExists(f):
echo "Building files in directory: ", f
for t in listFiles(f):
echo "file: ", t
if t[^4..^1] == ".nim":
nimRun(t)
if f.isNil:
echo "unknown build target: ", cmd