For PR787:

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
This commit is contained in:
Reid Spencer 2006-06-07 23:18:34 +00:00
parent 2991b01b18
commit 32f5553c03
5 changed files with 34 additions and 0 deletions

View File

@ -74,6 +74,10 @@ namespace sys {
///< this function will wait until the child finishes or forever if ///< this function will wait until the child finishes or forever if
///< it doesn't. ///< it doesn't.
); );
// These methods change the specified standard stream (stdin or stdout) to
// binary mode.
static void ChangeStdinToBinary();
static void ChangeStdoutToBinary();
}; };
} }
} }

View File

@ -19,6 +19,7 @@
#include "llvm/Instructions.h" #include "llvm/Instructions.h"
#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringExtras.h"
#include "llvm/System/MappedFile.h" #include "llvm/System/MappedFile.h"
#include "llvm/System/Program.h"
#include <cerrno> #include <cerrno>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
@ -132,6 +133,7 @@ namespace {
BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H ) BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
: BytecodeReader(H) : BytecodeReader(H)
{ {
sys::Program::ChangeStdinToBinary();
char Buffer[4096*4]; char Buffer[4096*4];
// Read in all of the data from stdin, we cannot mmap stdin... // Read in all of the data from stdin, we cannot mmap stdin...

View File

@ -29,6 +29,7 @@
#include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/Compressor.h" #include "llvm/Support/Compressor.h"
#include "llvm/Support/MathExtras.h" #include "llvm/Support/MathExtras.h"
#include "llvm/System/Program.h"
#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Statistic.h" #include "llvm/ADT/Statistic.h"
#include <cstring> #include <cstring>
@ -1217,6 +1218,11 @@ void llvm::WriteBytecodeToFile(const Module *M, std::ostream &Out,
bool compress ) { bool compress ) {
assert(M && "You can't write a null module!!"); 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 // Create a vector of unsigned char for the bytecode output. We
// reserve 256KBytes of space in the vector so that we avoid doing // reserve 256KBytes of space in the vector so that we avoid doing
// lots of little allocations. 256KBytes is sufficient for a large // lots of little allocations. 256KBytes is sufficient for a large

View 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.
}
} }

View File

@ -12,8 +12,10 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "Win32.h" #include "Win32.h"
#include <cstdio>
#include <malloc.h> #include <malloc.h>
#include <io.h> #include <io.h>
#include <fcntl.h>
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
//=== WARNING: Implementation here must contain only Win32 specific code //=== WARNING: Implementation here must contain only Win32 specific code
@ -218,4 +220,16 @@ Program::ExecuteAndWait(const Path& path,
return status; 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.");
}
} }