diff --git a/Makefile-gir.am b/Makefile-gir.am
index 86ab5bf1..28d9da8b 100644
--- a/Makefile-gir.am
+++ b/Makefile-gir.am
@@ -68,7 +68,8 @@ GLib_2_0_gir_SCANNERFLAGS = \
             --symbol-prefix=g \
             --symbol-prefix=glib \
             --c-include="glib.h" \
-            $(GLib_2_0_gir_DOCSRC)
+            $(GLib_2_0_gir_DOCSRC) \
+	    --library-path=$(GLIB_LIBDIR)
 GLib_2_0_gir_PACKAGES = glib-2.0
 GLib_2_0_gir_CFLAGS = \
             -I$(GLIB_INCLUDEDIR) \
@@ -87,7 +88,12 @@ endif
 
 BUILT_GIRSOURCES += GLib-2.0.gir
 
-GLib-2.0.gir: g-ir-scanner g-ir-compiler$(EXEEXT)
+if BUILD_SCANNER
+G_IR_SCANNER_DEPENDENCY = g-ir-scanner
+else
+G_IR_SCANNER_DEPENDENCY =
+endif
+GLib-2.0.gir: $(G_IR_SCANNER_DEPENDENCY) g-ir-compiler$(EXEEXT)
 
 gir/DBusGLib-1.0.typelib: GObject-2.0.gir
 
@@ -105,7 +111,8 @@ GObject_2_0_gir_SCANNERFLAGS = \
             --identifier-prefix=G \
             --c-include="glib-object.h" \
             --add-include-path=. \
-            $(GObject_2_0_gir_DOCSRC)
+            $(GObject_2_0_gir_DOCSRC) \
+	    --library-path=$(GOBJECT_LIBDIR)
 
 GObject_2_0_gir_PACKAGES = gobject-2.0
 GObject_2_0_gir_INCLUDES = GLib-2.0
@@ -133,7 +140,8 @@ GModule_2_0_gir_SCANNERFLAGS = \
             --identifier-prefix=G \
             --c-include="gmodule.h" \
             --add-include-path=. \
-            $(GModule_2_0_gir_DOCSRC)
+            $(GModule_2_0_gir_DOCSRC) \
+	    --library-path=$(GMODULE_LIBDIR)
 
 GModule_2_0_gir_PACKAGES = gmodule-2.0
 GModule_2_0_gir_INCLUDES = GLib-2.0
@@ -173,7 +181,8 @@ Gio_2_0_gir_SCANNERFLAGS = \
             --c-include="gio/gio.h" \
             $(GIO_CINCLUDES) \
             --add-include-path=. \
-            $(Gio_2_0_gir_DOCSRC)
+            $(Gio_2_0_gir_DOCSRC) \
+	    --library-path=$(GIO_LIBDIR)
 
 Gio_2_0_gir_PACKAGES = gio-2.0 $(GIO_UNIX_PACKAGES)
 Gio_2_0_gir_INCLUDES = GObject-2.0
diff --git a/Makefile-giscanner.am b/Makefile-giscanner.am
index 06bc4be4..628a8ce2 100644
--- a/Makefile-giscanner.am
+++ b/Makefile-giscanner.am
@@ -1,17 +1,21 @@
 ## Process this file with automake to produce Makefile.in
 
+if BUILD_SCANNER
 BUILT_SOURCES += 			\
 	giscanner/scannerparser.c	\
 	giscanner/scannerparser.h 	\
 	giscanner/scannerlexer.c 	\
 	giscanner/scannerlexer.h
+endif
 
 AM_YFLAGS = -d -t
 
 # Why do I have to do this automake?
 giscanner/scannerlexer.h: giscanner/scannerlexer.c
 
+if BUILD_SCANNER
 noinst_LTLIBRARIES += libgiscanner.la
+endif
 
 libgiscanner_la_SOURCES = \
 	giscanner/sourcescanner.c				\
@@ -24,7 +28,9 @@ libgiscanner_la_CFLAGS = $(GOBJECT_CFLAGS) $(GIO_CFLAGS)
 
 # Python module
 pkgpyexecdir = $(pkglibdir)/giscanner
+if BUILD_SCANNER
 pkgpyexec_LTLIBRARIES = _giscanner.la
+endif
 pkgpyexec_PYTHON = 			\
 	giscanner/__init__.py		\
 	giscanner/annotationmain.py	\
diff --git a/Makefile-tools.am b/Makefile-tools.am
index 95293735..cb9754a7 100644
--- a/Makefile-tools.am
+++ b/Makefile-tools.am
@@ -1,5 +1,7 @@
 bin_PROGRAMS += g-ir-compiler g-ir-generate g-ir-inspect
