diff --git a/src/libtracker-sparql/Makefile.am b/src/libtracker-sparql/Makefile.am
index 369ad72..d367cbd 100644
--- a/src/libtracker-sparql/Makefile.am
+++ b/src/libtracker-sparql/Makefile.am
@@ -1,7 +1,7 @@
 noinst_LTLIBRARIES = libtracker-sparql.la
 
 libtracker_sparql_la_VALAFLAGS =                       \
-	--gir Tracker-$(TRACKER_API_VERSION).gir       \
+	--gir Tracker_Vala-$(TRACKER_API_VERSION).gir       \
 	--library tracker-sparql-$(TRACKER_API_VERSION) \
 	--includedir=libtracker-sparql                 \
 	--header $(vala_header:.h=-no-checks.h)        \
@@ -76,6 +76,15 @@ libtracker_sparqlinclude_HEADERS =                     \
 tracker-sparql-$(TRACKER_API_VERSION).deps: tracker-sparql.deps
 	$(AM_V_GEN)cp $^ $@
 
+# Due to the unique way libtracker-sparql is built, one of the functions
+# needed at link time is in a different directory. We can't built it yet
+# but we need it to link the introspection binary. It is possible to build
+# all the introspection from the libtracker-sparql-backend Makefile but
+# it is painful as we can't reuse anything from this Makefile. Instead we
+# build a stub library that we pass to the g-ir-scanner.
+noinst_LTLIBRARIES += libtracker-sparql-backend-stubs.la
+libtracker_sparql_backend_stubs_la_SOURCES = tracker-sparql-backend-stubs.c
+
 # PkgConfig
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = tracker-sparql-$(TRACKER_API_VERSION).pc
@@ -110,10 +119,36 @@ EXTRA_DIST =                                           \
 	tracker-sparql.deps                            \
 	$(vala_header:.h=-no-checks.h)
 
-# Introspection foo
+# We have Vala and C code in this library. The Vala compiler produces a
+# .gir for the Vala code only. Below we produce a second .gir from the
+# C code, then merge them both using a simple custom tool.
 -include $(INTROSPECTION_MAKEFILE)
+INTROSPECTION_GIRS =
+INTROSPECTION_SCANNER_ARGS = \
+	-I $(top_srcdir)/src \
+	-D TRACKER_COMPILATION \
+	--c-include="libtracker-sparql/tracker-sparql.h" \
+	--identifier-prefix=Tracker \
+	--symbol-prefix=tracker \
+	--warn-all
 
 if HAVE_INTROSPECTION
+INTROSPECTION_CFLAGS += $(AM_CPPFLAGS)
+introspection_sources = \
+	$(libtracker_sparql_la_SOURCES)
+
+Tracker_C-@TRACKER_API_VERSION@.gir: $(INTROSPECTION_SCANNER) libtracker-sparql.la libtracker-sparql-backend-stubs.la Makefile
+Tracker_C_@TRACKER_API_VERSION_UNDERSCORES@_gir_INCLUDES = Gio-2.0
+Tracker_C_@TRACKER_API_VERSION_UNDERSCORES@_gir_CFLAGS = $(INCLUDES)
+Tracker_C_@TRACKER_API_VERSION_UNDERSCORES@_gir_NAMESPACE = "Tracker"
+Tracker_C_@TRACKER_API_VERSION_UNDERSCORES@_gir_LIBS = libtracker-sparql.la libtracker-sparql-backend-stubs.la
+Tracker_C_@TRACKER_API_VERSION_UNDERSCORES@_gir_FILES = $(introspection_sources)
+
+INTROSPECTION_GIRS += Tracker_C-$(TRACKER_API_VERSION).gir
+
+Tracker-$(TRACKER_API_VERSION).gir: Tracker-$(TRACKER_API_VERSION).gir Tracker_C-$(TRACKER_API_VERSION).gir
+	$(top_srcdir)/utils/g-ir-merge $^ > $@
+
 Tracker-@TRACKER_API_VERSION@.typelib: Tracker-$(TRACKER_API_VERSION).gir
 	@INTROSPECTION_COMPILER@ --shared-library=libtracker-sparql-@TRACKER_API_VERSION@.so.0 -o $@ $^
 
