nimbus.nim: add depfile support

This commit is contained in:
Anna “CyberTailor” 2022-07-13 14:38:02 +05:00
parent 057853015c
commit d86ce2f26c
Signed by: CyberTaIlor
GPG Key ID: E7B76EDC50864BB1
4 changed files with 28 additions and 12 deletions

View File

@ -6,7 +6,8 @@
* nimbleexecutor.nim (toJsonString): query variables in bulk
* nimbus.nim: add debug output
* nimbus.nim: support patched Nim compilers with --depfile option; add
debug output
* options.nim (initLogger): set default log level to NOTICE

View File

@ -14,6 +14,7 @@
.Op Fl Fl nimbleDir : Ns Ar path
.Op Fl Fl nim : Ns Ar path
.Op Fl Fl url : Ns Ar url
.Op Fl Fl useDepfile
.Op nim opts...
.Ar sourceDir
.Op Ar buildDir
@ -45,14 +46,18 @@ Set the executable directory, where project's binaries will be installed.
.It Fl Fl debug
Show debugging information.
.
.It Fl Fl nim : Ns Ar path
Set the Nim executable.
.
.It Fl Fl nimbleDir : Ns Ar path
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 url : Ns Ar url
If this options is set,
.
.It Fl Fl useDepfile
Tell the Nim compiler to make a depfile.
Only available with patched Nim.
.Nm
will generate and install a
.Pa nimblemeta.json

View File

@ -10,6 +10,7 @@ type
Options* = object
showHelp*: bool
debug*: bool
useDepfile*: bool
nimbleDir*: string
binDir*: string
nim*: string # Nim compiler location
@ -22,8 +23,8 @@ type
const
help* = """
Usage: nimbus [-h] [--debug] [--nimbleDir:path] [--binDir:path] [--nim:path]
[--url:url] [nim opts...] sourceDir [buildDir]
Usage: nimbus [-h] [--debug] [--useDepfile] [--nimbleDir:path] [--binDir:path]
[--nim:path] [--url:url] [nim opts...] sourceDir [buildDir]
positional arguments:
sourceDir
@ -32,6 +33,7 @@ positional arguments:
optional arguments:
-h, --help show this help message and exit
--debug Show debugging information.
--useDepfile Tell Nim compiler to make a depfile.
--nimbleDir:path Nimble directory (default: $1).
--binDir:path Executable directory (default: $2).
--nim:path Nim compiler (default: nim).
@ -158,6 +160,7 @@ func parseFlag(flag, val: string, result: var Options, kind = cmdLongOption) =
case f
of "help", "h": result.showHelp = true
of "debug": result.debug = true
of "usedepfile": result.useDepfile = true
of "nimbledir": result.nimbleDir = val
of "bindir": result.binDir = val
of "nim": result.nim = val
@ -177,6 +180,7 @@ proc parseCmdLine*(): Options =
# set default values here
result.debug = false
result.showHelp = false
result.useDepfile = false
var argc = 0
for kind, key, val in getOpt():

View File

@ -21,17 +21,22 @@ proc processDependencies(requires: seq[string], options: Options): seq[string] =
else:
result.add(dep.getPath(options).quoteShell)
proc application(ninja: File, input, output: string, paths: seq[string]) =
proc application(ninja: File, input, output: string, paths: seq[string],
useDepfile: bool) =
debug(fmt"[build.ninja] Generating target for application '{output}'")
var vars = newStringTable()
if paths.len != 0:
vars["paths"] = "-p:" & paths.join(" -p:")
if useDepfile:
let depfile = quoteShell(output & ".d")
vars["depfileopt"] = "--depfile:" & depfile
ninja.build([output],
rule = "nimc",
inputs = [input],
implicit = ["PHONY"], # FIXME: add depfile support to the Nim compiler
implicit = if useDepfile: newSeq[string]()
else: @["PHONY"],
variables = vars
)
@ -147,9 +152,10 @@ proc setup(options: Options) =
if pkgInfo.bin.len != 0:
debug("[build.ninja] Generating 'nimc' rule")
ninja.rule("nimc",
command = "$nim --hints:off $nimflags c --nimcache:$nimcache " &
"-o:$out $paths $in",
description = "Compiling Nim application $out")
command = "$nim --hints:off $nimflags c --nimcache:$nimcache -o:$out " &
"$depfileopt $paths $in",
description = "Compiling Nim application $out",
depfile = "$out.d")
ninja.newline()
debug("[build.ninja] Generating 'PHONY' target")
@ -176,7 +182,7 @@ proc setup(options: Options) =
for bin in pkgInfo.bin:
let output = bin.lastPathPart.addFileExt(ExeExt)
let input = pkgInfo.getSourceDir(options) / bin.addFileExt("nim")
ninja.application(input, output, depPaths)
ninja.application(input, output, depPaths, options.useDepfile)
ninja.newline()
debug("[build.ninja] Generating 'all' target")