i965: Build the driver into a shared mesa_dri_drivers.so .

Previously, we've split things such that mesa core is in libdricore,
exposing the whole Mesa core interface in the global namespace, and the
i965_dri.so code all links against that.  Along with polluting application
namespace terribly, it requires extra PLT indirections and prevents LTO.

Instead, we can build all of the driver contents into the same .so with
just a few symbols exposed to be referenced from the actual driver .so
file, allowing LTO and reducing our exposed symbol count massively.

FPS improvement on GLB2.7 with INTEL_NO_HW=1: 2.61061% +/- 1.16957% (n=50)
(without LTO, just the PLT reductions from this commit)

Note that the X Server requires commit
7ecfab47eb221dbb996ea6c033348b8eceaeb893 to successfully load this driver!

v2: Set a global driverAPI variable so loaders don't have to update to
    createNewScreen2() (though they may want to for thread safety).
v3: Drop AM_CPPFLAGS addition (Emil pointed out I'd missed some cflags
    that would be necessary, though only if we actually relied on them).
v4: Fix install with DESTDIR set.

Reviewed-by: Matt Turner <mattst88@gmail.com> (v1)
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com> (v2)
diff --git a/configure.ac b/configure.ac
index dfa35b4..d47636d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -711,8 +711,6 @@
 AM_CONDITIONAL(HAVE_DRI_GLX, test "x$enable_glx" = xyes -a \
                                   "x$enable_dri" = xyes)
 AM_CONDITIONAL(HAVE_DRI, test "x$enable_dri" = xyes)
-AM_CONDITIONAL(NEED_LIBMESA, test "x$enable_xlib_glx" = xyes -o \
-                                  "x$enable_osmesa" = xyes)
 
 AC_ARG_ENABLE([shared-glapi],
     [AS_HELP_STRING([--enable-shared-glapi],
@@ -864,8 +862,6 @@
 AC_SUBST([GLESv2_LIB_DEPS])
 AC_SUBST([GLESv2_PC_LIB_PRIV])
 
-DRI_LIB_DEPS="\$(top_builddir)/src/mesa/libdricore/libdricore${VERSION}.la"
-
 AC_SUBST([HAVE_XF86VIDMODE])
 
 dnl
@@ -1039,10 +1035,32 @@
 
     DRI_DRIVER_LDFLAGS="-module -avoid-version -shared -Wl,-Bsymbolic"
 fi
-AM_CONDITIONAL(NEED_LIBDRICORE, test -n "$DRI_DIRS")
+
+enable_dricore=no
+enable_megadriver=no
+for driver in $DRI_DIRS; do
+    if test $driver != "i965"; then
+        enable_dricore=yes
+    else
+        enable_megadriver=yes
+    fi
+done
+
+# megadriver wants to use libmesa.la, while non-megadrivers want to
+# automatically get libdricore.  Some day hopefully we'll transition
+# everything to megadriver.
+MEGADRIVER_DRI_LIB_DEPS=$DRI_LIB_DEPS
+DRI_LIB_DEPS="\$(top_builddir)/src/mesa/libdricore/libdricore${VERSION}.la $DRI_LIB_DEPS"
+
+AM_CONDITIONAL(NEED_LIBDRICORE, test "x$enable_dricore" = xyes)
+AM_CONDITIONAL(NEED_MEGADRIVER, test "x$enable_megadriver" = xyes)
+AM_CONDITIONAL(NEED_LIBMESA, test "x$enable_xlib_glx" = xyes -o \
+                                  "x$enable_osmesa" = xyes -o \
+                                  "x$enable_megadriver" = xyes)
 AC_SUBST([EXPAT_INCLUDES])
 AC_SUBST([DRI_LIB_DEPS])
 AC_SUBST([DRI_DRIVER_LDFLAGS])
+AC_SUBST([MEGADRIVER_DRI_LIB_DEPS])
 AC_SUBST([GALLIUM_DRI_LIB_DEPS])
 
 case $DRI_DIRS in
@@ -1946,6 +1964,7 @@
 AM_CONDITIONAL(NEED_LIBPROGRAM, test "x$with_gallium_drivers" != x -o \
                                      "x$enable_xlib_glx" = xyes -o \
                                      "x$enable_osmesa" = xyes -o \
+                                     "x$enable_megadriver" = xyes -o \
                                      "x$enable_gallium_osmesa" = xyes)
 AM_CONDITIONAL(HAVE_X11_DRIVER, test "x$enable_xlib_glx" = xyes)
 AM_CONDITIONAL(HAVE_OSMESA, test "x$enable_osmesa" = xyes)