1 //===-- BitWriter.cpp -----------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm-c/BitWriter.h"
11 #include "llvm/Bitcode/ReaderWriter.h"
12 #include "llvm/Support/CHelpers.h"
13 #include <fstream>
14 
15 using namespace llvm;
16 
17 
18 /*===-- Operations on modules ---------------------------------------------===*/
19 
20 int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
21   std::ofstream OS(Path);
22 
23   if (!OS.fail())
24     WriteBitcodeToFile(unwrap(M), OS);
25 
26   if (OS.fail())
27     return -1;
28 
29   return 0;
30 }
31 
32 #ifdef __GNUC__
33 #include <ext/stdio_filebuf.h>
34 
35 // FIXME: Control this with configure? Provide some portable abstraction in
36 // libSystem? As is, the user will just get a linker error if they use this on
37 // non-GCC. Some C++ stdlibs even have ofstream::ofstream(int fd).
38 int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
39   __gnu_cxx::stdio_filebuf<char> Buffer(FileHandle, std::ios_base::out);
40   std::ostream OS(&Buffer);
41 
42   if (!OS.fail())
43     WriteBitcodeToFile(unwrap(M), OS);
44 
45   if (OS.fail())
46     return -1;
47 
48   return 0;
49 }
50 
51 #endif
52