+if BUILD_SCANNER
 bin_SCRIPTS += g-ir-scanner g-ir-annotation-tool
+endif
 
 if BUILD_DOCTOOL
 bin_SCRIPTS += g-ir-doc-tool
@@ -10,6 +12,7 @@ EXTRA_DIST += 				\
 
 TOOL_SUBSTITUTIONS = -e s,@libdir\@,$(libdir), -e s,@datarootdir\@,$(datarootdir), -e s,@PYTHON\@,$(PYTHON),
 
+if BUILD_SCANNER
 g-ir-scanner: tools/g-ir-tool-template.in _giscanner.la Makefile
 	$(AM_V_GEN) sed $(TOOL_SUBSTITUTIONS) -e s,@TOOL_MODULE\@,scannermain, -e s,@TOOL_FUNCTION\@,scanner_main, $< > $@.tmp && mv $@.tmp $@
 	@chmod a+x $@
@@ -17,10 +20,13 @@ g-ir-scanner: tools/g-ir-tool-template.in _giscanner.la Makefile
 g-ir-annotation-tool: tools/g-ir-tool-template.in _giscanner.la Makefile
 	$(AM_V_GEN) sed $(TOOL_SUBSTITUTIONS) -e s,@TOOL_MODULE\@,annotationmain, -e s,@TOOL_FUNCTION\@,annotation_main, $< > $@.tmp && mv $@.tmp $@
 	@chmod a+x $@
+endif
 
+if BUILD_DOCTOOL
 g-ir-doc-tool: tools/g-ir-tool-template.in _giscanner.la Makefile
 	$(AM_V_GEN) sed $(TOOL_SUBSTITUTIONS) -e s,@TOOL_MODULE\@,docmain, -e s,@TOOL_FUNCTION\@,doc_main, $< > $@.tmp && mv $@.tmp $@
 	@chmod a+x $@
+endif
 
 g_ir_compiler_SOURCES = tools/compiler.c
 g_ir_compiler_CPPFLAGS = -DGIREPO_DEFAULT_SEARCH_PATH="\"$(libdir)\"" \
@@ -50,4 +56,12 @@ GCOVSOURCES =					\
 	$(g_ir_compiler_SOURCES)		\
 	$(g_ir_generate_SOURCES)
 
-CLEANFILES += g-ir-scanner g-ir-annotation-tool g-ir-doc-tool g-ir-inspect
+if BUILD_SCANNER
+CLEANFILES += g-ir-scanner g-ir-annotation-tool
+endif
+
+if BUILD_DOCTOOL
+CLEANFILES += g-ir-doc-tool
+endif
+
+CLEANFILES += g-ir-inspect
diff --git a/Makefile.am b/Makefile.am
index dafea384..80d8c997 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -35,8 +35,11 @@ DISTCHECK_CONFIGURE_FLAGS = --enable-gtk-doc --enable-doctool
 
 man_MANS += 			\
 	docs/g-ir-compiler.1	\
-	docs/g-ir-generate.1	\
+	docs/g-ir-generate.1
+if BUILD_SCANNER
+man_MANS +=	\
 	docs/g-ir-scanner.1
+endif
 
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = gobject-introspection-1.0.pc gobject-introspection-no-export-1.0.pc
diff --git a/common.mk b/common.mk
index b778f7aa..9d10e65e 100644
--- a/common.mk
+++ b/common.mk
@@ -9,11 +9,14 @@
 INTROSPECTION_SCANNER = \
     env PATH=".libs:$(PATH)" \
         LPATH=.libs \
-        CC="$(CC)" \
+        CC="$(CC)"
+if BUILD_SCANNER
+INTROSPECTION_SCANNER += \
         PYTHONPATH=$(top_builddir):$(top_srcdir) \
         UNINSTALLED_INTROSPECTION_SRCDIR=$(top_srcdir) \
-        UNINSTALLED_INTROSPECTION_BUILDDIR=$(top_builddir) \
-        $(top_builddir)/g-ir-scanner
+        UNINSTALLED_INTROSPECTION_BUILDDIR=$(top_builddir)
+endif
+INTROSPECTION_SCANNER += $(G_IR_SCANNER)
 
 INTROSPECTION_SCANNER_ARGS = \
     --verbose \
diff --git a/configure.ac b/configure.ac
index 367f2dba..a275b0ce 100644
--- a/configure.ac
+++ b/configure.ac
@@ -251,32 +251,55 @@ AC_FUNC_STRTOD
 AC_CHECK_FUNCS([memchr strchr strspn strstr strtol strtoull])
 AC_CHECK_FUNCS([backtrace backtrace_symbols])
 
