From 9efb45a0e14bfdb801d542c02ff02fc326787cb3 Mon Sep 17 00:00:00 2001
From: Jocelyn Turcotte <jocelyn.turcotte@digia.com>
Date: Fri, 5 Sep 2014 19:03:39 +0200
Subject: [PATCH] Generate moc and rcc files in the gyp build directory

The real problem that this patch fixes is that moc and rcc generated
files are added to the gyp sources variable with an absolute path,
which for some reason triggers gyp to place the output file in the
same directory as the source instead of the Release or Debug
directories. This causes the last compiled file to be shared by both
the release and debug builds when building debug-and-release, and
MSVC will fail linking if the linked files have incompatible CRTs.

This patch fixes the issue by moving both the generated cpp files
and their compiled objects under <(SHARED_INTERMEDIATE_DIR), which
points to (Release|Debug)/gen.

Change-Id: I4143340acf56c3c7ed665aaae8f6221c310aafa9
Reviewed-by: Andras Becsi <andras.becsi@digia.com>
Reviewed-by: Michael Bruning <michael.bruning@digia.com>
---
 .../qmake/mkspecs/features/gyp_generator.prf  | 22 ++++++++++---------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/tools/qmake/mkspecs/features/gyp_generator.prf b/tools/qmake/mkspecs/features/gyp_generator.prf
index 0b5a6ab6a..5ef3110cc 100644
--- a/tools/qmake/mkspecs/features/gyp_generator.prf
+++ b/tools/qmake/mkspecs/features/gyp_generator.prf
@@ -6,6 +6,9 @@ load(functions)
 load(moc)
 load(resources)
 
+MOC_GEN_DIR = <(SHARED_INTERMEDIATE_DIR)/moc
+RCC_GEN_DIR = <(SHARED_INTERMEDIATE_DIR)/rcc
+
 defineReplace(mocAction) {
     INPUT_FILE = $$1
     OUTPUT_NAME = $$mocOutput($$INPUT_FILE)
@@ -15,7 +18,7 @@ defineReplace(mocAction) {
     MOC_COMMAND = $$replace(MOC_COMMAND, $$re_escape("$(DEFINES)"), $$DEFINES_LIST)
     MOC_COMMAND = $$replace(MOC_COMMAND, $$re_escape("$(INCPATH)"), $$INCPATH)
     MOC_COMMAND = $$split(MOC_COMMAND, " ")
-    OUTPUT_FILE = $$absolute_path($$MOC_DIR, $$OUT_PWD)$${QMAKE_DIR_SEP}$${OUTPUT_NAME}
+    OUTPUT_FILE = $$MOC_GEN_DIR/$${OUTPUT_NAME}
     contents = "      {" \
                "        'action_name':'$$OUTPUT_NAME'," \
                "        'inputs': ['$$INPUT_FILE',]," \
@@ -35,7 +38,7 @@ defineReplace(rccAction) {
     INPUT_FILE = $$1
     OUTPUT_NAME = $$rccOutput($$INPUT_FILE)
     EXTERN_FUNC = $$rccExternFunc($$INPUT_FILE)
-    OUTPUT_FILE = $$absolute_path($$RCC_DIR, $$OUT_PWD)$${QMAKE_DIR_SEP}$${OUTPUT_NAME}
+    OUTPUT_FILE = $$RCC_GEN_DIR/$${OUTPUT_NAME}
     contents = "      {" \
                "        'action_name':'$$OUTPUT_NAME'," \
                "        'inputs': ['$$INPUT_FILE',]," \
@@ -110,24 +113,23 @@ for (headerfile, HEADERS): GYP_CONTENTS += "      '$$headerfile',"
 for (resourcefile, RESOURCES) {
   RCC_CPP = $$replace(resourcefile, .qrc, .cpp)
   RCC_CPP = $$join(RCC_CPP, "qrc_", qrc_)
-  RCC_CPP = $$absolute_path($$RCC_DIR, $$OUT_PWD)$${QMAKE_DIR_SEP}$${RCC_CPP}
+  RCC_CPP = $$RCC_GEN_DIR/$${RCC_CPP}
   GYP_CONTENTS += "      '$$RCC_CPP',"
 }
 
 # Add moc output files to compile that aren't included at the end of any other source
-MOC_OUT_PATH = $$absolute_path($$MOC_DIR, $$OUT_PWD)$${QMAKE_DIR_SEP}
 for (mocable, MOCABLES) {
     !contains(INCLUDED_MOC_FILES, $$mocOutput($$mocable)) {
-        GYP_CONTENTS += "      '$$MOC_OUT_PATH$$mocOutput($$mocable)',"
+        GYP_CONTENTS += "      '$$MOC_GEN_DIR/$$mocOutput($$mocable)',"
     }
 }
 
 GYP_CONTENTS += "    ],"
-!isEmpty(INCLUDEPATH) {
-  GYP_CONTENTS += "    'include_dirs': ["
-  for (path, INCLUDEPATH): GYP_CONTENTS += "      '$$path',"
-  GYP_CONTENTS += "    ],"
-}
+GYP_CONTENTS += "    'include_dirs': ["
+for (path, INCLUDEPATH): GYP_CONTENTS += "      '$$path',"
+# qmake already added MOC_DIR to INCLUDEPATH, but we're telling gyp to use a different one.
+GYP_CONTENTS += "      '$$MOC_GEN_DIR',"
+GYP_CONTENTS += "    ],"
 
 # Generate the actions for moc and rcc
 GYP_CONTENTS += "    'actions': ["
-- 
GitLab