Commit 8b1900e5df

Andrew Kelley <andrew@ziglang.org>
2019-09-01 06:30:35
Revert "Merge pull request #2991 from emekoi/mingw-ci"
This reverts commit ec7d7a5b14540ea3b2bab9f11318630338467965, reversing changes made to 81c441f8855d4c58f0b2ff86d3d007cf0bf395d3. It looks like this broke colors in Windows Command Prompt (#3147) and this method of detecting native C ABI isn't working well (#3121).
1 parent ec6f15b
Changed files (4)
src/os.cpp
@@ -1125,27 +1125,29 @@ Error os_get_cwd(Buf *out_cwd) {
 #endif
 }
 
+#if defined(ZIG_OS_WINDOWS)
 #define is_wprefix(s, prefix) \
     (wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0)
-bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd) {
-#if defined(ZIG_OS_WINDOWS)
-    HANDLE handle = (HANDLE)_get_osfhandle(fd);
-
-    // Cygwin/msys's pty is a pipe.
-    if (handle == INVALID_HANDLE_VALUE || GetFileType(handle) != FILE_TYPE_PIPE) {
+static bool is_stderr_cyg_pty(void) {
+    HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
+    if (stderr_handle == INVALID_HANDLE_VALUE)
         return false;
-    }
 
     int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH;
+    FILE_NAME_INFO *nameinfo;
     WCHAR *p = NULL;
 
-    FILE_NAME_INFO *nameinfo = (FILE_NAME_INFO *)allocate<char>(size);
+    // Cygwin/msys's pty is a pipe.
+    if (GetFileType(stderr_handle) != FILE_TYPE_PIPE) {
+        return 0;
+    }
+    nameinfo = (FILE_NAME_INFO *)allocate<char>(size);
     if (nameinfo == NULL) {
-        return false;
+        return 0;
     }
     // Check the name of the pipe:
     // '\{cygwin,msys}-XXXXXXXXXXXXXXXX-ptyN-{from,to}-master'
-    if (GetFileInformationByHandleEx(handle, FileNameInfo, nameinfo, size)) {
+    if (GetFileInformationByHandleEx(stderr_handle, FileNameInfo, nameinfo, size)) {
         nameinfo->FileName[nameinfo->FileNameLength / sizeof(WCHAR)] = L'\0';
         p = nameinfo->FileName;
         if (is_wprefix(p, L"\\cygwin-")) {      /* Cygwin */
@@ -1178,14 +1180,12 @@ bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd) {
     }
     free(nameinfo);
     return (p != NULL);
-#else
-    return false;
-#endif
 }
+#endif
 
 bool os_stderr_tty(void) {
 #if defined(ZIG_OS_WINDOWS)
-    return _isatty(fileno(stderr)) != 0 || os_is_cygwin_pty(fileno(stderr));
+    return _isatty(_fileno(stderr)) != 0 || is_stderr_cyg_pty();
 #elif defined(ZIG_OS_POSIX)
     return isatty(STDERR_FILENO) != 0;
 #else
@@ -1486,7 +1486,7 @@ WORD original_console_attributes = FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BL
 
 void os_stderr_set_color(TermColor color) {
 #if defined(ZIG_OS_WINDOWS)
-    if (os_stderr_tty()) {
+    if (is_stderr_cyg_pty()) {
         set_color_posix(color);
         return;
     }
src/os.hpp
@@ -11,7 +11,6 @@
 #include "list.hpp"
 #include "buffer.hpp"
 #include "error.hpp"
-#include "target.hpp"
 #include "zig_llvm.h"
 #include "windows_sdk.h"
 
@@ -89,11 +88,6 @@ struct Termination {
 #define OsFile int
 #endif
 
-#if defined(ZIG_OS_WINDOWS)
-#undef fileno
-#define fileno _fileno
-#endif
-
 struct OsTimeStamp {
     uint64_t sec;
     uint64_t nsec;
@@ -158,8 +152,6 @@ Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf
 Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);
 Error ATTRIBUTE_MUST_USE os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);
 
-bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd);
-
 Error ATTRIBUTE_MUST_USE os_self_exe_shared_libs(ZigList<Buf *> &paths);
 
 #endif
src/target.cpp
@@ -491,16 +491,6 @@ Error target_parse_glibc_version(ZigGLibCVersion *glibc_ver, const char *text) {
     return ErrorNone;
 }
 
-static ZigLLVM_EnvironmentType target_get_win32_abi() {
-    FILE* files[] = { stdin, stdout, stderr, nullptr };
-    for (int i = 0; files[i] != nullptr; i++) {
-        if (os_is_cygwin_pty(fileno(files[i]))) {
-            return ZigLLVM_GNU;
-        }
-    }
-    return ZigLLVM_MSVC;
-}
-
 void get_native_target(ZigTarget *target) {
     // first zero initialize
     *target = {};
@@ -515,9 +505,6 @@ void get_native_target(ZigTarget *target) {
             &target->abi,
             &oformat);
     target->os = get_zig_os_type(os_type);
-    if (target->os == OsWindows) {
-        target->abi = target_get_win32_abi();
-    }
     target->is_native = true;
     if (target->abi == ZigLLVM_UnknownEnvironment) {
         target->abi = target_default_abi(target->arch, target->os);
@@ -1614,7 +1601,7 @@ ZigLLVM_EnvironmentType target_default_abi(ZigLLVM_ArchType arch, Os os) {
             return ZigLLVM_GNU;
         case OsUefi:
         case OsWindows:
-            return ZigLLVM_MSVC;            
+            return ZigLLVM_MSVC;
         case OsLinux:
         case OsWASI:
             return ZigLLVM_Musl;
std/os/windows.zig
@@ -65,7 +65,7 @@ pub const CreateFileError = error{
     InvalidUtf8,
 
     /// On Windows, file paths cannot contain these characters:
-    /// '*', '?', '"', '<', '>', '|', and '/' (when the ABI is not GNU)
+    /// '/', '*', '?', '"', '<', '>', '|'
     BadPathName,
 
     Unexpected,
@@ -836,10 +836,11 @@ pub fn sliceToPrefixedSuffixedFileW(s: []const u8, comptime suffix: []const u16)
     // > converting the name to an NT-style name, except when using the "\\?\"
     // > prefix as detailed in the following sections.
     // from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation
+    // Because we want the larger maximum path length for absolute paths, we
+    // disallow forward slashes in zig std lib file functions on Windows.
     for (s) |byte| {
         switch (byte) {
-            '*', '?', '"', '<', '>', '|' => return error.BadPathName,
-            '/' => if (builtin.abi == .msvc) return error.BadPathName,
+            '/', '*', '?', '"', '<', '>', '|' => return error.BadPathName,
             else => {},
         }
     }