176a0374bSGordon Henriksen //===-- BitWriter.cpp -----------------------------------------------------===// 276a0374bSGordon Henriksen // 376a0374bSGordon Henriksen // The LLVM Compiler Infrastructure 476a0374bSGordon Henriksen // 5*f3ebc3f3SChris Lattner // This file is distributed under the University of Illinois Open Source 6*f3ebc3f3SChris Lattner // License. See LICENSE.TXT for details. 776a0374bSGordon Henriksen // 876a0374bSGordon Henriksen //===----------------------------------------------------------------------===// 976a0374bSGordon Henriksen 1076a0374bSGordon Henriksen #include "llvm-c/BitWriter.h" 1176a0374bSGordon Henriksen #include "llvm/Bitcode/ReaderWriter.h" 1276a0374bSGordon Henriksen #include <fstream> 1376a0374bSGordon Henriksen 1476a0374bSGordon Henriksen using namespace llvm; 1576a0374bSGordon Henriksen 1676a0374bSGordon Henriksen 1776a0374bSGordon Henriksen /*===-- Operations on modules ---------------------------------------------===*/ 1876a0374bSGordon Henriksen 1976a0374bSGordon Henriksen int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) { 204814cd22SGordon Henriksen std::ofstream OS(Path, std::ios_base::out|std::ios::trunc|std::ios::binary); 2176a0374bSGordon Henriksen 2276a0374bSGordon Henriksen if (!OS.fail()) 2376a0374bSGordon Henriksen WriteBitcodeToFile(unwrap(M), OS); 2476a0374bSGordon Henriksen 2576a0374bSGordon Henriksen if (OS.fail()) 2676a0374bSGordon Henriksen return -1; 2776a0374bSGordon Henriksen 2876a0374bSGordon Henriksen return 0; 2976a0374bSGordon Henriksen } 3076a0374bSGordon Henriksen 3176a0374bSGordon Henriksen #ifdef __GNUC__ 3276a0374bSGordon Henriksen #include <ext/stdio_filebuf.h> 3376a0374bSGordon Henriksen 3476a0374bSGordon Henriksen // FIXME: Control this with configure? Provide some portable abstraction in 3576a0374bSGordon Henriksen // libSystem? As is, the user will just get a linker error if they use this on 3676a0374bSGordon Henriksen // non-GCC. Some C++ stdlibs even have ofstream::ofstream(int fd). 3776a0374bSGordon Henriksen int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) { 38bf96d993SAnton Korobeynikov __gnu_cxx::stdio_filebuf<char> Buffer(FileHandle, std::ios_base::out | 39bf96d993SAnton Korobeynikov std::ios::trunc | 40bf96d993SAnton Korobeynikov std::ios::binary); 41bf96d993SAnton Korobeynikov std::ostream OS(&Buffer); 4276a0374bSGordon Henriksen 4376a0374bSGordon Henriksen if (!OS.fail()) 4476a0374bSGordon Henriksen WriteBitcodeToFile(unwrap(M), OS); 4576a0374bSGordon Henriksen 4676a0374bSGordon Henriksen if (OS.fail()) 4776a0374bSGordon Henriksen return -1; 4876a0374bSGordon Henriksen 4976a0374bSGordon Henriksen return 0; 5076a0374bSGordon Henriksen } 5176a0374bSGordon Henriksen 5276a0374bSGordon Henriksen #endif 53