From d88db0975210366b381dfdd6d78e475e57d80048 Mon Sep 17 00:00:00 2001
From: Gabriel Ravier <gabrielravier@gabrielAncientIBMv2>
Date: Wed, 29 Jan 2020 19:30:18 +0100
Subject: [PATCH] Added CMake option for native optimizations (-march=native)

Signed-off-by: Gabriel Ravier <gabrielravier@gabrielAncientIBMv2>
---
 CMakeLists.txt | 13 +++++++++++++
 README.md      |  1 +
 2 files changed, 14 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0ab79edc..e345855f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -16,6 +16,8 @@ option(DEBUG_SAVE "Re-enable the ability to drag-and-drop save files onto the wi
 set(RENDERER "SDLTexture" CACHE STRING "Which renderer the game should use: 'OpenGL3' for an OpenGL 3.2 renderer, 'OpenGLES2' for an OpenGL ES 2.0 renderer, 'SDLTexture' for SDL2's hardware-accelerated Texture API, 'SDLSurface' for SDL2's software-rendered Surface API, or 'Software' for a handwritten software renderer")
 
 option(LTO "Enable link-time optimisation" OFF)
+option(NATIVE_OPTIMIZATIONS "Enable processor-specific optimisations (executable might not work on other architectures) (GCC-compatible compilers only)" OFF)
+
 option(WARNINGS "Enable common compiler warnings (for GCC-compatible compilers and MSVC only)" OFF)
 option(WARNINGS_ALL "Enable ALL compiler warnings (for Clang and MSVC only)" OFF)
 option(WARNINGS_FATAL "Stop compilation on any compiler warning (for GCC-compatible compilers and MSVC only)" OFF)
@@ -450,6 +452,17 @@ if(LTO)
 	endif()
 endif()
 
+# Enable -march=native if available
+if(NATIVE_OPTIMIZATIONS)
+	include(CheckCXXCompilerFlag)
+	CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE)
+	if(COMPILER_SUPPORTS_MARCH_NATIVE)
+		target_compile_options(CSE2 PRIVATE -march=native)
+	else()
+		message(WARNING "Couldn't activate native optimizations ! (Unsupported compiler)")
+	endif()
+endif()
+
 # Find dependencies
 
 if(NOT FORCE_LOCAL_LIBS)
diff --git a/README.md b/README.md
index 4de9cb95..21613d15 100644
--- a/README.md
+++ b/README.md
@@ -61,6 +61,7 @@ You can also add the following flags:
 Name | Function
 --------|--------
 `-DLTO=ON` | Enable link-time optimisation
+`-DNATIVE_OPTIMIZATIONS=ON` | Enable processor-specific optimisations (executable might not work on other architectures) (GCC-compatible compilers only)
 `-DJAPANESE=ON` | Enable the Japanese-language build (instead of the unofficial Aeon Genesis English translation)
 `-DFIX_BUGS=ON` | Fix various bugs in the game
 `-DDEBUG_SAVE=ON` | Re-enable the ability to drag-and-drop save files onto the window