#include <fstream>
#include <memory>
#include <set>
-
-/// IsArchive - Returns true IFF the file named FN appears to be a "ar" library
-/// archive. The file named FN must exist.
-///
-static inline bool IsArchive(const std::string &FN) {
- // Inspect the beginning of the file to see if it contains the "ar" magic
- // string.
- std::string Magic("!<arch>\012");
- char buf[1 + Magic.size()];
- std::ifstream f(FN.c_str());
- f.read(buf, Magic.size());
- buf[Magic.size()] = '\0';
- return Magic == buf;
-}
-
-/// IsBytecode - Returns true IFF the file named FN appears to be an
-/// LLVM bytecode file. The file named FN must exist.
-///
-static inline bool IsBytecode(const std::string &FN) {
- // Inspect the beginning of the file to see if it contains the LLVM
- // bytecode format magic string.
- std::string Magic("llvm");
- char buf[1 + Magic.size()];
- std::ifstream f(FN.c_str());
- f.read(buf, Magic.size());
- buf[Magic.size()] = '\0';
- return Magic == buf;
-}
-
-/// FindLib - locates a particular library. It will prepend and append
-/// various directories, prefixes, and suffixes until it can find the library.
-///
-/// Inputs:
-/// Filename - Name of the file to find.
-/// Paths - List of directories to search.
+using namespace llvm;
+
+/// FindLib - Try to convert Filename into the name of a file that we can open,
+/// if it does not already name a file we can open, by first trying to open
+/// Filename, then libFilename.<suffix> for each of a set of several common
+/// library suffixes, in each of the directories in Paths and the directory
+/// named by the value of the environment variable LLVM_LIB_SEARCH_PATH. Returns
+/// an empty string if no matching file can be found.
///
-/// Outputs:
-/// None.
-///
-/// Return value:
-/// The name of the file is returned.
-/// If the file is not found, an empty string is returned.
-///
-static std::string
-FindLib(const std::string &Filename, const std::vector<std::string> &Paths) {
+std::string llvm::FindLib(const std::string &Filename,
+ const std::vector<std::string> &Paths,
+ bool SharedObjectOnly) {
// Determine if the pathname can be found as it stands.
if (FileOpenable(Filename))
return Filename;
for (unsigned Index = 0; Index != Paths.size(); ++Index) {
std::string Directory = Paths[Index] + "/";
- if (FileOpenable(Directory + LibName + ".bc"))
+ if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".bc"))
return Directory + LibName + ".bc";
if (FileOpenable(Directory + LibName + ".so"))
return Directory + LibName + ".so";
- if (FileOpenable(Directory + LibName + ".a"))
+ if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".a"))
return Directory + LibName + ".a";
}
/// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the
/// name of each externally-visible symbol defined in M.
///
-void GetAllDefinedSymbols(Module *M, std::set<std::string> &DefinedSymbols) {
+void llvm::GetAllDefinedSymbols(Module *M,
+ std::set<std::string> &DefinedSymbols) {
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
DefinedSymbols.insert(I->getName());
/// UndefinedSymbols - A set of C++ strings containing the name of all
/// undefined symbols.
///
-/// Return value:
-/// None.
-///
void
-GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
+llvm::GetAllUndefinedSymbols(Module *M,
+ std::set<std::string> &UndefinedSymbols) {
std::set<std::string> DefinedSymbols;
UndefinedSymbols.clear(); // Start out empty
}
-/// LoadObject - reads the specified bytecode object file.
-///
-/// Inputs:
-/// FN - The name of the file to load.
-///
-/// Outputs:
-/// OutErrorMessage - The error message to give back to the caller.
-///
-/// Return Value:
-/// A pointer to a module represening the bytecode file is returned.
-/// If an error occurs, the pointer is 0.
+/// LoadObject - Read in and parse the bytecode file named by FN and return the
+/// module it contains (wrapped in an auto_ptr), or 0 and set ErrorMessage if an
+/// error occurs.
///
-std::auto_ptr<Module>
-LoadObject(const std::string & FN, std::string &OutErrorMessage) {
- std::string ErrorMessage;
- Module *Result = ParseBytecodeFile(FN, &ErrorMessage);
+std::auto_ptr<Module> llvm::LoadObject(const std::string &FN,
+ std::string &ErrorMessage) {
+ std::string ParserErrorMessage;
+ Module *Result = ParseBytecodeFile(FN, &ParserErrorMessage);
if (Result) return std::auto_ptr<Module>(Result);
- OutErrorMessage = "Bytecode file '" + FN + "' corrupt!";
- if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage;
+ ErrorMessage = "Bytecode file '" + FN + "' could not be loaded";
+ if (ParserErrorMessage.size()) ErrorMessage += ": " + ParserErrorMessage;
return std::auto_ptr<Module>();
}
const std::set<std::string> &DefSymbols = DefinedSymbols[i];
bool ObjectRequired = false;
- for (std::set<std::string>::iterator I = UndefinedSymbols.begin(),
- E = UndefinedSymbols.end(); I != E; ++I)
- if (DefSymbols.count(*I)) {
- if (Verbose)
- std::cerr << " Found object providing symbol '" << *I << "'...\n";
- ObjectRequired = true;
- break;
- }
-
+
+ //
+ // If the object defines main(), then it is automatically required.
+ // Otherwise, look to see if it defines a symbol that is currently
+ // undefined.
+ //
+ if ((DefSymbols.find ("main")) == DefSymbols.end()) {
+ for (std::set<std::string>::iterator I = UndefinedSymbols.begin(),
+ E = UndefinedSymbols.end(); I != E; ++I)
+ if (DefSymbols.count(*I)) {
+ if (Verbose)
+ std::cerr << " Found object '"
+ << Objects[i]->getModuleIdentifier ()
+ << "' providing symbol '" << *I << "'...\n";
+ ObjectRequired = true;
+ break;
+ }
+ } else {
+ ObjectRequired = true;
+ }
+
// We DO need to link this object into the program...
if (ObjectRequired) {
if (LinkModules(M, Objects[i], &ErrorMessage))
/// FALSE - No errors.
/// TRUE - Some error occurred.
///
-bool LinkFiles(const char *progname,
- Module *HeadModule,
- const std::vector<std::string> &Files,
- bool Verbose)
-{
+bool llvm::LinkFiles(const char *progname, Module *HeadModule,
+ const std::vector<std::string> &Files, bool Verbose) {
// String in which to receive error messages.
std::string ErrorMessage;
/// FALSE - No error.
/// TRUE - Error.
///
-bool LinkLibraries(const char *progname,
- Module *HeadModule,
- const std::vector<std::string> &Libraries,
- const std::vector<std::string> &LibPaths,
- bool Verbose,
- bool Native)
-{
+void llvm::LinkLibraries(const char *progname, Module *HeadModule,
+ const std::vector<std::string> &Libraries,
+ const std::vector<std::string> &LibPaths,
+ bool Verbose, bool Native) {
// String in which to receive error messages.
std::string ErrorMessage;
// we're doing a native link and give an error if we're doing a bytecode
// link.
if (!Native) {
- PrintAndReturn(progname, "Cannot find library -l" + Libraries[i] + "\n");
- return true;
+ std::cerr << progname << ": WARNING: Cannot find library -l"
+ << Libraries[i] << "\n";
+ continue;
}
}
// is not installed as a library. Detect that and link the library.
if (IsArchive(Pathname)) {
if (Verbose)
- std::cerr << "Trying to link archive '" << Pathname << "' (-l" << Libraries[i] << ")\n";
+ std::cerr << "Trying to link archive '" << Pathname << "' (-l"
+ << Libraries[i] << ")\n";
if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) {
- PrintAndReturn(progname, ErrorMessage,
- ": Error linking in archive '" + Pathname + "' (-l" + Libraries[i] + ")");
- return true;
+ std::cerr << progname << ": " << ErrorMessage
+ << ": Error linking in archive '" << Pathname << "' (-l"
+ << Libraries[i] << ")\n";
+ exit(1);
}
} else if (IsBytecode(Pathname)) {
if (Verbose)
- std::cerr << "Trying to link bytecode file '" << Pathname << "' (-l" << Libraries[i] << ")\n";
+ std::cerr << "Trying to link bytecode file '" << Pathname
+ << "' (-l" << Libraries[i] << ")\n";
if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
- PrintAndReturn(progname, ErrorMessage,
- ": error linking in bytecode file '" + Pathname + "' (-l" + Libraries[i] + ")");
- return true;
+ std::cerr << progname << ": " << ErrorMessage
+ << ": error linking in bytecode file '" << Pathname << "' (-l"
+ << Libraries[i] << ")\n";
+ exit(1);
}
}
}
-
- return false;
}