cross compilation targetting windows now supports nim r: nim r -d:mingw main (#18682)

* cross compilation targetting windows now supports `nim r`: `nim r -d:mingw main`

* quoteShell

* address comment: remove `conf.getConfigVar("nimrun.exe")`
This commit is contained in:
Timothee Cour 2021-08-16 00:32:12 -07:00 • committed by GitHub
commit 1acba63cb7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 5 deletions

View file

@ -482,6 +482,9 @@
nim r main # uses cached binary nim r main # uses cached binary
nim r main arg1 arg2 # ditto (runtime arguments are irrelevant) nim r main arg1 arg2 # ditto (runtime arguments are irrelevant)
- `nim r` now supports cross compilation from unix to windows when specifying `-d:mingw` by using wine,
e.g.: `nim r --eval:'import os; echo "a" / "b"'` prints `a\b`
- The style checking of the compiler now supports a `--styleCheck:usages` switch. This switch - The style checking of the compiler now supports a `--styleCheck:usages` switch. This switch
enforces that every symbol is written as it was declared, not enforcing enforces that every symbol is written as it was declared, not enforcing
the official Nim style guide. To be enabled, this has to be combined either the official Nim style guide. To be enabled, this has to be combined either

View file

@ -70,6 +70,13 @@ proc processCmdLine(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
config.arguments.len > 0 and config.cmd notin {cmdTcc, cmdNimscript, cmdCrun}: config.arguments.len > 0 and config.cmd notin {cmdTcc, cmdNimscript, cmdCrun}:
rawMessage(config, errGenerated, errArgsNeedRunOption) rawMessage(config, errGenerated, errArgsNeedRunOption)
proc getNimRunExe(conf: ConfigRef): string =
# xxx consider defining `conf.getConfigVar("nimrun.exe")` to allow users to
# customize the binary to run the command with, e.g. for custom `nodejs` or `wine`.
if conf.isDefined("mingw"):
if conf.isDefined("i386"): result = "wine"
elif conf.isDefined("amd64"): result = "wine64"
proc handleCmdLine(cache: IdentCache; conf: ConfigRef) = proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
let self = NimProg( let self = NimProg(
supportsStdinFile: true, supportsStdinFile: true,
@ -95,18 +102,21 @@ proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
let output = conf.absOutFile let output = conf.absOutFile
case conf.cmd case conf.cmd
of cmdBackends, cmdTcc: of cmdBackends, cmdTcc:
var cmdPrefix = "" let nimRunExe = getNimRunExe(conf)
var cmdPrefix: string
if nimRunExe.len > 0: cmdPrefix.add nimRunExe.quoteShell
case conf.backend case conf.backend
of backendC, backendCpp, backendObjc: discard of backendC, backendCpp, backendObjc: discard
of backendJs: of backendJs:
# D20210217T215950:here this flag is needed for node < v15.0.0, otherwise # D20210217T215950:here this flag is needed for node < v15.0.0, otherwise
# tasyncjs_fail` would fail, refs https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode # tasyncjs_fail` would fail, refs https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode
cmdPrefix = findNodeJs() & " --unhandled-rejections=strict " if cmdPrefix.len == 0: cmdPrefix = findNodeJs().quoteShell
cmdPrefix.add " --unhandled-rejections=strict"
else: doAssert false, $conf.backend else: doAssert false, $conf.backend
# No space before command otherwise on windows you'd get a cryptic: if cmdPrefix.len > 0: cmdPrefix.add " "
# without the `cmdPrefix.len > 0` check, on windows you'd get a cryptic:
# `The parameter is incorrect` # `The parameter is incorrect`
execExternalProgram(conf, cmdPrefix & output.quoteShell & ' ' & conf.arguments) execExternalProgram(conf, cmdPrefix & output.quoteShell & ' ' & conf.arguments)
# execExternalProgram(conf, cmdPrefix & ' ' & output.quoteShell & ' ' & conf.arguments)
of cmdDocLike, cmdRst2html, cmdRst2tex: # bugfix(cmdRst2tex was missing) of cmdDocLike, cmdRst2html, cmdRst2tex: # bugfix(cmdRst2tex was missing)
if conf.arguments.len > 0: if conf.arguments.len > 0:
# reserved for future use # reserved for future use

View file

@ -317,6 +317,8 @@ To cross-compile for Windows from Linux or macOS using the MinGW-w64 toolchain:
.. code:: cmd .. code:: cmd
nim c -d:mingw myproject.nim nim c -d:mingw myproject.nim
# `nim r` also works, running the binary via `wine` or `wine64`:
nim r -d:mingw --eval:'import os; echo "a" / "b"'
Use `--cpu:i386`:option: or `--cpu:amd64`:option: to switch the CPU architecture. Use `--cpu:i386`:option: or `--cpu:amd64`:option: to switch the CPU architecture.