Standard output binary mode on windows

Hello,

On windows, the standard output is not set to binary mode by default so
all '\n' characters are replaced with '\r\n'. This is a pain for any
command using stdout, like "llvm-as < input.ll > out.bc", because out.bc
is then most likely corrupted.

This is an old story, fixed a while ago:
http://llvm.org/bugs/show_bug.cgi?id=787

And here is the thread on LLVMDev:
http://thread.gmane.org/gmane.comp.compilers.llvm.devel/4320/focus=4322

Unfortunately, this bug is back. ChangeStdoutToBinary is never called in
the current LLVM tree. I guess the call was lost during the
Bytecode->Bitcode transition.

The attached patches fix that by calling ChangeStdoutToBinary in
WriteBitcodeToFile if the output stream is llvm::cout. This works for
all the LLVM tools, because they use llvm::cout, but it doesn't work for
llvm-gcc for example, because it is creating a new ostream from stdout,
so I have another patch for the llvm-backend.

Is there a canonical / portable way to check if an ostream is the
standard output ?

Hope this helps,
Julien

llvm-bin-output.diff (792 Bytes)

llvm-gcc-bin-output.diff (1.08 KB)

Hi Julien,

why do you only set binary mode for stdout? Don't you have the same problem
when writing bitcode to file?

Gr.

Matthijs

Hello, Matthijs

why do you only set binary mode for stdout? Don't you have the same problem
when writing bitcode to file?

No, all files are opened in binary mode. This is really ugly thing
(stdout opened in text mode) which really hurt bunch of projects during
porting, since fallout from such thing can be pretty subtle.

Should I open a new PR to track this issue or reopen #787 ? I think the
binary releases of llvm and llvm-gcc for MingW32 on
http://llvm.org/releases/download.html#2.2 have the problem, stdout
cannot be used.

Thanks,
Julien

Hello, Julien

Should I open a new PR to track this issue or reopen #787 ?

Don't worry, I'll test your patches and include into mingw32 binaries.

I think the binary releases of llvm and llvm-gcc for MingW32 on
http://llvm.org/releases/download.html#2.2 have the problem, stdout
cannot be used.

Yes, completely forgot to test this feature :frowning: