Skip to content

Commit 59f27ad

Browse files
committed
Refactor
1 parent 6ba56ef commit 59f27ad

File tree

5 files changed

+95
-59
lines changed

5 files changed

+95
-59
lines changed

nimalyzer.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ directory tests
99
check assignments shorthand
1010

1111
check caseStatements min 2
12-
check complexity all 3
12+
# check complexity all 3
1313

1414
check forStatements all
1515
# check not forStatements iterators

src/bootstrap.nim

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ proc cleanBootstrap*() =
1414
"../../glaucus/cross",
1515
"../../glaucus/log",
1616
"../../glaucus/tmp",
17-
"../../glaucus/toolchain"]
17+
"../../glaucus/toolchain",
18+
]
1819

1920
for i in dirs:
2021
removeDir(i)
@@ -26,8 +27,8 @@ proc prepareToolchain() =
2627
"../../glaucus/pkg",
2728
"../../glaucus/src",
2829
"../../glaucus/tmp",
29-
"../../glaucus/toolchain"]
30-
30+
"../../glaucus/toolchain",
31+
]
3132
const exes = [
3233
"autoconf", "automake", "autopoint", "awk",
3334
"bash", "booster", "bzip2",
@@ -43,7 +44,8 @@ proc prepareToolchain() =
4344
"tar",
4445
"xz",
4546
"yacc",
46-
"zstd"]
47+
"zstd",
48+
]
4749

4850
for i in exes:
4951
if findExe(i).isEmptyOrWhitespace():
@@ -55,17 +57,17 @@ proc prepareToolchain() =
5557
createDir(i)
5658

5759
proc configureToolchain() =
58-
putEnv("GLAUCUS_CROSS", absolutePath("../../glaucus/cross"))
59-
putEnv("GLAUCUS_TOOLCHAIN", absolutePath("../../glaucus/toolchain"))
60-
putEnv("PATH", absolutePath("../../glaucus/toolchain/usr/bin") & PathSep & getEnv("PATH"))
60+
putEnv(
61+
"PATH", absolutePath("../../glaucus/toolchain/usr/bin") & PathSep & getEnv("PATH")
62+
)
6163

6264
proc bootstrapToolchain*() =
6365
prepareToolchain()
6466
configureToolchain()
65-
buildPackages(parseInfo("toolchain").run.split(), true, toolchain)
67+
buildPackages(parseInfo($toolchain).run.split(), true, toolchain)
6668

6769
proc prepareCross() =
68-
const dir = "../tmp"
70+
const dir = "../../glaucus/tmp"
6971

7072
removeDir(dir)
7173
createDir(dir)
@@ -86,14 +88,15 @@ proc configureCross() =
8688
("RANLIB", "x86_64-glaucus-linux-musl-gcc-ranlib"),
8789
("READELF", "x86_64-glaucus-linux-musl-readelf"),
8890
("SIZE", "x86_64-glaucus-linux-musl-size"),
89-
("STRIP", "x86_64-glaucus-linux-musl-strip")]
90-
91+
("STRIP", "x86_64-glaucus-linux-musl-strip"),
92+
]
9193
const envPkgConfig = [
9294
("PKG_CONFIG_LIBDIR", "../../glaucus/cross/usr/lib/pkgconfig"),
9395
("PKG_CONFIG_PATH", "../../glaucus/cross/usr/lib/pkgconfig"),
9496
("PKG_CONFIG_SYSROOT_DIR", "../../glaucus/cross/"),
9597
("PKG_CONFIG_SYSTEM_INCLUDE_PATH", "../../glaucus/cross/usr/include"),
96-
("PKG_CONFIG_SYSTEM_LIBRARY_PATH", "../../glaucus/cross/usr/lib")]
98+
("PKG_CONFIG_SYSTEM_LIBRARY_PATH", "../../glaucus/cross/usr/lib"),
99+
]
97100

98101
for (i, j) in envExes:
99102
putEnv(i, j)
@@ -104,4 +107,4 @@ proc bootstrapCross*() =
104107
prepareCross()
105108
configureCross()
106109
configureToolchain()
107-
buildPackages(parseInfo("cross").run.split(), true, cross)
110+
buildPackages(parseInfo($cross).run.split(), true, cross)

src/options.nim

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import std/[os, parseopt, strutils]
1010
import bootstrap, packages, utils
1111

1212
proc options*() =
13-
const help = """
13+
const help =
14+
"""
1415
USAGE:
1516
rad [ COMMAND ]
1617
@@ -26,7 +27,8 @@ COMMANDS:
2627
update Update repositories
2728
version Show rad version"""
2829

29-
const helpBootstrap = """
30+
const helpBootstrap =
31+
"""
3032
USAGE:
3133
rad bootstrap [ COMMAND ]
3234
@@ -37,7 +39,8 @@ COMMANDS:
3739
2, stage2 Bootstrap stage 2 (cross)
3840
3, stage3 Bootstrap stage 3 (native)"""
3941

