summaryrefslogtreecommitdiff
path: root/lib/Bitcode/Writer/BitWriter.cpp
blob: 7b90586fd7064bc683c7d069a12a20d5db6c62c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//===-- BitWriter.cpp -----------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm-c/BitWriter.h"
#include "llvm/CHelpers.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include <fstream>

using namespace llvm;


/*===-- Operations on modules ---------------------------------------------===*/

int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
  std::ofstream OS(Path);
  
  if (!OS.fail())
    WriteBitcodeToFile(unwrap(M), OS);
  
  if (OS.fail())
    return -1;
  
  return 0;
}

#ifdef __GNUC__
#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) {
  __gnu_cxx::stdio_filebuf<char> Buffer(FileHandle, std::ios_base::out);
  std::ostream OS(&Buffer);
  
  if (!OS.fail())
    WriteBitcodeToFile(unwrap(M), OS);
  
  if (OS.fail())
    return -1;
  
  return 0;
}

#endif