<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/76050>76050</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            LLDB build for MinGW broken by unexpected include of unistd.h
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lldb,
            platform:windows
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mstorsjo
      </td>
    </tr>
</table>

<pre>
    Builds of LLDB for MinGW targets, with libc++ as the C++ standard library, was recently broken by an entirely unrelated change in libc++, commit 2fd4084fca0c474c2232533263182945bd1ebe17. An example of such a failed build: https://github.com/mstorsjo/llvm-mingw/actions/runs/7269329294/job/19809313900

To lay out the situation; mingw-w64's `getopt.h` header, https://github.com/mingw-w64/mingw-w64/blob/v11.0.1/mingw-w64-headers/crt/getopt.h, does define `optreset` only if `_BSD_SOURCE` was defined.

LLDB's use of `getopt.h` does require using `optreset`. To make sure this is available, LLDB does define `_BSD_SOURCE` right before including `getopt.h`: https://github.com/llvm/llvm-project/blob/main/lldb/include/lldb/Host/HostGetOpt.h#L12-L18

Mingw-w64's `unistd.h` also includes `getopt.h`, as one can expect to have `getopt` available after including `unistd.h`: https://github.com/mingw-w64/mingw-w64/blob/v11.0.1/mingw-w64-headers/crt/unistd.h#L12

Now at this point, the innocent libc++ change in 2fd4084fca0c474c2232533263182945bd1ebe17 caused more of libc++'s headers to include `<unistd.h>`, e.g. here: https://github.com/llvm/llvm-project/blob/2fd4084fca0c474c2232533263182945bd1ebe17/libcxx/include/print#L52-L54 (that line was preexisting, but the change made more headers include `<print>`).

Thus, LLDB includes a bunch of C++ standard headers, which end up bringing in `unistd.h`, which includes `getopt.h`. This then renders a later `#define _BSD_SOURCE` `#include <getopt.h>` in `HostGetOpt.h` without effect.

I guess this generally is a brittle pattern - defines such as `_BSD_SOURCE` or `_GNU_SOURCE` generally need to be defined on the command line, to be sure they applies as headers can be included transitively, earlier than expected.

Alternatively, the mingw-w64 `getopt.h` header could be adapted to react to being reincluded with `_BSD_SOURCE` defined, after first being included once without it.

CC @DavidSpickett 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVl2TmzoS_TXyS9dQIPAHD34Ye-LsVk2Sqs2k9jElpAaUCImVxHj877ckMP5I7k1u5WU8INTd5_TpIzHnZKMRt2S5I8unBRt8a-y2c95Y980sKiNO290glXBganh-ftpBbSx8kPr9f8Ez26B3hO7hKH0LSlac0B2hO2AOfIuwnx6dZ1owK8InltlT3MIcWOSovTpBZc131FCdgGlA7aVFdYJBW1TMowDeMt0gSH2VJAThpuukB1qLIt0UNWcpL9YFpzSnyzynqzzb0LJYViLDCrN1Ao8a8I11vcIAyA28BQY1kwoFVAEoyR-h9b53JH8k9EDooZG-HaqEm47Qw5kaQg9KvXYPndTNkdAD414a7Qg92CH-rOmqzGlJy4LQwzdTEXrIyk1a5llepilJn0j6OP59MaDYCczgI2dO-oGFYCTfQQz_cFwVhK4dkFXaoDe9T1qySqFFJtAGGv6u4EuEm_8rFWt6zbIkTbLrxYcxbgDBrQ_xzjnpHoRBBwJrqTGUY3pv0aEP5RitTiDr8Prr7vPT18-fvvxn_y6shFaPe0RyjTzoKQIbXGzHHb6Yy-L_BmkRBid1c5cygRcDHfuO4AaL4FvpQDpgr0wqVikMBUfN3lV9V56VTeuhwtrYIDGuBjHluqrmF7oIYjhrorfmG3J_IbljUsdFEZ7GDHh58S_j_PTzHv2nker8OaMPz9nmmq8P92oYtHRejGwx5cxUPd5LJRDBHBiNwMOAvfXIPXgDLXvFy7cxzJk8YLVHe8vHVb5fzcmfy25OFrm45uGjOQLzY797I7UP-MLsSK1NcJRrK7pYx--6BHA2OBTQBT2Y-sZy1m4aOxfom-iGyMd-Ljh_N3GOSZNAixb_RD2_W3YIICv-9najsd5GfvLnJX14XhZA6Ma3LDCkMQ5mbxHfpPNSN6HkarKhibaOCRyJOMO-xTyG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