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