blob: 8f562e98d9f1ea86b3407481ba589f12e905ce8c [file] [log] [blame]
Gordon Henriksen8b94a142007-09-18 03:18:57 +00001//===-- 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"
Gordon Henriksen8b94a142007-09-18 03:18:57 +000011#include "llvm/Bitcode/ReaderWriter.h"
Gordon Henriksen8b94a142007-09-18 03:18:57 +000012#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);
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#ifdef __GNUC__
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::ostream OS(&Buffer);
40
41 if (!OS.fail())
42 WriteBitcodeToFile(unwrap(M), OS);
43
44 if (OS.fail())
45 return -1;
46
47 return 0;
48}
49
50#endif