[llvm] [clang] [Cygwin] Cygwin general (PR #74936)

εΎζŒζ’ Xu Chiheng via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 9 07:49:32 PST 2023


https://github.com/xu-chiheng created https://github.com/llvm/llvm-project/pull/74936

Remove some uses of macro __CYGWIN__ .
Fix build error by Clang due to the conflict of CIndexer.cpp and mm_malloc.h. In mm_malloc.h, _WIN32 and __CYGWIN__ can't both be defined, but CIndexer.cpp define both.
Override Cygwin's buggy getpagesize() to Win32 computePageSize().


>From d0578d50172d217618af652d5dfa87537225e41b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=BE=90=E6=8C=81=E6=81=92=20Xu=20Chiheng?=
 <chiheng.xu at gmail.com>
Date: Sat, 9 Dec 2023 23:47:37 +0800
Subject: [PATCH] 1

---
 clang/lib/Headers/mm_malloc.h        |  6 +++---
 clang/tools/libclang/CIndexer.cpp    | 10 ++--------
 llvm/lib/Support/MemoryBuffer.cpp    |  8 --------
 llvm/lib/Support/Process.cpp         | 19 +++++++++++++++++++
 llvm/lib/Support/Unix/Process.inc    |  4 +++-
 llvm/lib/Support/Windows/Process.inc | 13 -------------
 llvm/lib/Support/raw_ostream.cpp     |  4 ----
 llvm/tools/lli/lli.cpp               |  7 -------
 8 files changed, 27 insertions(+), 44 deletions(-)

diff --git a/clang/lib/Headers/mm_malloc.h b/clang/lib/Headers/mm_malloc.h
index d32fe59416277..6f46f10ee50f3 100644
--- a/clang/lib/Headers/mm_malloc.h
+++ b/clang/lib/Headers/mm_malloc.h
@@ -12,7 +12,7 @@
 
 #include <stdlib.h>
 
-#ifdef _WIN32
+#if defined(_WIN32) && !defined(__CYGWIN__)
 #include <malloc.h>
 #else
 #ifndef __cplusplus
@@ -41,7 +41,7 @@ _mm_malloc(size_t __size, size_t __align) {
   void *__mallocedMemory;
 #if defined(__MINGW32__)
   __mallocedMemory = __mingw_aligned_malloc(__size, __align);
-#elif defined(_WIN32)
+#elif defined(_WIN32) && !defined(__CYGWIN__)
   __mallocedMemory = _aligned_malloc(__size, __align);
 #else
   if (posix_memalign(&__mallocedMemory, __align, __size))
@@ -56,7 +56,7 @@ _mm_free(void *__p)
 {
 #if defined(__MINGW32__)
   __mingw_aligned_free(__p);
-#elif defined(_WIN32)
+#elif defined(_WIN32) && !defined(__CYGWIN__)
   _aligned_free(__p);
 #else
   free(__p);
diff --git a/clang/tools/libclang/CIndexer.cpp b/clang/tools/libclang/CIndexer.cpp
index 77da2e4fa5ead..d1bd7ad49c5f4 100644
--- a/clang/tools/libclang/CIndexer.cpp
+++ b/clang/tools/libclang/CIndexer.cpp
@@ -26,12 +26,10 @@
 #include <mutex>
 
 #ifdef __CYGWIN__
-#include <cygwin/version.h>
 #include <sys/cygwin.h>
-#define _WIN32 1
 #endif
 
-#ifdef _WIN32
+#if defined(_WIN32) || defined(__CYGWIN__)
 #include <windows.h>
 #elif defined(_AIX)
 #include <errno.h>
@@ -104,7 +102,7 @@ const std::string &CIndexer::getClangResourcesPath() {
   SmallString<128> LibClangPath;
 
   // Find the location where this library lives (libclang.dylib).
-#ifdef _WIN32
+#if defined(_WIN32) || defined(__CYGWIN__)
   MEMORY_BASIC_INFORMATION mbi;
   char path[MAX_PATH];
   VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
@@ -114,11 +112,7 @@ const std::string &CIndexer::getClangResourcesPath() {
 #ifdef __CYGWIN__
   char w32path[MAX_PATH];
   strcpy(w32path, path);
-#if CYGWIN_VERSION_API_MAJOR > 0 || CYGWIN_VERSION_API_MINOR >= 181
   cygwin_conv_path(CCP_WIN_A_TO_POSIX, w32path, path, MAX_PATH);
-#else
-  cygwin_conv_to_full_posix_path(w32path, path);
-#endif
 #endif
 
   LibClangPath += path;
diff --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp
index 4cc4fe019b75b..8184b32575f21 100644
--- a/llvm/lib/Support/MemoryBuffer.cpp
+++ b/llvm/lib/Support/MemoryBuffer.cpp
@@ -384,14 +384,6 @@ static bool shouldUseMmap(sys::fs::file_t FD,
   if ((FileSize & (PageSize -1)) == 0)
     return false;
 
-#if defined(__CYGWIN__)
-  // Don't try to map files that are exactly a multiple of the physical page size
-  // if we need a null terminator.
-  // FIXME: We should reorganize again getPageSize() on Win32.
-  if ((FileSize & (4096 - 1)) == 0)
-    return false;
-#endif
-
   return true;
 }
 
diff --git a/llvm/lib/Support/Process.cpp b/llvm/lib/Support/Process.cpp
index 30c64d3ed9ed1..6d4a7275f5fa6 100644
--- a/llvm/lib/Support/Process.cpp
+++ b/llvm/lib/Support/Process.cpp
@@ -23,6 +23,10 @@
 #include <optional>
 #include <stdlib.h> // for _Exit
 
+#if defined(_WIN32) || defined(__CYGWIN__)
+#include <windows.h>
+#endif
+
 using namespace llvm;
 using namespace sys;
 
@@ -102,6 +106,21 @@ bool Process::AreCoreFilesPrevented() { return coreFilesPrevented; }
     ::exit(RetCode);
 }
 
+#if defined(_WIN32) || defined(__CYGWIN__)
+// This function retrieves the page size using GetNativeSystemInfo() and is
+// present solely so it can be called once to initialize the self_process member
+// below.
+static unsigned computePageSize() {
+  // GetNativeSystemInfo() provides the physical page size which may differ
+  // from GetSystemInfo() in 32-bit applications running under WOW64.
+  SYSTEM_INFO info;
+  GetNativeSystemInfo(&info);
+  // FIXME: FileOffset in MapViewOfFile() should be aligned to not dwPageSize,
+  // but dwAllocationGranularity.
+  return static_cast<unsigned>(info.dwPageSize);
+}
+#endif
+
 // Include the platform-specific parts of this class.
 #ifdef LLVM_ON_UNIX
 #include "Unix/Process.inc"
diff --git a/llvm/lib/Support/Unix/Process.inc b/llvm/lib/Support/Unix/Process.inc
index 2babf07944bf7..2bf53ae188ebd 100644
--- a/llvm/lib/Support/Unix/Process.inc
+++ b/llvm/lib/Support/Unix/Process.inc
@@ -76,7 +76,9 @@ Process::Pid Process::getProcessId() {
 // On Cygwin, getpagesize() returns 64k(AllocationGranularity) and
 // offset in mmap(3) should be aligned to the AllocationGranularity.
 Expected<unsigned> Process::getPageSize() {
-#if defined(HAVE_GETPAGESIZE)
+#if defined(__CYGWIN__)
+  static const int page_size = computePageSize();
+#elif defined(HAVE_GETPAGESIZE)
   static const int page_size = ::getpagesize();
 #elif defined(HAVE_SYSCONF)
   static long page_size = ::sysconf(_SC_PAGE_SIZE);
diff --git a/llvm/lib/Support/Windows/Process.inc b/llvm/lib/Support/Windows/Process.inc
index a54c06d46870b..9addfa6ec805e 100644
--- a/llvm/lib/Support/Windows/Process.inc
+++ b/llvm/lib/Support/Windows/Process.inc
@@ -50,19 +50,6 @@ Process::Pid Process::getProcessId() {
   return Pid(::GetCurrentProcessId());
 }
 
-// This function retrieves the page size using GetNativeSystemInfo() and is
-// present solely so it can be called once to initialize the self_process member
-// below.
-static unsigned computePageSize() {
-  // GetNativeSystemInfo() provides the physical page size which may differ
-  // from GetSystemInfo() in 32-bit applications running under WOW64.
-  SYSTEM_INFO info;
-  GetNativeSystemInfo(&info);
-  // FIXME: FileOffset in MapViewOfFile() should be aligned to not dwPageSize,
-  // but dwAllocationGranularity.
-  return static_cast<unsigned>(info.dwPageSize);
-}
-
 Expected<unsigned> Process::getPageSize() {
   static unsigned Ret = computePageSize();
   return Ret;
diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp
index 8908e7b6a150c..278eb988b2e9d 100644
--- a/llvm/lib/Support/raw_ostream.cpp
+++ b/llvm/lib/Support/raw_ostream.cpp
@@ -37,10 +37,6 @@
 # include <unistd.h>
 #endif
 
-#if defined(__CYGWIN__)
-#include <io.h>
-#endif
-
 #if defined(_MSC_VER)
 #include <io.h>
 #ifndef STDIN_FILENO
diff --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp
index 36fca4c40ed06..3e83e93946129 100644
--- a/llvm/tools/lli/lli.cpp
+++ b/llvm/tools/lli/lli.cpp
@@ -75,13 +75,6 @@
 #include <io.h>
 #endif
 
-#ifdef __CYGWIN__
-#include <cygwin/version.h>
-#if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
-#define DO_NOTHING_ATEXIT 1
-#endif
-#endif
-
 using namespace llvm;
 
 static codegen::RegisterCodeGenFlags CGF;



More information about the cfe-commits mailing list