1*0b57cec5SDimitry Andric //===-- BitWriter.cpp -----------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "llvm-c/BitWriter.h"
10*0b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h"
11*0b57cec5SDimitry Andric #include "llvm/IR/Module.h"
12*0b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
13*0b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
14*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
15*0b57cec5SDimitry Andric using namespace llvm;
16*0b57cec5SDimitry Andric 
17*0b57cec5SDimitry Andric 
18*0b57cec5SDimitry Andric /*===-- Operations on modules ---------------------------------------------===*/
19*0b57cec5SDimitry Andric 
LLVMWriteBitcodeToFile(LLVMModuleRef M,const char * Path)20*0b57cec5SDimitry Andric int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
21*0b57cec5SDimitry Andric   std::error_code EC;
22*0b57cec5SDimitry Andric   raw_fd_ostream OS(Path, EC, sys::fs::OF_None);
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric   if (EC)
25*0b57cec5SDimitry Andric     return -1;
26*0b57cec5SDimitry Andric 
27*0b57cec5SDimitry Andric   WriteBitcodeToFile(*unwrap(M), OS);
28*0b57cec5SDimitry Andric   return 0;
29*0b57cec5SDimitry Andric }
30*0b57cec5SDimitry Andric 
LLVMWriteBitcodeToFD(LLVMModuleRef M,int FD,int ShouldClose,int Unbuffered)31*0b57cec5SDimitry Andric int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,
32*0b57cec5SDimitry Andric                          int Unbuffered) {
33*0b57cec5SDimitry Andric   raw_fd_ostream OS(FD, ShouldClose, Unbuffered);
34*0b57cec5SDimitry Andric 
35*0b57cec5SDimitry Andric   WriteBitcodeToFile(*unwrap(M), OS);
36*0b57cec5SDimitry Andric   return 0;
37*0b57cec5SDimitry Andric }
38*0b57cec5SDimitry Andric 
LLVMWriteBitcodeToFileHandle(LLVMModuleRef M,int FileHandle)39*0b57cec5SDimitry Andric int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
40*0b57cec5SDimitry Andric   return LLVMWriteBitcodeToFD(M, FileHandle, true, false);
41*0b57cec5SDimitry Andric }
42*0b57cec5SDimitry Andric 
LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M)43*0b57cec5SDimitry Andric LLVMMemoryBufferRef LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M) {
44*0b57cec5SDimitry Andric   std::string Data;
45*0b57cec5SDimitry Andric   raw_string_ostream OS(Data);
46*0b57cec5SDimitry Andric 
47*0b57cec5SDimitry Andric   WriteBitcodeToFile(*unwrap(M), OS);
48*0b57cec5SDimitry Andric   return wrap(MemoryBuffer::getMemBufferCopy(OS.str()).release());
49*0b57cec5SDimitry Andric }
50