Skip to content

Commit

Permalink
fix #175; improve upon nim-lang#14306
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed May 16, 2020
1 parent a0c0c8d commit 1638a9f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 26 deletions.
22 changes: 15 additions & 7 deletions compiler/docgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,21 @@ proc newDocumentor*(filename: AbsoluteFile; cache: IdentCache; conf: ConfigRef,
# Include the current file if we're parsing a nim file
let importStmt = if d.isPureRst: "" else: "import \"$1\"\n" % [d.filename.replace("\\", "/")]
writeFile(outp, importStmt & content)
let c = if cmd.startsWith("nim <backend> "): os.getAppFilename() & " " & $conf.backend & cmd.substr("nim <backend>".len)
elif cmd.startsWith("nim "): os.getAppFilename() & cmd.substr("nim".len)
else: cmd
let c2 = c % quoteShell(outp)
rawMessage(conf, hintExecuting, c2)
if execShellCmd(c2) != status:
rawMessage(conf, errGenerated, "executing of external program failed: " & c2)

proc interpSnippetCmd(cmd: string): string =
result = cmd
# backward compatibility hacks; interpolation commands should explicitly use `$`
result = result.replace("nim ", "$nim")
result = result.replace("$1", "$options")
result = result % [
"nim", os.getAppFilename().quoteShell,
"backend", $d.conf.backend,
"options", outp.quoteShell,
]
let cmd = cmd.interpSnippetCmd
rawMessage(conf, hintExecuting, cmd)
if execShellCmd(cmd) != status:
rawMessage(conf, errGenerated, "executing of external program failed: " & cmd)
result.emitted = initIntSet()
result.destFile = getOutFile2(conf, presentationPath(conf, filename),
outExt, htmldocsDir, false)
Expand Down
14 changes: 8 additions & 6 deletions compiler/main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,10 @@ proc mainCommand*(graph: ModuleGraph) =
when false: setOutDir(conf)
if optUseNimcache in conf.globalOptions: setOutDir(conf)

proc customizeForBackend(backend2: TBackend) =
proc customizeForBackend() =
## sets backend specific options but don't compile to backend yet
conf.backend = backend2
defineSymbol(graph.config.symbols, $backend2)
case backend2
defineSymbol(graph.config.symbols, $conf.backend)
case conf.backend
of backendC:
if conf.exc == excNone: conf.exc = excSetjmp
of backendCpp:
Expand All @@ -221,8 +220,11 @@ proc mainCommand*(graph: ModuleGraph) =

proc compileToBackend(backend: TBackend = conf.backend, cmd = cmdCompileToBackend) =
commandAlreadyProcessed = true
customizeForBackend(backend)
conf.cmd = cmd
if conf.backend == backendInvalid:
# only set backend if wasn't already set, to allow override via `nim c -b:cpp`
conf.backend = backend
customizeForBackend()
case conf.backend
of backendC: commandCompileToC(graph)
of backendCpp: commandCompileToC(graph)
Expand All @@ -238,7 +240,7 @@ proc mainCommand*(graph: ModuleGraph) =
of "js", "compiletojs": compileToBackend(backendJs)
else:
# this ensures all other commands call this
customizeForBackend(conf.backend)
customizeForBackend()

## process all other commands
case conf.command.normalize
Expand Down
2 changes: 1 addition & 1 deletion compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ proc newConfigRef*(): ConfigRef =
cIncludes: @[], # directories to search for included files
cLibs: @[], # directories to search for lib files
cLinkedLibs: @[], # libraries to link
backend: backendC,
backend: backendInvalid,
externalToLink: @[],
linkOptionsCmd: "",
compileOptionsCmd: @[],
Expand Down
2 changes: 1 addition & 1 deletion lib/packages/docutils/rstgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ proc parseCodeBlockField(d: PDoc, n: PRstNode, params: var CodeBlockParams) =
of "test":
params.testCmd = n.getFieldValue.strip
if params.testCmd.len == 0:
params.testCmd = "nim <backend> -r $1" # The nim backend is auto-set in docgen.nim
params.testCmd = "$nim r --backend:$backend $options" # see `interpSnippetCmd`
else:
params.testCmd = unescape(params.testCmd)
of "status", "exitcode":
Expand Down
15 changes: 11 additions & 4 deletions tests/misc/mimportcpp.nim → tests/misc/mbackend.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ We can't merge this test inside a `when defined(cpp)` because some bug that was
fixed would not trigger in that case.
]#

## bugfix 1: this used to CT error with: Error: unhandled exception: mimportcpp.nim(6, 18) `defined(cpp)`
static: doAssert defined(cpp)
import std/compilesettings
import std/unittest

## checks that `--backend:c` has no side effect (ie, can be overridden by subsequent commands)
static: doAssert not defined(c)
static:
## bugfix 1: this used to CT error with: Error: unhandled exception: mimportcpp.nim(6, 18) `defined(cpp)`
doAssert defined(cpp)
doAssert querySetting(backend) == "cpp"

## checks that `--backend:c` has no side effect (ie, can be overridden by subsequent commands)
doAssert not defined(c)
doAssert not defined(js)
doAssert not defined(js)

type
std_exception {.importcpp: "std::exception", header: "<exception>".} = object
Expand Down
17 changes: 10 additions & 7 deletions tests/trunner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,18 @@ else: # don't run twice the same test
let cmd = fmt"{nim} r --backend:{mode} --hints:off --nimcache:{nimcache} {file}"
check execCmdEx(cmd) == ("ok3\n", 0)

block: # some importc tests
# issue #14314
let file = testsDir / "misc/mimportc.nim"
let cmd = fmt"{nim} r -b:cpp --hints:off --nimcache:{nimcache} --warningAsError:ProveInit {file}"
check execCmdEx(cmd) == ("witness\n", 0)

block: # further issues with `--backend`
let file = testsDir / "misc/mimportcpp.nim"
let file = testsDir / "misc/mbackend.nim"
var cmd = fmt"{nim} doc -b:cpp --hints:off --nimcache:{nimcache} {file}"
check execCmdEx(cmd) == ("", 0)
cmd = fmt"{nim} check -b:c -b:cpp --hints:off --nimcache:{nimcache} {file}"
check execCmdEx(cmd) == ("", 0)
# issue https://github.com/timotheecour/Nim/issues/175
cmd = fmt"{nim} c -b:js -b:cpp --hints:off --nimcache:{nimcache} {file}"
check execCmdEx(cmd) == ("", 0)

block: # some importc tests
# issue #14314
let file = testsDir / "misc/mimportc.nim"
let cmd = fmt"{nim} r -b:cpp --hints:off --nimcache:{nimcache} --warningAsError:ProveInit {file}"
check execCmdEx(cmd) == ("witness\n", 0)

0 comments on commit 1638a9f

Please sign in to comment.