From c5a19a2599298da6c1fc3d3602a1d9ef787704c6 Mon Sep 17 00:00:00 2001
From: Pierre Rossi <pierre.rossi@digia.com>
Date: Fri, 24 May 2013 17:20:33 +0200
Subject: [PATCH] Ease the switching between Debug and Release builds

Change the semantics of getOutDir, introduce getConfigDir.
Rely on Gyp's configurations mechanism to get a sensible
path for the BLINQ_PROCESS_PATH in both cases.
Also make the build smarter in that re-generation of gyp files
will lead to re-gyping.
---
 README.rst                                     |  4 ++--
 blinq.pro                                      | 11 +++++++++++
 build/build.pro                                |  6 +-----
 build/qmake/mkspecs/features/default_pre.prf   |  4 ++++
 build/qmake/mkspecs/features/functions.prf     |  9 +++++++--
 build/qmake/mkspecs/features/gyp_generator.prf | 17 +++++++++++++++++
 example/example.pro                            |  2 +-
 lib/lib.pro                                    |  6 ++++--
 8 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/README.rst b/README.rst
index 7f255880a..ce627e880 100644
--- a/README.rst
+++ b/README.rst
@@ -21,8 +21,8 @@ This is a prototype of allowing embedding Chromium/Blink into Qt.
     * set the CHROMIUM_SRC_DIR environment variable to point to /path/to/src/
     * Simply run qmake in the top-level directory (it will call ninja behind the scenes).
     Use qmake -r to forcefully re-gyp (without relying on make to determine if it's necessary).
-    * switching between a debug and a release build can still be a bit cumbersome, for now it seems:
-        rm **/Makefile && qmake -r CONFIG+=<debug or release> depending on what's needed should work well
+    * Release or debug builds can be obtained by running 'make release' or 'make debug' in the
+      top level directory (only lib and process for now, and not so smart with dependencies)
 
 (4) build with make ;)
 
diff --git a/blinq.pro b/blinq.pro
index cf5b1f5cd..a745302de 100644
--- a/blinq.pro
+++ b/blinq.pro
@@ -9,3 +9,14 @@ SUBDIRS = lib \
           build \ # This is where we use the generated qt_generated.gypi and run gyp
           example \
 
+# Extra targets that invoke ninja on the desired configuration added for convenience
+release.target = release
+release.commands = $$CHROMIUM_SRC_DIR/../depot_tools/ninja -C $$getOutDir()/Release
+release.depends: qmake
+
+debug.target = debug
+debug.commands = $$CHROMIUM_SRC_DIR/../depot_tools/ninja -C $$getOutDir()/Debug
+debug.depends: qmake
+
+QMAKE_EXTRA_TARGETS += release \
+                       debug
diff --git a/build/build.pro b/build/build.pro
index 83c074d60..0c3c87ba3 100644
--- a/build/build.pro
+++ b/build/build.pro
@@ -4,16 +4,12 @@
 
 TEMPLATE = aux
 
-# Fetched from environment for now
-CHROMIUM_SRC_DIR = $$(CHROMIUM_SRC_DIR)
-isEmpty(CHROMIUM_SRC_DIR):error("please set CHOMIUM_SRC_DIR")
-
 message(Running Gyp...)
 GYP_OUTPUT = $$system(./gyp_blinq)
 message($$GYP_OUTPUT)
 
 ninja.target = ninja
-ninja.commands = $$CHROMIUM_SRC_DIR/../depot_tools/ninja -C $$getOutDir()
+ninja.commands = $$CHROMIUM_SRC_DIR/../depot_tools/ninja -C $$getOutDir()/$$getConfigDir()
 ninja.depends: qmake
 QMAKE_EXTRA_TARGETS += ninja
 
diff --git a/build/qmake/mkspecs/features/default_pre.prf b/build/qmake/mkspecs/features/default_pre.prf
index 6a2c6f7f5..6d70a06ab 100644
--- a/build/qmake/mkspecs/features/default_pre.prf
+++ b/build/qmake/mkspecs/features/default_pre.prf
@@ -3,6 +3,10 @@ BLINQ_ROOT = $$replace(PWD, /build/qmake/mkspecs/features$,)
 
 BLINQ_PROCESS_NAME = blinq_process
 
+# Fetched from environment for now
+CHROMIUM_SRC_DIR = $$(CHROMIUM_SRC_DIR)
+isEmpty(CHROMIUM_SRC_DIR):error("please set CHOMIUM_SRC_DIR")
+
 load(functions)
 # So that moc generated files don't end up polluting the source tree
 OUT_PWD = $$getOutDir()
