CMake: fix interface deps in iree_cc_binary, add iree-dump-module.

Addresses failure noted at https://github.com/google/iree/issues/190#issuecomment-570209098
Adds configuration for `iree-dump-module` for https://github.com/google/iree/issues/393

Closes https://github.com/google/iree/pull/464

COPYBARA_INTEGRATE_REVIEW=https://github.com/google/iree/pull/464 from ScottTodd:cmake-tools 3b6865d01c8918719c3301b9dd8b97ae54645e0d
PiperOrigin-RevId: 289541324
diff --git a/build_tools/cmake/iree_cc_binary.cmake b/build_tools/cmake/iree_cc_binary.cmake
index cf01413..30f0c88 100644
--- a/build_tools/cmake/iree_cc_binary.cmake
+++ b/build_tools/cmake/iree_cc_binary.cmake
@@ -102,26 +102,36 @@
   #                  dependencies of ALWAYSLINK libraries should be linked in)

   set(_ALWAYS_LINK_DEPS "")

   set(_STANDARD_DEPS "")

-  foreach(DEP ${_RULE_DEPS})

-    get_target_property(_ALWAYS_LINK_DEP ${DEP} ALWAYSLINK)

-    if(_ALWAYS_LINK_DEP)

-      list(APPEND _ALWAYS_LINK_DEPS ${DEP})

+  foreach(_DEP ${_RULE_DEPS})

+    # Check if _DEP is a library with the ALWAYSLINK property set.

+    get_target_property(_DEP_TYPE ${_DEP} TYPE)

+    if(${_DEP_TYPE} STREQUAL "INTERFACE_LIBRARY")

+      # Can't be ALWAYSLINK since it's an INTERFACE library.

+      # We also can't even query for the property, since it isn't whitelisted.

+      set(_DEP_IS_ALWAYSLINK OFF)

+    else()

+      get_target_property(_DEP_IS_ALWAYSLINK ${_DEP} ALWAYSLINK)

+    endif()

+

+    # Append to the corresponding list of deps.

+    if(_DEP_IS_ALWAYSLINK)

+      list(APPEND _ALWAYS_LINK_DEPS ${_DEP})

 

       # For MSVC, also add a `-WHOLEARCHIVE:` version of the dep.

       # CMake treats -WHOLEARCHIVE[:lib] as a link flag and will not actually

       # try to link the library in, so we need the flag *and* the dependency.

       if(MSVC)

-        get_target_property(_ALIASED_TARGET ${DEP} ALIASED_TARGET)

+        get_target_property(_ALIASED_TARGET ${_DEP} ALIASED_TARGET)

         if (_ALIASED_TARGET)

           list(APPEND _ALWAYS_LINK_DEPS "-WHOLEARCHIVE:${_ALIASED_TARGET}")

         else()

-          list(APPEND _ALWAYS_LINK_DEPS "-WHOLEARCHIVE:${DEP}")

+          list(APPEND _ALWAYS_LINK_DEPS "-WHOLEARCHIVE:${_DEP}")

         endif()

       endif()

     else()

-      list(APPEND _STANDARD_DEPS ${DEP})

+      list(APPEND _STANDARD_DEPS ${_DEP})

     endif()

-  endforeach(DEP)

+  endforeach(_DEP)

 

   # Call into target_link_libraries with the lists of deps.

   # TODO(scotttodd): `-Wl,-force_load` version

diff --git a/iree/tools/CMakeLists.txt b/iree/tools/CMakeLists.txt
index 52e312f..96403ad 100644
--- a/iree/tools/CMakeLists.txt
+++ b/iree/tools/CMakeLists.txt
@@ -12,6 +12,22 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+iree_cc_binary(
+  NAME
+    iree-dump-module
+  OUT
+    iree-dump-module
+  SRCS
+    "dump_module_main.cc"
+  DEPS
+    iree::base::file_io
+    iree::base::flatbuffer_util
+    iree::base::init
+    iree::schemas::bytecode_module_def_cc_fbs
+    flatbuffers
+)
+add_executable(iree-dump-module ALIAS iree_tools_iree-dump-module)
+
 if (${IREE_ENABLE_LLVM})
   iree_cc_binary(
     NAME