annotate gen/linker.cpp @ 325:3c8c58c24aa8 trunk

[svn r346] Fixed #55 - llvmdc can link as fast as dmd now! Made runtime to compile to native by default. Changed linker.cpp to link in native runtime library. This change requires a runtime rebuild as well as a new symbolic link in lib/ to tango/lib/libtango-base-llvmdc-native.a.
author ChristianK
date Thu, 10 Jul 2008 19:38:34 +0200
parents 0baca2feb554
children 6788e98ec1cd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
244
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
1 #include "gen/llvm.h"
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
2 #include "llvm/Linker.h"
276
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
3 #include "llvm/System/Program.h"
306
0baca2feb554 [svn r327] Fixed some more MinGW32 issues. It's now very close to working.
lindquist
parents: 278
diff changeset
4 #if _WIN32
0baca2feb554 [svn r327] Fixed some more MinGW32 issues. It's now very close to working.
lindquist
parents: 278
diff changeset
5 #include "llvm/Support/SystemUtils.h"
0baca2feb554 [svn r327] Fixed some more MinGW32 issues. It's now very close to working.
lindquist
parents: 278
diff changeset
6 #endif
244
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
7
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
8 #include "root.h"
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
9 #include "mars.h"
276
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
10 #include "module.h"
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
11
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
12 #define NO_COUT_LOGGER
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
13 #include "gen/logger.h"
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
14
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
15 //////////////////////////////////////////////////////////////////////////////
244
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
16
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
17 typedef std::vector<llvm::Module*> Module_vector;
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
18
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
19 void linkModules(llvm::Module* dst, const Module_vector& MV)
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
20 {
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
21 if (MV.empty())
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
22 return;
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
23
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
24 llvm::Linker linker("llvmdc", dst);
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
25
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
26 std::string err;
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
27 for (Module_vector::const_iterator i=MV.begin(); i!=MV.end(); ++i)
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
28 {
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
29 if (!linker.LinkInModule(*i, &err))
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
30 {
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
31 error("%s", err.c_str());
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
32 fatal();
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
33 }
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
34 }
a95056b3c996 [svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
lindquist
parents:
diff changeset
35 }
276
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
36
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
37 //////////////////////////////////////////////////////////////////////////////
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
38
277
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
39 static llvm::sys::Path gExePath;
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
40
306
0baca2feb554 [svn r327] Fixed some more MinGW32 issues. It's now very close to working.
lindquist
parents: 278
diff changeset
41 int linkExecutable(const char* argv0)
276
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
42 {
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
43 Logger::println("*** Linking executable ***");
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
44
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
45 // error string
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
46 std::string errstr;
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
47
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
48 // find the llvm-ld program
306
0baca2feb554 [svn r327] Fixed some more MinGW32 issues. It's now very close to working.
lindquist
parents: 278
diff changeset
49 llvm::sys::Path ldpath = llvm::sys::Program::FindProgramByName("llvm-ld");
276
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
50 if (ldpath.isEmpty())
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
51 {
306
0baca2feb554 [svn r327] Fixed some more MinGW32 issues. It's now very close to working.
lindquist
parents: 278
diff changeset
52 ldpath.set("llvm-ld");
276
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
53 }
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
54
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
55 // build arguments
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
56 std::vector<const char*> args;
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
57
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
58 // first the program name ??
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
59 args.push_back("llvm-ld");
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
60
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
61 // output filename
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
62 std::string exestr;
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
63 if (global.params.exefile)
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
64 { // explicit
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
65 exestr = global.params.exefile;
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
66 }
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
67 else
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
68 { // inferred
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
69 // try root module name
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
70 if (Module::rootModule)
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
71 exestr = Module::rootModule->toChars();
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
72 else
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
73 exestr = "a.out";
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
74 }
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
75 if (global.params.isWindows)
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
76 exestr.append(".exe");
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
77
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
78 std::string outopt = "-o=" + exestr;
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
79 args.push_back(outopt.c_str());
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
80
277
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
81 // set the global gExePath
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
82 gExePath.set(exestr);
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
83 assert(gExePath.isValid());
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
84
276
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
85 // create path to exe
277
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
86 llvm::sys::Path exedir(gExePath);
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
87 exedir.set(gExePath.getDirname());
306
0baca2feb554 [svn r327] Fixed some more MinGW32 issues. It's now very close to working.
lindquist
parents: 278
diff changeset
88 if (!exedir.exists())
276
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
89 {
306
0baca2feb554 [svn r327] Fixed some more MinGW32 issues. It's now very close to working.
lindquist
parents: 278
diff changeset
90 exedir.createDirectoryOnDisk(true, &errstr);
0baca2feb554 [svn r327] Fixed some more MinGW32 issues. It's now very close to working.
lindquist
parents: 278
diff changeset
91 if (!errstr.empty())
0baca2feb554 [svn r327] Fixed some more MinGW32 issues. It's now very close to working.
lindquist
parents: 278
diff changeset
92 {
0baca2feb554 [svn r327] Fixed some more MinGW32 issues. It's now very close to working.
lindquist
parents: 278
diff changeset
93 error("failed to create path to linking output: %s\n%s", exedir.c_str(), errstr.c_str());
0baca2feb554 [svn r327] Fixed some more MinGW32 issues. It's now very close to working.
lindquist
parents: 278
diff changeset
94 fatal();
0baca2feb554 [svn r327] Fixed some more MinGW32 issues. It's now very close to working.
lindquist
parents: 278
diff changeset
95 }
0baca2feb554 [svn r327] Fixed some more MinGW32 issues. It's now very close to working.
lindquist
parents: 278
diff changeset
96 }
276
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
97
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
98 // strip debug info
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
99 if (!global.params.symdebug)
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
100 args.push_back("-strip-debug");
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
101
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
102 // optimization level
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
103 if (!global.params.optimize)
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
104 args.push_back("-disable-opt");
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
105 else
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
106 {
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
107 const char* s = 0;
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
108 switch(global.params.optimizeLevel)
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
109 {
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
110 case 0:
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
111 s = "-O0"; break;
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
112 case 1:
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
113 s = "-O1"; break;
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
114 case 2:
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
115 s = "-O2"; break;
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
116 case 3:
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
117 s = "-O3"; break;
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
118 case 4:
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
119 s = "-O4"; break;
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
120 case 5:
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
121 s = "-O5"; break;
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
122 default:
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
123 assert(0);
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
124 }
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
125 args.push_back(s);
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
126 }
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
127
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
128 // inlining
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
129 if (!(global.params.useInline || global.params.llvmInline))
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
130 {
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
131 args.push_back("-disable-inlining");
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
132 }
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
133
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
134 // additional linker switches
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
135 for (int i = 0; i < global.params.linkswitches->dim; i++)
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
136 {
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
137 char *p = (char *)global.params.linkswitches->data[i];
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
138 args.push_back(p);
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
139 }
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
140
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
141 // native please
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
142 args.push_back("-native");
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
143
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
144
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
145 // user libs
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
146 for (int i = 0; i < global.params.libfiles->dim; i++)
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
147 {
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
148 char *p = (char *)global.params.libfiles->data[i];
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
149 args.push_back(p);
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
150 }
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
151
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
152 // default libs
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
153 args.push_back("-lpthread");
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
154 args.push_back("-ldl");
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
155 args.push_back("-lm");
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
156
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
157 // object files
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
158 for (int i = 0; i < global.params.objfiles->dim; i++)
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
159 {
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
160 char *p = (char *)global.params.objfiles->data[i];
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
161 args.push_back(p);
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
162 }
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
163
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
164 // runtime library
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
165 // must be linked in last to null terminate the moduleinfo appending list
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
166 std::string runtime_path(global.params.runtimePath);
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
167 if (*runtime_path.rbegin() != '/')
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
168 runtime_path.append("/");
325
3c8c58c24aa8 [svn r346] Fixed #55 - llvmdc can link as fast as dmd now!
ChristianK
parents: 306
diff changeset
169 runtime_path.append("libtango-base-llvmdc-native.a");
276
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
170 args.push_back(runtime_path.c_str());
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
171
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
172 // print link command?
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
173 if (!global.params.quiet || global.params.verbose)
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
174 {
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
175 // Print it
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
176 for (int i = 0; i < args.size(); i++)
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
177 printf("%s ", args[i]);
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
178 printf("\n");
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
179 fflush(stdout);
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
180 }
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
181
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
182 // terminate args list
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
183 args.push_back(NULL);
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
184
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
185 // try to call linker!!!
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
186 if (int status = llvm::sys::Program::ExecuteAndWait(ldpath, &args[0], NULL, NULL, 0,0, &errstr))
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
187 {
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
188 error("linking failed:\nstatus: %d", status);
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
189 if (!errstr.empty())
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
190 error("message: %s", errstr.c_str());
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
191 fatal();
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
192 }
21f85bac0b1a [svn r297] Fixed: rewrote linker code to use LLVM's Program facilities instead of DMD's oldschool broken "native" approach.
lindquist
parents: 244
diff changeset
193 }
277
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
194
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
195 //////////////////////////////////////////////////////////////////////////////
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
196
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
197 void deleteExecutable()
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
198 {
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
199 if (!gExePath.isEmpty())
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
200 {
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
201 assert(gExePath.isValid());
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
202 assert(!gExePath.isDirectory());
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
203 gExePath.eraseFromDisk(false);
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
204 }
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
205 }
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
206
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
207 //////////////////////////////////////////////////////////////////////////////
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
208
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
209 int runExectuable()
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
210 {
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
211 assert(!gExePath.isEmpty());
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
212 assert(gExePath.isValid());
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
213
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
214 // build arguments
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
215 std::vector<const char*> args;
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
216 for (size_t i = 0; i < global.params.runargs_length; i++)
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
217 {
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
218 char *a = global.params.runargs[i];
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
219 args.push_back(a);
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
220 }
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
221 // terminate args list
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
222 args.push_back(NULL);
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
223
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
224 // try to call linker!!!
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
225 std::string errstr;
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
226 int status = llvm::sys::Program::ExecuteAndWait(gExePath, &args[0], NULL, NULL, 0,0, &errstr);
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
227 if (!errstr.empty())
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
228 {
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
229 error("failed to execute program");
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
230 if (!errstr.empty())
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
231 error("error message: %s", errstr.c_str());
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
232 fatal();
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
233 }
278
30941d8ee320 [svn r299] forgot to return status in runExecutable
lindquist
parents: 277
diff changeset
234 return status;
277
90a8c798b0db [svn r298] Eliminated the dmd/link.c source file entirely in favor of a llvm::sys based approach to the same functionality.
lindquist
parents: 276
diff changeset
235 }