Move Internal.Console to shared CoreLib & port to Unix (#42983)
authorMichal Strehovský <MichalStrehovsky@users.noreply.github.com>
Sun, 11 Oct 2020 16:01:54 +0000 (18:01 +0200)
committerGitHub <noreply@github.com>
Sun, 11 Oct 2020 16:01:54 +0000 (09:01 -0700)
18 files changed:
src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj
src/coreclr/src/System.Private.CoreLib/src/Internal/Console.cs [deleted file]
src/coreclr/src/vm/ecalllist.h
src/libraries/Common/src/Interop/Unix/System.Native/Interop.Log.cs [moved from src/libraries/Common/src/Interop/OSX/System.Native/Interop.Log.cs with 100% similarity]
src/libraries/Common/src/Interop/Unix/System.Native/Interop.PrintF.cs [deleted file]
src/libraries/Native/Unix/System.Native/CMakeLists.txt
src/libraries/Native/Unix/System.Native/pal_log.c [new file with mode: 0644]
src/libraries/Native/Unix/System.Native/pal_log.h
src/libraries/Native/Unix/System.Native/pal_log.m
src/libraries/Native/Unix/System.Native/pal_signal.c
src/libraries/Native/Unix/System.Native/pal_signal.h
src/libraries/System.Console/src/System.Console.csproj
src/libraries/System.Private.CoreLib/src/Internal/Console.Unix.cs [new file with mode: 0644]
src/libraries/System.Private.CoreLib/src/Internal/Console.Windows.cs [new file with mode: 0644]
src/libraries/System.Private.CoreLib/src/Internal/Console.cs [new file with mode: 0644]
src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems
src/mono/netcore/System.Private.CoreLib/System.Private.CoreLib.csproj
src/mono/netcore/System.Private.CoreLib/src/Mono/Console.Mono.cs [deleted file]

index 4d4c53f..2075234 100644 (file)
 
   <!-- Sources -->
   <ItemGroup>
-    <Compile Include="$(BclSourcesRoot)\Internal\Console.cs" />
     <Compile Include="$(BclSourcesRoot)\Internal\Runtime\InteropServices\ComponentActivator.cs" />
     <Compile Include="$(BclSourcesRoot)\Internal\Runtime\InteropServices\IsolatedComponentLoadContext.cs" />
     <Compile Include="$(BclSourcesRoot)\System\__Canon.cs" />
     <Compile Include="$(BclSourcesRoot)\System\ValueType.cs" />
     <Compile Include="$(BclSourcesRoot)\System\WeakReference.CoreCLR.cs" />
     <Compile Include="$(BclSourcesRoot)\System\WeakReference.T.CoreCLR.cs" />
-    <Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetStdHandle.cs">
-      <Link>Common\Interop\Windows\Kernel32\Interop.GetStdHandle.cs</Link>
-    </Compile>
-    <Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.HandleTypes.cs">
-      <Link>Common\Interop\Windows\Kernel32\Interop.HandleTypes.cs</Link>
-    </Compile>
   </ItemGroup>
   <!-- These classes are only used for FeatureCominterop, but they are referenced by tests
        in order to allow tests to be built on all platforms, these classes are included for all
diff --git a/src/coreclr/src/System.Private.CoreLib/src/Internal/Console.cs b/src/coreclr/src/System.Private.CoreLib/src/Internal/Console.cs
deleted file mode 100644 (file)
index 307fd91..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System;
-using System.Text;
-using Microsoft.Win32.SafeHandles;
-
-namespace Internal
-{
-    //
-    // Simple limited console class for internal printf-style debugging in System.Private.CoreLib
-    // and low-level tests that want to call System.Private.CoreLib directly
-    //
-
-    public static class Console
-    {
-        private static readonly SafeFileHandle _outputHandle =
-            new SafeFileHandle(Interop.Kernel32.GetStdHandle(Interop.Kernel32.HandleTypes.STD_OUTPUT_HANDLE), ownsHandle: false);
-
-        public static unsafe void Write(string s)
-        {
-            byte[] bytes = Encoding.UTF8.GetBytes(s);
-            fixed (byte* pBytes = bytes)
-            {
-                Interop.Kernel32.WriteFile(_outputHandle, pBytes, bytes.Length, out _, IntPtr.Zero);
-            }
-        }
-
-        public static void WriteLine(string? s) =>
-            Write(s + Environment.NewLineConst);
-
-        public static void WriteLine() =>
-            Write(Environment.NewLineConst);
-    }
-}
index ca9ef6a..c2bcec4 100644 (file)
@@ -1052,7 +1052,6 @@ FCFuncStart(gPalKernel32Funcs)
     QCFuncElement("FreeEnvironmentStrings", FreeEnvironmentStringsW)
     QCFuncElement("GetEnvironmentStrings", GetEnvironmentStringsW)
     QCFuncElement("GetEnvironmentVariable", GetEnvironmentVariableW)
-    QCFuncElement("GetStdHandle", GetStdHandle)
     QCFuncElement("OpenEvent", OpenEventW)
     QCFuncElement("OpenMutex", OpenMutexW)
     QCFuncElement("OpenSemaphore", OpenSemaphoreW)
@@ -1062,7 +1061,6 @@ FCFuncStart(gPalKernel32Funcs)
     QCFuncElement("ResetEvent", ResetEvent)
     QCFuncElement("SetEnvironmentVariable", SetEnvironmentVariableW)
     QCFuncElement("SetEvent", SetEvent)
-    QCFuncElement("WriteFile", WriteFile)
 FCFuncEnd()
 #endif
 
diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.PrintF.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.PrintF.cs
deleted file mode 100644 (file)
index 22198d4..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System;
-using System.Runtime.InteropServices;
-
-internal static partial class Interop
-{
-    internal static partial class Sys
-    {
-        // printf takes a variable number of arguments, which is difficult to represent in C#.
-        // Instead, since we only have a small and fixed number of call sites, we declare
-        // an overload for each of the specific argument sets we need.
-
-        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_PrintF", SetLastError = true)]
-        internal static extern unsafe int PrintF(string format, string arg1);
-    }
-}
index f1ae8e8..c7a34a8 100644 (file)
@@ -1,5 +1,9 @@
 project(System.Native C)
 
+if (NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS)
+    add_definitions(-DHAS_CONSOLE_SIGNALS)
+endif ()
+
 set(NATIVE_SOURCES
     pal_errno.c
     pal_interfaceaddresses.c
@@ -28,7 +32,9 @@ if (CLR_CMAKE_TARGET_IOS OR CLR_CMAKE_TARGET_TVOS)
         pal_log.m
         pal_searchpath.m)
 else ()
-    set(NATIVE_SOURCES ${NATIVE_SOURCES} pal_console.c)
+    set(NATIVE_SOURCES ${NATIVE_SOURCES}
+        pal_console.c
+        pal_log.c)
 endif ()
 
 if (CLR_CMAKE_TARGET_LINUX AND NOT CLR_CMAKE_TARGET_BROWSER)
diff --git a/src/libraries/Native/Unix/System.Native/pal_log.c b/src/libraries/Native/Unix/System.Native/pal_log.c
new file mode 100644 (file)
index 0000000..086ebe5
--- /dev/null
@@ -0,0 +1,13 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+#include "pal_config.h"
+#include "pal_log.h"
+
+#include <stdio.h>
+
+void SystemNative_Log(uint8_t* buffer, int32_t length)
+{
+    fwrite(buffer, 1, (size_t)length, stdout);
+    fflush(stdout);
+}
index dc232ad..1e950cd 100644 (file)
@@ -7,9 +7,3 @@
 #include "pal_types.h"
 
 PALEXPORT void SystemNative_Log(uint8_t* buffer, int32_t length);
-
-PALEXPORT int32_t SystemNative_InitializeTerminalAndSignalHandling(void);
-
-// Called by pal_signal.cpp to reinitialize the console on SIGCONT/SIGCHLD.
-void ReinitializeTerminal(void) {}
-void UninitializeTerminal(void) {}
index 0230daa..8590086 100644 (file)
@@ -39,8 +39,3 @@ void SystemNative_Log (uint8_t* buffer, int32_t length)
     }
     [msg release];
 }
