2007-09-18 03:18:57 +00:00
|
|
|
//===-- BitWriter.cpp -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-09-18 03:18:57 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c/BitWriter.h"
|
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
|
|
|
|
/*===-- Operations on modules ---------------------------------------------===*/
|
|
|
|
|
|
|
|
int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
|
2007-12-03 14:50:37 +00:00
|
|
|
std::ofstream OS(Path, std::ios_base::out|std::ios::trunc|std::ios::binary);
|
2007-09-18 03:18:57 +00:00
|
|
|
|
|
|
|
if (!OS.fail())
|
|
|
|
WriteBitcodeToFile(unwrap(M), OS);
|
|
|
|
|
|
|
|
if (OS.fail())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-11 10:46:24 +00:00
|
|
|
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR >= 4)
|
2007-09-18 03:18:57 +00:00
|
|
|
#include <ext/stdio_filebuf.h>
|
|
|
|
|
|
|
|
// FIXME: Control this with configure? Provide some portable abstraction in
|
|
|
|
// libSystem? As is, the user will just get a linker error if they use this on
|
|
|
|
// non-GCC. Some C++ stdlibs even have ofstream::ofstream(int fd).
|
|
|
|
int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
|
2007-12-03 14:35:57 +00:00
|
|
|
__gnu_cxx::stdio_filebuf<char> Buffer(FileHandle, std::ios_base::out |
|
|
|
|
std::ios::trunc |
|
|
|
|
std::ios::binary);
|
|
|
|
std::ostream OS(&Buffer);
|
2007-09-18 03:18:57 +00:00
|
|
|
|
|
|
|
if (!OS.fail())
|
|
|
|
WriteBitcodeToFile(unwrap(M), OS);
|
|
|
|
|
|
|
|
if (OS.fail())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-11 10:46:24 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
|
|
|
|
return -1; // Not supported.
|
|
|
|
}
|
|
|
|
|
2007-09-18 03:18:57 +00:00
|
|
|
#endif
|