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

    <tr>
        <th>Summary</th>
        <td>
            libcxx: when libcxx is compiled on Windows 8+ where chrono is used, the resulting binaries are unable to run on Windows 7.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    We recently received a bug report that the latest nightly builds of one of our products no longer function on Windows 7; throwing an error that GetSystemTimePreciseAsFileTime can not be found.

We have not updated our application to use this function, and neither have any of our third party components, but we did recently switch from gcc to clang for automated builds.

After investigating, a specific codepath within libcxx appears to be the problem.

libcxx contains the following code

https://github.com/llvm/llvm-project/blob/2e999b7dd1934a44d38c3a753460f1e5a217e9a5/libcxx/src/chrono.cpp#L105
```c++
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) || \
    (_WIN32_WINNT >= _WIN32_WINNT_WIN10)
  GetSystemTimePreciseAsFileTime(&ft);
#elif !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
 GetSystemTimeAsFileTime(&ft);
#else
 GetSystemTimeAsFileTimeFunc.fp(&ft);
#endif
```

When libcxx is compiled on Windows 8+, this results in generating a code path that makes use of GetSystemTimePreciseAsFileTime, which is unavailable on Windows 7; when an affacted build of libcxx is shipped as a pre-compiled binary, for example, by mingw-w64/msys2, linking against it results in an application that can not run on Windows 7, despite being otherwise compatible.

Interestingly, there is already a runtime check for this, so perhaps the #if chain is not required and `GetSystemTimeAsFileTimeFunc.fp(&ft);` can be used in all cases?

See
https://github.com/llvm/llvm-project/blob/2e999b7dd1934a44d38c3a753460f1e5a217e9a5/libcxx/src/chrono.cpp#L76
```c++
typedef void(WINAPI *GetSystemTimeAsFileTimePtr)(LPFILETIME);

class GetSystemTimeInit {
public:
  GetSystemTimeInit() {
    fp = (GetSystemTimeAsFileTimePtr)GetProcAddress(
 GetModuleHandleW(L"kernel32.dll"), "GetSystemTimePreciseAsFileTime");
 if (fp == nullptr)
      fp = GetSystemTimeAsFileTime;
  }
 GetSystemTimeAsFileTimePtr fp;
};
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMlsFu2zgTx5-GvgxqSJRtSQcfXDvqZ3xpamyzCPYUUORI4oYmtSQV12-_IOXYcbtN2z0tYFACNZyZ_3DmlzDnZKsRl2T-nsw3Ezb4ztjl7fCEv7sO7aQ24rh8QLDIUXt1jC_yGQUwqIcWLPbGevAdCwuCYh6dBy3bLljXg1TCgWnAaIyPwUJvjRi4d6ANKKNbtNAMmntpNBgND1ILc3CQk-w9-M6ag9QtMA1orbFjqA_oPx-dx_293OPOIpcOV66SCsMGcKZBGw81QmMGLaYk2ZBkNa4PCB17xmgw9IJ5FDEt1vdKchbT8AYGh-A76c65EboGpgVolL5DOzph-vgiy3fSCuiZ9UfgZt8bjdq7cKoePBwQhBSXOrqD9LyDxpo9tJyHiFwx3UJjLLDBm31MbCzgVf6rxqMFqZ_RedkyL3UbUwPXI5eN5MCNwJ75Dg7Sd1KDkjX_8iUIRGZdCFVjvK3emlrh_sr9yZgb7ZnULto1RqnxGoLr19ad970j2YrQitCqlb4b6ik3e0IrpZ5fHu96a_5E7gmtamVqQiuKZVnWuRBpmc3YbCaygmcsn2ezRdKkOGc0zbFk8-AgJkRo5SwntOKdNdpMed8Tmt2myfyUziIZf5zQ9-E37tJMNkBo8fiwvctoWO_ugWQ3JNvA672wFkDogtAFPGzvVrvtY7X6uL3943G3-u1-e7_9dEdocfpy3nrc3Hz-__2nHaEloSWQfE3yNZD5egwPAD8bPE2Cj9Opt9ub0ILQRePDgewiFFWUmv677MfIV4F_IqLDtw9Wg-bTpv_eeS1k89X1XU1qh-fmlS4OlVRhWi-QKOJ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