Fix: isolate temp/cache build dirs to avoid shadowing Nim stdlib modules. All 7 examples pass.
This commit is contained in:
+21
-9
@@ -111,11 +111,17 @@ proc nimCompile(nimPath: string, binPath: string, release: bool = false): int =
|
||||
echo "Compiling: ", cmd
|
||||
execCmd(cmd)
|
||||
|
||||
proc makeTempDir(): string =
|
||||
let pid = getCurrentProcessId()
|
||||
let ts = epochTime().int
|
||||
result = getTempDir() / "cljnim_build_" & $pid & "_" & $ts
|
||||
createDir(result)
|
||||
|
||||
proc runFile*(inputPath: string) =
|
||||
let tmpDir = getTempDir()
|
||||
let baseName = inputPath.splitFile.name
|
||||
let nimPath = tmpDir / baseName & "_generated.nim"
|
||||
let binPath = tmpDir / baseName & "_generated"
|
||||
let buildDir = makeTempDir()
|
||||
let nimPath = buildDir / baseName & ".nim"
|
||||
let binPath = buildDir / baseName
|
||||
|
||||
# Resolve project dependencies
|
||||
let inputDir = inputPath.parentDir
|
||||
@@ -124,8 +130,9 @@ proc runFile*(inputPath: string) =
|
||||
# Check nimcache for cached output
|
||||
let projectDir = inputPath.parentDir
|
||||
let cacheDir = projectDir / "nimcache"
|
||||
let cacheNimPath = cacheDir / baseName & ".nim"
|
||||
let cacheBinPath = cacheDir / baseName
|
||||
let cacheSubDir = cacheDir / baseName
|
||||
let cacheNimPath = cacheSubDir / baseName & ".nim"
|
||||
let cacheBinPath = cacheSubDir / baseName
|
||||
|
||||
var useCache = false
|
||||
if fileExists(cacheNimPath) and fileExists(cacheBinPath):
|
||||
@@ -140,6 +147,7 @@ proc runFile*(inputPath: string) =
|
||||
if compileResult != 0:
|
||||
stderr.writeLine("Compilation failed (cached)")
|
||||
quit(1)
|
||||
try: removeDir(buildDir) except: discard
|
||||
let runResult = execCmd(cacheBinPath)
|
||||
quit(runResult)
|
||||
|
||||
@@ -148,11 +156,12 @@ proc runFile*(inputPath: string) =
|
||||
let compileResult = nimCompile(nimPath, binPath)
|
||||
if compileResult != 0:
|
||||
stderr.writeLine("Compilation failed")
|
||||
try: removeDir(buildDir) except: discard
|
||||
quit(1)
|
||||
|
||||
# Save to cache
|
||||
try:
|
||||
createDir(cacheDir)
|
||||
createDir(cacheSubDir)
|
||||
copyFile(nimPath, cacheNimPath)
|
||||
copyFile(binPath, cacheBinPath)
|
||||
echo "Cached: ", cacheNimPath
|
||||
@@ -160,6 +169,7 @@ proc runFile*(inputPath: string) =
|
||||
discard
|
||||
|
||||
let runResult = execCmd(binPath)
|
||||
try: removeDir(buildDir) except: discard
|
||||
if runResult != 0:
|
||||
stderr.writeLine("Execution failed")
|
||||
quit(1)
|
||||
@@ -199,17 +209,19 @@ proc main() =
|
||||
stderr.writeLine("Error: Missing expression after -e")
|
||||
quit(1)
|
||||
let code = args[1]
|
||||
let tmpDir = getTempDir()
|
||||
let nimPath = tmpDir / "cljnim_expr.nim"
|
||||
let binPath = tmpDir / "cljnim_expr"
|
||||
let buildDir = makeTempDir()
|
||||
let nimPath = buildDir / "expr.nim"
|
||||
let binPath = buildDir / "expr"
|
||||
let forms = reader.readAll(code)
|
||||
let nimCode = emitter.emitProgram(forms)
|
||||
writeFile(nimPath, nimCode)
|
||||
let compileResult = nimCompile(nimPath, binPath)
|
||||
if compileResult != 0:
|
||||
stderr.writeLine("Compilation failed")
|
||||
try: removeDir(buildDir) except: discard
|
||||
quit(1)
|
||||
let runResult = execCmd(binPath)
|
||||
try: removeDir(buildDir) except: discard
|
||||
quit(runResult)
|
||||
|
||||
case cmd
|
||||
|
||||
Reference in New Issue
Block a user