From e47705d8fb2cfe1ce89e68d68e8591e1827d6725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Mon, 25 May 2020 16:33:44 +0200 Subject: [PATCH] Move more compiler workarounds to library setup These are details of the glfw CMake target and belong in the creation of that target. --- CMakeLists.txt | 49 -------------------------------------------- src/CMakeLists.txt | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 49 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b5755827..9f7ad4ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,17 +66,6 @@ endif() # Set compiler specific flags #-------------------------------------------------------------------- if (MSVC) - if (MSVC90) - # Workaround for VS 2008 not shipping with the DirectX 9 SDK - include(CheckIncludeFile) - check_include_file(dinput.h DINPUT_H_FOUND) - if (NOT DINPUT_H_FOUND) - message(FATAL_ERROR "DirectX 9 headers not found; install DirectX 9 SDK") - endif() - # Workaround for VS 2008 not shipping with stdint.h - list(APPEND glfw_INCLUDE_DIRS "${GLFW_SOURCE_DIR}/deps/vs2008") - endif() - if (NOT USE_MSVC_RUNTIME_LIBRARY_DLL) foreach (flag CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG @@ -95,44 +84,6 @@ if (MSVC) endif() endif() -if (MINGW) - # Workaround for legacy MinGW not providing XInput and DirectInput - include(CheckIncludeFile) - - check_include_file(dinput.h DINPUT_H_FOUND) - check_include_file(xinput.h XINPUT_H_FOUND) - if (NOT DINPUT_H_FOUND OR NOT XINPUT_H_FOUND) - list(APPEND glfw_INCLUDE_DIRS "${GLFW_SOURCE_DIR}/deps/mingw") - endif() - - # Enable link-time exploit mitigation features enabled by default on MSVC - include(CheckCCompilerFlag) - - # Compatibility with data execution prevention (DEP) - set(CMAKE_REQUIRED_FLAGS "-Wl,--nxcompat") - check_c_compiler_flag("" _GLFW_HAS_DEP) - if (_GLFW_HAS_DEP) - set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--nxcompat ${CMAKE_SHARED_LINKER_FLAGS}") - endif() - - # Compatibility with address space layout randomization (ASLR) - set(CMAKE_REQUIRED_FLAGS "-Wl,--dynamicbase") - check_c_compiler_flag("" _GLFW_HAS_ASLR) - if (_GLFW_HAS_ASLR) - set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--dynamicbase ${CMAKE_SHARED_LINKER_FLAGS}") - endif() - - # Compatibility with 64-bit address space layout randomization (ASLR) - set(CMAKE_REQUIRED_FLAGS "-Wl,--high-entropy-va") - check_c_compiler_flag("" _GLFW_HAS_64ASLR) - if (_GLFW_HAS_64ASLR) - set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--high-entropy-va ${CMAKE_SHARED_LINKER_FLAGS}") - endif() - - # Clear flags again to avoid breaking later tests - set(CMAKE_REQUIRED_FLAGS) -endif() - #-------------------------------------------------------------------- # Detect and select backend APIs #-------------------------------------------------------------------- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3fb2a54e..095a054b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -136,6 +136,28 @@ if (MINGW) target_compile_definitions(glfw PRIVATE UNICODE WINVER=0x0501) endif() +# Workaround for legacy MinGW not providing XInput and DirectInput +if (MINGW) + include(CheckIncludeFile) + check_include_file(dinput.h DINPUT_H_FOUND) + check_include_file(xinput.h XINPUT_H_FOUND) + if (NOT DINPUT_H_FOUND OR NOT XINPUT_H_FOUND) + target_include_directories(glfw PRIVATE "${GLFW_SOURCE_DIR}/deps/mingw") + endif() +endif() + +if (MSVC90) + # Workaround for VS 2008 not shipping with the DirectX 9 SDK + include(CheckIncludeFile) + check_include_file(dinput.h DINPUT_H_FOUND) + if (NOT DINPUT_H_FOUND) + message(FATAL_ERROR "DirectX 9 headers not found; install DirectX 9 SDK") + endif() + + # Workaround for VS 2008 not shipping with stdint.h + target_include_directories(glfw PRIVATE "${GLFW_SOURCE_DIR}/deps/vs2008") +endif() + if (BUILD_SHARED_LIBS) if (WIN32) if (MINGW) @@ -159,6 +181,35 @@ if (BUILD_SHARED_LIBS) INSTALL_NAME_DIR "${CMAKE_INSTALL_LIBDIR}") endif() + if (MINGW) + # Enable link-time exploit mitigation features enabled by default on MSVC + include(CheckCCompilerFlag) + + # Compatibility with data execution prevention (DEP) + set(CMAKE_REQUIRED_FLAGS "-Wl,--nxcompat") + check_c_compiler_flag("" _GLFW_HAS_DEP) + if (_GLFW_HAS_DEP) + target_link_libraries(glfw PRIVATE "-Wl,--nxcompat") + endif() + + # Compatibility with address space layout randomization (ASLR) + set(CMAKE_REQUIRED_FLAGS "-Wl,--dynamicbase") + check_c_compiler_flag("" _GLFW_HAS_ASLR) + if (_GLFW_HAS_ASLR) + target_link_libraries(glfw PRIVATE "-Wl,--dynamicbase") + endif() + + # Compatibility with 64-bit address space layout randomization (ASLR) + set(CMAKE_REQUIRED_FLAGS "-Wl,--high-entropy-va") + check_c_compiler_flag("" _GLFW_HAS_64ASLR) + if (_GLFW_HAS_64ASLR) + target_link_libraries(glfw PRIVATE "-Wl,--high-entropy-va") + endif() + + # Clear flags again to avoid breaking later tests + set(CMAKE_REQUIRED_FLAGS) + endif() + if (UNIX) # Hide symbols not explicitly tagged for export from the shared library target_compile_options(glfw PRIVATE "-fvisibility=hidden")