From 629c11baad034f9a5f899d08b0c310421789c658 Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Tue, 17 Jul 2018 11:27:58 +0200 Subject: [PATCH] Add windows_with_undefs.h to fix conflicts with windows.h windows.h adds macros such as `#define GetMessage GetMessageA` which cause conflicts in naming when for example a class definition is before windows.h and a use is after windows.h Add a header that can be used both to include windows.h and remove defines, and to only remove defines (when windows.h is included by) external dependencies. --- src/backend/d3d12/d3d12_platform.h | 6 ++++++ src/backend/opengl/DeviceGL.h | 6 ++++++ src/common/CMakeLists.txt | 1 + src/common/DynamicLib.cpp | 2 +- src/common/vulkan_platform.h | 6 ++++++ src/common/windows_with_undefs.h | 31 ++++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/common/windows_with_undefs.h diff --git a/src/backend/d3d12/d3d12_platform.h b/src/backend/d3d12/d3d12_platform.h index e3f08008a9..ad89bf3286 100644 --- a/src/backend/d3d12/d3d12_platform.h +++ b/src/backend/d3d12/d3d12_platform.h @@ -25,4 +25,10 @@ using Microsoft::WRL::ComPtr; +// Remove windows.h macros after d3d12's include of windows.h +#include "common/Platform.h" +#if defined(NXT_PLATFORM_WINDOWS) +# include "common/windows_with_undefs.h" +#endif + #endif // BACKEND_D3D12_D3D12PLATFORM_H_ diff --git a/src/backend/opengl/DeviceGL.h b/src/backend/opengl/DeviceGL.h index 38bec22e56..81b8b33974 100644 --- a/src/backend/opengl/DeviceGL.h +++ b/src/backend/opengl/DeviceGL.h @@ -19,9 +19,15 @@ #include "backend/Device.h" #include "backend/opengl/Forward.h" +#include "common/Platform.h" #include "glad/glad.h" +// Remove windows.h macros after glad's include of windows.h +#if defined(NXT_PLATFORM_WINDOWS) +# include "common/windows_with_undefs.h" +#endif + namespace backend { namespace opengl { class Device : public DeviceBase { diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index db0f642540..1c5672cdb6 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -30,6 +30,7 @@ list(APPEND COMMON_SOURCES ${COMMON_DIR}/SerialQueue.h ${COMMON_DIR}/SwapChainUtils.h ${COMMON_DIR}/vulkan_platform.h + ${COMMON_DIR}/windows_with_undefs.h ) add_library(nxt_common STATIC ${COMMON_SOURCES}) diff --git a/src/common/DynamicLib.cpp b/src/common/DynamicLib.cpp index 74eb3379a7..2579944cd1 100644 --- a/src/common/DynamicLib.cpp +++ b/src/common/DynamicLib.cpp @@ -17,7 +17,7 @@ #include "common/Platform.h" #if NXT_PLATFORM_WINDOWS -# include +# include "common/windows_with_undefs.h" #elif NXT_PLATFORM_POSIX # include #else diff --git a/src/common/vulkan_platform.h b/src/common/vulkan_platform.h index 1fd01f3616..13fe743918 100644 --- a/src/common/vulkan_platform.h +++ b/src/common/vulkan_platform.h @@ -87,4 +87,10 @@ class VkNonDispatchableHandle { # undef VK_NULL_HANDLE # define VK_NULL_HANDLE nullptr +// Remove windows.h macros after vulkan_platform's include of windows.h +#include "common/Platform.h" +#if defined(NXT_PLATFORM_WINDOWS) +# include "common/windows_with_undefs.h" +#endif + #endif // COMMON_VULKANPLATFORM_H_ diff --git a/src/common/windows_with_undefs.h b/src/common/windows_with_undefs.h new file mode 100644 index 0000000000..fdbef8fbe8 --- /dev/null +++ b/src/common/windows_with_undefs.h @@ -0,0 +1,31 @@ +// Copyright 2018 The NXT Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef COMMON_WINDOWS_WITH_UNDEFS_H_ +#define COMMON_WINDOWS_WITH_UNDEFS_H_ + +#include "common/Compiler.h" + +#if !defined(NXT_PLATFORM_WINDOWS) +# error "windows_with_undefs.h included on non-Windows" +#endif + +// This header includes but removes all the extra defines that conflict with identifiers +// in internal code. It should never be included in something that is part of the public interface. +#include + +// Macros defined for ANSI / Unicode support +#undef GetMessage + +#endif // COMMON_WINDOWS_WITH_UNDEFS_H_