-
-int32_t SystemNative_InitializeTerminalAndSignalHandling(void)
-{
-    return 0;
-}
index 1d29e7c..dfe30ad 100644 (file)
@@ -154,7 +154,9 @@ static void* SignalHandlerLoop(void* arg)
         }
         else if (signalCode == SIGCONT)
         {
+#ifdef HAS_CONSOLE_SIGNALS
             ReinitializeTerminal();
+#endif
         }
         else if (signalCode != SIGWINCH)
         {
@@ -189,7 +191,9 @@ void SystemNative_UnregisterForCtrl()
 void SystemNative_RestoreAndHandleCtrl(CtrlCode ctrlCode)
 {
     int signalCode = ctrlCode == Break ? SIGQUIT : SIGINT;
+#ifdef HAS_CONSOLE_SIGNALS
     UninitializeTerminal();
+#endif
     sigaction(signalCode, OrigActionFor(signalCode), NULL);
     kill(getpid(), signalCode);
 }
@@ -315,3 +319,12 @@ int32_t InitializeSignalHandlingCore()
 
     return 1;
 }
+
+#ifndef HAS_CONSOLE_SIGNALS
+
+int32_t SystemNative_InitializeTerminalAndSignalHandling()
+{
+    return 0;
+}
+
+#endif
index 1cc072f..6b0882c 100644 (file)
@@ -59,3 +59,14 @@ typedef void (*TerminalInvalidationCallback)(void);
   *
  */
 PALEXPORT void SystemNative_SetTerminalInvalidationHandler(TerminalInvalidationCallback callback);
