blob: 7ed651b77e2ef6117b6942ce076773a437668655 [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"
Chris Lattnerb515d752009-08-23 07:49:08 +000012#include "llvm/Support/raw_ostream.h"
Gordon Henriksen8b94a142007-09-18 03:18:57 +000013using namespace llvm;
14
15
16/*===-- Operations on modules ---------------------------------------------===*/
17
18int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
Chris Lattnerb515d752009-08-23 07:49:08 +000019 std::string ErrorInfo;
20 raw_fd_ostream OS(Path, ErrorInfo,
Dan Gohmanbaa26392009-08-25 15:34:52 +000021 raw_fd_ostream::F_Binary);
Gordon Henriksen8b94a142007-09-18 03:18:57 +000022
Chris Lattnerb515d752009-08-23 07:49:08 +000023 if (!ErrorInfo.empty())
Gordon Henriksen8b94a142007-09-18 03:18:57 +000024 return -1;
25
Chris Lattnerb515d752009-08-23 07:49:08 +000026 WriteBitcodeToFile(unwrap(M), OS);
Gordon Henriksen8b94a142007-09-18 03:18:57 +000027 return 0;
28}
29
Gordon Henriksena068fd32008-06-11 10:46:24 +000030#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR >= 4)
Gordon Henriksen8b94a142007-09-18 03:18:57 +000031#include <ext/stdio_filebuf.h>
32
Gordon Henriksen8b94a142007-09-18 03:18:57 +000033int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
Chris Lattnerb515d752009-08-23 07:49:08 +000034 raw_fd_ostream OS(FileHandle, false);
Gordon Henriksen8b94a142007-09-18 03:18:57 +000035
Chris Lattnerb515d752009-08-23 07:49:08 +000036 WriteBitcodeToFile(unwrap(M), OS);
Gordon Henriksen8b94a142007-09-18 03:18:57 +000037 return 0;
38}
39
Gordon Henriksena068fd32008-06-11 10:46:24 +000040#else
41
42int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
43 return -1; // Not supported.
44}
45
Gordon Henriksen8b94a142007-09-18 03:18:57 +000046#endif