Skip to content

Commit

Permalink
ref nim-lang#19830; multiple definition of in Nim generated static li…
Browse files Browse the repository at this point in the history
…braries (nim-lang#19934)

* ref nim-lang#19830; multiple definition of in Nim generated static libraries

* fix compile errors
  • Loading branch information
ringabout authored and capocasa committed Mar 31, 2023
1 parent a5858b4 commit 73c8ad5
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ proc getSomeInitName(m: BModule, suffix: string): Rope =
proc getInitName(m: BModule): Rope =
if sfMainModule in m.module.flags:
# generate constant name for main module, for "easy" debugging.
result = rope"NimMainModule"
result = rope(m.config.nimMainPrefix) & rope"NimMainModule"
else:
result = getSomeInitName(m, "Init000")

Expand Down Expand Up @@ -1356,35 +1356,35 @@ proc genMainProc(m: BModule) =
preMainCode.add("\tinitStackBottomWith_actual((void *)&inner);\L")
preMainCode.add("\t(*inner)();\L")
else:
preMainCode.add("\tPreMain();\L")
preMainCode.add("\t$1PreMain();\L" % [rope m.config.nimMainPrefix])

const
# not a big deal if we always compile these 3 global vars... makes the HCR code easier
PosixCmdLine =
"N_LIB_PRIVATE int cmdCount;$N" &
"N_LIB_PRIVATE char** cmdLine;$N" &
"N_LIB_PRIVATE char** gEnv;$N"
var posixCmdLine: Rope
if optNoMain notin m.config.globalOptions:
posixCmdLine.add "\tN_LIB_PRIVATE int cmdCount;\L"
posixCmdLine.add "\tN_LIB_PRIVATE char** cmdLine;\L"
posixCmdLine.add "\tN_LIB_PRIVATE char** gEnv;\L"

const
# The use of a volatile function pointer to call Pre/NimMainInner
# prevents inlining of the NimMainInner function and dependent
# functions, which might otherwise merge their stack frames.

PreMainVolatileBody =
"\tvoid (*volatile inner)(void);$N" &
"\tinner = PreMainInner;$N" &
"\tinner = $3PreMainInner;$N" &
"$1" &
"\t(*inner)();$N"

PreMainNonVolatileBody =
"$1" &
"\tPreMainInner();$N"
"\t$3PreMainInner();$N"

PreMainBodyStart = "$N" &
"N_LIB_PRIVATE void PreMainInner(void) {$N" &
"N_LIB_PRIVATE void $3PreMainInner(void) {$N" &
"$2" &
"}$N$N" &
PosixCmdLine &
"N_LIB_PRIVATE void PreMain(void) {$N"
"$4" &
"N_LIB_PRIVATE void $3PreMain(void) {$N"

PreMainBodyEnd =
"}$N$N"
Expand All @@ -1395,21 +1395,21 @@ proc genMainProc(m: BModule) =
MainProcsWithResult =
MainProcs & ("\treturn $1nim_program_result;$N")

NimMainInner = "N_LIB_PRIVATE N_CDECL(void, NimMainInner)(void) {$N" &
NimMainInner = "N_LIB_PRIVATE N_CDECL(void, $5NimMainInner)(void) {$N" &
"$1" &
"}$N$N"

NimMainVolatileBody =
"\tvoid (*volatile inner)(void);$N" &
"$4" &
"\tinner = NimMainInner;$N" &
"\tinner = $5NimMainInner;$N" &
"$2" &
"\t(*inner)();$N"

NimMainNonVolatileBody =
"$4" &
"$2" &
"\tNimMainInner();$N"
"\t$5NimMainInner();$N"

NimMainProcStart =
"N_CDECL(void, $5NimMain)(void) {$N"
Expand Down Expand Up @@ -1489,9 +1489,9 @@ proc genMainProc(m: BModule) =
else: ropecg(m, "\t#initStackBottomWith((void *)&inner);$N", [])
inc(m.labels)
if m.config.selectedGC notin {gcNone, gcArc, gcOrc}:
appcg(m, m.s[cfsProcs], PreMainBodyStart & PreMainVolatileBody & PreMainBodyEnd, [m.g.mainDatInit, m.g.otherModsInit])
appcg(m, m.s[cfsProcs], PreMainBodyStart & PreMainVolatileBody & PreMainBodyEnd, [m.g.mainDatInit, m.g.otherModsInit, m.config.nimMainPrefix, posixCmdLine])
else:
appcg(m, m.s[cfsProcs], PreMainBodyStart & PreMainNonVolatileBody & PreMainBodyEnd, [m.g.mainDatInit, m.g.otherModsInit])
appcg(m, m.s[cfsProcs], PreMainBodyStart & PreMainNonVolatileBody & PreMainBodyEnd, [m.g.mainDatInit, m.g.otherModsInit, m.config.nimMainPrefix, posixCmdLine])

if m.config.target.targetOS == osWindows and
m.config.globalOptions * {optGenGuiApp, optGenDynLib} != {}:
Expand Down

0 comments on commit 73c8ad5

Please sign in to comment.