Skip to content

Commit be46df5

Browse files
committed
store/load ConfigRef fields
1 parent 3a71cfe commit be46df5

File tree

1 file changed

+74
-4
lines changed

1 file changed

+74
-4
lines changed

compiler/nimconfcache.nim

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,37 @@ proc fromNif2[T: enum](result: var set[T]; n: var Cursor) =
3636
inc n
3737
inc n
3838

39+
proc fromNif(result: StringTableRef; n: var Cursor) =
40+
result.clear
41+
assert n.kind in {ParLe, ParRi}
42+
while n.kind != ParRi:
43+
expectTag n, "kv"
44+
inc n
45+
assert n.kind == StringLit
46+
let key = pool.strings[n.litId]
47+
inc n
48+
let val = pool.strings[n.litId]
49+
inc n
50+
result[key] = val
51+
assert n.kind == ParRi
52+
inc n
53+
inc n
54+
55+
proc fromNif(result: var AbsoluteDir; n: var Cursor) =
56+
let d = pool.strings[n.litId]
57+
inc n
58+
result = if d.len > 0: d.toAbsoluteDir else: AbsoluteDir""
59+
3960
template buildTree(dest: var TokenBuf; tag: string; body: untyped): untyped =
4061
buildTree(dest, pool.tags.getOrIncl(tag), NoLineInfo, body)
4162

63+
proc toNif(dest: var TokenBuf; tag: string; tab: StringTableRef) =
64+
dest.buildTree tag:
65+
for key, val in pairs(tab):
66+
dest.buildTree "kv":
67+
dest.addStrLit key
68+
dest.addStrLit val
69+
4270
proc configToNif(conf: ConfigRef; dest: var TokenBuf) =
4371
# store data used to decide whether to use cache or eval config files
4472
dest.addStrLit conf.commandLine
@@ -132,6 +160,8 @@ proc configToNif(conf: ConfigRef; dest: var TokenBuf) =
132160
dest.addIntLit conf.maxLoopIterationsVM
133161
dest.addIntLit conf.maxCallDepthVM
134162

163+
dest.toNif "configVars", conf.configVars
164+
135165
dest.buildTree "defines":
136166
for def in definedSymbolNames(conf.symbols):
137167
dest.addStrLit def
@@ -151,6 +181,12 @@ proc configToNif(conf: ConfigRef; dest: var TokenBuf) =
151181
for p in paths:
152182
dest.addStrLit p
153183

184+
dest.addStrLit conf.outFile.string
185+
dest.addStrLit conf.outDir.toNifPath
186+
dest.addStrLit conf.prefixDir.toNifPath
187+
dest.addStrLit conf.libpath.toNifPath
188+
dest.addStrLit conf.nimcacheDir.toNifPath
189+
154190
proc cfgCachePath(conf: ConfigRef): (AbsoluteDir, RelativeFile) =
155191
(conf.projectPath / RelativeDir"nimcache", RelativeFile"cache.cfg.nif")
156192

@@ -273,6 +309,10 @@ proc loadConfigsFromNif(conf: ConfigRef; n: var Cursor) =
273309
conf.maxCallDepthVM = pool.integers[n.intId]
274310
inc n
275311

312+
expectTag n, "configVars"
313+
inc n
314+
fromNif(conf.configVars, n)
315+
276316
conf.symbols.clear
277317
expectTag n, "defines"
278318
inc n
@@ -307,6 +347,13 @@ proc loadConfigsFromNif(conf: ConfigRef; n: var Cursor) =
307347
#echo "conf.searchPaths"
308348
#echo conf.searchPaths
309349

350+
conf.outFile = pool.strings[n.litId].RelativeFile
351+
inc n
352+
fromNif(conf.outDir, n)
353+
fromNif(conf.prefixDir, n)
354+
fromNif(conf.libpath, n)
355+
fromNif(conf.nimcacheDir, n)
356+
310357
proc sourceChanged*(conf: ConfigRef): HashSet[string] =
311358
result = HashSet[string]()
312359
let (dir, file) = conf.cfgCachePath()
@@ -348,13 +395,22 @@ proc storeConfigs*(conf: ConfigRef) =
348395
writeFile dir / file, "(.nif24)\n" & toString(dest)
349396

350397
when isMainModule:
351-
proc `==`(x, y: StringTableRef): bool =
398+
proc eqlKeys(x, y: StringTableRef): bool =
352399
for k, v in x:
353400
if k notin y:
354401
return false
355402

356403
return true
357404

405+
proc `==`(x, y: StringTableRef): bool =
406+
for k, v in x.pairs:
407+
if k notin y:
408+
return false
409+
elif y[k] != v:
410+
return false
411+
412+
return true
413+
358414
proc assertEq(x, y: ConfigRef) =
359415
template assertImpl(f: untyped) =
360416
assert x.f == y.f, $x.f & " / " & $y.f
@@ -364,8 +420,8 @@ when isMainModule:
364420
assert x.target.targetCPU == y.target.targetCPU
365421
assertImpl options
366422
assertImpl globalOptions
367-
assertImpl macrosToExpand
368-
assertImpl arcToExpand
423+
assert eqlKeys(x.macrosToExpand, y.macrosToExpand)
424+
assert eqlKeys(x.arcToExpand, y.arcToExpand)
369425
assertImpl filenameOption
370426
assertImpl unitSep
371427
assertImpl selectedGC
@@ -386,9 +442,15 @@ when isMainModule:
386442
assertImpl errorMax
387443
assertImpl maxLoopIterationsVM
388444
assertImpl maxCallDepthVM
389-
assertImpl symbols
445+
assertImpl configVars
446+
assert eqlKeys(x.symbols, y.symbols)
390447
assertImpl nimblePaths
391448
assertImpl searchPaths
449+
assertImpl outFile
450+
assertImpl outDir
451+
assertImpl prefixDir
452+
assertImpl libpath
453+
assertImpl nimcacheDir
392454

393455
proc testConfig(conf1: ConfigRef) =
394456
var dest = createTokenBuf()
@@ -447,8 +509,16 @@ when isMainModule:
447509
conf.errorMax = 7
448510
conf.maxLoopIterationsVM = 1234
449511
conf.maxCallDepthVM = 111
512+
conf.setConfigVar("foo.bar", "baz")
513+
conf.setConfigVar("abc.def.ghi", "123")
514+
conf.setConfigVar(".", "")
450515
conf.symbols.initDefines()
451516
conf.symbols.defineSymbol("test")
452517
conf.nimblePaths = @[AbsoluteDir"/foo", AbsoluteDir"/lib/nimble"]
453518
conf.searchPaths = @[AbsoluteDir"/lib", AbsoluteDir"/user/lib"]
519+
conf.outFile = RelativeFile"foo"
520+
conf.outDir = AbsoluteDir"/foo/var"
521+
conf.prefixDir = AbsoluteDir"/home/foo/Nim"
522+
conf.libpath = AbsoluteDir"/home/foo/Nim/lib"
523+
conf.nimcacheDir = AbsoluteDir"/root/nimcache"
454524
testConfig conf

0 commit comments

Comments
 (0)