1009b1c42SEd Schouten //===-- BitWriter.cpp -----------------------------------------------------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten
9009b1c42SEd Schouten #include "llvm-c/BitWriter.h"
10b915e9e0SDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h"
1159d6cff9SDimitry Andric #include "llvm/IR/Module.h"
125ca98fd9SDimitry Andric #include "llvm/Support/FileSystem.h"
13b915e9e0SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
1459850d08SRoman Divacky #include "llvm/Support/raw_ostream.h"
15009b1c42SEd Schouten using namespace llvm;
16009b1c42SEd Schouten
17009b1c42SEd Schouten
18009b1c42SEd Schouten /*===-- Operations on modules ---------------------------------------------===*/
19009b1c42SEd Schouten
LLVMWriteBitcodeToFile(LLVMModuleRef M,const char * Path)20009b1c42SEd Schouten int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
2167c32a98SDimitry Andric std::error_code EC;
221d5ae102SDimitry Andric raw_fd_ostream OS(Path, EC, sys::fs::OF_None);
23009b1c42SEd Schouten
2467c32a98SDimitry Andric if (EC)
25009b1c42SEd Schouten return -1;
26009b1c42SEd Schouten
27eb11fae6SDimitry Andric WriteBitcodeToFile(*unwrap(M), OS);
28009b1c42SEd Schouten return 0;
29009b1c42SEd Schouten }
30009b1c42SEd Schouten
LLVMWriteBitcodeToFD(LLVMModuleRef M,int FD,int ShouldClose,int Unbuffered)31f5a3459aSRoman Divacky int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,
32f5a3459aSRoman Divacky int Unbuffered) {
33f5a3459aSRoman Divacky raw_fd_ostream OS(FD, ShouldClose, Unbuffered);
34009b1c42SEd Schouten
35eb11fae6SDimitry Andric WriteBitcodeToFile(*unwrap(M), OS);
36009b1c42SEd Schouten return 0;
37009b1c42SEd Schouten }
38009b1c42SEd Schouten
LLVMWriteBitcodeToFileHandle(LLVMModuleRef M,int FileHandle)39009b1c42SEd Schouten int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
40f5a3459aSRoman Divacky return LLVMWriteBitcodeToFD(M, FileHandle, true, false);
41009b1c42SEd Schouten }
4267c32a98SDimitry Andric
LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M)4367c32a98SDimitry Andric LLVMMemoryBufferRef LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M) {
4467c32a98SDimitry Andric std::string Data;
4567c32a98SDimitry Andric raw_string_ostream OS(Data);
4667c32a98SDimitry Andric
47eb11fae6SDimitry Andric WriteBitcodeToFile(*unwrap(M), OS);
4867c32a98SDimitry Andric return wrap(MemoryBuffer::getMemBufferCopy(OS.str()).release());
4967c32a98SDimitry Andric }
50