Skip to content

Commit

Permalink
Move Internal.Console to shared CoreLib & port to Unix (#42983)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalStrehovsky authored Oct 11, 2020
1 parent 61d444a commit 58f28f6
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@

<!-- 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" />
Expand Down Expand Up @@ -244,12 +243,6 @@
<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
Expand Down
35 changes: 0 additions & 35 deletions src/coreclr/src/System.Private.CoreLib/src/Internal/Console.cs

This file was deleted.

2 changes: 0 additions & 2 deletions src/coreclr/src/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -1062,7 +1061,6 @@ FCFuncStart(gPalKernel32Funcs)
QCFuncElement("ResetEvent", ResetEvent)
QCFuncElement("SetEnvironmentVariable", SetEnvironmentVariableW)
QCFuncElement("SetEvent", SetEvent)
QCFuncElement("WriteFile", WriteFile)
FCFuncEnd()
#endif

Expand Down

This file was deleted.

8 changes: 7 additions & 1 deletion src/libraries/Native/Unix/System.Native/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions src/libraries/Native/Unix/System.Native/pal_log.c
Original file line number Diff line number Diff line change
@@ -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);
}
6 changes: 0 additions & 6 deletions src/libraries/Native/Unix/System.Native/pal_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
5 changes: 0 additions & 5 deletions src/libraries/Native/Unix/System.Native/pal_log.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,3 @@ void SystemNative_Log (uint8_t* buffer, int32_t length)
}
[msg release];
}

int32_t SystemNative_InitializeTerminalAndSignalHandling(void)
{
return 0;
}
13 changes: 13 additions & 0 deletions src/libraries/Native/Unix/System.Native/pal_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ static void* SignalHandlerLoop(void* arg)
}
else if (signalCode == SIGCONT)
{
#ifdef HAS_CONSOLE_SIGNALS
ReinitializeTerminal();
#endif
}
else if (signalCode != SIGWINCH)
{
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -315,3 +319,12 @@ int32_t InitializeSignalHandlingCore()

return 1;
}

#ifndef HAS_CONSOLE_SIGNALS

int32_t SystemNative_InitializeTerminalAndSignalHandling()
{
return 0;
}

#endif
11 changes: 11 additions & 0 deletions src/libraries/Native/Unix/System.Native/pal_signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/libraries/System.Console/src/System.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
20 changes: 20 additions & 0 deletions src/libraries/System.Private.CoreLib/src/Internal/Console.Unix.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
21 changes: 21 additions & 0 deletions src/libraries/System.Private.CoreLib/src/Internal/Console.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down Expand Up @@ -1389,6 +1390,9 @@
<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>
Expand Down Expand Up @@ -1416,6 +1420,9 @@
<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>
Expand Down Expand Up @@ -1584,6 +1591,7 @@
<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" />
Expand Down Expand Up @@ -1667,6 +1675,9 @@
<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>
Expand Down Expand Up @@ -1733,6 +1744,9 @@
<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>
Expand Down Expand Up @@ -1787,6 +1801,7 @@
<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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@

<!-- 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" />
Expand Down
18 changes: 0 additions & 18 deletions src/mono/netcore/System.Private.CoreLib/src/Mono/Console.Mono.cs

This file was deleted.

0 comments on commit 58f28f6

Please sign in to comment.