--- mkspecs/features/qt_functions.prf.orig
+++ mkspecs/features/qt_functions.prf
@@ -16,35 +16,108 @@
 }
 
 defineTest(qtAddLibrary) {
+#    message(qtAddLibrary: 'LIBS' before $$1 is '$${LIBS}')
+#    message(qtAddLibrary: 'INCLUDEPATH' before $$1 is '$${INCLUDEPATH}')
+#    message(qtAddLibrary: 'QMAKE_LFLAGS' before $$1 is '$${QMAKE_LFLAGS}')
+
+    # reorder the includes path: user, this library, Qt headers
+    INCLUDEPATH -= $$QMAKE_INCDIR_QT
     INCLUDEPATH -= $$QMAKE_INCDIR_QT/$$1
-    INCLUDEPATH = $$QMAKE_INCDIR_QT/$$1 $$INCLUDEPATH
+    INCLUDEPATH += $$QMAKE_INCDIR_QT/$$1 $$QMAKE_INCDIR_QT
+
+    # first time through, append the path for Qt's installed libraries:
+    # -after- all of the other necessary libraries and paths
+
+    mac:!static:contains(QT_CONFIG, qt_framework) {
+      isEmpty(QMAKE_FRAMEWORKDIR_QT) {
+         !contains(LIBS,-F$$[QT_INSTALL_PREFIX]/Library/Frameworks) {
+	   LIBS = $$LIBS -F$$[QT_INSTALL_PREFIX]/Library/Frameworks
+	 }
+      } else {
+        !contains(LIBS,-F$$QMAKE_FRAMEWORKDIR_QT) {
+ 	  LIBS = $$LIBS -F$$QMAKE_FRAMEWORKDIR_QT
+	}
+      }
+      isEmpty(QMAKE_LIBDIR_QT) {
+         !contains(LIBS,-F$$[QT_INSTALL_PREFIX]/lib) {
+	   LIBS = $$LIBS -F$$[QT_INSTALL_PREFIX]/lib
+	 }
+      } else {
+        !contains(LIBS,-F$$QMAKE_LIBDIR_QT) {
+ 	  LIBS = $$LIBS -F$$QMAKE_LIBDIR_QT
+	}
+      }
+    }
+    isEmpty(QMAKE_LIBDIR_QT) {
+       !contains(LIBS,-L$$[QT_INSTALL_PREFIX]/lib) {
+         LIBS = $$LIBS -L$$[QT_INSTALL_PREFIX]/lib
+       }
+    } else {
+      !contains(LIBS,-L$$QMAKE_LIBDIR_QT) {
+        LIBS = $$LIBS -L$$QMAKE_LIBDIR_QT
+      }
+    }
 
     LIB_NAME = $$1
     unset(LINKAGE)
-    mac {
-       CONFIG(qt_framework, qt_framework|qt_no_framework) { #forced
-          QMAKE_FRAMEWORKPATH *= $${QMAKE_LIBDIR_QT}
-          FRAMEWORK_INCLUDE = $$QMAKE_LIBDIR_QT/$${LIB_NAME}.framework/Headers
-          !qt_no_framework_direct_includes:exists($$FRAMEWORK_INCLUDE) {
-             INCLUDEPATH -= $$FRAMEWORK_INCLUDE
-             INCLUDEPATH = $$FRAMEWORK_INCLUDE $$INCLUDEPATH
-           }
-           LINKAGE = -framework $${LIB_NAME}$${QT_LIBINFIX}
-        } else:!qt_no_framework { #detection
-           for(frmwrk_dir, $$list($$QMAKE_LIBDIR_QT $$QMAKE_LIBDIR $$(DYLD_FRAMEWORK_PATH) /Library/Frameworks)) {
-       	      exists($${frmwrk_dir}/$${LIB_NAME}.framework) {
-                QMAKE_FRAMEWORKPATH *= $${frmwrk_dir}
+
+    # see if the framework, and exists in the current known paths
+
+    # add LIBS entries QMAKE_FRAMEWORKPATH, as appropriate
+
+    all_frmwrks = $$find(LIBS, -F.*) $$find(QMAKE_LFLAGS, -F.*)
+#    message(qtAddLibrary: all frameworks is $$all_frmwrks)
+
+    for(frmwrk_dir, all_frmwrks) {
+
+#      message(qtAddLibrary: this frmwrk_dir is $$frmwrk_dir)
+      tfp = $$frmwrk_dir
+      tfp ~= s,-F,,
+      QMAKE_FRAMEWORKPATH *= $$tfp
+#      message(qtAddLibrary: this frmwrk_path is $$tfp)
+
+    }
+
+    mac:!qt_no_framework {
+       for(frmwrk_dir, $$list($$QMAKE_FRAMEWORKPATH $$QMAKE_FRAMEWORKDIR_QT $$QMAKE_LIBDIR_QT $$QMAKE_LIBDIR $$(DYLD_FRAMEWORK_PATH))) {
+
+#           message(qtAddLibrary: looking for framework $$LIB_NAME in directory $$frmwrk_dir)
+           exists($${frmwrk_dir}/$${LIB_NAME}.framework) {
+
+                message(qtAddLibrary: found framework $$LIB_NAME in directory $$frmwrk_dir)
     	        FRAMEWORK_INCLUDE = $$frmwrk_dir/$${LIB_NAME}.framework/Headers
     	        !qt_no_framework_direct_includes:exists($$FRAMEWORK_INCLUDE) {
     	          INCLUDEPATH -= $$FRAMEWORK_INCLUDE
     	          INCLUDEPATH = $$FRAMEWORK_INCLUDE $$INCLUDEPATH
                 }
     	        LINKAGE = -framework $${LIB_NAME}
+
+# Apple's ld has provided "-framework name,option" since at least
+# 10.4, allowing linking with a framework's option name if it exists
+# -- but defaulting to the basic name if it exists (and if neither
+# exists, then generating an error).  All GCC, LLVM, CLANG, and Apple
+# compilers available in Xcode or MacPorts back to 10.4 seem to parse
+# this feature correctly when compiling, as well as pass it through to
+# Apple's ld.  Apple used to provide debug kernel frameworks and
+# libraries that could be used by setting the shell environment variable:
+#    DYLD_IMAGE_SUFFIX=_debug
+# but Apple hasn't done so since 10.6, and does not look to be doing
+# so any time soon.  Hence, the below use of the optional framework
+# name is really the best way to link to and use debug frameworks.  For
+# QtWebKit, which has no debug version, only the main library will
+# ever be found and used.
+
+		if(!debug_and_release|build_pass):CONFIG(debug, debug|release) {
+			LINKAGE = $${LINKAGE},_debug
+		}
        	        break()
+
               }
-           }
-       }
+         }
     }
+
+    # special for symbian
+
     symbian {
         isEqual(LIB_NAME, QtCore) {
             #workaround for dependency from f32file.h on e32svr.h which has moved location in symbian3
@@ -70,10 +143,56 @@
         export(TARGET.EPOCHEAPSIZE)
         export(TARGET.CAPABILITY)
     }
+
     isEmpty(LINKAGE) {
+
+      # not a framework or symbian
+      # see if the library exists in the current known paths
+
+      # add LIBS entries QMAKE_LIBPATH, as appropriate
+
+      all_libs = $$find(LIBS, -L.*) $$find(QMAKE_LFLAGS, -L.*)
+#      message(qtAddLibrary: all library paths is $$all_libs)
+
+      for(lib_dir, all_libs) {
+
+#        message(qtAddLibrary: this lib_dir is $$lib_dir)
+        tlp = $$lib_dir
+        tlp ~= s,-L,,
+        QMAKE_LIBPATH *= $$tlp
+#        message(qtAddLibrary: this lib_path is $$tlp)
+
+      }
+
+      for(lib_dir, $$list($$QMAKE_LIBPATH $$QMAKE_LIBDIR_QT $$QMAKE_LIBDIR)) {
+
+#           message(qtAddLibrary: looking for library $${LIB_NAME}$${QT_LIBINFIX} in directory $$lib_dir)
+           exists($${lib_dir}/lib$${LIB_NAME}$${QT_LIBINFIX}.*) {
+
+                message(qtAddLibrary: found library $${LIB_NAME}$${QT_LIBINFIX} in directory $$lib_dir)
+    	        LINKAGE = -l$${LIB_NAME}$${QT_LIBINFIX}
+       	        break()
+
+              }
+         }
+    }
+
+    isEmpty(LINKAGE) {
+
+       message(WARNING: Could not find library or framework $$LIB_NAME in the current known search directories; assuming a default library.)
+
        if(!debug_and_release|build_pass):CONFIG(debug, debug|release) {
            win32:LINKAGE = -l$${LIB_NAME}$${QT_LIBINFIX}d
-           mac:LINKAGE = -l$${LIB_NAME}$${QT_LIBINFIX}_debug
+	   # special for mac: cannot build QtWebKit as debug,
+	   # no-framework, and universal.  If things got here,
+	   # then this is no-framwork; just take care of the rest.
+	   mac {
+	       contains(QT_CONFIG, x86):contains(QT_CONFIG, x86_64):isEqual(LIB_NAME, QtWebKit) {
+	           LINKAGE = -l$${LIB_NAME}$${QT_LIBINFIX}
+	       } else {
+                   LINKAGE = -l$${LIB_NAME}$${QT_LIBINFIX}_debug
+               }
+           }
        }
        isEmpty(LINKAGE):LINKAGE = -l$${LIB_NAME}$${QT_LIBINFIX}
     }
@@ -83,9 +202,13 @@
         QMAKE_LFLAGS *= --lsb-shared-libs=$${LIB_NAME}$${QT_LIBINFIX}
     }
     LIBS += $$LINKAGE
+#    message(qtAddLibrary: 'LIBS' after $$1 is now '$${LIBS}')
+#    message(qtAddLibrary: 'INCLUDEPATH' after $$1 is now '$${INCLUDEPATH}')
+#    message(qtAddLibrary: 'QMAKE_LFLAGS' after $$1 is now '$${QMAKE_LFLAGS}')
     export(LIBS)
     export(INCLUDEPATH)
     export(QMAKE_FRAMEWORKPATH)
+    export(QMAKE_LIBPATH)
     export(QMAKE_LFLAGS)
     return(true)
 }
@@ -119,4 +242,3 @@
 
     return(true)
 }
-