Skip to content

Commit adde711

Browse files
author
Dentcho Bankov
committed
Fix issue #3795 - Fix LDC linked dynamically
with Phobos and DRuntime when built with GDMD. Prepend arguments starting with "-B" in D_LINKER_ARGS with "-Wl," when using GDMD. The latter is needed because the arguments reported by GDMD are what is passed to COLLECT2 while D_LINKER_ARGS are later passed to CXX. The problem with that is that without "-Wl," prefix the -Bstatic and -Bdynamic arguments before and after -lgphobos and -libgdruntime are dropped. And that is a problem because without these the produced LDC is linked dynamically to libgphobos and libgdruntime so these have to be available whereever LDC is used. Once libgphobos and libgdruntime are linked statically symbol conflicts for _d_allocmemory, _d_newclass, _d_newitemiT and _d_newitemT symbols were revealed which are fixed by checking if these symbols are marked as weak in libgdruntime.a and if not "-Wl,-allow-multiple-definition" link option is added to avoid link failure.
1 parent 054df3a commit adde711

1 file changed

Lines changed: 36 additions & 1 deletion

File tree

cmake/Modules/ExtractDMDSystemLinker.cmake

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,47 @@ list(REMOVE_AT linker_line 0)
6666

6767
if("${D_COMPILER_ID}" STREQUAL "GDMD")
6868
# Filter linker arguments for those we know can be safely reused
69+
set(STATIC OFF)
6970
set(D_LINKER_ARGS)
7071
foreach(arg ${linker_line})
71-
if("${arg}" MATCHES ^-L.*|^-l.*|^-B.*)
72+
if("${arg}" MATCHES ^-L.*|^-l.*)
7273
list(APPEND D_LINKER_ARGS "${arg}")
74+
if(STATIC)
75+
if("${arg}" STREQUAL -lgphobos)
76+
set(STATIC_GPHOBOS ON)
77+
endif()
78+
endif()
79+
elseif("${arg}" MATCHES ^-B.*)
80+
list(APPEND D_LINKER_ARGS "-Wl,${arg}")
81+
if("${arg}" STREQUAL -Bstatic)
82+
set(STATIC ON)
83+
elseif("${arg}" STREQUAL -Bdynamic)
84+
set(STATIC OFF)
85+
endif()
7386
endif()
7487
endforeach()
88+
if(STATIC_GPHOBOS)
89+
execute_process(
90+
COMMAND "${D_COMPILER}" -q,-print-file-name=libgdruntime.a
91+
COMMAND "head" -n1
92+
COMMAND "xargs" nm -gC
93+
COMMAND "grep"
94+
-e _d_allocmemory
95+
-e _d_newclass
96+
-e _d_newitemiT
97+
-e _d_newitemT
98+
COMMAND "cut" -d " " -f 2
99+
COMMAND "sort"
100+
COMMAND "uniq"
101+
OUTPUT_VARIABLE LIB_GDRUNTIME_LIFETIME_SYMBOL_TYPES
102+
ERROR_QUIET)
103+
string(STRIP
104+
${LIB_GDRUNTIME_LIFETIME_SYMBOL_TYPES}
105+
LIB_GDRUNTIME_LIFETIME_SYMBOL_TYPES)
106+
if(NOT LIB_GDRUNTIME_LIFETIME_SYMBOL_TYPES STREQUAL W)
107+
list(APPEND D_LINKER_ARGS "-Wl,--allow-multiple-definition")
108+
endif()
109+
endif()
75110
else()
76111
set(D_LINKER_ARGS ${linker_line})
77112
endif()

0 commit comments

Comments
 (0)