+
+#ifndef HAS_CONSOLE_SIGNALS
+
+/**
+ * Initializes signal handling and terminal for use by System.Console and System.Diagnostics.Process.
+ *
+ * Returns 1 on success; otherwise returns 0 and sets errno.
+ */
+PALEXPORT int32_t SystemNative_InitializeTerminalAndSignalHandling(void);
+
+#endif
index fbe4c66..1098aa9 100644 (file)
@@ -21,8 +21,8 @@
   <!-- iOS/tvOS -->
   <ItemGroup Condition="'$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true'">
     <Compile Include="System\ConsolePal.iOS.cs" />
-    <Compile Include="$(CommonPath)Interop\OSX\System.Native\Interop.Log.cs"
-             Link="Common\Interop\OSX\Interop.Log.cs" />
+    <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Log.cs"
+             Link="Common\Interop\Unix\Interop.Log.cs" />
     <Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
              Link="Common\Interop\Unix\Interop.Libraries.cs" />
   </ItemGroup>
diff --git a/src/libraries/System.Private.CoreLib/src/Internal/Console.Unix.cs b/src/libraries/System.Private.CoreLib/src/Internal/Console.Unix.cs
new file mode 100644 (file)
index 0000000..3dcf49a
--- /dev/null
@@ -0,0 +1,20 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System;
+using System.Text;
+
+namespace Internal
+{
+    public static partial class Console
+    {
+        public static unsafe void Write(string s)
+        {
+            byte[] bytes = Encoding.UTF8.GetBytes(s);
+            fixed (byte* pBytes = bytes)
+            {
+                Interop.Sys.Log(pBytes, bytes.Length);
+            }
+        }
+    }
+}
diff --git a/src/libraries/System.Private.CoreLib/src/Internal/Console.Windows.cs b/src/libraries/System.Private.CoreLib/src/Internal/Console.Windows.cs
new file mode 100644 (file)
index 0000000..e9027c8
--- /dev/null
@@ -0,0 +1,23 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System;
+using System.Text;
+
+namespace Internal
+{
+    public static partial class Console
+    {
+        private static readonly IntPtr s_outputHandle =
+            Interop.Kernel32.GetStdHandle(Interop.Kernel32.HandleTypes.STD_OUTPUT_HANDLE);
+
+        public static unsafe void Write(string s)
+        {
+            byte[] bytes = Encoding.UTF8.GetBytes(s);
+            fixed (byte* pBytes = bytes)
+            {
+                Interop.Kernel32.WriteFile(s_outputHandle, pBytes, bytes.Length, out _, IntPtr.Zero);
+            }
+        }
+    }
+}
diff --git a/src/libraries/System.Private.CoreLib/src/Internal/Console.cs b/src/libraries/System.Private.CoreLib/src/Internal/Console.cs
new file mode 100644 (file)
index 0000000..5034a2b
--- /dev/null
@@ -0,0 +1,21 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System;
+
+namespace Internal
+{
+    //
+    // Simple limited console class for internal printf-style debugging in System.Private.CoreLib
+    // and low-level tests that want to call System.Private.CoreLib directly
+    //
+
+    public static partial class Console
+    {
+        public static void WriteLine(string? s) =>
+            Write(s + Environment.NewLineConst);
+
+        public static void WriteLine() =>
+            Write(Environment.NewLineConst);
+    }
+}
index 93c774f..ad02631 100644 (file)
@@ -37,6 +37,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="$(MSBuildThisFileDirectory)Internal\AssemblyAttributes.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Internal\Console.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Internal\IO\File.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Internal\Padding.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Internal\Runtime\CompilerServices\Unsafe.cs" />
     <Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetProcessTimes_IntPtr.cs">
       <Link>Common\Interop\Windows\Kernel32\Interop.GetProcessTimes_IntPtr.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetStdHandle.cs">
