Skip to content

Commit b7a0c08

Browse files
Araqnarimiran
authored andcommitted
added --nimMainPrefix switch; fixes #15955; refs #16945 [backport:1.6] (#19235)
(cherry picked from commit 7ff43d0)
1 parent 4627512 commit b7a0c08

File tree

7 files changed

+36
-17
lines changed

7 files changed

+36
-17
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@
6060
- The `gc` switch has been renamed to `mm` ("memory management") in order to reflect the
6161
reality better. (Nim moved away from all techniques based on "tracing".)
6262

63+
- There is a new switch `--nimMainPrefix:prefix` to influence the `NimMain` that the
64+
compiler produces. This is particularly useful for generating static libraries.

compiler/cgen.nim

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ macro ropecg(m: BModule, frmt: static[FormatStr], args: untyped): Rope =
154154
inc(i)
155155
result.add newCall(formatValue, resVar, args[num])
156156
inc(num)
157+
of '^':
158+
flushStrLit()
159+
inc(i)
160+
result.add newCall(formatValue, resVar, args[^1])
161+
inc(num)
157162
of '0'..'9':
158163
var j = 0
159164
while true:
@@ -1366,7 +1371,7 @@ proc genMainProc(m: BModule) =
13661371
"}$N$N"
13671372

13681373
MainProcs =
1369-
"\tNimMain();$N"
1374+
"\t$^NimMain();$N"
13701375

13711376
MainProcsWithResult =
13721377
MainProcs & ("\treturn $1nim_program_result;$N")
@@ -1376,7 +1381,7 @@ proc genMainProc(m: BModule) =
13761381
"}$N$N"
13771382

13781383
NimMainProc =
1379-
"N_CDECL(void, NimMain)(void) {$N" &
1384+
"N_CDECL(void, $5NimMain)(void) {$N" &
13801385
"\tvoid (*volatile inner)(void);$N" &
13811386
"$4" &
13821387
"\tinner = NimMainInner;$N" &
@@ -1456,28 +1461,27 @@ proc genMainProc(m: BModule) =
14561461
if optGenGuiApp in m.config.globalOptions:
14571462
const nimMain = WinNimMain
14581463
appcg(m, m.s[cfsProcs], nimMain,
1459-
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode])
1464+
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
14601465
else:
14611466
const nimMain = WinNimDllMain
14621467
appcg(m, m.s[cfsProcs], nimMain,
1463-
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode])
1468+
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
14641469
elif m.config.target.targetOS == osGenode:
14651470
const nimMain = GenodeNimMain
14661471
appcg(m, m.s[cfsProcs], nimMain,
1467-
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode])
1472+
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
14681473
elif optGenDynLib in m.config.globalOptions:
14691474
const nimMain = PosixNimDllMain
14701475
appcg(m, m.s[cfsProcs], nimMain,
1471-
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode])
1476+
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
14721477
elif m.config.target.targetOS == osStandalone:
14731478
const nimMain = NimMainBody
14741479
appcg(m, m.s[cfsProcs], nimMain,
1475-
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode])
1480+
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
14761481
else:
14771482
const nimMain = NimMainBody
14781483
appcg(m, m.s[cfsProcs], nimMain,
1479-
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode])
1480-
1484+
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
14811485

14821486
if optNoMain notin m.config.globalOptions:
14831487
if m.config.cppCustomNamespace.len > 0:
@@ -1487,23 +1491,22 @@ proc genMainProc(m: BModule) =
14871491
m.config.globalOptions * {optGenGuiApp, optGenDynLib} != {}:
14881492
if optGenGuiApp in m.config.globalOptions:
14891493
const otherMain = WinCMain
1490-
appcg(m, m.s[cfsProcs], otherMain, [if m.hcrOn: "*" else: ""])
1494+
appcg(m, m.s[cfsProcs], otherMain, [if m.hcrOn: "*" else: "", m.config.nimMainPrefix])
14911495
else:
14921496
const otherMain = WinCDllMain
1493-
appcg(m, m.s[cfsProcs], otherMain, [])
1497+
appcg(m, m.s[cfsProcs], otherMain, [m.config.nimMainPrefix])
14941498
elif m.config.target.targetOS == osGenode:
14951499
const otherMain = ComponentConstruct
1496-
appcg(m, m.s[cfsProcs], otherMain, [])
1500+
appcg(m, m.s[cfsProcs], otherMain, [m.config.nimMainPrefix])
14971501
elif optGenDynLib in m.config.globalOptions:
14981502
const otherMain = PosixCDllMain
1499-
appcg(m, m.s[cfsProcs], otherMain, [])
1503+
appcg(m, m.s[cfsProcs], otherMain, [m.config.nimMainPrefix])
15001504
elif m.config.target.targetOS == osStandalone:
15011505
const otherMain = StandaloneCMain
1502-
appcg(m, m.s[cfsProcs], otherMain, [])
1506+
appcg(m, m.s[cfsProcs], otherMain, [m.config.nimMainPrefix])
15031507
else:
15041508
const otherMain = PosixCMain
1505-
appcg(m, m.s[cfsProcs], otherMain, [if m.hcrOn: "*" else: ""])
1506-
1509+
appcg(m, m.s[cfsProcs], otherMain, [if m.hcrOn: "*" else: "", m.config.nimMainPrefix])
15071510

