88
99set (IMEX_EXTERNAL_PROJECT_BUILD OFF )
1010
11- if (NOT (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR ) AND NOT MLIR_BINARY_DIR )
11+ if (NOT (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR ) AND NOT DEFINED MLIR_BINARY_DIR )
1212 # Building as part of LLVM via the external project mechanism.
1313 set (IMEX_EXTERNAL_PROJECT_BUILD ON )
1414 list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR} /cmake/modules" )
@@ -39,14 +39,21 @@ if(IMEX_EXTERNAL_PROJECT_BUILD)
3939 message (FATAL_ERROR "Invalid llvm version" )
4040 endif ()
4141
42+ # Variables needed for mlir_tablegen() to function
4243 set (MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR} /../mlir )
4344 set (MLIR_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR} /include )
45+
46+ set (LLVM_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR} /include )
47+
48+ # LLVM headers in source tree are already included
49+ # as part of LLVM_EXTERNAL_PROJECTS build
50+
51+ # MLIR headers in source tree
52+ include_directories (${MLIR_INCLUDE_DIR} )
53+ # Generated MLIR headers
4454 set (MLIR_GENERATED_INCLUDE_DIR ${LLVM_BINARY_DIR} /tools/mlir/include )
45- include_directories (SYSTEM ${MLIR_INCLUDE_DIR} )
46- include_directories (SYSTEM ${MLIR_GENERATED_INCLUDE_DIR} )
47- include_directories (SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR} )
55+ include_directories (${MLIR_GENERATED_INCLUDE_DIR} )
4856
49- set (BACKEND_PACKAGE_STRING "${PACKAGE_STRING} " )
5057 list (APPEND CMAKE_MODULE_PATH "${MLIR_MAIN_SRC_DIR} /cmake/modules" )
5158else ()
5259 message (STATUS "Building IMEX with external MLIR" )
@@ -66,17 +73,82 @@ else()
6673 message (STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR} " )
6774 message (STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR} " )
6875
76+ # For redirecting llvm_add_library() to IMEX's bin and lib folders
6977 set (LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR} /bin)
7078 set (LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR} /lib)
71- set (MLIR_BINARY_DIR ${CMAKE_BINARY_DIR} )
79+
80+ # LLVM_INCLUDE_DIRS is the same as MLIR_INCLUDE_DIRS in an MLIR install tree
81+ include_directories (${MLIR_INCLUDE_DIRS} )
7282
7383 list (APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR} " )
7484 list (APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR} " )
7585endif ()
7686
87+ # For redirecting add_mlir_doc() to IMEX's doc folder
88+ set (MLIR_BINARY_DIR ${CMAKE_BINARY_DIR} )
89+
7790include (TableGen)
7891include (AddLLVM)
7992include (AddMLIR)
93+
94+ # Instead of creating a separate AddIMEX,
95+ # redefine some functions included from include(AddMLIR)
96+ # and change the behavior of add_mlir_* functions to
97+ # properly generate IMEXConfig.cmake and IMEXTargets.cmake
98+ function (add_mlir_library_install name )
99+ get_target_export_arg(${name} IMEX export_to_mlirtargets UMBRELLA imex-libraries)
100+ install (TARGETS ${name}
101+ COMPONENT ${name}
102+ ${export_to_mlirtargets}
103+ LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
104+ ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
105+ RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR} "
106+ # Note that CMake will create a directory like:
107+ # objects-${CMAKE_BUILD_TYPE}/obj.LibName
108+ # and put object files there.
109+ OBJECTS DESTINATION lib${LLVM_LIBDIR_SUFFIX}
110+ )
111+
112+ add_llvm_install_targets(install -${name}
113+ DEPENDS ${name}
114+ COMPONENT ${name} )
115+ set_property (GLOBAL APPEND PROPERTY IMEX_ALL_LIBS ${name} )
116+ set_property (GLOBAL APPEND PROPERTY IMEX_EXPORTS ${name} )
117+ endfunction ()
118+
119+ # Declare the library associated with a dialect.
120+ function (add_mlir_dialect_library name )
121+ set_property (GLOBAL APPEND PROPERTY IMEX_DIALECT_LIBS ${name} )
122+ add_mlir_library(${ARGV} DEPENDS mlir-headers)
123+ endfunction (add_mlir_dialect_library)
124+
125+ # Declare the library associated with a conversion.
126+ function (add_mlir_conversion_library name )
127+ set_property (GLOBAL APPEND PROPERTY IMEX_CONVERSION_LIBS ${name} )
128+ add_mlir_library(${ARGV} DEPENDS mlir-headers)
129+ endfunction (add_mlir_conversion_library)
130+
131+ # Declare the library associated with a translation.
132+ function (add_mlir_translation_library name )
133+ set_property (GLOBAL APPEND PROPERTY IMEX_TRANSLATION_LIBS ${name} )
134+ add_mlir_library(${ARGV} DEPENDS mlir-headers)
135+ endfunction (add_mlir_translation_library)
136+
137+ macro (add_imex_tool name )
138+ add_llvm_executable(${name} ${ARGN} )
139+ get_target_export_arg(${name} IMEX export_to_imexexports)
140+ install (TARGETS ${name}
141+ ${export_to_imexexports}
142+ RUNTIME DESTINATION ${IMEX_TOOLS_INSTALL_DIR}
143+ COMPONENT ${name} )
144+
145+ add_llvm_install_targets(install -${name}
146+ DEPENDS ${name}
147+ COMPONENT ${name} )
148+ set_property (GLOBAL APPEND PROPERTY IMEX_EXPORTS ${name} )
149+ set_target_properties (${name} PROPERTIES FOLDER "Tools" )
150+ endmacro ()
151+
80152include (HandleLLVMOptions)
81153
82154set (IMEX_ENABLE_SYCL_RUNTIME 0 CACHE BOOL "Enable the Sycl Runtime" )
@@ -110,11 +182,11 @@ if(NOT IMEX_EXTERNAL_PROJECT_BUILD)
110182 COMPONENTS Interpreter)
111183endif ()
112184
113- include_directories (${LLVM_INCLUDE_DIRS} )
114- include_directories (${MLIR_INCLUDE_DIRS} )
185+ # IMEX headers in source tree
115186include_directories (${CMAKE_CURRENT_SOURCE_DIR} /include )
187+ # Generated IMEX headers
116188include_directories (${CMAKE_CURRENT_BINARY_DIR} /include )
117- link_directories ( ${LLVM_BUILD_LIBRARY_DIR} )
189+ message ( STATUS "LLVM_DEFINITIONS: ${LLVM_DEFINITIONS} " )
118190add_definitions (${LLVM_DEFINITIONS} )
119191
120192set (LLVM_LIT_ARGS "-sv" CACHE STRING "lit default options" )
@@ -128,6 +200,9 @@ else()
128200 set (IMEX_LIB_DIR ${IMEX_BINARY_DIR} /lib)
129201endif ()
130202
203+ set (IMEX_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR} " CACHE STRING
204+ "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR} ')" )
205+
131206include (sanitizers)
132207
133208add_subdirectory (include )
@@ -140,9 +215,14 @@ if (IMEX_INCLUDE_DOCS)
140215 add_subdirectory (docs)
141216endif ()
142217
143- install (DIRECTORY ${PROJECT_SOURCE_DIR} /include /mlir
218+ # Custom target to install all imex headers
219+ add_custom_target (imex-headers)
220+ set_target_properties (imex-headers PROPERTIES FOLDER "Misc" )
221+
222+ # Headers in source tree
223+ install (DIRECTORY include /imex
144224 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR} "
145- COMPONENT mlir -headers
225+ COMPONENT imex -headers
146226 FILES_MATCHING
147227 PATTERN "*.def"
148228 PATTERN "*.h"
@@ -151,9 +231,10 @@ install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/mlir
151231 PATTERN "LICENSE.TXT"
152232)
153233
154- install (DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} /include
234+ # Generated headers
235+ install (DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} /include /imex
155236 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR} "
156- COMPONENT mlir -headers
237+ COMPONENT imex -headers
157238 FILES_MATCHING
158239 PATTERN "*.def"
159240 PATTERN "*.h"
@@ -164,4 +245,26 @@ install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include
164245 PATTERN "config.h" EXCLUDE
165246)
166247
248+ add_llvm_install_targets(install -imex-headers
249+ DEPENDS imex-headers
250+ COMPONENT imex-headers)
251+
252+
253+ # Custom target to install all imex libraries
254+ add_custom_target (imex-libraries)
255+ set_target_properties (imex-libraries PROPERTIES FOLDER "Misc" )
256+
257+ add_llvm_install_targets(install -imex-libraries
258+ DEPENDS imex-libraries
259+ COMPONENT imex-libraries)
260+
261+ get_property (IMEX_LIBS GLOBAL PROPERTY IMEX_ALL_LIBS)
262+ if (IMEX_LIBS)
263+ list (REMOVE_DUPLICATES IMEX_LIBS)
264+ foreach (lib ${IMEX_LIBS} )
265+ add_dependencies (imex-libraries ${lib} )
266+ add_dependencies (install -imex-libraries install -${lib} )
267+ endforeach ()
268+ endif ()
269+
167270add_subdirectory (cmake/modules)
0 commit comments