-# Python
-# option to specify python interpreter to use; this just sets $PYTHON, so that
-# we will fallback to reading $PYTHON if --with-python is not given, and
-# python.m4 will get the expected input
-AC_ARG_WITH(python,
-  AS_HELP_STRING([--with-python=PATH],[Path to Python interpreter; searches $PATH if only a program name is given; if not given, searches for a few standard names such as "python3" or "python2"]),
-  [PYTHON="$withval"], [])
-if test x"$PYTHON" = xyes; then
-    AC_MSG_ERROR([--with-python option requires a path or program argument])
-fi
-if test -n "$PYTHON" && ! which "$PYTHON"; then
-    AC_MSG_ERROR([Python interpreter $PYTHON does not exist])
-fi
-
-AM_PATH_PYTHON([2.7])
-case "$host" in
-*-*-mingw*)
-	# Change backslashes to forward slashes in pyexecdir to avoid
-	# quoting issues
-	pyexecdir=`echo $pyexecdir | tr '\\\\' '/'`
-	;;
+AC_ARG_WITH(g_ir_scanner,
+            AS_HELP_STRING([--with-g-ir-scanner],
+                           [Use existing g-ir-scanner instead of building. @<:@default=no@:>@]),
+            [],
+            [with_g_ir_scanner=no])
+case "x${with_g_ir_scanner}" in
+xyes)
+        AC_PATH_PROG(G_IR_SCANNER,
+                     [g-ir-scanner],
+                     [AC_MSG_ERROR([g-ir-scanner not found.])])
+        build_g_ir_scanner=no
+        ;;
+xno)
+	# Python
+	# option to specify python interpreter to use; this just sets $PYTHON, so that
+	# we will fallback to reading $PYTHON if --with-python is not given, and
+	# python.m4 will get the expected input
+	AC_ARG_WITH(python,
+	  AS_HELP_STRING([--with-python=PATH],[Path to Python interpreter; searches $PATH if only a program name is given; if not given, searches for a few standard names such as "python3" or "python2"]),
+	  [PYTHON="$withval"], [])
+	if test x"$PYTHON" = xyes; then
+	    AC_MSG_ERROR([--with-python option requires a path or program argument])
+	fi
+	if test -n "$PYTHON" && ! which "$PYTHON"; then
+	    AC_MSG_ERROR([Python interpreter $PYTHON does not exist])
+	fi
+	
+	AM_PATH_PYTHON([2.7])
+	case "$host" in
+	*-*-mingw*)
+		# Change backslashes to forward slashes in pyexecdir to avoid
+		# quoting issues
+		pyexecdir=`echo $pyexecdir | tr '\\\\' '/'`
+                ;;
+        esac
+        AM_CHECK_PYTHON_HEADERS(, AC_MSG_ERROR([Python headers not found]))
+        if test "x$os_win32" = "xyes"; then
+          AM_CHECK_PYTHON_LIBS(, AC_MSG_ERROR([Python libs not found. Windows requires Python modules to be explicitly linked to libpython.]))
+        fi
+        G_IR_SCANNER="\$(top_builddir)/g-ir-scanner"
+        build_g_ir_scanner=yes
+        ;;
+*)
+        G_IR_SCANNER="${with_g_ir_scanner}"
+        build_g_ir_scanner=no
+        ;;
 esac
-AM_CHECK_PYTHON_HEADERS(, AC_MSG_ERROR([Python headers not found]))
-if test "x$os_win32" = "xyes"; then
-  AM_CHECK_PYTHON_LIBS(, AC_MSG_ERROR([Python libs not found. Windows requires Python modules to be explicitly linked to libpython.]))
-fi
+AC_SUBST(G_IR_SCANNER)
+AM_CONDITIONAL(BUILD_SCANNER, test x${build_g_ir_scanner} = xyes)
 
 dnl Not enabled by default until 3.6 cycle when we can propose mako as
 dnl an external dependency
@@ -367,6 +390,13 @@ dnl
 AM_CONDITIONAL(MSVC_BASE_NO_TOOLSET_SET, [test x$MSVC_BASE_TOOLSET = x])
 AM_CONDITIONAL(MSVC_NO_TOOLSET_SET, [test x$MSVC_TOOLSET = x])
 
