Windows: Fixed ordering of system headers. This patch consolidates the inclusion of the ordering-dependent Windows system headers `WinSock2.h`, `WS2tcpip.h`, `Windows.h`, etc.
For historical reasons, `Windows.h` will include `winsock.h`, the header for the Windows Sockets 1.1 library, which was last supported in Windows 2000. We use the Windows Sockets 2 library, which requires the `WinSock2.h` header, and this header must be included before `Windows.h`, otherwise symbol redefinition errors will occur. The `WS2tcpip.h` header includes the extensions to Windows Sockets 2, and any header which may include `Windows.h` must be included carefully. It is simplest to consolidate the inclusion of these problematic system headers into `stout/windows.hpp`, and elsewhere include that with a comment as to which header specifically the file is requiring. Doing so will prevent incorrect ordering from being introduced. Note that the erroneous inclusion of `winsock.h` was removed. Review: https://reviews.apache.org/r/62508/ Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/93f670e9 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/93f670e9 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/93f670e9 Branch: refs/heads/master Commit: 93f670e923399511618eb816144da6a02ca3b8c4 Parents: 5d4328b Author: Andrew Schwartzmeyer <[email protected]> Authored: Mon Oct 2 12:09:02 2017 -0700 Committer: Joseph Wu <[email protected]> Committed: Mon Oct 2 14:36:56 2017 -0700 ---------------------------------------------------------------------- 3rdparty/stout/include/stout/ip.hpp | 6 ++--- 3rdparty/stout/include/stout/os/windows/dup.hpp | 4 +-- 3rdparty/stout/include/stout/os/windows/fd.hpp | 4 +-- .../stout/include/stout/os/windows/sendfile.hpp | 2 +- .../stout/include/stout/os/windows/socket.hpp | 5 ++-- 3rdparty/stout/include/stout/windows.hpp | 27 +++++++++++++------- 3rdparty/stout/include/stout/windows/ip.hpp | 2 +- 3rdparty/stout/include/stout/windows/mac.hpp | 2 +- 3rdparty/stout/include/stout/windows/net.hpp | 3 ++- 3rdparty/stout/include/stout/windows/os.hpp | 15 ++++++----- 10 files changed, 39 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/93f670e9/3rdparty/stout/include/stout/ip.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/ip.hpp b/3rdparty/stout/include/stout/ip.hpp index a722fa4..b4abcb9 100644 --- a/3rdparty/stout/include/stout/ip.hpp +++ b/3rdparty/stout/include/stout/ip.hpp @@ -34,9 +34,7 @@ // Note: Header grouping and ordering is considered before // inclusion/exclusion by platform. // For 'inet_pton', 'inet_ntop'. -#ifdef __WINDOWS__ -#include <Ws2tcpip.h> -#else +#ifndef __WINDOWS__ #include <netinet/in.h> #include <sys/socket.h> #endif // __WINDOWS__ @@ -66,7 +64,7 @@ #include <stout/unreachable.hpp> #ifdef __WINDOWS__ -#include <stout/windows.hpp> +#include <stout/windows.hpp> // For `WS2tcpip.h`. #endif // __WINDOWS__ namespace net { http://git-wip-us.apache.org/repos/asf/mesos/blob/93f670e9/3rdparty/stout/include/stout/os/windows/dup.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/os/windows/dup.hpp b/3rdparty/stout/include/stout/os/windows/dup.hpp index 1c9dda0..b37aaa6 100644 --- a/3rdparty/stout/include/stout/os/windows/dup.hpp +++ b/3rdparty/stout/include/stout/os/windows/dup.hpp @@ -13,12 +13,10 @@ #ifndef __STOUT_OS_WINDOWS_DUP_HPP__ #define __STOUT_OS_WINDOWS_DUP_HPP__ -#include <io.h> -#include <Winsock2.h> - #include <stout/error.hpp> #include <stout/try.hpp> #include <stout/unreachable.hpp> +#include <stout/windows.hpp> // For `WinSock2.h`. #include <stout/os/windows/fd.hpp> http://git-wip-us.apache.org/repos/asf/mesos/blob/93f670e9/3rdparty/stout/include/stout/os/windows/fd.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/os/windows/fd.hpp b/3rdparty/stout/include/stout/os/windows/fd.hpp index ae2db27..8f31f46 100644 --- a/3rdparty/stout/include/stout/os/windows/fd.hpp +++ b/3rdparty/stout/include/stout/os/windows/fd.hpp @@ -13,9 +13,6 @@ #ifndef __STOUT_OS_WINDOWS_FD_HPP__ #define __STOUT_OS_WINDOWS_FD_HPP__ -#include <windows.h> -#include <WinSock2.h> - #include <array> #include <memory> #include <ostream> @@ -24,6 +21,7 @@ #include <stout/nothing.hpp> #include <stout/try.hpp> #include <stout/unreachable.hpp> +#include <stout/windows.hpp> // For `WinSock2.h`. namespace os { http://git-wip-us.apache.org/repos/asf/mesos/blob/93f670e9/3rdparty/stout/include/stout/os/windows/sendfile.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/os/windows/sendfile.hpp b/3rdparty/stout/include/stout/os/windows/sendfile.hpp index d50c89e..fff5872 100644 --- a/3rdparty/stout/include/stout/os/windows/sendfile.hpp +++ b/3rdparty/stout/include/stout/os/windows/sendfile.hpp @@ -17,7 +17,7 @@ #include <stout/error.hpp> #include <stout/try.hpp> -#include <stout/windows.hpp> +#include <stout/windows.hpp> // For `winioctl.h`. #include <stout/os/windows/fd.hpp> http://git-wip-us.apache.org/repos/asf/mesos/blob/93f670e9/3rdparty/stout/include/stout/os/windows/socket.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/os/windows/socket.hpp b/3rdparty/stout/include/stout/os/windows/socket.hpp index 18d2ecf..020c5e2 100644 --- a/3rdparty/stout/include/stout/os/windows/socket.hpp +++ b/3rdparty/stout/include/stout/os/windows/socket.hpp @@ -13,13 +13,14 @@ #ifndef __STOUT_OS_WINDOWS_SOCKET_HPP__ #define __STOUT_OS_WINDOWS_SOCKET_HPP__ -#include <winsock.h> - #include <glog/logging.h> #include <stout/abort.hpp> +#include <stout/windows.hpp> // For `WinSock2.h`. + #include <stout/os/windows/fd.hpp> + namespace net { // Initialize Windows socket stack. http://git-wip-us.apache.org/repos/asf/mesos/blob/93f670e9/3rdparty/stout/include/stout/windows.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/windows.hpp b/3rdparty/stout/include/stout/windows.hpp index 1d865f8..fd7ffea 100644 --- a/3rdparty/stout/include/stout/windows.hpp +++ b/3rdparty/stout/include/stout/windows.hpp @@ -13,6 +13,23 @@ #ifndef __STOUT_WINDOWS_HPP__ #define __STOUT_WINDOWS_HPP__ +// We include `WinSock2.h` before `Windows.h` explicitly to avoid symbol +// re-definitions. This is a documented pattern, because `Windows.h` will +// otherwise include `winsock.h` for "historical reasons". Note that +// `winsock.h` is for Windows Sockets 1.1, last used in Windows 2000. +// +// NOTE: The capitalization of these headers is based on the files +// included in the SDK, rather than MSDN documentation. +// +// https://msdn.microsoft.com/en-us/library/windows/desktop/ms738562(v=vs.85).aspx +// NOLINT(whitespace/line_length) +#include <WinSock2.h> // For Windows Sockets 2. +#include <WS2tcpip.h> // For `getaddrinfo` etc. +#include <iphlpapi.h> // For `GetAdaptersInfo`. +#include <MSWSock.h> // For `TransmitFile`. +#include <winioctl.h> // For `DeviceIoControl` +#include <Windows.h> // For everything else. + #include <direct.h> // For `_mkdir`. #include <errno.h> // For `_set_errno`. #include <fcntl.h> // For file access flags like `_O_CREAT`. @@ -22,15 +39,7 @@ #include <sys/stat.h> // For permissions flags. -#include <BaseTsd.h> // For `SSIZE_T`. -// We include `Winsock2.h` before `Windows.h` explicitly to avoid symbold -// re-definitions. This is a known pattern in the windows community. -#include <WS2tcpip.h> -#include <Winsock2.h> -#include <iphlpapi.h> -#include <mswsock.h> -#include <winioctl.h> -#include <Windows.h> +#include <basetsd.h> // For `SSIZE_T`. #include <memory> http://git-wip-us.apache.org/repos/asf/mesos/blob/93f670e9/3rdparty/stout/include/stout/windows/ip.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/windows/ip.hpp b/3rdparty/stout/include/stout/windows/ip.hpp index d7738f2..16c075d 100644 --- a/3rdparty/stout/include/stout/windows/ip.hpp +++ b/3rdparty/stout/include/stout/windows/ip.hpp @@ -20,7 +20,7 @@ #include <stout/none.hpp> #include <stout/result.hpp> #include <stout/try.hpp> -#include <stout/windows.hpp> +#include <stout/windows.hpp> // For `iphlpapi.h`. namespace net { http://git-wip-us.apache.org/repos/asf/mesos/blob/93f670e9/3rdparty/stout/include/stout/windows/mac.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/windows/mac.hpp b/3rdparty/stout/include/stout/windows/mac.hpp index 3ebf4e1..44f2815 100644 --- a/3rdparty/stout/include/stout/windows/mac.hpp +++ b/3rdparty/stout/include/stout/windows/mac.hpp @@ -20,7 +20,7 @@ #include <stout/none.hpp> #include <stout/result.hpp> #include <stout/stringify.hpp> -#include <stout/windows.hpp> +#include <stout/windows.hpp> // For `iphlpapi.h`. // Network utilities. http://git-wip-us.apache.org/repos/asf/mesos/blob/93f670e9/3rdparty/stout/include/stout/windows/net.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/windows/net.hpp b/3rdparty/stout/include/stout/windows/net.hpp index 364509f..d70f967 100644 --- a/3rdparty/stout/include/stout/windows/net.hpp +++ b/3rdparty/stout/include/stout/windows/net.hpp @@ -22,7 +22,8 @@ #include <stout/nothing.hpp> #include <stout/stringify.hpp> #include <stout/try.hpp> -#include <stout/windows.hpp> +#include <stout/windows.hpp> // For `iphlpapi.h`. + #include <stout/windows/os.hpp> http://git-wip-us.apache.org/repos/asf/mesos/blob/93f670e9/3rdparty/stout/include/stout/windows/os.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/windows/os.hpp b/3rdparty/stout/include/stout/windows/os.hpp index a70a61c..09ddec6 100644 --- a/3rdparty/stout/include/stout/windows/os.hpp +++ b/3rdparty/stout/include/stout/windows/os.hpp @@ -13,12 +13,6 @@ #ifndef __STOUT_WINDOWS_OS_HPP__ #define __STOUT_WINDOWS_OS_HPP__ -#include <direct.h> -#include <io.h> -#include <Psapi.h> -#include <TlHelp32.h> -#include <Userenv.h> - #include <sys/utime.h> #include <list> @@ -46,6 +40,15 @@ #include <stout/os/raw/environment.hpp> #include <stout/os/windows/fd.hpp> +// NOTE: These system headers must be included after `stout/windows.hpp` +// as they may include `Windows.h`. See comments in `stout/windows.hpp` +// for why this ordering is important. +#include <direct.h> +#include <io.h> +#include <Psapi.h> +#include <TlHelp32.h> +#include <Userenv.h> + namespace os { namespace internal {
