Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Shared/CommunicationsUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,17 @@ internal unsafe static char[] GetEnvironmentCharArray()
/// </summary>
internal static Dictionary<string, string> GetEnvironmentVariables()
{
Dictionary<string, string> table = new Dictionary<string, string>(200, StringComparer.OrdinalIgnoreCase); // Razzle has 150 environment variables
if (PlatformUtilities.IsUnix){
// Mono does have expensive security checks
foreach (DictionaryEntry ke in Environment.GetEnvironmentVariables ()){
table.Add ((string)ke.Key, (string)ke.Value);
}
return table;
}
char[] block = GetEnvironmentCharArray();

Dictionary<string, string> table = new Dictionary<string, string>(200, StringComparer.OrdinalIgnoreCase); // Razzle has 150 environment variables


// Copy strings out, parsing into pairs and inserting into the table.
// The first few environment variable entries start with an '='!
Expand Down
4 changes: 4 additions & 0 deletions src/Shared/EncodingUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ static internal Encoding CurrentSystemOemEncoding
// fall back to default ANSI encoding if we have problems
s_currentOemEncoding = Encoding.Default;

if (PlatformUtilities.IsUnix){
// The Mono default encoding is already the system encoding.
return s_currentOemEncoding;
}
try
{
// get the current OEM code page
Expand Down
12 changes: 12 additions & 0 deletions src/Shared/FileUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ private unsafe static bool AreStringsEqual(char* buffer, int len, string s)
/// </summary>
internal unsafe static string NormalizePath(string path)
{
if (PlatformUtilities.IsUnix)
return Path.GetFullPath (path);

ErrorUtilities.VerifyThrowArgumentLength(path, "path");

int errorCode = 0; // 0 == success in Win32
Expand Down Expand Up @@ -740,6 +743,9 @@ internal static bool DirectoryExistsNoThrow(string fullPath)
{
fullPath = AttemptToShortenPath(fullPath);

if (PlatformUtilities.IsUnix)
return Directory.Exists (fullPath);

NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA data = new NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
bool success = false;

Expand All @@ -762,6 +768,9 @@ internal static bool FileExistsNoThrow(string fullPath)
{
fullPath = AttemptToShortenPath(fullPath);

if (PlatformUtilities.IsUnix)
return File.Exists (fullPath);

NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA data = new NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
bool success = false;

Expand All @@ -784,6 +793,9 @@ internal static bool FileOrDirectoryExistsNoThrow(string fullPath)
{
fullPath = AttemptToShortenPath(fullPath);

if (PlatformUtilities.IsUnix)
return Directory.Exists (fullPath) || File.Exists (fullPath);

NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA data = new NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
bool success = false;

Expand Down
19 changes: 14 additions & 5 deletions src/Shared/MSBuildNameIgnoreCaseComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,22 @@ internal class MSBuildNameIgnoreCaseComparer : EqualityComparer<string>, IEquali
/// We need a static contructor to retrieve the running ProcessorArchitecture that way we can
/// Avoid using optimized code that will not run correctly on IA64 due to alignment issues
/// </summary>

//
// The side effect of this is to set s_runningProcessorArchitecture, which is not used
// in any of the code that has been released, so we skip it for Unix.
//
static MSBuildNameIgnoreCaseComparer()
{
NativeMethodsShared.SYSTEM_INFO systemInfo = new NativeMethodsShared.SYSTEM_INFO();

NativeMethodsShared.GetSystemInfo(ref systemInfo);

s_runningProcessorArchitecture = systemInfo.wProcessorArchitecture;
if (PlatformUtilities.IsUnix){
return;
} else {
NativeMethodsShared.SYSTEM_INFO systemInfo = new NativeMethodsShared.SYSTEM_INFO();

NativeMethodsShared.GetSystemInfo(ref systemInfo);

s_runningProcessorArchitecture = systemInfo.wProcessorArchitecture;
}
}

/// <summary>
Expand Down
67 changes: 64 additions & 3 deletions src/Shared/NativeMethodsShared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ internal struct SYSTEM_INFO
internal ushort wProcessorRevision;
}


/// <summary>
/// Wrap the intptr returned by OpenProcess in a safe handle.
/// </summary>
Expand Down Expand Up @@ -370,6 +370,15 @@ internal static bool GetLastWriteDirectoryUtcTime(string fullPath, out DateTime
// there

fileModifiedTimeUtc = DateTime.MinValue;

if (PlatformUtilities.IsUnix){
if (Directory.Exists (fullPath)){
fileModifiedTimeUtc = File.GetLastWriteTimeUtc (fullPath);
return true;
}
return false;
}

WIN32_FILE_ATTRIBUTE_DATA data = new WIN32_FILE_ATTRIBUTE_DATA();
bool success = false;

Expand Down Expand Up @@ -481,6 +490,14 @@ internal static MemoryStatus GetMemoryStatus()
internal static DateTime GetLastWriteFileUtcTime(string fullPath)
{
DateTime fileModifiedTime = DateTime.MinValue;
if (PlatformUtilities.IsUnix){
if (File.Exists (fullPath))
return File.GetLastWriteTimeUtc (fullPath);
if (Directory.Exists (fullPath))
return Directory.GetLastWriteTimeUtc (fullPath);
return fileModifiedTime;
}

WIN32_FILE_ATTRIBUTE_DATA data = new WIN32_FILE_ATTRIBUTE_DATA();
bool success = false;

Expand Down Expand Up @@ -529,6 +546,7 @@ public static void ThrowExceptionForErrorCode(int errorCode)
Marshal.ThrowExceptionForHR(errorCode);
}

static string []paths;
/// <summary>
/// Looks for the given file in the system path i.e. all locations in
/// the %PATH% environment variable.
Expand All @@ -537,6 +555,17 @@ public static void ThrowExceptionForErrorCode(int errorCode)
/// <returns>The location of the file, or null if file not found.</returns>
internal static string FindOnPath(string filename)
{
if (PlatformUtilities.IsUnix){
if (paths == null)
paths = Environment.GetEnvironmentVariable ("PATH").Split (':');
foreach (var p in paths){
var full = Path.Combine (p, filename);
// TODO: not only it needs to exist, but it also should be executable
if (File.Exists (full))
return full;
}
return null;
}
StringBuilder pathBuilder = new StringBuilder(MAX_PATH + 1);
string pathToFile = null;

Expand Down Expand Up @@ -747,6 +776,9 @@ internal static List<KeyValuePair<int, SafeProcessHandle>> GetChildProcessIds(in
/// <returns></returns>
internal static string GetCurrentDirectory()
{
if (PlatformUtilities.IsUnix)
return Environment.CurrentDirectory;

StringBuilder sb = new StringBuilder(MAX_PATH);
int pathLength = GetCurrentDirectory(MAX_PATH, sb);

Expand Down Expand Up @@ -838,11 +870,22 @@ internal static extern uint GetRequestedRuntimeInfo(String pExe,
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern int GetCurrentDirectory(int nBufferLength, [Out] StringBuilder lpBuffer);

[DllImport("libc", SetLastError = true)]
internal static extern int chdir (string path);

[SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass", Justification = "Class name is NativeMethodsShared for increased clarity")]
[SuppressMessage("Microsoft.Usage", "CA2205:UseManagedEquivalentsOfWin32Api", Justification = "Using unmanaged equivalent for performance reasons")]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DllImport("kernel32.dll", EntryPoint="SetCurrentDirectory", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetCurrentDirectory(string path);
static extern bool _SetCurrentDirectory(string path);
internal static bool SetCurrentDirectory (string path)
{
if (PlatformUtilities.IsUnix)
return chdir (path) == 0;
else
return _SetCurrentDirectory (path);
}


[SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass", Justification = "Class name is NativeMethodsShared for increased clarity")]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
Expand Down Expand Up @@ -927,4 +970,22 @@ internal static bool MsgWaitOne(this WaitHandle handle, int timeout)

#endregion
}
internal class PlatformUtilities {
static int platform;

internal static bool IsUnix {
get {
if (platform == 0){
int p = (int) Environment.OSVersion.Platform;

if ((p == 4) || (p == 6) || (p == 128)) {
platform = 1;
} else {
platform = 2;
}
}
return platform == 1;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,9 @@ private void EvaluateRequestStates()
[SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.GC.Collect", Justification = "We're trying to get rid of memory because we're running low, so we need to collect NOW in order to free it up ASAP")]
private void CheckMemoryUsage()
{
if (PlatformUtilities.IsUnix)
return;

// Jeffrey Richter suggests that when the memory load in the system exceeds 80% it is a good
// idea to start finding ways to unload unnecessary data to prevent memory starvation. We use this metric in
// our calculations below.
Expand Down
32 changes: 18 additions & 14 deletions src/XMakeBuildEngine/Logging/BaseConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
Expand Down Expand Up @@ -342,19 +342,23 @@ internal void WriteLinePretty(int indentLevel, string formattedString)
/// </summary>
internal void IsRunningWithCharacterFileType()
{
// Get the std out handle
IntPtr stdHandle = NativeMethodsShared.GetStdHandle(NativeMethodsShared.STD_OUTPUT_HANDLE);

if (stdHandle != Microsoft.Build.BackEnd.NativeMethods.InvalidHandle)
{
uint fileType = NativeMethodsShared.GetFileType(stdHandle);

// The std out is a char type(LPT or Console)
runningWithCharacterFileType = (fileType == NativeMethodsShared.FILE_TYPE_CHAR);
}
else
{
runningWithCharacterFileType = false;
if (PlatformUtilities.IsRunningOnUnix){
runningWithCharacterFileType = !Console.IsInputRedirected && !Console.IsOutputRedirected;
} else {
// Get the std out handle
IntPtr stdHandle = NativeMethodsShared.GetStdHandle(NativeMethodsShared.STD_OUTPUT_HANDLE);

if (stdHandle != Microsoft.Build.BackEnd.NativeMethods.InvalidHandle)
{
uint fileType = NativeMethodsShared.GetFileType(stdHandle);

// The std out is a char type(LPT or Console)
runningWithCharacterFileType = (fileType == NativeMethodsShared.FILE_TYPE_CHAR);
}
else
{
runningWithCharacterFileType = false;
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/XMakeTasks/Exec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,10 @@ internal bool ValidateParametersAccessor()
protected override string GenerateFullPathToTool()
{
// Get the fully qualified path to cmd.exe
return ToolLocationHelper.GetPathToSystemFile("cmd.exe");
if (PlatformUtilities.IsUnix)
return "sh";
else
return ToolLocationHelper.GetPathToSystemFile("cmd.exe");
}

/// <summary>
Expand Down