1 //===----------------------------------------------------------------------===//
2 // LLVM 'GCCLD' UTILITY
4 // This utility is intended to be compatible with GCC, and follows standard
5 // system ld conventions. As such, the default output file is ./a.out.
6 // Additionally, this program outputs a shell script that is used to invoke LLI
7 // to execute the program. In this manner, the generated executable (a.out for
8 // example), is directly executable, whereas the bytecode file actually lives in
9 // the a.out.bc file generated by this program. Also, Force is on by default.
11 // Note that if someone (or a script) deletes the executable program generated,
12 // the .bc file will be left around. Considering that this is a temporary hack,
13 // I'm not to worried about this.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Linker.h"
18 #include "llvm/Bytecode/Reader.h"
19 #include "llvm/Bytecode/Writer.h"
20 #include "llvm/Module.h"
21 #include "llvm/Method.h"
22 #include "Support/CommandLine.h"
25 #include <sys/types.h> // For FileExists
29 cl::StringList InputFilenames("", "Load <arg> files, linking them together",
31 cl::String OutputFilename("o", "Override output filename", cl::NoFlags,"a.out");
32 cl::Flag Verbose ("v", "Print information about actions taken");
33 cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore);
34 cl::StringList Libraries ("l", "Specify libraries to link to", cl::ZeroOrMore);
37 // FileExists - Return true if the specified string is an openable file...
38 static inline bool FileExists(const std::string &FN) {
40 return stat(FN.c_str(), &StatBuf) != -1;
43 // LoadFile - Read the specified bytecode file in and return it. This routine
44 // searches the link path for the specified file to try to find it...
46 static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
47 std::string Filename = FN;
48 std::string ErrorMessage;
50 unsigned NextLibPathIdx = 0;
51 bool FoundAFile = false;
54 if (Verbose) cerr << "Loading '" << Filename << "'\n";
55 if (FileExists(Filename)) FoundAFile = true;
56 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
57 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
60 cerr << "Error opening bytecode file: '" << Filename << "'";
61 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
65 if (NextLibPathIdx == LibPaths.size()) break;
66 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
70 cerr << "Bytecode file '" << FN << "' corrupt! "
71 << "Use 'link -v ...' for more info.\n";
73 cerr << "Could not locate bytecode file: '" << FN << "'\n";
74 return std::auto_ptr<Module>();
80 int main(int argc, char **argv) {
81 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n",
82 cl::EnableSingleLetterArgValue |
83 cl::DisableSingleLetterArgGrouping);
84 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
87 std::string ErrorMessage;
89 if (!Libraries.empty())
90 cerr << "LLVM Linker Warning: Linking to libraries is unimplemented!\n";
92 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
93 if (Composite.get() == 0) return 1;
95 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
96 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
97 if (M.get() == 0) return 1;
99 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
101 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
102 cerr << "Error linking in '" << InputFilenames[i] << "': "
103 << ErrorMessage << endl;
108 std::ofstream Out((OutputFilename+".bc").c_str());
110 cerr << "Error opening '" << OutputFilename << ".bc' for writing!\n";
114 if (Verbose) cerr << "Writing bytecode...\n";
115 WriteBytecodeToFile(Composite.get(), Out);
118 // Output the script to start the program...
119 std::ofstream Out2(OutputFilename.c_str());
121 cerr << "Error openeing '" << OutputFilename << "' for writing!\n";
124 Out2 << "#!/bin/sh\nlli -q $0.bc\n";
127 // Make the script executable...
128 chmod(OutputFilename.c_str(), 0755);