From efd2ea8ea720833f9602154221d9654aea1f2e6f Mon Sep 17 00:00:00 2001
From: Kai Koehne <kai.koehne@qt.io>
Date: Mon, 2 May 2016 17:09:11 +0200
Subject: [PATCH] Determine the compiler's default include and lib directories
 at qmake time

This fixes a long-standing issue for Qt packages, where the
paths detected at configure time do not necessarily match the
paths on the user's machine. Hence they have been stripped
manually from qconfig.pri so far, preventing moc from resolving
some includes.

The same logic in configure is left alone for the time being,
since the paths there are also used to filter paths returned
by pg_config and mysql_config. I expect that this will
eventually be removed too in a bigger refactoring going on
right now in dev.

Asking the compiler for implicit paths only works for non-msvc
builds - that is, gcc, clang and icc fortunately have a
compatible way to retrieve the paths. MSVC works
solely on environment variables, which will be taken into
account by a separate patch.

[ChangeLog][qmake] The implicit compiler directories that
moc needs for resolving include files are now determined
when qmake runs. So far QMAKE_DEFAULT_INCDIR was determined
at configure time, which might be wrong for relocated
installations.

Task-number: QTBUG-52687
Change-Id: If0706e8c56a5aca2b6e777e79e90342c498726f3
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
---
 configure                        |  9 -------
 mkspecs/features/default_pre.prf | 41 ++++++++++++++++++++++++++++++++
 tools/configure/configureapp.cpp |  5 ----
 3 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/configure b/configure
index 3e455c61639..43b55f07a3b 100755
--- a/configure
+++ b/configure
@@ -115,13 +115,6 @@ shellEscape()
     echo "$@" | sed 's/ /\ /g'
 }
 
-shellQuoteLines()
-{
-    # The call of the outer echo makes the shell word-split the output of
-    # the nested pipe, thus effectively converting newlines to spaces.
-    echo `echo "$1" | sed 's,^[^ ]* .*$,"&",'`
-}
-
 makeabs()
 {
     local FILE="$1"
@@ -7163,8 +7156,6 @@ host_build {
     QT_TARGET_ARCH = $CFG_ARCH
 } else {
     QT_ARCH = $CFG_ARCH
-    QMAKE_DEFAULT_LIBDIRS = `shellQuoteLines "$DEFAULT_LIBDIRS"`
-    QMAKE_DEFAULT_INCDIRS = `shellQuoteLines "$DEFAULT_INCDIRS"`
 }
 QT_CONFIG += $QT_CONFIG
 
diff --git a/mkspecs/features/default_pre.prf b/mkspecs/features/default_pre.prf
index a247b46a72e..cffffdcf255 100644
--- a/mkspecs/features/default_pre.prf
+++ b/mkspecs/features/default_pre.prf
@@ -24,3 +24,44 @@ contains(QT_CONFIG, c++11):lessThan(QT_COMPILER_STDCXX, 201103): CONFIG += c++11
     }
     unset(today)
 }
+
+isEmpty(QMAKE_DEFAULT_INCDIRS):!host_build {
+    #
+    # Get default include and library paths from compiler
+    #
+    gcc {
+        equals(QMAKE_DIR_SEP, /) {
+            cmd_prefix = "LC_ALL=C"
+            cmd_suffix = "</dev/null >/dev/null"
+        } else {
+            cmd_prefix = "set LC_ALL=C&"
+            cmd_suffix = "<NUL >NUL"
+        }
+        output = $$system("$$cmd_prefix $$QMAKE_CXX $$QMAKE_CXXFLAGS -xc++ -E -v - 2>&1 $$cmd_suffix", lines)
+        add_includes = false
+        for (line, output) {
+            line ~= s/^ *//  # remove leading spaces
+            contains(line, "LIBRARY_PATH=.*") {
+                line ~= s/^LIBRARY_PATH=//  # remove leading LIBRARY_PATH=
+                paths = $$split(line, $$QMAKE_DIRLIST_SEP)
+                for (path, paths): \
+                    QMAKE_DEFAULT_LIBDIRS += $$clean_path($$path)
+            } else: contains(line, "$${LITERAL_HASH}include <.*") {  # #include <...> search starts here:
+                add_includes = true
+            } else: contains(line, "End of search list.*") {
+                add_includes = false
+            } else {
+                $$add_includes: QMAKE_DEFAULT_INCDIRS += $$clean_path($$line)
+            }
+        }
+        QMAKE_DEFAULT_LIBDIRS = $$unique(QMAKE_DEFAULT_LIBDIRS)
+    }
+
+    unix {
+        isEmpty(QMAKE_DEFAULT_INCDIRS): QMAKE_DEFAULT_INCDIRS = /usr/include /usr/local/include
+        isEmpty(QMAKE_DEFAULT_LIBDIRS): QMAKE_DEFAULT_LIBDIRS = /lib /usr/lib
+    }
+
+    !isEmpty(QMAKE_DEFAULT_INCDIRS): cache(QMAKE_DEFAULT_INCDIRS, set stash)
+    !isEmpty(QMAKE_DEFAULT_LIBDIRS): cache(QMAKE_DEFAULT_LIBDIRS, set stash)
+}
diff --git a/tools/configure/configureapp.cpp b/tools/configure/configureapp.cpp
index e3eb5220bec..d490f73496a 100644
--- a/tools/configure/configureapp.cpp
+++ b/tools/configure/configureapp.cpp
@@ -3496,11 +3496,6 @@ void Configure::generateQConfigPri()
         configStream << "    QT_TARGET_ARCH = " << dictionary["QT_ARCH"] << endl;
         configStream << "} else {" << endl;
         configStream << "    QT_ARCH = " << dictionary["QT_ARCH"] << endl;
-        if (dictionary.contains("XQMAKESPEC")) {
-            // FIXME: add detection
-            configStream << "    QMAKE_DEFAULT_LIBDIRS = /lib /usr/lib" << endl;
-            configStream << "    QMAKE_DEFAULT_INCDIRS = /usr/include /usr/local/include" << endl;
-        }
         configStream << "}" << endl;
         configStream << "QT_CONFIG += " << qtConfig.join(' ') << endl;
 
-- 
GitLab