15081511
if m.config.cppCustomNamespace.len > 0:
15091512
m.s[cfsProcs].add openNamespaceNim(m.config.cppCustomNamespace)
@@ -1885,7 +1888,7 @@ proc writeHeader(m: BModule) =
18851888

18861889
if optGenDynLib in m.config.globalOptions:
18871890
result.add("N_LIB_IMPORT ")
1888-
result.addf("N_CDECL(void, NimMain)(void);$n", [])
1891+
result.addf("N_CDECL(void, $1NimMain)(void);$n", [rope m.config.nimMainPrefix])
18891892
if m.config.cppCustomNamespace.len > 0: result.add closeNamespaceNim()
18901893
result.addf("#endif /* $1 */$n", [guard])
18911894
if not writeRope(result, m.filename):

compiler/commands.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,7 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
10521052
of "": # comes from "-" in for example: `nim c -r -` (gets stripped from -)
10531053
handleStdinInput(conf)
10541054
of "nilseqs", "nilchecks", "mainmodule", "m", "symbol", "taintmode", "cs", "deadcodeelim": warningOptionNoop(switch)
1055+
of "nimmainprefix": conf.nimMainPrefix = arg
10551056
else:
10561057
if strutils.find(switch, '.') >= 0: options.setConfigVar(conf, switch, arg)
10571058
else: invalidCmdLineOption(conf, pass, switch, info)

compiler/options.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ type
390390
structuredErrorHook*: proc (config: ConfigRef; info: TLineInfo; msg: string;
391391
severity: Severity) {.closure, gcsafe.}
392392
cppCustomNamespace*: string
393+
nimMainPrefix*: string
393394
vmProfileData*: ProfileData
394395

395396
proc parseNimVersion*(a: string): NimVer =

doc/advopt.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ Advanced options:
135135
--cppCompileToNamespace:namespace
136136
use the provided namespace for the generated C++ code,
137137
if no namespace is provided "Nim" will be used
138+
--nimMainPrefix:prefix use `{prefix}NimMain` instead of `NimMain` in the produced
139+
C/C++ code
138140
--expandMacro:MACRO dump every generated AST from MACRO
139141
--expandArc:PROCNAME show how PROCNAME looks like after diverse optimizations
140142
before the final backend phase (mostly ARC/ORC specific)

doc/backends.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ Also, C code requires you to specify a forward declaration for functions or
246246
the compiler will assume certain types for the return value and parameters
247247
which will likely make your program crash at runtime.
248248

249+
The name `NimMain` can be influenced via the `--nimMainPrefix:prefix` switch.
250+
Use `--nimMainPrefix:MyLib` and the function to call is named `MyLibNimMain`.
251+
249252

250253
Nim invocation example from C
251254
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

doc/nimc.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ of your program.
371371
NimMain() # initialize garbage collector memory, types and stack
372372
373373
374+
The name `NimMain` can be influenced via the `--nimMainPrefix:prefix` switch.
375+
Use `--nimMainPrefix:MyLib` and the function to call is named `MyLibNimMain`.
376+
377+
374378
Cross-compilation for iOS
375379
=========================
376380

@@ -399,6 +403,9 @@ of your program.
399403
Note: XCode's "make clean" gets confused about the generated nim.c files,
400404
so you need to clean those files manually to do a clean build.
401405

406+
The name `NimMain` can be influenced via the `--nimMainPrefix:prefix` switch.
407+
Use `--nimMainPrefix:MyLib` and the function to call is named `MyLibNimMain`.
408+
402409

403410
Cross-compilation for Nintendo Switch
404411
=====================================

0 commit comments

Comments
 (0)