cmake: Improve support for users of shallow aom clones.

Previous to this change when using shallow clones libaom would
always be relinked in all builds because the version check did
not properly handle lack of tags in shallow clones of the git
repository.

Change-Id: I49ca3c75c916fc1356f2a94a4c68340ea03ef740
diff --git a/build/cmake/version.cmake b/build/cmake/version.cmake
index ab7f215..dd953a3 100644
--- a/build/cmake/version.cmake
+++ b/build/cmake/version.cmake
@@ -26,13 +26,19 @@
 if(EXISTS "${GIT_EXECUTABLE}")
   execute_process(COMMAND ${GIT_EXECUTABLE} --git-dir=${AOM_ROOT}/.git describe
                   OUTPUT_VARIABLE aom_version
-                  ERROR_QUIET)
-  string(STRIP "${aom_version}" aom_version)
+                  ERROR_QUIET
+                  RESULT_VARIABLE version_check_result)
 
-  # Remove the leading 'v' from the version string.
-  string(FIND "${aom_version}" "v" v_pos)
-  if(${v_pos} EQUAL 0)
-    string(SUBSTRING "${aom_version}" 1 -1 aom_version)
+  if(${version_check_result} EQUAL 0)
+    string(STRIP "${aom_version}" aom_version)
+
+    # Remove the leading 'v' from the version string.
+    string(FIND "${aom_version}" "v" v_pos)
+    if(${v_pos} EQUAL 0)
+      string(SUBSTRING "${aom_version}" 1 -1 aom_version)
+    endif()
+  else()
+    set(aom_version "")
   endif()
 endif()
 
@@ -41,17 +47,19 @@
 endif()
 
 unset(last_aom_version)
-if(EXISTS "${AOM_CONFIG_DIR}/config/aom_version.h")
-  extract_version_string("${AOM_CONFIG_DIR}/config/aom_version.h"
-                         last_aom_version)
+set(version_file "${AOM_CONFIG_DIR}/config/aom_version.h")
+if(EXISTS "${version_file}")
+  extract_version_string("${version_file}" last_aom_version)
+  if("${aom_version}" MATCHES "CHANGELOG$")
+    set(aom_version "${last_aom_version}")
+  endif()
 endif()
 
 if(NOT "${aom_version}" STREQUAL "${last_aom_version}")
-
   # TODO(tomfinegan): Perl dependency is unnecessary. CMake can do everything
-  # that is done by version.pl on its own (if a bit more verbose...).
-  execute_process(
-    COMMAND ${PERL_EXECUTABLE} "${AOM_ROOT}/build/cmake/version.pl"
-            --version_data=${aom_version}
-            --version_filename=${AOM_CONFIG_DIR}/config/aom_version.h VERBATIM)
+  # that is done by version.pl on its own (if a bit more verbosely...).
+  execute_process(COMMAND ${PERL_EXECUTABLE}
+                          "${AOM_ROOT}/build/cmake/version.pl"
+                          --version_data=${aom_version}
+                          --version_filename=${version_file} VERBATIM)
 endif()