Index: /trunk/src/kmk/output.c
===================================================================
--- /trunk/src/kmk/output.c	(revision 3189)
+++ /trunk/src/kmk/output.c	(revision 3190)
@@ -71,4 +71,8 @@
 static void  release_semaphore (void *);
 static int   log_working_directory (int);
+
+/* Is make's stdout going to the same place as stderr?
+   Also, did we already sync_init (== -1)?  */
+static int combined_output = -1;
 
 /* Internal worker for output_dump and membuf_dump_most. */
@@ -319,17 +323,25 @@
 }
 
-/* write/fwrite like function. */
+
+
+/* write/fwrite like function, binary mode. */
 void
-output_write (struct output *out, int is_err, const char *src, size_t len)
+output_write_bin (struct output *out, int is_err, const char *src, size_t len)
 {
   if (!out || !out->syncout)
     {
       FILE *f = is_err ? stderr : stdout;
-#ifdef KBUILD_OS_WINDOWS
+# ifdef KBUILD_OS_WINDOWS
+      /* On windows we need to disable \n -> \r\n convers that is common on
+         standard output/error.  Also optimize for console output. */
+      int fd = fileno (f);
+      int prev_mode = _setmode (fd, _O_BINARY);
       maybe_con_fwrite (src, len, 1, f);
-#else
+      fflush (f);
+      _setmode (fd, prev_mode);
+# else
       fwrite (src, len, 1, f);
-#endif
       fflush (f);
+# endif
     }
   else
@@ -354,4 +366,40 @@
 }
 
+/* write/fwrite like function, text mode. */
+void
+output_write_text (struct output *out, int is_err, const char *src, size_t len)
+{
+# if defined (KBUILD_OS_WINDOWS) || defined (KBUILD_OS_OS2) || defined (KBUILD_OS_DOS)
+  if (out && out->syncout)
+    {
+      /* ASSUME fwrite does the desired conversion. */
+      FILE *f = is_err ? stderr : stdout;
+# ifdef KBUILD_OS_WINDOWS
+      maybe_con_fwrite (src, len, 1, f);
+# else
+      fwrite (src, len, 1, f);
+# endif
+      fflush (f);
+    }
+  else
+    {
+      /* Work the buffer line by line, replacing each \n with \r\n. */
+      while (len > 0)
+        {
+          const char *nl = memchr ( src, '\n', len);
+          size_t line_len = nl ? nl - src : len;
+          output_write_bin (out, is_err, src, line_len);
+          if (!nl)
+              break;
+          output_write_bin (out, is_err, "\r\n", 2);
+          len -= line_len + 1;
+          src += line_len + 1;
+        }
+    }
+# else
+  output_write_bin (out, is_err, src, len);
+# endif
+}
+
 #endif /* CONFIG_WITH_OUTPUT_IN_MEMORY */
 
@@ -362,5 +410,5 @@
 {
 #ifdef CONFIG_WITH_OUTPUT_IN_MEMORY
-  output_write (out, is_err, msg, strlen (msg));
+  output_write_text (out, is_err, msg, strlen (msg));
 #else  /* !CONFIG_WITH_OUTPUT_IN_MEMORY */
   if (! out || ! out->syncout)
@@ -913,5 +961,10 @@
 output_start (void)
 {
-#ifndef CONFIG_WITH_OUTPUT_IN_MEMORY
+#ifdef CONFIG_WITH_OUTPUT_IN_MEMORY
+  /* If we're syncing output make sure the sempahore (win) is set up. */
+  if (output_context && output_context->syncout)
+    if (combined_output < 0)
+      combined_output = sync_init ();
+#else
 #ifndef NO_OUTPUT_SYNC
   /* If we're syncing output make sure the temporary file is set up.  */
Index: /trunk/src/kmk/output.h
===================================================================
--- /trunk/src/kmk/output.h	(revision 3189)
+++ /trunk/src/kmk/output.h	(revision 3190)
@@ -80,4 +80,8 @@
 /* Show a message on stdout or stderr.  Will start the output if needed.  */
 void outputs (int is_err, const char *msg);
+#ifdef CONFIG_WITH_OUTPUT_IN_MEMORY
+void output_write_bin (struct output *out, int is_err, const char *src, size_t len);
+void output_write_text (struct output *out, int is_err, const char *src, size_t len);
+#endif
 
 #ifndef NO_OUTPUT_SYNC
Index: /trunk/src/kmk/w32/winchildren.c
===================================================================
--- /trunk/src/kmk/w32/winchildren.c	(revision 3189)
+++ /trunk/src/kmk/w32/winchildren.c	(revision 3190)
@@ -637,12 +637,21 @@
 static void mkWinChildcareWorkerFlushUnwritten(PWINCHILD pChild, PWINCCWPIPE pPipe)
 {
-    /** @todo integrate with output.c   */
     DWORD cbUnwritten = pPipe->cbWritten - pPipe->offPendingRead;
     if (cbUnwritten)
     {
-        DWORD cbWritten = 0;
-        if (WriteFile(GetStdHandle(pPipe->iWhich == 1 ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
-                      &pPipe->pbBuffer[pPipe->cbWritten], cbUnwritten, &cbWritten, NULL))
-            pPipe->cbWritten += cbWritten <= cbUnwritten ? cbWritten : cbUnwritten; /* paranoia */
+#ifdef CONFIG_WITH_OUTPUT_IN_MEMORY
+        if (pChild->pMkChild)
+        {
+            output_write_bin(&pChild->pMkChild->output, pPipe->iWhich == 2, &pPipe->pbBuffer[pPipe->cbWritten], cbUnwritten);
+            pPipe->cbWritten += cbUnwritten;
+        }
+        else
+#endif
+        {
+            DWORD cbWritten = 0;
+            if (WriteFile(GetStdHandle(pPipe->iWhich == 1 ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
+                          &pPipe->pbBuffer[pPipe->cbWritten], cbUnwritten, &cbWritten, NULL))
+                pPipe->cbWritten += cbWritten <= cbUnwritten ? cbWritten : cbUnwritten; /* paranoia */
+        }
     }
 }
@@ -693,13 +702,23 @@
         if (offRest > offStart)
         {
-            /** @todo integrate with output.c   */
             /* Write out offStart..offRest. */
             DWORD cbToWrite = offRest - offStart;
-            DWORD cbWritten = 0;
-            if (WriteFile(GetStdHandle(pPipe->iWhich == 1 ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
-                          &pPipe->pbBuffer[offStart], cbToWrite, &cbWritten, NULL))
-            {
-                offStart += cbWritten <= cbToWrite ? cbWritten : cbToWrite; /* paranoia */
+#ifdef CONFIG_WITH_OUTPUT_IN_MEMORY
+            if (pChild->pMkChild)
+            {
+                output_write_bin(&pChild->pMkChild->output, pPipe->iWhich == 2, &pPipe->pbBuffer[offStart], cbToWrite);
+                offStart += cbToWrite;
                 pPipe->cbWritten = offStart;
+            }
+            else
+#endif
+            {
+                DWORD cbWritten = 0;
+                if (WriteFile(GetStdHandle(pPipe->iWhich == 1 ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
+                              &pPipe->pbBuffer[offStart], cbToWrite, &cbWritten, NULL))
+                {
+                    offStart += cbWritten <= cbToWrite ? cbWritten : cbToWrite; /* paranoia */
+                    pPipe->cbWritten = offStart;
+                }
             }
         }
