Skip to content

Commit

Permalink
fix nim-lang#15955: --app:staticlib -d:nimLinkerWeakSymbols allows us…
Browse files Browse the repository at this point in the history
…ing weak symbols
  • Loading branch information
timotheecour committed Feb 8, 2021
1 parent 2319d0f commit 6ea6fea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
5 changes: 2 additions & 3 deletions compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1361,9 +1361,8 @@ proc genMainProc(m: BModule) =
"$1" &
"}$N$N"


NimMainProc =
"N_LIB_WEAK N_CDECL(void, NimMain)(void) /*zook1*/ {$N" &
"N_LIB_WEAK_MAYBE N_CDECL(void, NimMain)(void) /*zook1*/ {$N" &
"\tvoid (*volatile inner)(void);$N" &
"$4" &
"\tinner = NimMainInner;$N" &
Expand Down Expand Up @@ -1856,7 +1855,7 @@ proc writeHeader(m: BModule) =

if optGenDynLib in m.config.globalOptions:
result.add("N_LIB_IMPORT ")
result.addf("N_LIB_PRIVATE N_CDECL(void, NimMain)(void);$n", [])
result.addf("N_LIB_WEAK_MAYBE N_CDECL(void, NimMain)(void);$n", [])
if m.config.cppCustomNamespace.len > 0: result.add closeNamespaceNim()
result.addf("#endif /* $1 */$n", [guard])
if not writeRope(result, m.filename):
Expand Down
23 changes: 10 additions & 13 deletions lib/nimbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,12 @@ __AVR__
// semantics differ a bit from __attribute__((weak)),
// see https://stackoverflow.com/questions/2290587/gcc-style-weak-linking-in-visual-studio
// for alternative based on `/alternatename:` if this isn't enough.
# define N_LIB_WEAK __declspec(selectany)
# define N_LIB_PRIVATE
# define N_LIB_WEAK_MAYBE __declspec(selectany)
# else
# define N_LIB_WEAK
# define N_LIB_PRIVATE
# define N_LIB_WEAK_MAYBE
# endif

# define N_LIB_PRIVATE N_LIB_WEAK_MAYBE
# define N_CDECL(rettype, name) rettype __cdecl name
# define N_STDCALL(rettype, name) rettype __stdcall name
# define N_SYSCALL(rettype, name) rettype __syscall name
Expand All @@ -205,19 +204,17 @@ __AVR__
# endif
# define N_LIB_EXPORT_VAR __declspec(dllexport)
# define N_LIB_IMPORT extern __declspec(dllimport)

#else
# define N_LIB_WEAK __attribute__((weak))
// # define N_LIB_PRIVATE N_LIB_WEAK
// # define N_LIB_PRIVATE __attribute__((visibility("hidden")))
// # define N_LIB_PRIVATE __attribute__((visibility("hidden"))) __attribute__((weak))
// # define N_LIB_PRIVATE __attribute__((weak))

# ifdef NIM_nimLinkerWeakSymbols
# define N_LIB_WEAK __attribute__((weak))
# define N_LIB_PRIVATE __attribute__((weak)) __attribute__((visibility("hidden")))
# define N_LIB_WEAK_MAYBE __attribute__((weak))
# else
# define N_LIB_WEAK
# define N_LIB_PRIVATE __attribute__((visibility("hidden")))
# define N_LIB_WEAK_MAYBE
# endif

# define N_LIB_PRIVATE N_LIB_WEAK_MAYBE __attribute__((visibility("hidden")))

# if defined(__GNUC__)
# define N_CDECL(rettype, name) rettype name
# define N_STDCALL(rettype, name) rettype name
Expand Down
5 changes: 4 additions & 1 deletion tests/misc/tapp_lib_staticlib.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ joinable: false
"""

# bug #16949
# bug #15955

when defined case1:
proc foo(): int {.exportc.} = 10
Expand All @@ -20,8 +21,10 @@ else:
file = currentSourcePath
nim = getCurrentCompilerExe()
mode = querySetting(backend)

proc test(lib, options: string) =
runCmd fmt"{nim} {mode} -o:{lib} --nomain {options} -f {file}"
# runCmd fmt"{nim} r -b:{mode} --passl:{lib} -d:caseMain -f {file}" # pending https://github.com/nim-lang/Nim/pull/16945
runCmd fmt"{nim} r -b:{mode} --passl:{lib} -d:caseMain -f {file}"

test(buildDir / "libD20210205T172314.a", "--app:staticlib -d:nimLinkerWeakSymbols -d:case1")
test(buildDir / DynlibFormat % "D20210205T172720", "--app:lib -d:case2")

0 comments on commit 6ea6fea

Please sign in to comment.