diff --git a/build/qmake/mkspecs/features/functions.prf b/build/qmake/mkspecs/features/functions.prf
index 5064ec441..bb1e152a2 100644
--- a/build/qmake/mkspecs/features/functions.prf
+++ b/build/qmake/mkspecs/features/functions.prf
@@ -9,8 +9,13 @@ defineReplace(toGypTargetType) {
 }
 
 defineReplace(getOutDir) {
-  CONFIG(release, debug|release):return("$$BLINQ_ROOT/out/Release")
-  return("$$BLINQ_ROOT/out/Debug")
+  # FIXME: rely on env variable in here and in the gyp_blinq script, à la WEBKITOUTPUTDIR
+  return("$$BLINQ_ROOT/out")
+}
+
+defineReplace(getConfigDir) {
+  CONFIG(release, debug|release):return("Release")
+  return("Debug")
 }
 
 defineReplace(findMocables) {
diff --git a/build/qmake/mkspecs/features/gyp_generator.prf b/build/qmake/mkspecs/features/gyp_generator.prf
index 4d46f84ec..47aafd258 100644
--- a/build/qmake/mkspecs/features/gyp_generator.prf
+++ b/build/qmake/mkspecs/features/gyp_generator.prf
@@ -55,6 +55,20 @@ GYPI_CONTENTS += "    ],"
   for (define, DEFINES): GYPI_CONTENTS += "      '$$define',"
   GYPI_CONTENTS += "    ],"
 }
+!isEmpty(PER_CONFIG_DEFINES) {
+  GYPI_CONTENTS += "    'configurations': {"\
+                   "      'Release': {" \
+                   "        'defines': ["
+  for (define, PER_CONFIG_DEFINES): GYPI_CONTENTS += "          '$$replace(define,%config,Release)',"
+  GYPI_CONTENTS += "        ]," \
+                   "      }," \
+                   "      'Debug': {" \
+                   "        'defines': ["
+  for (define, PER_CONFIG_DEFINES): GYPI_CONTENTS += "          '$$replace(define,%config,Debug)',"
+  GYPI_CONTENTS += "        ]," \
+                   "      }," \
+                   "    },"
+}
 GYPI_CONTENTS += "    'sources': ["
 for (sourcefile, SOURCES): GYPI_CONTENTS += "      '$$sourcefile',"
 for (headerfile, HEADERS): GYPI_CONTENTS += "      '$$headerfile',"
@@ -80,6 +94,9 @@ GYPI_CONTENTS += "  }," \
 
 write_file($$GYPI_FILE, GYPI_CONTENTS)
 
+# Overwriting the generated gyp file seems like a good reason to re-gyp
+unix: phony_variable_name_for_qmake_to_be_happy=$$system("touch $$BLINQ_ROOT/build/build.pro")
+
 # The generated Makefile shouldn't build anything by itself, just re-run qmake if necessary
 TEMPLATE = aux
 SOURCES =
diff --git a/example/example.pro b/example/example.pro
index c727b06aa..7c76bb9a7 100644
--- a/example/example.pro
+++ b/example/example.pro
@@ -5,7 +5,7 @@ SOURCES = main.cpp
 
 INCLUDEPATH += ../lib
 
-LIBPATH = $$getOutDir()/lib
+LIBPATH = $$getOutDir()/$$getConfigDir()/lib
 
 LIBS += -L$$LIBPATH -lblinq
 QMAKE_RPATHDIR += $$LIBPATH
diff --git a/lib/lib.pro b/lib/lib.pro
index fbd5bbb13..48fe29fcd 100644
--- a/lib/lib.pro
+++ b/lib/lib.pro
@@ -8,8 +8,10 @@ TEMPLATE = lib
 TARGET = blinq
 
 # Defining keywords such as 'signal' clashes with the chromium code base.
-DEFINES += QT_NO_KEYWORDS \
-           BLINQ_PROCESS_PATH=\\\"$$getOutDir()/$$BLINQ_PROCESS_NAME\\\"
+DEFINES += QT_NO_KEYWORDS
+
+# We need a way to tap into gyp's Debug vs. Release configuration
+PER_CONFIG_DEFINES = BLINQ_PROCESS_PATH=\\\"$$getOutDir()/%config/$$BLINQ_PROCESS_NAME\\\"
 
 # Keep Skia happy
 CONFIG(release, debug|release): DEFINES += NDEBUG
-- 
GitLab