Don't use WINDOWS/POSIX/NOMINMAX macros defined in cmake, move them to source code instead (#1070)

This commit is contained in:
Anton Kolesnyk 2020-12-04 11:55:59 -08:00 committed by GitHub
parent 3ca1104e91
commit ce98087f7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 141 additions and 123 deletions

View File

@ -29,9 +29,6 @@ if(DEFINED ENV{VCPKG_DEFAULT_TRIPLET} AND NOT DEFINED VCPKG_TARGET_TRIPLET)
set(VCPKG_TARGET_TRIPLET "$ENV{VCPKG_DEFAULT_TRIPLET}" CACHE STRING "")
endif()
# Define WINDOWS of POSIX for specific code implementations like FileStream
include(DefinePlatform)
# Project definition
project(az LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)

View File

@ -1,21 +0,0 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# SPDX-License-Identifier: MIT
#
# Cmake variables:
# UNIX : is TRUE on all UNIX-like OS's, including Apple OS X and CygWin
# WIN32 : is TRUE on Windows. Prior to 2.8.4 this included CygWin
# MINGW : is TRUE when using the MinGW compiler in Windows
# MSYS : is TRUE when using the MSYS developer environment in Windows
# CYGWIN : is TRUE on Windows when using the CygWin version of cmake
# APPLE : is TRUE on Apple systems. Note this does not imply the
# system is Mac OS X, only that APPLE is #defined in C/C++
# header files.
if (WIN32 OR MINGW OR MSYS OR CYGWIN)
add_compile_definitions(WINDOWS)
endif ()
if (UNIX)
add_compile_definitions(POSIX)
endif ()

View File

