示例#1
0
文件: linker.cpp 项目: roysc/ldc
static int ExecuteToolAndWait(llvm::sys::Path tool, std::vector<std::string> args, bool verbose = false)
{
    // Construct real argument list.
    // First entry is the tool itself, last entry must be NULL.
    std::vector<const char *> realargs;
    realargs.reserve(args.size() + 2);
    realargs.push_back(tool.c_str());
    for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it)
    {
        realargs.push_back((*it).c_str());
    }
    realargs.push_back(NULL);

    // Print command line if requested
    if (verbose)
    {
        // Print it
        for (int i = 0; i < realargs.size()-1; i++)
            printf("%s ", realargs[i]);
        printf("\n");
        fflush(stdout);
    }

    // Execute tool.
    std::string errstr;
    if (int status = llvm::sys::Program::ExecuteAndWait(tool, &realargs[0], NULL, NULL, 0, 0, &errstr))
    {
        error("%s failed with status: %d", tool.c_str(), status);
        if (!errstr.empty())
            error("message: %s", errstr.c_str());
        return status;
    }
    return 0;
}
示例#2
0
bool SerializationTest::Serialize(llvm::sys::Path& Filename,
                                  llvm::sys::Path& FNameDeclPrint,
                                  ASTContext &Ctx) {
    {
        // Pretty-print the decls to a temp file.
        std::string Err;
        llvm::raw_fd_ostream DeclPP(FNameDeclPrint.c_str(), true, Err);
        assert (Err.empty() && "Could not open file for printing out decls.");
        llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));

        TranslationUnitDecl *TUD = Ctx.getTranslationUnitDecl();
        for (DeclContext::decl_iterator I = TUD->decls_begin(Ctx),
                E = TUD->decls_end(Ctx);
                I != E; ++I)
            FilePrinter->HandleTopLevelDecl(DeclGroupRef(*I));
    }

    // Serialize the translation unit.

    // Reserve 256K for bitstream buffer.
    std::vector<unsigned char> Buffer;
    Buffer.reserve(256*1024);

    Ctx.EmitASTBitcodeBuffer(Buffer);

    // Write the bits to disk.
    if (FILE* fp = fopen(Filename.c_str(),"wb")) {
        fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
        fclose(fp);
        return true;
    }

    return false;
}
示例#3
0
int runExecutable()
{
    assert(!gExePath.isEmpty());
    assert(gExePath.isValid());

    // build arguments
    std::vector<const char*> args;
    // args[0] should be the name of the executable
    args.push_back(gExePath.c_str());
    // Skip first argument to -run; it's a D source file.
    for (size_t i = 1, length = opts::runargs.size(); i < length; i++)
    {
        args.push_back(opts::runargs[i].c_str());
    }
    // terminate args list
    args.push_back(NULL);

    // try to call linker!!!
    std::string errstr;
    int status = llvm::sys::Program::ExecuteAndWait(gExePath, &args[0], NULL, NULL, 0,0, &errstr);
    if (status < 0)
    {
        error("program received signal %d (%s)", -status, strsignal(-status));
        return -status;
    }

    if (!errstr.empty())
    {
        error("failed to execute program");
        if (!errstr.empty())
            error("error message: %s", errstr.c_str());
        fatal();
    }
    return status;
}
示例#4
0
文件: ldmd.cpp 项目: John-Colvin/ldc
/**
 * Prints usage information to stdout.
 */
