From 92692c166625bcee48fa180dfcd287d9127191cb Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 24 Jun 2022 01:47:03 +0300
Subject: [PATCH 34/50] qt5.m4: Disable PIE when enabling PIC

Qt headers that require PIC also don't accept PIE.
When we disable PIE inside freeciv bootstrap just for Qt-facing
parts of code user is not required to disabled it from
the entire build.

The problem was seen on build of hardened freeciv
debian package on x32.

See osdn #44917

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 m4/qt5.m4 | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/m4/qt5.m4 b/m4/qt5.m4
index f5ce7f7abc..0ad30b96ff 100644
--- a/m4/qt5.m4
+++ b/m4/qt5.m4
@@ -95,9 +95,17 @@ AC_DEFUN([FC_QT5_COMPILETEST],
      CXXFLAGS="${CXXFLAGS} -fPIC"
      AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <QApplication>]],
 [[int a; QApplication app(a, 0);]])],
-      [qt5_headers=yes
-       FC_QT5_CPPFLAGS="${FC_QT5_CPPFLAGS}${CPPFADD}"
-       FC_QT5_CXXFLAGS="${FC_QT5_CXXFLAGS} -fPIC"])
+       [qt5_headers=yes
+        FC_QT5_CPPFLAGS="${FC_QT5_CPPFLAGS}${CPPFADD}"
+        FC_QT5_CXXFLAGS="${FC_QT5_CXXFLAGS} -fPIC"
+        dnl So, Qt requires -fPIC. At the same time it might conflict with -fPIE
+        dnl Try to disable pie (but even if that fails, we have -fPIC)
+        dnl Only add -no-pie if it works also with -Werror
+        CXXFLAGS="${CXXFLAGS} -no-pie -Werror"
+        AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <QApplication>]],
+[[int a; QApplication app(a, 0);]])],
+          [FC_QT5_CXXFLAGS="${FC_QT5_CXXFLAGS} -no-pie"])
+       ])
      CXXFLAGS="${CXXFLAGS_SAVE}"])
 
   CPPFLAGS="$CPPFLAGS_SAVE"
-- 
2.35.1