diff --git a/ChangeLog b/ChangeLog index 06c64fe..a7bc26b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -8,6 +8,8 @@ * nimbus.nimble: add clean task + * Use separate cache directories for each target + 2023-04-25 Anna * nimbus.nim: drop support for patched Nim compilers diff --git a/man/nimbus.1 b/man/nimbus.1 index b6c8594..30035ab 100644 --- a/man/nimbus.1 +++ b/man/nimbus.1 @@ -13,6 +13,7 @@ .Op Fl Fl binDir : Ns Ar path .Op Fl Fl nimbleDir : Ns Ar path .Op Fl Fl nim : Ns Ar path +.Op Fl Fl nimcache : Ns Ar path .Op Fl Fl url : Ns Ar url .Op nim opts... .Ar sourceDir @@ -51,6 +52,10 @@ Set the Nimble directory, where project's sources will be installed. .It Fl Fl nim : Ns Ar path Set the Nim executable. . +.It Fl Fl nimcache : Ns Ar path +Set the Nim cache base directory. +Caches for individual targets will be created there. +. .It Fl Fl url : Ns Ar url If this options is set, .Nm diff --git a/src/nimbs/builderscript.nim b/src/nimbs/builderscript.nim index 0ad697f..6c0186f 100644 --- a/src/nimbs/builderscript.nim +++ b/src/nimbs/builderscript.nim @@ -18,7 +18,7 @@ mode = Verbose const nimBin = $1.quoteShell nimFlags = $2 - nimCache = $3 + nimCacheBaseDir = $3 type Options = object @@ -32,12 +32,14 @@ proc build(options: Options) = if options.paths.len != 0: paths = "-p:" & options.paths.join(" -p:") + let targetName = options.target.extractFilename + let nimCache = nimCacheBaseDir / targetName exec fmt"{nimBin} {nimFlags} c --genScript:on --nimcache:{nimCache.quoteShell}" & fmt" -o:{options.outFile.quoteShell} {paths} {options.inFile.quoteShell}" let txt2deps = findExe("txt2deps") if txt2deps.len != 0: - let nimDepsFile = nimCache / options.target.addFileExt("deps") + let nimDepsFile = nimCache / targetName.addFileExt("deps") let gccDepsFile = options.target.addFileExt("d") exec fmt"{txt2deps} -T:{options.target.quoteShell}" & fmt" -i:{nimDepsFile.quoteShell} -o:{gccDepsFile.quoteShell}" @@ -50,6 +52,7 @@ proc parseCmdLine(): Options = of cmdShortOption: case key.normalize() of "t": + # full path! result.target = val result.outFile = val.addFileExt(ExeExt) of "p": @@ -63,4 +66,4 @@ let opts = parseCmdLine() build(opts) """ % [options.getNimBin().tripleQuoted, options.getNimFlags().tripleQuoted, - options.getNimCache().tripleQuoted]) + options.getNimCacheBaseDir().tripleQuoted]) diff --git a/src/nimbs/options.nim b/src/nimbs/options.nim index 92c981e..eebe39d 100644 --- a/src/nimbs/options.nim +++ b/src/nimbs/options.nim @@ -17,13 +17,14 @@ type sourceDir*: string buildDir*: string logger*: ConsoleLogger + nimCacheBaseDir*: string passNimFlags*: seq[string] cmdLine*: seq[string] # only flags, not arguments const help* = fmt""" Usage: nimbus [-h] [--debug] [--nimbleDir:path] [--binDir:path] [--nim:path] - [--url:url] [nim opts...] sourceDir [buildDir] + [--nimcache:path] [--url:url] [nim opts...] sourceDir [buildDir] positional arguments: sourceDir @@ -35,6 +36,7 @@ optional arguments: --nimbleDir:path Nimble directory (default: {defaultNimbleDir}). --binDir:path Executable directory (default: {defaultBinDir}). --nim:path Nim compiler (default: nim). + --nimcache:path Base directory for Nim cache (default: {nimCacheDirName}). --url:url Package URL. Unrecognized flags are passed to the Nim compiler. @@ -61,8 +63,11 @@ proc setLogger*(options: var Options) = func getBuildDir*(options: Options): string = return options.buildDir -func getNimCache*(options: Options): string = - return options.getBuildDir() / nimCacheDirName +func getNimCacheBaseDir*(options: Options): string = + if options.nimCacheBaseDir.len == 0: + return options.getBuildDir() / nimCacheDirName + else: + return options.nimCacheBaseDir proc setBuildDir*(options: var Options) = if options.buildDir.len != 0: @@ -163,6 +168,7 @@ func parseFlag(flag, val: string, result: var Options, kind = cmdLongOption) = of "bindir": result.binDir = val of "nim": result.nim = val of "url": result.url = val + of "nimcache": result.nimCacheBaseDir = val else: result.passNimFlags.add(flagString) result.cmdline.add(flagString) diff --git a/src/nimbs/testerscript.nim b/src/nimbs/testerscript.nim index 120f0fd..393b787 100644 --- a/src/nimbs/testerscript.nim +++ b/src/nimbs/testerscript.nim @@ -13,14 +13,19 @@ import std/[os, strformat, strutils] const nimBin = $1.quoteShell nimFlags = $2 - nimCache = $3.quoteShell + nimCacheBaseDir = $3 withDir($4): for test in listFiles("tests"): if test.startsWith("tests/t") and test.endsWith(".nim"): + let nimCacheDir = nimCacheBaseDir / test.multiReplace( + ('/', '_'), + ('\\', '_') + ) echo "-- Running test ", test, "..." - exec(fmt"{nimBin} --hints:off {nimFlags} r --nimcache:{nimCacheDir} {test.quoteShell}") + exec fmt"{nimBin} --hints:off {nimFlags} r" & + fmt" --nimcache:{nimCacheDir.quoteShell} {test.quoteShell}" """ % [options.getNimBin().tripleQuoted, options.getNimFlags().tripleQuoted, - options.getNimCache().tripleQuoted, + options.getNimCacheBaseDir().tripleQuoted, options.getSourceDir().tripleQuoted]) diff --git a/src/nimbus.nim b/src/nimbus.nim index 7bc01b2..d67f20b 100644 --- a/src/nimbus.nim +++ b/src/nimbus.nim @@ -34,12 +34,14 @@ proc processDependencies(requires: seq[string], options: Options): seq[string] = else: result.add(dep.getPath(options).quoteShell) -proc application(ninja: File, input, target: string, paths: seq[string]) = +proc application(ninja: File, input, target: string; + paths: seq[string], options: Options) = debug(fmt"[build.ninja] Generating target for application '{target}'") var vars = newStringTable() vars["target"] = "$builddir" / target.escape(body = true) vars["sourcefile"] = input.escape(body = true) + vars["nimcache"] = options.getNimCacheBaseDir() / target if paths.len != 0: vars["paths"] = escape("-p:" & paths.join(" -p:"), body = true) @@ -49,6 +51,7 @@ proc application(ninja: File, input, target: string, paths: seq[string]) = inputs = [input.escape, "$builder"], variables = vars ) + ninja.newline() ninja.build([target.addFileExt(ExeExt).escape], rule = "jsonscript", @@ -56,6 +59,7 @@ proc application(ninja: File, input, target: string, paths: seq[string]) = implicit = [jsonScript], variables = vars ) + ninja.newline() proc task(ninja: File, nimsFile, taskName: string) = debug(fmt"[build.ninja] Generating target for task '{taskName}'") @@ -142,7 +146,6 @@ proc setup(options: Options) = debug("[build.ninja] Writing variables") ninja.variable("nim", options.getNimBin().escape(body = true)) ninja.variable("nimbus", getAppFilename().escape(body = true)) - ninja.variable("nimcache", options.getNimCache().escape(body = true)) ninja.variable("cmdline", options.getCmdLine().escape(body = true)) ninja.variable("builder", builderFileName.escape(body = true)) ninja.newline() @@ -213,8 +216,8 @@ proc setup(options: Options) = for bin in pkgInfo.bin: let input = pkgInfo.getSourceDir(options) / bin.addFileExt("nim") - ninja.application(input, bin.lastPathPart, depPaths) - ninja.newline() + let target = bin.lastPathPart + ninja.application(input, target, depPaths, options) debug("[build.ninja] Generating 'all' target") ninja.build(["all"], diff --git a/tests/scripts/builderscript/test.nim b/tests/scripts/builderscript/test.nim index 3490a51..8c981d3 100644 --- a/tests/scripts/builderscript/test.nim +++ b/tests/scripts/builderscript/test.nim @@ -17,7 +17,7 @@ mode = Verbose const nimBin = @/usr/bin/nim@.quoteShell nimFlags = @-d:release --threads:on@ - nimCache = @build dir/nimcache@ + nimCacheBaseDir = @build dir/nimcache@ """.replace("@", '"'.repeat(3)) let opts = Options(buildDir: "build dir", diff --git a/tests/scripts/testerscript/test.nim b/tests/scripts/testerscript/test.nim index 79e1dcf..ec805e6 100644 --- a/tests/scripts/testerscript/test.nim +++ b/tests/scripts/testerscript/test.nim @@ -15,7 +15,7 @@ import std/[os, strformat, strutils] const nimBin = @/usr/bin/nim@.quoteShell nimFlags = @-d:release --threads:on@ - nimCache = @build dir/nimcache@.quoteShell + nimCacheBaseDir = @build dir/nimcache@ withDir(@tests/testerscript@): """.replace("@", '"'.repeat(3))