40-
const version = """
42+
const version =
43+
"""
4144
rad version 0.1.0
4245
4346
Licensed under the Mozilla Public License Version 2.0 (MPL-2.0)

src/packages.nim

Lines changed: 69 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ type Package = object
1919
ver, url, sum, bld, run*, opt = "nil"
2020

2121
type Stages* = enum
22-
cross, native, toolchain
22+
cross
23+
native
24+
toolchain
2325

2426
proc cleanCache*() =
2527
removeDir(radTmp)
@@ -37,7 +39,7 @@ proc parseInfo*(nom: string): Package =
3739
if line.isEmptyOrWhitespace() or line.startsWith('#'):
3840
continue
3941

40-
if line.contains("= \"") or line.contains(" =\"") or '=' notin line:
42+
if line.contains(" =\"") or line.contains("= \"") or '=' notin line:
4143
abort(&"""{"nom":8}{"whitespace found":48}""")
4244

4345
let pair = line.split('=', 1)
@@ -77,10 +79,13 @@ idx nom ver cmd now
7779
proc genContents(dir, contents: string) =
7880
var entries: seq[string]
7981

80-
for entry in walkDirRec(dir, yieldFilter = {pcFile .. pcLinkToDir}, relative = true, skipSpecial = true):
82+
for entry in walkDirRec(
83+
dir, yieldFilter = {pcFile .. pcLinkToDir}, relative = true, skipSpecial = true
84+
):
8185
entries &= (
8286
if getFileInfo(dir / entry, followSymlink = false).kind == pcDir: entry & '/'
83-
else: entry)
87+
else: entry
88+
)
8489

8590
let contents = open(contents, fmWrite)
8691

@@ -131,7 +136,9 @@ proc fetchPackages(packages: openArray[string]) =
131136
createDir(tmp)
132137
discard extractTar(archive, tmp)
133138

134-
proc resolveDeps(nom: string, packages: var seq[string], deps: var Table[string, seq[string]]) =
139+
proc resolveDeps(
140+
nom: string, packages: var seq[string], deps: var Table[string, seq[string]]
141+
) =
135142
if nom in packages:
136143
return
137144