+AC_ARG_ENABLE(tests,
+              [AS_HELP_STRING([--disable-tests],
+                              [disable tests])],
+              [],
+              [enable_tests=yes])
+AM_CONDITIONAL(ENABLE_TESTS, test x"$enable_tests" = xyes)
+
 AC_CONFIG_FILES([
 Makefile
 tests/Makefile
diff --git a/tests/Makefile.am b/tests/Makefile.am
index bdd0fa7c..70f18256 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -17,7 +17,9 @@ tests_DATA =		    \
     gimarshallingtests.h    \
     gitestmacros.h
 
+if ENABLE_TESTS
 EXTRA_LTLIBRARIES = libeverything-1.0.la libgimarshallingtests-1.0.la
+endif
 
 libeverything_1_0_la_SOURCES = everything.c
 libgimarshallingtests_1_0_la_SOURCES = gimarshallingtests.c
@@ -27,7 +29,9 @@ EXTRA_DIST += \
 	gi-tester \
 	gitestmacros.h
 
+if ENABLE_TESTS
 BUILT_SOURCES += everything.c everything.h
+endif
 
 CLEANFILES += \
     $(BUILT_SOURCES) \
diff --git a/tests/offsets/Makefile.am b/tests/offsets/Makefile.am
index 0df9c151..2d8a2ba6 100644
--- a/tests/offsets/Makefile.am
+++ b/tests/offsets/Makefile.am
@@ -11,7 +11,9 @@ EXTRA_PROGRAMS =
 
 ############################################################
 
+if ENABLE_TESTS
 EXTRA_LTLIBRARIES += liboffsets.la
+endif
 
 liboffsets_la_SOURCES = \
 	offsets.h	\
@@ -32,7 +34,9 @@ CLEANFILES += Offsets-1.0.gir Offsets-1.0.typelib liboffsets.la
 
 ############################################################
 
+if ENABLE_TESTS
 EXTRA_PROGRAMS += gitestoffsets
+endif
 
 nodist_gitestoffsets_SOURCES = gitestoffsets.c
 gitestoffsets_CPPFLAGS = $(GIREPO_CFLAGS) -I$(top_srcdir)/girepository -I$(top_srcdir)/tests
@@ -41,9 +45,11 @@ gitestoffsets_LDADD = $(top_builddir)/libgirepository-1.0.la $(GIREPO_LIBS)
 gitestoffsets.c: gen-gitestoffsets offsets.h Offsets-1.0.typelib
 	$(AM_V_GEN) $(PYTHON) $(srcdir)/gen-gitestoffsets $(srcdir)/offsets.h > $@ || ( rm -f $@ && false )
 
+if ENABLE_TESTS
 EXTRA_DIST += gen-gitestoffsets
 BUILT_SOURCES += gitestoffsets.c
 CLEANFILES += gitestoffsets.c $(EXTRA_PROGRAMS)
+endif
 
 ############################################################
 
diff --git a/tests/repository/Makefile.am b/tests/repository/Makefile.am
index 6b4b782f..d3851005 100644
--- a/tests/repository/Makefile.am
+++ b/tests/repository/Makefile.am
@@ -2,8 +2,10 @@ AM_CFLAGS = $(GOBJECT_CFLAGS)
 AM_LDFLAGS = -module -avoid-version
 LIBS = $(GOBJECT_LIBS)
 
+if ENABLE_TESTS
 EXTRA_PROGRAMS = gitestrepo giteststructinfo gitestthrows gitypelibtest
 CLEANFILES = $(EXTRA_PROGRAMS)
+endif
 
 gitestrepo_SOURCES = gitestrepo.c
 gitestrepo_CPPFLAGS = $(GIREPO_CFLAGS) -I$(top_srcdir)/girepository
diff --git a/tests/scanner/Makefile.am b/tests/scanner/Makefile.am
index 6b705f5d..33d4e6e5 100644
--- a/tests/scanner/Makefile.am
+++ b/tests/scanner/Makefile.am
@@ -9,6 +9,7 @@ INTROSPECTION_SCANNER_ARGS += --warn-all --reparse-validate
 
 CLEANFILES =
 
+if ENABLE_TESTS
 EXTRA_LTLIBRARIES = \
 	libsletter.la \
 	libutility.la \
@@ -19,6 +20,7 @@ EXTRA_LTLIBRARIES = \
 	libtypedefs.la \
 	$(NULL)
 CLEANFILES += $(EXTRA_LTLIBRARIES)
+endif
 
 GI_SCANNER_CFLAGS = -I$(top_srcdir)/tests
 AM_CPPFLAGS = -I$(top_srcdir)/girepository
@@ -147,7 +149,7 @@ Typedefs_1_0_gir_FILES = $(libtypedefs_la_SOURCES)
 Typedefs_1_0_gir_SCANNERFLAGS = $(INTROSPECTION_SCANNER_ARGS) --c-include="typedefs.h" --identifier-prefix=Typedefs --symbol-prefix=typedefs
 GIRS += Typedefs-1.0.gir
 
-if !OS_WIN32
+if ENABLE_TESTS
 EXTRA_PROGRAMS = barapp
 CLEANFILES += $(EXTRA_PROGRAMS)
 
