For PR787:
authorReid Spencer <rspencer@reidspencer.com>
Wed, 7 Jun 2006 23:18:34 +0000 (23:18 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Wed, 7 Jun 2006 23:18:34 +0000 (23:18 +0000)
Provide new llvm::sys::Program facilities for converting the stdout and
stdin to binary mode. There is no standard way to do this and the available
mechanisms are platform specific. Adjust the bytecode reader and writer to
use these methods when their input is stdin or output is stdout. THis avoids
the problem with \n writing CRLF to a bytecode file on windows.

Patch Contributed by Michael Smith.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28722 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/System/Program.h
lib/Bytecode/Reader/ReaderWrappers.cpp
lib/Bytecode/Writer/Writer.cpp
lib/System/Unix/Program.inc
lib/System/Win32/Program.inc

index 2f33ea574608e7ec17aaef404c3e0d64f43036f3..0a237bccbcf74d58786cafa9a404c7b1fe3e725c 100644 (file)
@@ -74,6 +74,10 @@ namespace sys {
           ///< this function will wait until the child finishes or forever if
           ///< it doesn't.
       );
+      // These methods change the specified standard stream (stdin or stdout) to
+      // binary mode.
+      static void ChangeStdinToBinary();
+      static void ChangeStdoutToBinary();
   };
 }
 }
index 1ea5c0262f19e0b230acf6ec466856a8a689101a..956d1ede696fa1ce1bb4ac1e0e1138d035fced62 100644 (file)
@@ -19,6 +19,7 @@
 #include "llvm/Instructions.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/System/MappedFile.h"
+#include "llvm/System/Program.h"
 #include <cerrno>
 #include <iostream>
 #include <memory>
@@ -132,6 +133,7 @@ namespace {
 BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
   : BytecodeReader(H)
 {
+  sys::Program::ChangeStdinToBinary();
   char Buffer[4096*4];
 
   // Read in all of the data from stdin, we cannot mmap stdin...
index 83e8a57acfe0176f8f24ee4ba1dea99d539bab2a..4dc80d18090529bb77c6ae97e250572c5e5902bd 100644 (file)
@@ -29,6 +29,7 @@
 #include "llvm/Support/GetElementPtrTypeIterator.h"
 #include "llvm/Support/Compressor.h"
 #include "llvm/Support/MathExtras.h"
+#include "llvm/System/Program.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/Statistic.h"
 #include <cstring>
@@ -1217,6 +1218,11 @@ void llvm::WriteBytecodeToFile(const Module *M, std::ostream &Out,
                                bool compress ) {
   assert(M && "You can't write a null module!!");
 
+  // Make sure that std::cout is put into binary mode for systems
+  // that care.
+  if (&Out == std::cout)
+    sys::Program::ChangeStdoutToBinary();
+
   // Create a vector of unsigned char for the bytecode output. We
   // reserve 256KBytes of space in the vector so that we avoid doing
   // lots of little allocations. 256KBytes is sufficient for a large
index 47810bdbeba72eba13eb0774000600eb385d36c1..01eefe0d52eb641ec957b7ffb6b9408a3aec89af 100644 (file)
@@ -227,4 +227,12 @@ Program::ExecuteAndWait(const Path& path,
     
 }
 
+void Program::ChangeStdinToBinary(){
+  // Do nothing, as Unix doesn't differentiate between text and binary.
+}
+
+void Program::ChangeStdoutToBinary(){
+  // Do nothing, as Unix doesn't differentiate between text and binary.
+}
+
 }
index 95f56b231f148fa239350bd57a8d4f37a4ce111b..c29adf0bd13c5be1ae9fb0f76bf1880c5d59e7b6 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "Win32.h"
+#include <cstdio>
 #include <malloc.h>
 #include <io.h>
+#include <fcntl.h>
 
 //===----------------------------------------------------------------------===//
 //=== WARNING: Implementation here must contain only Win32 specific code 
@@ -218,4 +220,16 @@ Program::ExecuteAndWait(const Path& path,
   return status;
 }
 
+void Program::ChangeStdinToBinary(){
+  int result = _setmode( _fileno(stdin), _O_BINARY );
+  if( result == -1 )
+    throw std::string("Cannot set input mode on stdin to binary.");
+}
+
+void Program::ChangeStdoutToBinary(){
+  int result = _setmode( _fileno(stdout), _O_BINARY );
+  if( result == -1 )
+    throw std::string("Cannot set output mode on stdout to binary.");
+}
+
 }