diff --git a/mlir/CMakeLists.txt b/mlir/CMakeLists.txt --- 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) diff --git a/mlir/cmake/modules/AddMLIR.cmake b/mlir/cmake/modules/AddMLIR.cmake new file mode 100644 --- /dev/null +++ b/mlir/cmake/modules/AddMLIR.cmake @@ -0,0 +1,27 @@ +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() + +# 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:${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) diff --git a/mlir/cmake/modules/CMakeLists.txt b/mlir/cmake/modules/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/mlir/cmake/modules/CMakeLists.txt @@ -0,0 +1,67 @@ +# Generate a list of CMake library targets so that other CMake projects can +# link against them. LLVM calls its version of this file LLVMExports.cmake, but +# the usual CMake convention seems to be ${Project}Targets.cmake. +set(MLIR_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm) +set(mlir_cmake_builddir "${CMAKE_BINARY_DIR}/${MLIR_INSTALL_PACKAGE_DIR}") + +# Keep this in sync with llvm/cmake/CMakeLists.txt! +set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm) +set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}") + +get_property(MLIR_EXPORTS GLOBAL PROPERTY MLIR_EXPORTS) +export(TARGETS ${MLIR_EXPORTS} FILE ${mlir_cmake_builddir}/MlirTargets.cmake) + +# Generate MlirConfig.cmake for the build tree. +set(MLIR_CONFIG_CMAKE_DIR "${mlir_cmake_builddir}") +set(MLIR_CONFIG_LLVM_CMAKE_DIR "${llvm_cmake_builddir}") +set(MLIR_CONFIG_EXPORTS_FILE "${mlir_cmake_builddir}/MlirTargets.cmake") +set(MLIR_CONFIG_INCLUDE_DIRS + "${MLIR_SOURCE_DIR}/include" + "${MLIR_BINARY_DIR}/include" + ) +set(MLIR_CONFIG_CMAKE_DIR) +set(MLIR_CONFIG_LLVM_CMAKE_DIR) +set(MLIR_CONFIG_EXPORTS_FILE) + +# Generate MlirConfig.cmake for the install tree. +set(MLIR_CONFIG_CODE " +# Compute the installation prefix from this LLVMConfig.cmake file location. +get_filename_component(MLIR_INSTALL_PREFIX \"\${CMAKE_CURRENT_LIST_FILE}\" PATH)") +# Construct the proper number of get_filename_component(... PATH) +# calls to compute the installation prefix. +string(REGEX REPLACE "/" ";" _count "${MLIR_INSTALL_PACKAGE_DIR}") +foreach(p ${_count}) + set(MLIR_CONFIG_CODE "${MLIR_CONFIG_CODE} +get_filename_component(MLIR_INSTALL_PREFIX \"\${MLIR_INSTALL_PREFIX}\" PATH)") +endforeach(p) +set(MLIR_CONFIG_CMAKE_DIR "\${MLIR_INSTALL_PREFIX}/${MLIR_INSTALL_PACKAGE_DIR}") +set(MLIR_CONFIG_LLVM_CMAKE_DIR "\${MLIR_INSTALL_PREFIX}/${LLVM_INSTALL_PACKAGE_DIR}") +set(MLIR_CONFIG_EXPORTS_FILE "\${MLIR_CMAKE_DIR}/MlirTargets.cmake") +set(MLIR_CONFIG_INCLUDE_DIRS + "\${MLIR_INSTALL_PREFIX}/include" + ) +set(MLIR_CONFIG_CODE) +set(MLIR_CONFIG_CMAKE_DIR) +set(MLIR_CONFIG_EXPORTS_FILE) + +if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY) + # Not TOOLCHAIN ONLY, so install the MLIR parts as well + # Include the cmake files so other tools can use mlir-tblgen, etc. + get_property(mlir_has_exports GLOBAL PROPERTY MLIR_HAS_EXPORTS) + if(mlir_has_exports) + install(EXPORT MlirTargets DESTINATION ${MLIR_INSTALL_PACKAGE_DIR} + COMPONENT mlir-cmake-exports) + endif() + + install(FILES #${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MlirConfig.cmake + ${CMAKE_CURRENT_SOURCE_DIR}/AddMLIR.cmake + DESTINATION ${MLIR_INSTALL_PACKAGE_DIR} + COMPONENT mlir-cmake-exports) + + if(NOT LLVM_ENABLE_IDE) + # Add a dummy target so this can be used with LLVM_DISTRIBUTION_COMPONENTS + add_custom_target(mlir-cmake-exports) + add_llvm_install_targets(install-mlir-cmake-exports + COMPONENT mlir-cmake-exports) + endif() +endif()