[Lldb-commits] [lldb] [LLDB] Define _BSD_SOURCE globally, to get optreset available in mingw's getopt.h (PR #76137)

Martin Storsjö via lldb-commits lldb-commits at lists.llvm.org
Thu Dec 21 01:28:32 PST 2023


https://github.com/mstorsjo created https://github.com/llvm/llvm-project/pull/76137

We previously were defining _BSD_SOURCE right before including getopt.h. However, on mingw-w64, getopt.h is also transitively included by unistd.h, and unistd.h can be transitively included by many headers (recently, by some libc++ headers).

Therefore, to be safe, we need to define _BSD_SOURCE before including any header. Thus do this in CMake.

This fixes https://github.com/llvm/llvm-project/issues/76050.

>From 69552a132c6ff99a2a1caf8cd0075f796174d95a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin at martin.st>
Date: Thu, 21 Dec 2023 11:23:18 +0200
Subject: [PATCH] [LLDB] Define _BSD_SOURCE globally, to get optreset available
 in mingw's getopt.h

We previously were defining _BSD_SOURCE right before including
getopt.h. However, on mingw-w64, getopt.h is also transitively
included by unistd.h, and unistd.h can be transitively included
by many headers (recently, by some libc++ headers).

Therefore, to be safe, we need to define _BSD_SOURCE before
including any header. Thus do this in CMake.

This fixes https://github.com/llvm/llvm-project/issues/76050.
---
 lldb/CMakeLists.txt                 | 4 ++++
 lldb/include/lldb/Host/HostGetOpt.h | 4 ----
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lldb/CMakeLists.txt b/lldb/CMakeLists.txt
index 4a53d7ef3d0da0..7844d93d78d29a 100644
--- a/lldb/CMakeLists.txt
+++ b/lldb/CMakeLists.txt
@@ -44,6 +44,10 @@ endif()
 
 if (WIN32)
   add_definitions(-D_ENABLE_EXTENDED_ALIGNED_STORAGE)
+  if (NOT MSVC)
+    # _BSD_SOURCE is required for MinGW's getopt.h to define optreset
+    add_definitions(-D_BSD_SOURCE)
+  endif()
 endif()
 
 if (LLDB_ENABLE_PYTHON)
diff --git a/lldb/include/lldb/Host/HostGetOpt.h b/lldb/include/lldb/Host/HostGetOpt.h
index 746e03e1bd1ee2..52cfdf4dbb89c2 100644
--- a/lldb/include/lldb/Host/HostGetOpt.h
+++ b/lldb/include/lldb/Host/HostGetOpt.h
@@ -11,10 +11,6 @@
 
 #if !defined(_MSC_VER) && !defined(__NetBSD__)
 
-#ifdef _WIN32
-#define _BSD_SOURCE // Required so that getopt.h defines optreset
-#endif
-
 #include <getopt.h>
 #include <unistd.h>
 



More information about the lldb-commits mailing list