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.
This commit is contained in:
Corentin Wallez 2018-07-17 11:27:58 +02:00 committed by Corentin Wallez
parent 6aaa10fa60
commit 629c11baad
6 changed files with 51 additions and 1 deletions

View File

@ -25,4 +25,10 @@
using Microsoft::WRL::ComPtr; 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_ #endif // BACKEND_D3D12_D3D12PLATFORM_H_

View File

@ -19,9 +19,15 @@
#include "backend/Device.h" #include "backend/Device.h"
#include "backend/opengl/Forward.h" #include "backend/opengl/Forward.h"
#include "common/Platform.h"
#include "glad/glad.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 { namespace backend { namespace opengl {
class Device : public DeviceBase { class Device : public DeviceBase {

View File

@ -30,6 +30,7 @@ list(APPEND COMMON_SOURCES
${COMMON_DIR}/SerialQueue.h ${COMMON_DIR}/SerialQueue.h
${COMMON_DIR}/SwapChainUtils.h ${COMMON_DIR}/SwapChainUtils.h
${COMMON_DIR}/vulkan_platform.h ${COMMON_DIR}/vulkan_platform.h
${COMMON_DIR}/windows_with_undefs.h
) )
add_library(nxt_common STATIC ${COMMON_SOURCES}) add_library(nxt_common STATIC ${COMMON_SOURCES})

View File

@ -17,7 +17,7 @@
#include "common/Platform.h" #include "common/Platform.h"
#if NXT_PLATFORM_WINDOWS #if NXT_PLATFORM_WINDOWS
# include <windows.h> # include "common/windows_with_undefs.h"
#elif NXT_PLATFORM_POSIX #elif NXT_PLATFORM_POSIX
# include <dlfcn.h> # include <dlfcn.h>
#else #else

View File

@ -87,4 +87,10 @@ class VkNonDispatchableHandle {
# undef VK_NULL_HANDLE # undef VK_NULL_HANDLE
# define VK_NULL_HANDLE nullptr # 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_ #endif // COMMON_VULKANPLATFORM_H_

View File

@ -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 <windows.h> 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 <windows.h>
// Macros defined for ANSI / Unicode support
#undef GetMessage
#endif // COMMON_WINDOWS_WITH_UNDEFS_H_