1 //===-- BitWriter.cpp -----------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm-c/BitWriter.h"
11 #include "llvm/Bitcode/BitcodeWriter.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/MemoryBuffer.h"
15 #include "llvm/Support/raw_ostream.h"
16 using namespace llvm;
17
18
19 /*===-- Operations on modules ---------------------------------------------===*/
20
LLVMWriteBitcodeToFile(LLVMModuleRef M,const char * Path)21 int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
22 std::error_code EC;
23 raw_fd_ostream OS(Path, EC, sys::fs::F_None);
24
25 if (EC)
26 return -1;
27
28 WriteBitcodeToFile(*unwrap(M), OS);
29 return 0;
30 }
31
LLVMWriteBitcodeToFD(LLVMModuleRef M,int FD,int ShouldClose,int Unbuffered)32 int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,
33 int Unbuffered) {
34 raw_fd_ostream OS(FD, ShouldClose, Unbuffered);
35
36 WriteBitcodeToFile(*unwrap(M), OS);
37 return 0;
38 }
39
LLVMWriteBitcodeToFileHandle(LLVMModuleRef M,int FileHandle)40 int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
41 return LLVMWriteBitcodeToFD(M, FileHandle, true, false);
42 }
43
LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M)44 LLVMMemoryBufferRef LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M) {
45 std::string Data;
46 raw_string_ostream OS(Data);
47
48 WriteBitcodeToFile(*unwrap(M), OS);
49 return wrap(MemoryBuffer::getMemBufferCopy(OS.str()).release());
50 }
51