Portable macro definition. (#8999)

This commit is contained in:
Jiaming Yuan 2023-03-31 20:48:59 +08:00 committed by GitHub
parent bac22734fb
commit bcb55d3b6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
/*! /**
* Copyright (c) 2022 by XGBoost Contributors * Copyright (c) 2022-2023, XGBoost Contributors
*/ */
#pragma once #pragma once
@ -18,7 +18,11 @@
#include <utility> // std::swap #include <utility> // std::swap
#if !defined(xgboost_IS_MINGW) #if !defined(xgboost_IS_MINGW)
#define xgboost_IS_MINGW() defined(__MINGW32__)
#if defined(__MINGW32__)
#define xgboost_IS_MINGW 1
#endif // defined(__MINGW32__)
#endif // xgboost_IS_MINGW #endif // xgboost_IS_MINGW
#if defined(_WIN32) #if defined(_WIN32)
@ -32,7 +36,7 @@ using in_port_t = std::uint16_t;
#pragma comment(lib, "Ws2_32.lib") #pragma comment(lib, "Ws2_32.lib")
#endif // _MSC_VER #endif // _MSC_VER
#if !xgboost_IS_MINGW() #if !defined(xgboost_IS_MINGW)
using ssize_t = int; using ssize_t = int;
#endif // !xgboost_IS_MINGW() #endif // !xgboost_IS_MINGW()
@ -62,10 +66,10 @@ using ssize_t = int;
namespace xgboost { namespace xgboost {
#if xgboost_IS_MINGW() #if defined(xgboost_IS_MINGW)
// see the dummy implementation of `poll` in rabit for more info. // see the dummy implementation of `poll` in rabit for more info.
inline void MingWError() { LOG(FATAL) << "Distributed training on mingw is not supported."; } inline void MingWError() { LOG(FATAL) << "Distributed training on mingw is not supported."; }
#endif // xgboost_IS_MINGW() #endif // defined(xgboost_IS_MINGW)
namespace system { namespace system {
inline std::int32_t LastError() { inline std::int32_t LastError() {
@ -144,7 +148,7 @@ inline void SocketFinalize() {
#endif // defined(_WIN32) #endif // defined(_WIN32)
} }
#if defined(_WIN32) && xgboost_IS_MINGW() #if defined(_WIN32) && defined(xgboost_IS_MINGW)
// dummy definition for old mysys32. // dummy definition for old mysys32.
inline const char *inet_ntop(int, const void *, char *, socklen_t) { // NOLINT inline const char *inet_ntop(int, const void *, char *, socklen_t) { // NOLINT
MingWError(); MingWError();
@ -152,7 +156,7 @@ inline const char *inet_ntop(int, const void *, char *, socklen_t) { // NOLINT
} }
#else #else
using ::inet_ntop; using ::inet_ntop;
#endif #endif // defined(_WIN32) && defined(xgboost_IS_MINGW)
} // namespace system } // namespace system
@ -296,9 +300,8 @@ class TCPSocket {
#else #else
struct sockaddr sa; struct sockaddr sa;
socklen_t sizeofsa = sizeof(sa); socklen_t sizeofsa = sizeof(sa);
xgboost_CHECK_SYS_CALL( xgboost_CHECK_SYS_CALL(getsockname(handle_, &sa, &sizeofsa), 0);
getsockname(handle_, &sa, &sizeofsa), 0); if (sizeofsa < sizeof(uchar_t) * 2) {
if (sizeofsa < sizeof(uchar_t)*2) {
return ret_iafamily(AF_INET); return ret_iafamily(AF_INET);
} }
return ret_iafamily(sa.sa_family); return ret_iafamily(sa.sa_family);
@ -508,7 +511,7 @@ class TCPSocket {
* \brief Create a TCP socket on specified domain. * \brief Create a TCP socket on specified domain.
*/ */
static TCPSocket Create(SockDomain domain) { static TCPSocket Create(SockDomain domain) {
#if xgboost_IS_MINGW() #if defined(xgboost_IS_MINGW)
MingWError(); MingWError();
return {}; return {};
#else #else
@ -522,7 +525,7 @@ class TCPSocket {
socket.domain_ = domain; socket.domain_ = domain;
#endif // defined(__APPLE__) #endif // defined(__APPLE__)
return socket; return socket;
#endif // xgboost_IS_MINGW() #endif // defined(xgboost_IS_MINGW)
} }
}; };
@ -544,4 +547,7 @@ inline std::string GetHostName() {
} // namespace xgboost } // namespace xgboost
#undef xgboost_CHECK_SYS_CALL #undef xgboost_CHECK_SYS_CALL
#if defined(xgboost_IS_MINGW)
#undef xgboost_IS_MINGW #undef xgboost_IS_MINGW
#endif