[Flang] add some cmake code to allow for out-of-tree building of MLIR and LLVM

Differential Revision: https://reviews.llvm.org/D72418
diff --git a/mlir/CMakeLists.txt b/mlir/CMakeLists.txt
index cd4f05f..9c463ef 100644
--- a/mlir/CMakeLists.txt
+++ b/mlir/CMakeLists.txt
@@ -6,11 +6,7 @@
 set(MLIR_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
 set(MLIR_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
 
-function(mlir_tablegen ofn)
-  tablegen(MLIR ${ARGV} "-I${MLIR_MAIN_SRC_DIR}" "-I${MLIR_INCLUDE_DIR}")
-  set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
-      PARENT_SCOPE)
-endfunction()
+list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
 
 function(add_mlir_dialect dialect dialect_doc_filename)
   set(LLVM_TARGET_DEFINITIONS ${dialect}.td)
@@ -33,34 +29,14 @@
   add_dependencies(mlir-doc ${dialect_doc_filename}DocGen)
 endfunction()
 
+include(AddMLIR)
+
 # Installing the headers and docs needs to depend on generating any public
 # tablegen'd targets.
 add_custom_target(mlir-headers)
 set_target_properties(mlir-headers PROPERTIES FOLDER "Misc")
 add_custom_target(mlir-doc)
 
-# TODO: This is to handle the current static registration, but should be
-# factored out a bit.
-function(whole_archive_link target)
-  if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
-    set(link_flags "-L${CMAKE_BINARY_DIR}/lib ")
-    FOREACH(LIB ${ARGN})
-      string(CONCAT link_flags ${link_flags} "-Wl,-force_load ${CMAKE_BINARY_DIR}/lib/lib${LIB}.a ")
-    ENDFOREACH(LIB)
-  elseif(MSVC)
-    FOREACH(LIB ${ARGN})
-      string(CONCAT link_flags ${link_flags} "/WHOLEARCHIVE:${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/${LIB}.lib ")
-    ENDFOREACH(LIB)
-  else()
-    set(link_flags "-L${CMAKE_BINARY_DIR}/lib -Wl,--whole-archive,")
-    FOREACH(LIB ${ARGN})
-      string(CONCAT link_flags ${link_flags} "-l${LIB},")
-    ENDFOREACH(LIB)
-    string(CONCAT link_flags ${link_flags} "--no-whole-archive")
-  endif()
-  set_target_properties(${target} PROPERTIES LINK_FLAGS ${link_flags})
-endfunction(whole_archive_link)
-
 # Build the CUDA conversions and run according tests if the NVPTX backend
 # is available
 if ("NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)
@@ -100,9 +76,11 @@
     DESTINATION include
     COMPONENT mlir-headers
     FILES_MATCHING
+    PATTERN "*.def"
     PATTERN "*.h"
     PATTERN "*.gen"
     PATTERN "*.inc"
+    PATTERN "*.td"
     PATTERN "CMakeFiles" EXCLUDE
     PATTERN "config.h" EXCLUDE
     )
@@ -113,3 +91,5 @@
                              COMPONENT mlir-headers)
   endif()
 endif()
+
+add_subdirectory(cmake/modules)