BitWriter.cpp revision 193323
178344Sobrien//===-- BitWriter.cpp -----------------------------------------------------===//
278344Sobrien//
398184Sgordon//                     The LLVM Compiler Infrastructure
478344Sobrien//
578344Sobrien// This file is distributed under the University of Illinois Open Source
678344Sobrien// License. See LICENSE.TXT for details.
798184Sgordon//
878344Sobrien//===----------------------------------------------------------------------===//
9104985Sschweikh
1078344Sobrien#include "llvm-c/BitWriter.h"
11#include "llvm/Bitcode/ReaderWriter.h"
12#include <fstream>
13
14using namespace llvm;
15
16
17/*===-- Operations on modules ---------------------------------------------===*/
18
19int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
20  std::ofstream OS(Path, std::ios_base::out|std::ios::trunc|std::ios::binary);
21
22  if (!OS.fail())
23    WriteBitcodeToFile(unwrap(M), OS);
24
25  if (OS.fail())
26    return -1;
27
28  return 0;
29}
30
31#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR >= 4)
32#include <ext/stdio_filebuf.h>
33
34// FIXME: Control this with configure? Provide some portable abstraction in
35// libSystem? As is, the user will just get a linker error if they use this on
36// non-GCC. Some C++ stdlibs even have ofstream::ofstream(int fd).
37int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
38  __gnu_cxx::stdio_filebuf<char> Buffer(FileHandle, std::ios_base::out |
39                                                    std::ios::trunc |
40                                                    std::ios::binary);
41  std::ostream OS(&Buffer);
42
43  if (!OS.fail())
44    WriteBitcodeToFile(unwrap(M), OS);
45
46  if (OS.fail())
47    return -1;
48
49  return 0;
50}
51
52#else
53
54int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
55  return -1; // Not supported.
56}
57
58#endif
59