+      <Link>Common\Interop\Windows\Kernel32\Interop.GetStdHandle.cs</Link>
+    </Compile>
     <Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetSystemDirectoryW.cs">
       <Link>Common\Interop\Windows\Kernel32\Interop.GetSystemDirectoryW.cs</Link>
     </Compile>
     <Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GlobalMemoryStatusEx.cs">
       <Link>Common\Interop\Windows\Kernel32\Interop.GlobalMemoryStatusEx.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.HandleTypes.cs">
+      <Link>Common\Interop\Windows\Kernel32\Interop.HandleTypes.cs</Link>
+    </Compile>
     <Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.IsWow64Process_IntPtr.cs">
       <Link>Common\Interop\Windows\Kernel32\Interop.IsWow64Process_IntPtr.cs</Link>
     </Compile>
     <Compile Include="$(CommonPath)Interop\Windows\User32\Interop.USEROBJECTFLAGS.cs">
       <Link>Common\Interop\Windows\User32\Interop.USEROBJECTFLAGS.cs</Link>
     </Compile>
+    <Compile Include="$(MSBuildThisFileDirectory)Internal\Console.Windows.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Internal\IO\File.Windows.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Internal\Win32\RegistryKey.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeFileHandle.Windows.cs" />
     <Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.WriteFile_SafeHandle_IntPtr.cs">
       <Link>Common\Interop\Windows\Kernel32\Interop.WriteFile_SafeHandle_IntPtr.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.WriteFile_IntPtr.cs">
+      <Link>Common\Interop\Windows\Kernel32\Interop.WriteFile_SafeHandle_IntPtr.cs</Link>
+    </Compile>
     <Compile Include="$(CommonPath)System\IO\Win32Marshal.cs">
       <Link>Common\System\IO\Win32Marshal.cs</Link>
     </Compile>
     <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.LockFileRegion.cs">
       <Link>Common\Interop\Unix\System.Native\Interop.LockFileRegion.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Log.cs">
+      <Link>Common\Interop\Unix\System.Native\Interop.Log.cs</Link>
+    </Compile>
     <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.LowLevelMonitor.cs">
       <Link>Common\Interop\Unix\System.Native\Interop.LowLevelMonitor.cs</Link>
     </Compile>
     <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Write.cs">
       <Link>Common\Interop\Unix\System.Native\Interop.Write.cs</Link>
     </Compile>
+    <Compile Include="$(MSBuildThisFileDirectory)Internal\Console.Unix.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Internal\IO\File.Unix.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeFileHandle.Unix.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\AppDomain.Unix.cs" />
index 954e3f9..bb55203 100644 (file)
 
   <!-- Sources -->
   <ItemGroup>
-      <Compile Include="$(BclSourcesRoot)\Mono\Console.Mono.cs" />
       <Compile Include="$(BclSourcesRoot)\Mono\MonoListItem.cs" />
       <Compile Include="$(BclSourcesRoot)\Mono\MonoDomain.cs" />
       <Compile Include="$(BclSourcesRoot)\Mono\MonoDomainSetup.cs" />
diff --git a/src/mono/netcore/System.Private.CoreLib/src/Mono/Console.Mono.cs b/src/mono/netcore/System.Private.CoreLib/src/Mono/Console.Mono.cs
deleted file mode 100644 (file)
index 6206ed6..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System;
-using System.Diagnostics;
-
-namespace Internal
-{
-    // Some CoreCLR tests use it for internal printf-style debugging in System.Private.CoreLib
-    public static class Console
-    {
-        public static void Write(string? s) => DebugProvider.WriteCore(s ?? string.Empty);
-
-        public static void WriteLine(string? s) => Write(s + Environment.NewLineConst);
-
-        public static void WriteLine() => Write(Environment.NewLineConst);
-    }
-}