void printUsage(const char* argv0, ls::Path ldcPath)
{
    // Print version information by actually invoking ldc -version.
    const char* args[] = { ldcPath.c_str(), "-version", NULL };
    execute(ldcPath, args);

    printf("\n\
Usage:\n\
  %s files.d ... { -switch }\n\
示例#5
0
文件: ldmd.cpp 项目: John-Colvin/ldc
/**
 * Runs the given executable, returning its error code.
 */
int execute(ls::Path exePath, const char** args)
{
    std::string errorMsg;
    int rc = ls::Program::ExecuteAndWait(exePath, args, NULL, NULL,
                                         0, 0, &errorMsg);
    if (!errorMsg.empty())
    {
        error("Could not execute %s: %s", exePath.c_str(), errorMsg.c_str());
    }
    return rc;
}
示例#6
0
void clang::LoadSerializedDiagnostics(const llvm::sys::Path &DiagnosticsPath,
                                      unsigned num_unsaved_files,
                                      struct CXUnsavedFile *unsaved_files,
                                      FileManager &FileMgr,
                                      SourceManager &SourceMgr,
                                     SmallVectorImpl<StoredDiagnostic> &Diags) {
  using llvm::MemoryBuffer;
  using llvm::StringRef;
  MemoryBuffer *F = MemoryBuffer::getFile(DiagnosticsPath.c_str());
  if (!F)
    return;

  // Enter the unsaved files into the file manager.
  for (unsigned I = 0; I != num_unsaved_files; ++I) {
    const FileEntry *File = FileMgr.getVirtualFile(unsaved_files[I].Filename,
                                                   unsaved_files[I].Length,
                                                   0);
    if (!File) {
      // FIXME: Hard to localize when we have no diagnostics engine!
      Diags.push_back(StoredDiagnostic(Diagnostic::Fatal,
                            (Twine("could not remap from missing file ") +
                                   unsaved_files[I].Filename).str()));
      delete F;
      return;
    }

    MemoryBuffer *Buffer
      = MemoryBuffer::getMemBuffer(unsaved_files[I].Contents,
                           unsaved_files[I].Contents + unsaved_files[I].Length);
    if (!Buffer) {
      delete F;
      return;
    }
    
    SourceMgr.overrideFileContents(File, Buffer);
    SourceMgr.createFileID(File, SourceLocation(), SrcMgr::C_User);
  }

  // Parse the diagnostics, emitting them one by one until we've
  // exhausted the data.
  StringRef Buffer = F->getBuffer();
  const char *Memory = Buffer.data(), *MemoryEnd = Memory + Buffer.size();
  while (Memory != MemoryEnd) {
    StoredDiagnostic Stored = StoredDiagnostic::Deserialize(FileMgr, SourceMgr,
                                                            Memory, MemoryEnd);
    if (!Stored)
      break;

    Diags.push_back(Stored);
  }
  delete F;
}
示例#7
0
bool SerializationTest::Deserialize(llvm::sys::Path& Filename,
                                    llvm::sys::Path& FNameDeclPrint) {

    // Deserialize the translation unit.
    ASTContext *NewCtx;

    {
        // Create the memory buffer that contains the contents of the file.
        llvm::OwningPtr<llvm::MemoryBuffer>
        MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str()));

        if (!MBuffer)
            return false;

        NewCtx = ASTContext::ReadASTBitcodeBuffer(*MBuffer, FMgr);
    }

    if (!NewCtx)
        return false;

    {
        // Pretty-print the deserialized decls to a temp file.
        std::string Err;
        llvm::raw_fd_ostream DeclPP(FNameDeclPrint.c_str(), true, Err);
        assert (Err.empty() && "Could not open file for printing out decls.");
        llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));

        TranslationUnitDecl *TUD = NewCtx->getTranslationUnitDecl();
        for (DeclContext::decl_iterator I = TUD->decls_begin(*NewCtx),
                E = TUD->decls_end(*NewCtx);
                I != E; ++I)
            FilePrinter->HandleTopLevelDecl(DeclGroupRef(*I));
    }

    delete NewCtx;

    return true;
}
示例#8
0
文件: main.cpp 项目: ceninan/clay
static bool generateBinary(llvm::Module *module,
                           const string &outputFile,
                           const llvm::sys::Path &gccPath,
                           unsigned optLevel,
                           bool /*exceptions*/,
                           bool sharedLib,
                           bool genPIC,
                           const vector<string> &arguments)
{
    llvm::sys::Path tempAsm("clayasm");
    string errMsg;
    if (tempAsm.createTemporaryFileOnDisk(false, &errMsg)) {
        cerr << "error: " << errMsg << '\n';
        return false;
    }
    llvm::sys::RemoveFileOnSignal(tempAsm);

    errMsg.clear();
    llvm::raw_fd_ostream asmOut(tempAsm.c_str(),
                                errMsg,
                                llvm::raw_fd_ostream::F_Binary);
    if (!errMsg.empty()) {
        cerr << "error: " << errMsg << '\n';
        return false;
    }

    generateAssembly(module, &asmOut, false, optLevel, sharedLib, genPIC);
    asmOut.close();

    vector<const char *> gccArgs;
    gccArgs.push_back(gccPath.c_str());

    switch (llvmTargetData->getPointerSizeInBits()) {
    case 32 :
        gccArgs.push_back("-m32");
        break;
    case 64 :
        gccArgs.push_back("-m64");
        break;
    default :
        assert(false);
    }

    if (sharedLib)
        gccArgs.push_back("-shared");
    gccArgs.push_back("-o");
    gccArgs.push_back(outputFile.c_str());
    gccArgs.push_back("-x");
    gccArgs.push_back("assembler");
    gccArgs.push_back(tempAsm.c_str());
    for (unsigned i = 0; i < arguments.size(); ++i)
        gccArgs.push_back(arguments[i].c_str());
    gccArgs.push_back(NULL);

    int result = llvm::sys::Program::ExecuteAndWait(gccPath, &gccArgs[0]);

    if (tempAsm.eraseFromDisk(false, &errMsg)) {
        cerr << "error: " << errMsg << '\n';
        return false;
    }

    return (result == 0);
}