Gordon Henriksen | 8b94a14 | 2007-09-18 03:18:57 +0000 | [diff] [blame] | 1 | //===-- BitWriter.cpp -----------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Gordon Henriksen | 8b94a14 | 2007-09-18 03:18:57 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm-c/BitWriter.h" |
Gordon Henriksen | 8b94a14 | 2007-09-18 03:18:57 +0000 | [diff] [blame] | 11 | #include "llvm/Bitcode/ReaderWriter.h" |
Gordon Henriksen | 8b94a14 | 2007-09-18 03:18:57 +0000 | [diff] [blame] | 12 | #include <fstream> |
| 13 | |
| 14 | using namespace llvm; |
| 15 | |
| 16 | |
| 17 | /*===-- Operations on modules ---------------------------------------------===*/ |
| 18 | |
| 19 | int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) { |
Gordon Henriksen | 94007ec | 2007-12-03 14:50:37 +0000 | [diff] [blame] | 20 | std::ofstream OS(Path, std::ios_base::out|std::ios::trunc|std::ios::binary); |
Gordon Henriksen | 8b94a14 | 2007-09-18 03:18:57 +0000 | [diff] [blame] | 21 | |
| 22 | if (!OS.fail()) |
| 23 | WriteBitcodeToFile(unwrap(M), OS); |
| 24 | |
| 25 | if (OS.fail()) |
| 26 | return -1; |
| 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | |
Gordon Henriksen | a068fd3 | 2008-06-11 10:46:24 +0000 | [diff] [blame] | 31 | #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR >= 4) |
Gordon Henriksen | 8b94a14 | 2007-09-18 03:18:57 +0000 | [diff] [blame] | 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). |
| 37 | int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) { |
Anton Korobeynikov | 3194012 | 2007-12-03 14:35:57 +0000 | [diff] [blame] | 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); |
Gordon Henriksen | 8b94a14 | 2007-09-18 03:18:57 +0000 | [diff] [blame] | 42 | |
| 43 | if (!OS.fail()) |
| 44 | WriteBitcodeToFile(unwrap(M), OS); |
| 45 | |
| 46 | if (OS.fail()) |
| 47 | return -1; |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
Gordon Henriksen | a068fd3 | 2008-06-11 10:46:24 +0000 | [diff] [blame] | 52 | #else |
| 53 | |
| 54 | int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) { |
| 55 | return -1; // Not supported. |
| 56 | } |
| 57 | |
Gordon Henriksen | 8b94a14 | 2007-09-18 03:18:57 +0000 | [diff] [blame] | 58 | #endif |