From 25d53ecd2cc9228f651e852184020c5bc0481225 Mon Sep 17 00:00:00 2001
From: "alexey.lysiuk" <alexey.lysiuk@gmail.com>
Date: Fri, 29 Dec 2017 09:39:01 +0200
Subject: [PATCH] Enabled position independent executable for generic GCC/Clang

This affects mostly Linux (and probably MinGW) as default toolchains for Windows and macOS have the corresponding option turned on by default
---
 CMakeLists.txt | 6 ++++++
 src/m_fixed.h  | 2 +-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 345fd480d..5809118d8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -234,6 +234,12 @@ else()
 	if( APPLE AND "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" )
 		set( ALL_C_FLAGS "-static-libgcc" )
 	endif()
+	
+	if( NOT APPLE )
+		# Generic GCC/Clang requires position independent executable to be enabled explicitly
+		set( ALL_C_FLAGS "${ALL_C_FLAGS} -fPIE" )
+		set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie" )
+	endif()
 endif()
 
 set( CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${REL_LINKER_FLAGS}" )
diff --git a/src/m_fixed.h b/src/m_fixed.h
index b751d6b1f..8bbb2dbf9 100644
--- a/src/m_fixed.h
+++ b/src/m_fixed.h
@@ -7,7 +7,7 @@
 // Unfortunately, the Scale function still gets badly handled on 32 bit x86 platforms so it's the last remaining piece of inline assembly 
 
 // GCC inlines
-#if defined(__GNUC__) && defined(__i386__) && !defined(__clang__)
+#if defined(__GNUC__) && defined(__i386__) && !defined(__clang__) && !defined(__PIC__)
 #ifndef alloca
 // MinGW does not seem to come with alloca defined.
 #define alloca __builtin_alloca