blob: 3747122fc98f5e706bf1549597c9f6f3372c4985 [file] [log] [blame]
Gordon Henriksen8b94a142007-09-18 03:18:57 +00001//===-- BitWriter.cpp -----------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Gordon Henriksen8b94a142007-09-18 03:18:57 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "llvm-c/BitWriter.h"
Gordon Henriksen8b94a142007-09-18 03:18:57 +000011#include "llvm/Bitcode/ReaderWriter.h"
Filip Pizlo40be1e82013-05-01 20:59:00 +000012#include "llvm/IR/Module.h"
Stephen Hinesdce4a402014-05-29 02:49:00 -070013#include "llvm/Support/FileSystem.h"
Chris Lattnerb515d752009-08-23 07:49:08 +000014#include "llvm/Support/raw_ostream.h"
Gordon Henriksen8b94a142007-09-18 03:18:57 +000015using namespace llvm;
16
17
18/*===-- Operations on modules ---------------------------------------------===*/
19
20int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
Chris Lattnerb515d752009-08-23 07:49:08 +000021 std::string ErrorInfo;
Stephen Hines36b56882014-04-23 16:57:46 -070022 raw_fd_ostream OS(Path, ErrorInfo, sys::fs::F_None);
Joe Abbey170a15e2012-11-25 15:23:39 +000023
Chris Lattnerb515d752009-08-23 07:49:08 +000024 if (!ErrorInfo.empty())
Gordon Henriksen8b94a142007-09-18 03:18:57 +000025 return -1;
Joe Abbey170a15e2012-11-25 15:23:39 +000026
Chris Lattnerb515d752009-08-23 07:49:08 +000027 WriteBitcodeToFile(unwrap(M), OS);
Gordon Henriksen8b94a142007-09-18 03:18:57 +000028 return 0;
29}
30
Erick Tryzelaare6230502010-03-06 00:30:06 +000031int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,
32 int Unbuffered) {
33 raw_fd_ostream OS(FD, ShouldClose, Unbuffered);
Joe Abbey170a15e2012-11-25 15:23:39 +000034
Chris Lattnerb515d752009-08-23 07:49:08 +000035 WriteBitcodeToFile(unwrap(M), OS);
Gordon Henriksen8b94a142007-09-18 03:18:57 +000036 return 0;
37}
Erick Tryzelaare6230502010-03-06 00:30:06 +000038
39int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
40 return LLVMWriteBitcodeToFD(M, FileHandle, true, false);
41}