Explicitly support user supplied flags in the CMake build.

Add AOM_EXTRA_<TYPE>_FLAGS variables. Currently AS, C, CXX, and
LINKER are the available type values. These variables are added
to argument strings for each type as the last step in generation
to ensure that user flags have precedence over flags added by
the AOM build itself.

Change-Id: I8e01682ee6c7dad2253e01c446a5ced7d06596c7
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 90f8fea..053df79 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -454,6 +454,9 @@
   set_target_properties(aom PROPERTIES SOVERSION 0)
 endif ()
 
+# Handle user supplied compile and link flags last to ensure they're obeyed.
+set_user_flags()
+
 # Aomedia documentation rule.
 if (ENABLE_DOCS)
   include(FindDoxygen)
diff --git a/build/cmake/compiler_flags.cmake b/build/cmake/compiler_flags.cmake
index 8559f37..7d9063f 100644
--- a/build/cmake/compiler_flags.cmake
+++ b/build/cmake/compiler_flags.cmake
@@ -320,4 +320,37 @@
   endif ()
 endfunction ()
 
+# Appends flags in $AOM_EXTRA_<TYPE>_FLAGS variables to the flags used at build
+# time.
+function (set_user_flags)
+  # Linker flags are handled first because some C/CXX flags require that a
+  # linker flag is present at link time.
+  if (AOM_EXTRA_EXE_LINKER_FLAGS)
+    is_flag_present(AOM_EXE_LINKER_FLAGS "${AOM_EXTRA_EXE_LINKER_FLAGS}"
+                    extra_present)
+    if (NOT ${extra_present})
+      require_linker_flag("${AOM_EXTRA_EXE_LINKER_FLAGS}")
+    endif ()
+  endif ()
+  if (AOM_EXTRA_AS_FLAGS)
+    # TODO(tomfinegan): assembler flag testing would be a good thing to have.
+    is_flag_present(AOM_AS_FLAGS "${AOM_EXTRA_AS_FLAGS}" extra_present)
+    if (NOT ${extra_present})
+      append_flag(AOM_AS_FLAGS "${AOM_EXTRA_AS_FLAGS}")
+    endif ()
+  endif ()
+  if (AOM_EXTRA_C_FLAGS)
+    is_flag_present(AOM_C_FLAGS "${AOM_EXTRA_C_FLAGS}" extra_present)
+    if (NOT ${extra_present})
+      require_c_flag("${AOM_EXTRA_C_FLAGS}" YES)
+    endif ()
+  endif ()
+  if (AOM_EXTRA_CXX_FLAGS)
+    is_flag_present(AOM_CXX_FLAGS "${AOM_EXTRA_CXX_FLAGS}" extra_present)
+    if (NOT ${extra_present})
+      require_cxx_flag("${AOM_EXTRA_CXX_FLAGS}" YES)
+    endif ()
+  endif ()
+endfunction ()
+
 endif ()  # AOM_BUILD_CMAKE_COMPILER_FLAGS_CMAKE_