Fix race condition in BUILD_SHARED_LIBS configs.

Duplicate the ASM custom build rules: build each ASM object
once for each library. This avoids creating a race condition
on the output objects for the assembly custom build rules
when building the static and shared libraries from the same
configuration.

BUG=aomedia:3214

Change-Id: I8c7c44d26ffc1b6c2b9fe2556bf9da800d90c496
(cherry picked from commit 4d5b12223a416ffaea17297c7c6c596e205ff375)
diff --git a/aom_ports/aom_ports.cmake b/aom_ports/aom_ports.cmake
index b4cceb7..5d9f69a 100644
--- a/aom_ports/aom_ports.cmake
+++ b/aom_ports/aom_ports.cmake
@@ -45,6 +45,7 @@
 function(setup_aom_ports_targets)
   if(WIN32 AND "${AOM_TARGET_CPU}" STREQUAL "x86_64")
     add_asm_library("aom_ports" "AOM_PORTS_ASM_X86")
+    set(aom_ports_asm_lib 1)
     set(aom_ports_has_symbols 1)
   elseif("${AOM_TARGET_CPU}" MATCHES "arm")
     add_library(aom_ports OBJECT ${AOM_PORTS_SOURCES_ARM})
@@ -67,7 +68,16 @@
   # libaom_srcs.*; if it becomes necessary for a particular generator another
   # method should be used.
   if(aom_ports_has_symbols)
-    target_sources(aom_ports PRIVATE ${AOM_PORTS_INCLUDES})
+    if(aom_ports_asm_lib)
+      # When aom_ports is an asm library its name changes based on build
+      # configuration. This handles adding sources to the correct target(s).
+      target_sources(aom_ports_static PRIVATE ${AOM_PORTS_INCLUDES})
+      if(BUILD_SHARED_LIBS)
+        target_sources(aom_ports_shared PRIVATE ${AOM_PORTS_INCLUDES})
+      endif()
+    else()
+      target_sources(aom_ports PRIVATE ${AOM_PORTS_INCLUDES})
+    endif()
     set(AOM_LIB_TARGETS ${AOM_LIB_TARGETS} PARENT_SCOPE)
   else()
     target_sources(aom PRIVATE ${AOM_PORTS_INCLUDES})
diff --git a/build/cmake/aom_optimization.cmake b/build/cmake/aom_optimization.cmake
index 9c2afe9..0203165 100644
--- a/build/cmake/aom_optimization.cmake
+++ b/build/cmake/aom_optimization.cmake
@@ -130,47 +130,59 @@
 # into the aom library target(s). Generates a dummy C file with a dummy function
 # to ensure that all cmake generators can determine the linker language, and
 # that build tools don't complain that an object exposes no symbols.
+#
+# In shared library configs every step described above happens twice, and
+# directory/target/object names are updated to include _shared and _static
+# suffixes.
 function(add_asm_library lib_name asm_sources)
   if("${${asm_sources}}" STREQUAL "")
     return()
   endif()
-  set(asm_lib_obj_dir "${AOM_CONFIG_DIR}/asm_objects/${lib_name}")
-  if(NOT EXISTS "${asm_lib_obj_dir}")
-    file(MAKE_DIRECTORY "${asm_lib_obj_dir}")
+
+  list(APPEND asm_configs "static")
+  if(BUILD_SHARED_LIBS)
+    list(APPEND asm_configs "shared")
   endif()
 
-  # TODO(tomfinegan): If cmake ever allows addition of .o files to OBJECT lib
-  # targets, make this OBJECT instead of STATIC to hide the target from
-  # consumers of the AOM cmake build.
-  add_library(${lib_name} STATIC ${${asm_sources}})
-  set_property(TARGET ${lib_name} PROPERTY FOLDER ${AOM_TARGET_CPU})
-
-  foreach(asm_source ${${asm_sources}})
-    get_filename_component(asm_source_name "${asm_source}" NAME)
-    set(asm_object "${asm_lib_obj_dir}/${asm_source_name}.o")
-    add_custom_command(OUTPUT "${asm_object}"
-                       COMMAND ${AS_EXECUTABLE} ARGS ${AOM_AS_FLAGS}
-                               -I${AOM_ROOT}/ -I${AOM_CONFIG_DIR}/ -o
-                               "${asm_object}" "${asm_source}"
-                       DEPENDS "${asm_source}"
-                       COMMENT "Building ASM object ${asm_object}"
-                       WORKING_DIRECTORY "${AOM_CONFIG_DIR}"
-                       VERBATIM)
-    target_sources(aom PRIVATE "${asm_object}")
-    if(BUILD_SHARED_LIBS)
-      target_sources(aom_static PRIVATE "${asm_object}")
+  foreach(asm_config ${asm_configs})
+    set(asm_lib_name ${lib_name}_${asm_config})
+    set(asm_lib_obj_dir "${AOM_CONFIG_DIR}/asm_objects/${asm_lib_name}")
+    if(NOT EXISTS "${asm_lib_obj_dir}")
+      file(MAKE_DIRECTORY "${asm_lib_obj_dir}")
     endif()
+
+    add_library(${asm_lib_name} STATIC ${${asm_sources}})
+    set_property(TARGET ${asm_lib_name} PROPERTY FOLDER ${AOM_TARGET_CPU})
+
+    foreach(asm_source ${${asm_sources}})
+      get_filename_component(asm_source_name "${asm_source}" NAME)
+      set(asm_object "${asm_lib_obj_dir}/${asm_source_name}.o")
+      add_custom_command(OUTPUT "${asm_object}"
+                         COMMAND ${AS_EXECUTABLE} ARGS ${AOM_AS_FLAGS}
+                                 -I${AOM_ROOT}/ -I${AOM_CONFIG_DIR}/ -o
+                                 "${asm_object}" "${asm_source}"
+                         DEPENDS "${asm_source}"
+                         COMMENT "Building ASM object ${asm_object}"
+                         WORKING_DIRECTORY "${AOM_CONFIG_DIR}"
+                         VERBATIM)
+      if(BUILD_SHARED_LIBS AND "${asm_config}" STREQUAL "static")
+        target_sources(aom_static PRIVATE "${asm_object}")
+      else()
+        target_sources(aom PRIVATE "${asm_object}")
+      endif()
+    endforeach()
+
+    # The above created a target containing only ASM sources. CMake needs help
+    # here to determine the linker language. Add a dummy C file to force the
+    # linker language to C. We don't bother with setting the LINKER_LANGUAGE
+    # property on the library target because not all generators obey it (looking
+    # at you, Xcode generator).
+    add_dummy_source_file_to_target("${asm_lib_name}" "c")
+
+    # Add the new lib target to the global list of aom library targets.
+    list(APPEND AOM_LIB_TARGETS ${asm_lib_name})
   endforeach()
 
-  # The above created a target containing only ASM sources. Cmake needs help
-  # here to determine the linker language. Add a dummy C file to force the
-  # linker language to C. We don't bother with setting the LINKER_LANGUAGE
-  # property on the library target because not all generators obey it (looking
-  # at you, xcode generator).
-  add_dummy_source_file_to_target("${lib_name}" "c")
-
-  # Add the new lib target to the global list of aom library targets.
-  list(APPEND AOM_LIB_TARGETS ${lib_name})
   set(AOM_LIB_TARGETS ${AOM_LIB_TARGETS} PARENT_SCOPE)
 endfunction()