@ -8,17 +8,19 @@
#pragma once
#ifdef POSIX
#include <unistd.h>
#endif
#include <azure/core/platform.hpp>
#ifdef WINDOWS
#ifdef AZ_PLATFORM_POSIX
#include <unistd.h>
#elif defined(AZ_PLATFORM_WINDOWS)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <Windows.h>
#endif // Windows
#include <windows.h>
#endif
#include "azure/core/context.hpp"
@ -168,7 +170,7 @@ namespace Azure { namespace Core { namespace Http {
}
};
#ifdef POSIX
#ifdef AZ_PLATFORM_POSIX
/**
* @brief #BodyStream providing its data from a file.
*/
@ -201,9 +203,7 @@ namespace Azure { namespace Core { namespace Http {
int64_t Length() const override { return this->m_length; };
};
#endif
#ifdef WINDOWS
#elif defined(AZ_PLATFORM_WINDOWS)
/**
* @brief #BodyStream providing its data from a file.
*/
@ -236,7 +236,7 @@ namespace Azure { namespace Core { namespace Http {
int64_t Length() const override { return this->m_length; };
};
#endif // Windows
#endif
/**
* @brief #BodyStream that provides its data from another #BodyStream.

View File

@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
/**
* @file
* @brief Defines platform-specific macros.
*/
#pragma once
#ifdef _WIN32
#define AZ_PLATFORM_WINDOWS
#elif defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
#define AZ_PLATFORM_POSIX
#endif

View File

@ -2,17 +2,22 @@
// SPDX-License-Identifier: MIT
#include "azure/core/datetime.hpp"
#include <azure/core/platform.hpp>
#include <cstdio>
#include <cstring>
#include <limits>
#include <stdexcept>
#ifdef _WIN32
#ifdef AZ_PLATFORM_WINDOWS
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#else
#elif defined(AZ_PLATFORM_POSIX)
#include <sys/time.h>
#endif
@ -20,7 +25,7 @@ using namespace Azure::Core;
DateTime DateTime::Now()
{
#ifdef _WIN32
#ifdef AZ_PLATFORM_WINDOWS
FILETIME fileTime = {};
GetSystemTimeAsFileTime(&fileTime);
@ -29,7 +34,7 @@ DateTime DateTime::Now()
largeInt.HighPart = fileTime.dwHighDateTime;
return DateTime(Duration(largeInt.QuadPart));
#else
#elif defined(AZ_PLATFORM_POSIX)
static constexpr int64_t WindowsToPosixOffsetSeconds = 11644473600LL;
struct timeval time = {};

View File

@ -1,16 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#ifdef POSIX
#include <azure/core/platform.hpp>
#ifdef AZ_PLATFORM_POSIX
#include <errno.h>
#include <unistd.h>
#endif
#ifdef WINDOWS
#elif defined(AZ_PLATFORM_WINDOWS)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#include <Windows.h>
#endif // Windows
#endif
#include <windows.h>
#endif
#include "azure/core/context.hpp"
#include "azure/core/http/body_stream.hpp"
@ -79,8 +83,7 @@ int64_t MemoryBodyStream::Read(Context const& context, uint8_t* buffer, int64_t
return copy_length;
}
#ifdef POSIX
#ifdef AZ_PLATFORM_POSIX
int64_t FileBodyStream::Read(Azure::Core::Context const& context, uint8_t* buffer, int64_t count)
{
context.ThrowIfCanceled();
@ -99,10 +102,7 @@ int64_t FileBodyStream::Read(Azure::Core::Context const& context, uint8_t* buffe
this->m_offset += result;
return result;
}
#endif
#ifdef WINDOWS
#elif defined(AZ_PLATFORM_WINDOWS)
int64_t FileBodyStream::Read(Azure::Core::Context const& context, uint8_t* buffer, int64_t count)
{
context.ThrowIfCanceled();
@ -134,7 +134,7 @@ int64_t FileBodyStream::Read(Azure::Core::Context const& context, uint8_t* buffe
this->m_offset += numberOfBytesRead;
return numberOfBytesRead;
}
#endif // Windows
#endif
int64_t LimitBodyStream::Read(Context const& context, uint8_t* buffer, int64_t count)
{

View File

@ -6,16 +6,16 @@
#include "azure/core/http/policy.hpp"
#include "azure/core/http/transport.hpp"
#include "azure/core/internal/log.hpp"
#include <azure/core/platform.hpp>
// Private incude
#include "curl_connection_pool_private.hpp"
#include "curl_connection_private.hpp"
#include "curl_session_private.hpp"
#ifdef POSIX
#ifdef AZ_PLATFORM_POSIX
#include <poll.h> // for poll()
#endif
#ifdef WINDOWS
#elif defined(AZ_PLATFORM_WINDOWS)
#include <winsock2.h> // for WSAPoll();
#endif
@ -67,12 +67,9 @@ int pollSocketUntilEventOrTimeout(
PollSocketDirection direction,
long timeout)
{
#ifndef POSIX
#ifndef WINDOWS
#if !defined(AZ_PLATFORM_WINDOWS) && !defined(AZ_PLATFORM_POSIX)
// platform does not support Poll().
throw TransportException("Error while sending request. Platform does not support Poll()");
#endif
#endif
struct pollfd poller;
@ -105,10 +102,9 @@ int pollSocketUntilEventOrTimeout(
{
// check cancelation
context.ThrowIfCanceled();
#ifdef POSIX
#ifdef AZ_PLATFORM_POSIX
result = poll(&poller, 1, interval);
#endif
#ifdef WINDOWS
#elif defined(AZ_PLATFORM_WINDOWS)
result = WSAPoll(&poller, 1, interval);
#endif
}
@ -116,7 +112,7 @@ int pollSocketUntilEventOrTimeout(
return result;
}
#ifdef WINDOWS
#ifdef AZ_PLATFORM_WINDOWS
// Windows needs this after every write to socket or performance would be reduced to 1/4 for
// uploading operation.
// https://github.com/Azure/azure-sdk-for-cpp/issues/644
@ -137,7 +133,7 @@ void WinSocketSetBuffSize(curl_socket_t socket)
+ " result = " + std::to_string(result));
}
}
#endif // WINDOWS
#endif
} // namespace
using Azure::Core::Context;
@ -375,9 +371,9 @@ CURLcode CurlConnection::SendBuffer(
}
};
}
#ifdef WINDOWS
#ifdef AZ_PLATFORM_WINDOWS
WinSocketSetBuffSize(m_curlSocket);
#endif // WINDOWS
#endif
return CURLE_OK;
}
@ -756,9 +752,9 @@ int64_t CurlConnection::ReadFromSocket(Context const& context, uint8_t* buffer,
}
}
}
#ifdef WINDOWS
#ifdef AZ_PLATFORM_WINDOWS
WinSocketSetBuffSize(m_curlSocket);
#endif // WINDOWS
#endif
return readBytes;
}

View File

@ -2,14 +2,20 @@
// SPDX-License-Identifier: MIT
#include "azure/core/http/policy.hpp"
#include <azure/core/platform.hpp>
#include <cctype>
#include <sstream>
#ifdef _WIN32
#ifdef AZ_PLATFORM_WINDOWS
#define NOMINMAX
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
namespace {
@ -72,7 +78,7 @@ std::string GetOSVersion()
} // namespace
#else
#elif defined(AZ_PLATFORM_POSIX)
#include <sys/utsname.h>

View File

@ -7,16 +7,19 @@
*/
#include <azure/core/http/pipeline.hpp>
#include <azure/core/platform.hpp>
#ifdef POSIX
#ifdef AZ_PLATFORM_POSIX
#include <fcntl.h>
#endif // Posix
#ifdef WINDOWS
#elif defined(AZ_PLATFORM_WINDOWS)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#include <Windows.h>
#endif // Windows
#endif
#include <windows.h>
#endif
#include <azure/core/http/curl/curl.hpp>
#include <azure/core/http/http.hpp>
@ -69,7 +72,7 @@ int main()
return 0;
}
#ifdef POSIX
#ifdef AZ_PLATFORM_POSIX
void doFileRequest(Context const& context, HttpPipeline& pipeline)
{
@ -101,9 +104,7 @@ void doFileRequest(Context const& context, HttpPipeline& pipeline)
auto body = Http::BodyStream::ReadToEnd(context, limitedResponse);
cout << body.data() << endl << body.size() << endl;
}
#endif // Posix
#ifdef WINDOWS
#elif defined(AZ_PLATFORM_WINDOWS)
void doFileRequest(Context const& context, HttpPipeline& pipeline)
{
(void)pipeline;
@ -128,7 +129,7 @@ void doFileRequest(Context const& context, HttpPipeline& pipeline)
CloseHandle(hFile);
}
#endif // Windows
#endif
void doGetRequest(Context const& context, HttpPipeline& pipeline)
{

View File

@ -1,15 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#ifdef POSIX
#include <fcntl.h>
#endif // Posix
#include <azure/core/platform.hpp>
#ifdef WINDOWS
#ifdef AZ_PLATFORM_POSIX
#include <fcntl.h>
#elif defined(AZ_PLATFORM_WINDOWS)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#include <Windows.h>
#endif // Windows
#endif
#include <windows.h>
#endif
#include "transport_adapter_base.hpp"
#include <azure/core/context.hpp>
@ -434,11 +438,10 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::Url host("http://httpbin.org/put");
std::string testDataPath(AZURE_TEST_DATA_PATH);
#ifdef POSIX
#ifdef AZ_PLATFORM_POSIX
testDataPath.append("/fileData");
int f = open(testDataPath.data(), O_RDONLY);
#endif
#ifdef WINDOWS
#elif defined(AZ_PLATFORM_WINDOWS)
testDataPath.append("\\fileData");
HANDLE f = CreateFile(
testDataPath.data(),
@ -448,6 +451,8 @@ namespace Azure { namespace Core { namespace Test {
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
#else
#error "Unknown platform"
#endif
auto requestBodyStream
= Azure::Core::Http::FileBodyStream(f, 0, Azure::Core::Test::Datails::c_fileSize);
@ -469,11 +474,10 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::Url host("http://httpbin.org/put");
std::string testDataPath(AZURE_TEST_DATA_PATH);
#ifdef POSIX
#ifdef AZ_PLATFORM_POSIX
testDataPath.append("/fileData");
int f = open(testDataPath.data(), O_RDONLY);
#endif
#ifdef WINDOWS
#elif defined(AZ_PLATFORM_WINDOWS)
testDataPath.append("\\fileData");
HANDLE f = CreateFile(
testDataPath.data(),
@ -483,7 +487,10 @@ namespace Azure { namespace Core { namespace Test {
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
#else
#error "Unknown platform"
#endif
auto requestBodyStream
= Azure::Core::Http::FileBodyStream(f, 0, Azure::Core::Test::Datails::c_fileSize);
auto request = Azure::Core::Http::Request(
@ -503,11 +510,10 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::Url host("http://httpbin.org/put");
std::string testDataPath(AZURE_TEST_DATA_PATH);
#ifdef POSIX
#ifdef AZ_PLATFORM_POSIX
testDataPath.append("/fileData");
int f = open(testDataPath.data(), O_RDONLY);
#endif
#ifdef WINDOWS
#elif defined(AZ_PLATFORM_WINDOWS)
testDataPath.append("\\fileData");
HANDLE f = CreateFile(
testDataPath.data(),
@ -517,7 +523,10 @@ namespace Azure { namespace Core { namespace Test {
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
#else
#error "Unknown platform"
#endif
auto requestBodyStream
= Azure::Core::Http::FileBodyStream(f, 0, Azure::Core::Test::Datails::c_fileSize);
auto request = Azure::Core::Http::Request(

View File

@ -10,12 +10,6 @@ set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
option(BUILD_STORAGE_SAMPLES "Build storage sample codes" ON)
if(MSVC)
add_compile_definitions(NOMINMAX)
endif()
add_executable(azure-storage-test)
if(BUILD_TESTING)
set_target_properties(azure-storage-test PROPERTIES EXCLUDE_FROM_ALL FALSE)

View File

@ -3,8 +3,16 @@
#pragma once
#ifdef _WIN32
#include <Windows.h>
#include <azure/core/platform.hpp>
#ifdef AZ_PLATFORM_WINDOWS
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#endif
#include <cstdint>
@ -12,9 +20,9 @@
namespace Azure { namespace Storage { namespace Details {
#ifdef _WIN32
#ifdef AZ_PLATFORM_WINDOWS
using FileHandle = HANDLE;
#else
#elif defined(AZ_PLATFORM_POSIX)
using FileHandle = int;
#endif

View File

@ -1,12 +1,16 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#include <azure/core/platform.hpp>
#include "azure/storage/common/crypt.hpp"
#ifdef _WIN32
#include <Windows.h>
#ifdef AZ_PLATFORM_WINDOWS
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <bcrypt.h>
#else
#elif defined(AZ_PLATFORM_POSIX)
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/evp.h>
@ -71,7 +75,7 @@ namespace Azure { namespace Storage {
}
} // namespace Details
#ifdef _WIN32
#ifdef AZ_PLATFORM_WINDOWS
namespace Details {
@ -345,7 +349,7 @@ namespace Azure { namespace Storage {
return decoded;
}
#else
#elif defined(AZ_PLATFORM_POSIX)
namespace Details {

View File

@ -1,9 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#include <azure/core/platform.hpp>
#include "azure/storage/common/file_io.hpp"
#ifndef _WIN32
#ifdef AZ_PLATFORM_POSIX
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
@ -15,7 +16,7 @@
namespace Azure { namespace Storage { namespace Details {
#ifdef _WIN32
#ifdef AZ_PLATFORM_WINDOWS
FileReader::FileReader(const std::string& filename)
{
m_handle = CreateFile(
@ -80,7 +81,7 @@ namespace Azure { namespace Storage { namespace Details {
throw std::runtime_error("failed to write file");
}
}
#else
#elif defined(AZ_PLATFORM_POSIX)
FileReader::FileReader(const std::string& filename)
{
m_handle = open(filename.data(), O_RDONLY);

View File

@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#include <azure/core/platform.hpp>
#include "azure/storage/common/storage_per_retry_policy.hpp"
#include <ctime>
@ -22,9 +23,9 @@ namespace Azure { namespace Storage { namespace Details {
// TODO: call helper function provided by Azure Core when they provide one.
time_t t = std::time(nullptr);
struct tm ct;
#ifdef _WIN32
#ifdef AZ_PLATFORM_WINDOWS
gmtime_s(&ct, &t);
#else
#elif defined(AZ_PLATFORM_POSIX)
gmtime_r(&t, &ct);
#endif
static const char* weekdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

View File

@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#include <azure/core/platform.hpp>
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
@ -240,9 +242,9 @@ namespace Azure { namespace Storage { namespace Test {
{
std::time_t epoch_seconds = std::chrono::system_clock::to_time_t(timePoint);
struct tm ct;
#ifdef _WIN32
#ifdef AZ_PLATFORM_WINDOWS
gmtime_s(&ct, &epoch_seconds);
#else
#elif defined(AZ_PLATFORM_POSIX)
gmtime_r(&epoch_seconds, &ct);
#endif
std::string time_str;
@ -269,9 +271,9 @@ namespace Azure { namespace Storage { namespace Test {
{
std::time_t epoch_seconds = std::chrono::system_clock::to_time_t(timePoint);
struct tm ct;
#ifdef _WIN32
#ifdef AZ_PLATFORM_WINDOWS
gmtime_s(&ct, &epoch_seconds);
#else
#elif defined(AZ_PLATFORM_POSIX)
gmtime_r(&epoch_seconds, &ct);
#endif
std::stringstream ss;
@ -286,9 +288,9 @@ namespace Azure { namespace Storage { namespace Test {
std::stringstream ss(timeStr);
ss.imbue(std::locale("C"));
ss >> std::get_time(&t, "%a, %d %b %Y %H:%M:%S GMT");
#ifdef _WIN32
#ifdef AZ_PLATFORM_WINDOWS
time_t tt = _mkgmtime(&t);
#else
#elif defined(AZ_PLATFORM_POSIX)
time_t tt = timegm(&t);
#endif
return std::chrono::system_clock::from_time_t(tt);