added --nimMainPrefix switch; fixes #15955; refs #16945 [backport:1.6] (#19235)

This commit is contained in:
Andreas Rumpf 2021-12-10 14:28:59 +01:00 • committed by GitHub
commit 7ff43d07b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 17 deletions

View file

@ -71,3 +71,5 @@
- The `gc` switch has been renamed to `mm` ("memory management") in order to reflect the - The `gc` switch has been renamed to `mm` ("memory management") in order to reflect the
reality better. (Nim moved away from all techniques based on "tracing".) reality better. (Nim moved away from all techniques based on "tracing".)
- There is a new switch `--nimMainPrefix:prefix` to influence the `NimMain` that the
compiler produces. This is particularly useful for generating static libraries.

View file

@ -154,6 +154,11 @@ macro ropecg(m: BModule, frmt: static[FormatStr], args: untyped): Rope =
inc(i) inc(i)
result.add newCall(formatValue, resVar, args[num]) result.add newCall(formatValue, resVar, args[num])
inc(num) inc(num)
of '^':
flushStrLit()
inc(i)
result.add newCall(formatValue, resVar, args[^1])
inc(num)
of '0'..'9': of '0'..'9':
var j = 0 var j = 0
while true: while true:
@ -1366,7 +1371,7 @@ proc genMainProc(m: BModule) =
"}$N$N" "}$N$N"
MainProcs = MainProcs =
"\tNimMain();$N" "\t$^NimMain();$N"
MainProcsWithResult = MainProcsWithResult =
MainProcs & ("\treturn $1nim_program_result;$N") MainProcs & ("\treturn $1nim_program_result;$N")
@ -1376,7 +1381,7 @@ proc genMainProc(m: BModule) =
"}$N$N" "}$N$N"
NimMainProc = NimMainProc =
"N_CDECL(void, NimMain)(void) {$N" & "N_CDECL(void, $5NimMain)(void) {$N" &
"\tvoid (*volatile inner)(void);$N" & "\tvoid (*volatile inner)(void);$N" &
"$4" & "$4" &
"\tinner = NimMainInner;$N" & "\tinner = NimMainInner;$N" &
@ -1456,28 +1461,27 @@ proc genMainProc(m: BModule) =
if optGenGuiApp in m.config.globalOptions: if optGenGuiApp in m.config.globalOptions:
const nimMain = WinNimMain const nimMain = WinNimMain
appcg(m, m.s[cfsProcs], nimMain, appcg(m, m.s[cfsProcs], nimMain,
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode]) [m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
else: else:
const nimMain = WinNimDllMain const nimMain = WinNimDllMain
appcg(m, m.s[cfsProcs], nimMain, appcg(m, m.s[cfsProcs], nimMain,
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode]) [m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
elif m.config.target.targetOS == osGenode: elif m.config.target.targetOS == osGenode:
const nimMain = GenodeNimMain const nimMain = GenodeNimMain
appcg(m, m.s[cfsProcs], nimMain, appcg(m, m.s[cfsProcs], nimMain,
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode]) [m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
elif optGenDynLib in m.config.globalOptions: elif optGenDynLib in m.config.globalOptions:
const nimMain = PosixNimDllMain const nimMain = PosixNimDllMain
appcg(m, m.s[cfsProcs], nimMain, appcg(m, m.s[cfsProcs], nimMain,
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode]) [m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
elif m.config.target.targetOS == osStandalone: elif m.config.target.targetOS == osStandalone:
const nimMain = NimMainBody const nimMain = NimMainBody
appcg(m, m.s[cfsProcs], nimMain, appcg(m, m.s[cfsProcs], nimMain,
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode]) [m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
else: else:
const nimMain = NimMainBody const nimMain = NimMainBody
appcg(m, m.s[cfsProcs], nimMain, appcg(m, m.s[cfsProcs], nimMain,
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode]) [m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
if optNoMain notin m.config.globalOptions: if optNoMain notin m.config.globalOptions:
if m.config.cppCustomNamespace.len > 0: if m.config.cppCustomNamespace.len > 0:
@ -1487,23 +1491,22 @@ proc genMainProc(m: BModule) =
m.config.globalOptions * {optGenGuiApp, optGenDynLib} != {}: m.config.globalOptions * {optGenGuiApp, optGenDynLib} != {}:
if optGenGuiApp in m.config.globalOptions: if optGenGuiApp in m.config.globalOptions:
const otherMain = WinCMain const otherMain = WinCMain
appcg(m, m.s[cfsProcs], otherMain, [if m.hcrOn: "*" else: ""]) appcg(m, m.s[cfsProcs], otherMain, [if m.hcrOn: "*" else: "", m.config.nimMainPrefix])
else: else:
const otherMain = WinCDllMain const otherMain = WinCDllMain
appcg(m, m.s[cfsProcs], otherMain, []) appcg(m, m.s[cfsProcs], otherMain, [m.config.nimMainPrefix])
elif m.config.target.targetOS == osGenode: elif m.config.target.targetOS == osGenode:
const otherMain = ComponentConstruct const otherMain = ComponentConstruct
appcg(m, m.s[cfsProcs], otherMain, []) appcg(m, m.s[cfsProcs], otherMain, [m.config.nimMainPrefix])
elif optGenDynLib in m.config.globalOptions: elif optGenDynLib in m.config.globalOptions:
const otherMain = PosixCDllMain const otherMain = PosixCDllMain
appcg(m, m.s[cfsProcs], otherMain, []) appcg(m, m.s[cfsProcs], otherMain, [m.config.nimMainPrefix])
elif m.config.target.targetOS == osStandalone: elif m.config.target.targetOS == osStandalone:
const otherMain = StandaloneCMain const otherMain = StandaloneCMain
appcg(m, m.s[cfsProcs], otherMain, []) appcg(m, m.s[cfsProcs], otherMain, [m.config.nimMainPrefix])
else: else:
const otherMain = PosixCMain const otherMain = PosixCMain
appcg(m, m.s[cfsProcs], otherMain, [if m.hcrOn: "*" else: ""]) appcg(m, m.s[cfsProcs], otherMain, [if m.hcrOn: "*" else: "", m.config.nimMainPrefix])
if m.config.cppCustomNamespace.len > 0: if m.config.cppCustomNamespace.len > 0:
m.s[cfsProcs].add openNamespaceNim(m.config.cppCustomNamespace) m.s[cfsProcs].add openNamespaceNim(m.config.cppCustomNamespace)
@ -1885,7 +1888,7 @@ proc writeHeader(m: BModule) =
if optGenDynLib in m.config.globalOptions: if optGenDynLib in m.config.globalOptions:
result.add("N_LIB_IMPORT ") result.add("N_LIB_IMPORT ")
result.addf("N_CDECL(void, NimMain)(void);$n", []) result.addf("N_CDECL(void, $1NimMain)(void);$n", [rope m.config.nimMainPrefix])
if m.config.cppCustomNamespace.len > 0: result.add closeNamespaceNim() if m.config.cppCustomNamespace.len > 0: result.add closeNamespaceNim()
result.addf("#endif /* $1 */$n", [guard]) result.addf("#endif /* $1 */$n", [guard])
if not writeRope(result, m.filename): if not writeRope(result, m.filename):

View file

@ -1052,6 +1052,7 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
of "": # comes from "-" in for example: `nim c -r -` (gets stripped from -) of "": # comes from "-" in for example: `nim c -r -` (gets stripped from -)
handleStdinInput(conf) handleStdinInput(conf)
of "nilseqs", "nilchecks", "mainmodule", "m", "symbol", "taintmode", "cs", "deadcodeelim": warningOptionNoop(switch) of "nilseqs", "nilchecks", "mainmodule", "m", "symbol", "taintmode", "cs", "deadcodeelim": warningOptionNoop(switch)
of "nimmainprefix": conf.nimMainPrefix = arg
else: else:
if strutils.find(switch, '.') >= 0: options.setConfigVar(conf, switch, arg) if strutils.find(switch, '.') >= 0: options.setConfigVar(conf, switch, arg)
else: invalidCmdLineOption(conf, pass, switch, info) else: invalidCmdLineOption(conf, pass, switch, info)

View file

@ -390,6 +390,7 @@ type
structuredErrorHook*: proc (config: ConfigRef; info: TLineInfo; msg: string; structuredErrorHook*: proc (config: ConfigRef; info: TLineInfo; msg: string;
severity: Severity) {.closure, gcsafe.} severity: Severity) {.closure, gcsafe.}
cppCustomNamespace*: string cppCustomNamespace*: string
nimMainPrefix*: string
vmProfileData*: ProfileData vmProfileData*: ProfileData
proc parseNimVersion*(a: string): NimVer = proc parseNimVersion*(a: string): NimVer =

View file

@ -135,6 +135,8 @@ Advanced options:
--cppCompileToNamespace:namespace --cppCompileToNamespace:namespace
use the provided namespace for the generated C++ code, use the provided namespace for the generated C++ code,
if no namespace is provided "Nim" will be used if no namespace is provided "Nim" will be used
--nimMainPrefix:prefix use `{prefix}NimMain` instead of `NimMain` in the produced
C/C++ code
--expandMacro:MACRO dump every generated AST from MACRO --expandMacro:MACRO dump every generated AST from MACRO
--expandArc:PROCNAME show how PROCNAME looks like after diverse optimizations --expandArc:PROCNAME show how PROCNAME looks like after diverse optimizations
before the final backend phase (mostly ARC/ORC specific) before the final backend phase (mostly ARC/ORC specific)

View file

@ -246,6 +246,9 @@ Also, C code requires you to specify a forward declaration for functions or
the compiler will assume certain types for the return value and parameters the compiler will assume certain types for the return value and parameters
which will likely make your program crash at runtime. which will likely make your program crash at runtime.
The name `NimMain` can be influenced via the `--nimMainPrefix:prefix` switch.
Use `--nimMainPrefix:MyLib` and the function to call is named `MyLibNimMain`.
Nim invocation example from C Nim invocation example from C
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View file

@ -385,6 +385,10 @@ of your program.
NimMain() # initialize garbage collector memory, types and stack NimMain() # initialize garbage collector memory, types and stack
The name `NimMain` can be influenced via the `--nimMainPrefix:prefix` switch.
Use `--nimMainPrefix:MyLib` and the function to call is named `MyLibNimMain`.
Cross-compilation for iOS Cross-compilation for iOS
========================= =========================
@ -413,6 +417,9 @@ of your program.
Note: XCode's "make clean" gets confused about the generated nim.c files, Note: XCode's "make clean" gets confused about the generated nim.c files,
so you need to clean those files manually to do a clean build. so you need to clean those files manually to do a clean build.
The name `NimMain` can be influenced via the `--nimMainPrefix:prefix` switch.
Use `--nimMainPrefix:MyLib` and the function to call is named `MyLibNimMain`.
Cross-compilation for Nintendo Switch Cross-compilation for Nintendo Switch
===================================== =====================================