Add option to use system gtest and/or gflags

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f666bf1..0d49670 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,8 +1,17 @@
 cmake_minimum_required (VERSION 2.8.12)
 
+project (open-vcdiff CXX C)
+set (VERSION 0.8.4)
+# For config.h.in to avoid generic define
+set (OPEN_VCDIFF_VERSION ${VERSION})
+set (PROJECT_SOVERSION 0)
+
+
 # Configuration options
 option(vcdiff_build_tests "Build all of vcdiff tests." ON)
 option(vcdiff_build_exec "Build vcdiff exectuable" ON)
+option(vcdiff_use_system_gflags "Use system wide installed gflags" OFF)
+option(vcdiff_use_system_gtest "Use system wide installed gtest" OFF)
 
 # Add subprojects first so we won't interfere with config.h.in
 
@@ -12,20 +21,13 @@
   set (GFLAGS_INSTALL_HEADERS OFF)
   set (GFLAGS_INSTALL_SHARED_LIBS OFF)
   set (GFLAGS_INSTALL_STATIC_LIBS OFF)
-  add_subdirectory (third_party/gflags)
+  if (${vcdiff_use_system_gflags})
+    find_library(gflags gflags)
+  else()
+    add_subdirectory (third_party/gflags)
+  endif()
 endif ()
 
-if (vcdiff_build_tests)
-  set (gtest_install OFF CACHE BOOL "Don't install gtest" FORCE)
-  add_subdirectory (third_party/googletest/googletest)
-endif ()
-
-project (open-vcdiff CXX C)
-set (PROJECT_VERSION 0.8.4)
-# For config.h.in to avoid generic define
-set (OPEN_VCDIFF_VERSION ${PROJECT_VERSION})
-set (PROJECT_SOVERSION 0)
-
 include (CheckIncludeFiles)
 check_include_files (ext/rope HAVE_EXT_ROPE)
 check_include_files (getopt.h HAVE_GETOPT_H)
@@ -123,6 +125,20 @@
 
 if (vcdiff_build_tests)
   enable_testing ()
+  if (${vcdiff_use_system_gtest})
+    find_package (GTest)
+    if (${GTEST_FOUND})
+      set (gtest_main ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBARY})
+      include_directories(${GTEST_INCLUDE_DIRS})
+    else()
+      # On Debian it doens't install libgtest.a. Instead it provides sources in
+      # /usr/src/gtest
+      add_subdirectory (/usr/src/gtest gtest)
+    endif()
+  else()
+    set (gtest_install OFF CACHE BOOL "Don't install gtest" FORCE)
+    add_subdirectory (third_party/googletest/googletest)
+  endif()
 
   add_library (vcdecoder_test_common
     "src/vcdecoder_test.cc"
diff --git a/README b/README
index ea56d2f..0e2f2a0 100644
--- a/README
+++ b/README
@@ -7,6 +7,14 @@
 For further details, please refer to:
 https://github.com/google/open-vcdiff/wiki/How-to-use-openvcdiff
 
+You will need to first synchronize gflags and gtest by running
+
+    git submodule update --init --recursive
+
+Or if you have system installed gflags and/or gtest libraries you can provide
+-Dvcdiff_use_system_gflags=ON and -Dvcdiff_use_system_gtest=ON` for cmake
+invokation in the build step.
+
 open-vcdiff comes with a CMake build script CMakeLists.txt that can be used on
 a wide range of platforms ("C" stands for cross-platform.). If you don't have
 CMake installed already, you can download it for free from
diff --git a/README.md b/README.md
index f40015a..208e744 100644
--- a/README.md
+++ b/README.md
@@ -5,7 +5,10 @@
 and Compression Data Format.
 
 You will need to first synchronize gflags and gtest by running
-`git submodule update --init --recursive`.
+`git submodule update --init --recursive`. Or if you have system installed
+gflags and/or gtest libraries you can provide ```-Dvcdiff_use_system_gflags=ON```
+and ```-Dvcdiff_use_system_gtest=ON``` for ```cmake``` invokation in the build
+step.
 
 A library with a simple API is included, as well as a command-line executable
 that can apply the encoder and decoder to source, target, and delta files.