|
24 | 24 | #include "gen/utils.h" |
25 | 25 | #include "ir/irtype.h" |
26 | 26 | #include "ir/irvar.h" |
| 27 | +#include "llvm/ADT/SmallString.h" |
27 | 28 |
|
28 | 29 | /* ================================================================== */ |
29 | 30 |
|
@@ -384,13 +385,38 @@ void PragmaDeclaration::codegen(IRState *p) |
384 | 385 | } |
385 | 386 | } |
386 | 387 |
|
387 | | - size_t const n = nameLen + 3; |
388 | | - char *arg = static_cast<char *>(mem.malloc(n)); |
389 | | - arg[0] = '-'; |
390 | | - arg[1] = 'l'; |
391 | | - memcpy(arg + 2, se->string, nameLen); |
392 | | - arg[n-1] = 0; |
393 | | - global.params.linkswitches->push(arg); |
| 388 | +#if LDC_LLVM_VER >= 303 |
| 389 | + // With LLVM 3.3 or later we can place the library name in the object |
| 390 | + // file. This seems to be supported only on Windows. |
| 391 | + if (global.params.targetTriple.getOS() == llvm::Triple::Win32) |
| 392 | + { |
| 393 | + llvm::SmallString<24> LibName(llvm::StringRef(static_cast<const char *>(se->string), nameLen)); |
| 394 | + |
| 395 | + // Win32: /DEFAULTLIB:"curl" |
| 396 | + if (LibName.endswith(".a")) |
| 397 | + LibName = LibName.substr(0, LibName.size()-2); |
| 398 | + if (LibName.endswith(".lib")) |
| 399 | + LibName = LibName.substr(0, LibName.size()-4); |
| 400 | + llvm::SmallString<24> tmp("/DEFAULTLIB:\""); |
| 401 | + tmp.append(LibName); |
| 402 | + tmp.append("\""); |
| 403 | + LibName = tmp; |
| 404 | + |
| 405 | + // Embedd library name as linker option in object file |
| 406 | + llvm::Value *Value = llvm::MDString::get(gIR->context(), LibName); |
| 407 | + gIR->LinkerMetadataArgs.push_back(llvm::MDNode::get(gIR->context(), Value)); |
| 408 | + } |
| 409 | + else |
| 410 | +#endif |
| 411 | + { |
| 412 | + size_t const n = nameLen + 3; |
| 413 | + char *arg = static_cast<char *>(mem.malloc(n)); |
| 414 | + arg[0] = '-'; |
| 415 | + arg[1] = 'l'; |
| 416 | + memcpy(arg + 2, se->string, nameLen); |
| 417 | + arg[n-1] = 0; |
| 418 | + global.params.linkswitches->push(arg); |
| 419 | + } |
394 | 420 | } |
395 | 421 | AttribDeclaration::codegen(p); |
396 | 422 | } |
|
0 commit comments