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
|
#
#
# Maintenance program for Nimrod
# (c) Copyright 2010 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
import
os, strutils, parseopt
const
HelpText = """
+-----------------------------------------------------------------+
| Maintenance program for Nimrod |
| Version $1|
| (c) 2010 Andreas Rumpf |
+-----------------------------------------------------------------+
Build time: $2, $3
Usage:
koch [options] command [options for command]
Options:
--help, -h shows this help and quits
Possible Commands:
boot [options] bootstraps with given command line options
clean cleans Nimrod project; removes generated files
web generates the website
csource [options] builds the C sources for installation
zip builds the installation ZIP package
inno builds the Inno Setup installer
"""
proc exec(cmd: string) =
echo(cmd)
if execShellCmd(cmd) != 0: quit("FAILURE")
proc tryExec(cmd: string): bool =
echo(cmd)
result = execShellCmd(cmd) == 0
proc csource(args: string) =
exec("nimrod cc $1 -r tools/niminst --var:version=$2 csource rod/nimrod $1" %
[args, NimrodVersion])
proc zip(args: string) =
exec("nimrod cc -r tools/niminst --var:version=$# zip rod/nimrod" %
NimrodVersion)
proc inno(args: string) =
exec("nimrod cc -r tools/niminst --var:version=$# inno rod/nimrod" %
NimrodVersion)
proc install(args: string) =
exec("sh ./build.sh")
proc web(args: string) =
exec("nimrod cc -r tools/nimweb.nim web/nimrod --putenv:nimrodversion=$#" %
NimrodVersion)
proc exe(f: string): string = return addFileExt(f, ExeExt)
# -------------- nim ----------------------------------------------------------
proc compileNimCmd(args: string): string =
var cwd = getCurrentDir()
result = ("fpc -Cs16777216 -gl -bl -Crtoi -Sgidh -vw -Se1 $4 -o\"$1\" " &
"-FU\"$2\" \"$3\"") % [cwd / "bin" / "nim".exe,
cwd / "obj",
cwd / "nim" / "nimrod.pas",
args]
proc nim(args: string) = exec(compileNimCmd(args))
# -------------- boot ---------------------------------------------------------
const
bootOptions = "" # options to pass to the bootstrap process
proc findStartNimrod: string =
# we try several things before giving up:
# * bin/nimrod
# * $PATH/nimrod
# * bin/nim
# If these fail, we build nimrod with the "build.sh" script
# (but only on UNIX). Otherwise we try to compile "nim" with FPC
# and use "bin/nim".
var nimrod = "nimrod".exe
result = "bin" / nimrod
if ExistsFile(result): return
for dir in split(getEnv("PATH"), PathSep):
if ExistsFile(dir / nimrod): return nimrod
result = "bin" / "nim".exe
if ExistsFile(result): return
when defined(Posix):
const buildScript = "build.sh"
if ExistsFile(buildScript):
if tryExec("./" & buildScript): return "bin" / nimrod
if tryExec(compileNimCmd("")): return
echo("Found no nimrod compiler and every attempt to build one failed!")
quit("FAILURE")
proc safeRemove(filename: string) =
if existsFile(filename): removeFile(filename)
proc bootIteration(args: string): bool =
var nimrod1 = "rod" / "nimrod1".exe
safeRemove(nimrod1)
moveFile(dest=nimrod1, source="rod" / "nimrod".exe)
exec "rod" / "nimrod1 cc $# $# rod/nimrod.nim" % [bootOptions, args]
# Nimrod does not produce an executable again if nothing changed. That's ok:
result = sameFileContent("rod" / "nimrod".exe, nimrod1)
safeRemove("bin" / "nimrod".exe)
var dest = "bin" / "nimrod".exe
copyFile(dest=dest, source="rod" / "nimrod".exe)
inclFilePermissions(dest, {fpUserExec})
safeRemove(nimrod1)
if result: echo "executables are equal: SUCCESS!"
proc boot(args: string) =
echo "iteration: 1"
exec findStartNimrod() & " cc $# $# rod" / "nimrod.nim" % [bootOptions, args]
echo "iteration: 2"
if not bootIteration(args):
echo "executables are not equal: compile once again..."
if not bootIteration(args):
echo "[Warning] executables are still not equal"
# -------------- clean --------------------------------------------------------
const
cleanExt = [
".ppu", ".o", ".obj", ".dcu", ".~pas", ".~inc", ".~dsk", ".~dpr",
".map", ".tds", ".err", ".bak", ".pyc", ".exe", ".rod", ".pdb", ".idb"
]
ignore = [
".bzrignore", "nimrod", "nimrod.exe", "koch", "koch.exe"
]
proc cleanAux(dir: string) =
for kind, path in walkDir(dir):
case kind
of pcFile:
var (dir, name, ext) = splitFile(path)
if ext == "" or cleanExt.contains(ext):
if not ignore.contains(name):
echo "removing: ", path
removeFile(path)
of pcDir:
case splitPath(path).tail
of "nimcache":
echo "removing dir: ", path
removeDir(path)
of "dist", ".bzr":
nil
else:
cleanAux(path)
else: nil
proc removePattern(pattern: string) =
for f in WalkFiles(pattern):
echo "removing: ", f
removeFile(f)
proc clean(args: string) =
if ExistsFile("koch.dat"): removeFile("koch.dat")
removePattern("web/*.html")
removePattern("doc/*.html")
cleanAux(getCurrentDir())
proc showHelp() =
quit(HelpText % [NimrodVersion & repeatChar(44-len(NimrodVersion)),
CompileDate, CompileTime])
var op = initOptParser()
op.next()
case op.kind
of cmdLongOption, cmdShortOption: showHelp()
of cmdArgument:
case normalize(op.key)
of "boot": boot(op.cmdLineRest)
of "clean": clean(op.cmdLineRest)
of "web": web(op.cmdLineRest)
of "csource": csource(op.cmdLineRest)
of "zip": zip(op.cmdLineRest)
of "inno": inno(op.cmdLineRest)
of "install": install(op.cmdLineRest)
of "nim": nim(op.cmdLineRest)
else: showHelp()
of cmdEnd: showHelp()
|