@@ -185,10 +192,9 @@ proc buildPackages*(packages: openArray[string], bootstrap = false, stage = nati
185192
for idx, nom in queue:
186193
let package = parseInfo(nom)
187194
let archive =
188-
if package.url == "nil":
189-
pkgCache / nom / nom & ".tar.zst"
190-
else:
191-
pkgCache / nom / nom & '-' & package.ver & ".tar.zst"
195+
pkgCache / nom / nom &
196+
(if package.url == "nil": ".tar.zst"
197+
else: '-' & package.ver & ".tar.zst")
192198

193199
printContent(idx, nom, package.ver, "build")
194200

@@ -197,10 +203,9 @@ proc buildPackages*(packages: openArray[string], bootstrap = false, stage = nati
197203
if fileExists(archive):
198204
continue
199205

200-
putEnv("dir", pkgCache / nom / "dir")
201-
createDir(getEnv("dir"))
206+
createDir(pkgCache / nom / "dir")
202207

203-
const env = [
208+
const envExes = [
204209
("AR", "gcc-ar"),
205210
("AWK", "mawk"),
206211
("CC", "gcc"),
@@ -212,48 +217,68 @@ proc buildPackages*(packages: openArray[string], bootstrap = false, stage = nati
212217
("NM", "gcc-nm"),
213218
("PKG_CONFIG", "u-config"),
214219
("RANLIB", "gcc-ranlib"),
215-
("YACC", "byacc")]
220+
("YACC", "byacc"),
221+
]
216222

217-
for (i, j) in env:
223+
for (i, j) in envExes:
218224
putEnv(i, j)
219225

220-
if dirExists(radTmp / nom):
221-
setCurrentDir(radTmp / nom)
222-
if dirExists(radTmp / nom / nom & '-' & package.ver):
223-
setCurrentDir(radTmp / nom / nom & '-' & package.ver)
226+
let cflags =
227+
"-pipe -O2 -fgraphite-identity -floop-nest-optimize" & (
228+
if "no-lto" notin package.opt:
229+
" -flto=auto -flto-compression-level=3 -fuse-linker-plugin "
230+
else:
231+
" "
232+
) &
233+
"-fstack-protector-strong -fstack-clash-protection -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-plt -march=x86-64-v3 -mfpmath=sse -mabi=sysv -malign-data=cacheline -mtls-dialect=gnu2"
224234

225235
let env = [
226236
("ARCH", "x86-64"),
227-
("GLAUCUS_BUILD", execCmdEx(coreRepo / "slibtool/files/config.guess").output.strip()),
228-
("GLAUCUS_HOST", "x86_64-glaucus-linux-musl"),
229-
("GLAUCUS_TARGET", "x86_64-pc-linux-musl"),
230-
("PRETTY_NAME", "glaucus s6 x86-64-v3 " & now().format("YYYYMMdd"))]
237+
("CFLAGS", cflags),
238+
("CXXFLAGS", cflags),
239+
(
240+
"LDFLAGS",
241+
"-Wl,-O1,-s,-z,noexecstack,-z,now,-z,pack-relative-relocs,-z,relro,-z,x86-64-v3,--as-needed,--gc-sections,--sort-common,--hash-style=gnu" &
242+
(
243+
if "no-lto" notin package.opt:
244+
" " & cflags
245+
else:
246+
""
247+
),
248+
),
249+
("MAKEFLAGS", if "no-parallel" notin package.opt: "-j 5 -O" else: "-j 1"),
250+
("PRETTY_NAME", "glaucus s6 x86-64-v3 " & now().format("YYYYMMdd")),
251+
]
231252

232253
for (i, j) in env:
233254
putEnv(i, j)
234255

235-
let cflags =
236-
if "no-lto" notin package.opt:
237-
"-pipe -O2 -fgraphite-identity -floop-nest-optimize -flto=auto -flto-compression-level=3 -fuse-linker-plugin -fstack-protector-strong -fstack-clash-protection -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-plt -march=x86-64-v3 -mfpmath=sse -mabi=sysv -malign-data=cacheline -mtls-dialect=gnu2"
238-
else:
239-
"-pipe -O2 -fgraphite-identity -floop-nest-optimize -fstack-protector-strong -fstack-clash-protection -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-plt -march=x86-64-v3 -mfpmath=sse -mabi=sysv -malign-data=cacheline -mtls-dialect=gnu2"
256+
let crossShell =
257+
&"""
258+
build={execCmdEx(coreRepo / "slibtool/files/config.guess").output.strip()}
259+
host=x86_64-glaucus-linux-musl
260+
target=x86_64-pc-linux-musl
240261
241-
let envFlags = [
242-
("CFLAGS", cflags),
243-
("CXXFLAGS", cflags),
244-
("LDFLAGS",
245-
if "no-lto" notin package.opt:
246-
"-Wl,-O1,-s,-z,noexecstack,-z,now,-z,pack-relative-relocs,-z,relro,-z,x86-64-v3,--as-needed,--gc-sections,--sort-common,--hash-style=gnu " &
247-
cflags
248-
else:
249-
"-Wl,-O1,-s,-z,noexecstack,-z,now,-z,pack-relative-relocs,-z,relro,-z,x86-64-v3,--as-needed,--gc-sections,--sort-common,--hash-style=gnu"),
250-
("MAKEFLAGS", if "no-parallel" notin package.opt: "-j 5 -O" else: "-j 1")]
262+
cross={absolutePath("../../glaucus/cross")}
263+
toolchain={absolutePath("../../glaucus/toolchain")}"""
251264

252-
for (i, j) in envFlags:
253-
putEnv(i, j)
265+
if dirExists(radTmp / nom):
266+
setCurrentDir(radTmp / nom)
267+
if dirExists(radTmp / nom / nom & '-' & package.ver):
268+
setCurrentDir(radTmp / nom / nom & '-' & package.ver)
254269

255270
let shell = execCmdEx(
256271
&"""sh -efu -c '
272+
{(if bootstrap: crossShell else: ":")}
273+
274+
core={coreRepo}
275+
tmp={radTmp}
276+
277+
dir={pkgCache / nom / "dir"}
278+
279+
nom={nom}
280+
ver={package.ver}
281+
257282
. {coreRepo / nom / (if stage == native: "build" else: "build" & '-' & $stage)}
258283
259284
for i in prepare configure build; do
@@ -262,9 +287,12 @@ proc buildPackages*(packages: openArray[string], bootstrap = false, stage = nati
262287
fi
263288
done
264289
265-
package'""")
290+
package'"""
291+
)
266292

267-
writeFile(radLog / nom & (if stage == native: "" else: '.' & $stage), shell.output.strip())
293+
writeFile(
294+
radLog / nom & (if stage == native: "" else: '.' & $stage), shell.output.strip()
295+
)
268296

269297
if shell.exitCode != QuitSuccess:
270298
abort(&"{shell.exitCode:<8}{nom:24}{package.ver:24}")

src/utils.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ proc interrupt() {.noconv.} =
4141

4242
proc lock*(lock = "/var/tmp/rad.lock") =
4343
if fileExists(lock):
44-
styledEcho fgRed, styleBright, &"""{QuitFailure:<8}{"lock exists":48}{"abort":8}""" & now().format("hh:mm tt")
44+
styledEcho fgRed,
45+
styleBright,
46+
&"""{QuitFailure:<8}{"lock exists":48}{"abort":8}""" & now().format("hh:mm tt")
4547

4648
quit(QuitFailure)
4749

0 commit comments

Comments
 (0)