diff --git a/mlir/cmake/modules/AddMLIR.cmake b/mlir/cmake/modules/AddMLIR.cmake --- a/mlir/cmake/modules/AddMLIR.cmake +++ b/mlir/cmake/modules/AddMLIR.cmake @@ -201,25 +201,9 @@ add_dependencies(mlir-doc ${output_file}DocGen) endfunction() -# Declare an mlir library which can be compiled in libMLIR.so -# In addition to everything that llvm_add_library accepts, this -# also has the following option: -# EXCLUDE_FROM_LIBMLIR -# Don't include this library in libMLIR.so. This option should be used -# for test libraries, executable-specific libraries, or rarely used libraries -# with large dependencies. -# ENABLE_AGGREGATION -# Forces generation of an OBJECT library, exports additional metadata, -# and installs additional object files needed to include this as part of an -# aggregate shared library. -# TODO: Make this the default for all MLIR libraries once all libraries -# are compatible with building an object library. -function(add_mlir_library name) - cmake_parse_arguments(ARG - "SHARED;INSTALL_WITH_TOOLCHAIN;EXCLUDE_FROM_LIBMLIR;DISABLE_INSTALL;ENABLE_AGGREGATION" - "" - "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS" - ${ARGN}) +# Sets ${srcs} to contain the list of additional headers for the target. Extra +# arguments are included into the list of additional headers. +function(_set_mlir_additional_headers_as_srcs) set(srcs) if(MSVC_IDE OR XCODE) # Add public headers @@ -245,13 +229,83 @@ endif() endif() endif(MSVC_IDE OR XCODE) - if(srcs OR ARG_ADDITIONAL_HEADERS) + if(srcs OR ARGN) set(srcs ADDITIONAL_HEADERS ${srcs} - ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args. + ${ARGN} # It may contain unparsed unknown args. + PARENT_SCOPE ) endif() +endfunction() + +# Checks that the LLVM components are not listed in the extra arguments, +# assumed to be coming from the LINK_LIBS variable. +function(_check_llvm_components_usage name) + # LINK_COMPONENTS is necessary to allow libLLVM.so to be properly + # substituted for individual library dependencies if LLVM_LINK_LLVM_DYLIB + # Perhaps this should be in llvm_add_library instead? However, it fails + # on libclang-cpp.so + get_property(llvm_component_libs GLOBAL PROPERTY LLVM_COMPONENT_LIBS) + foreach(lib ${ARGN}) + if(${lib} IN_LIST llvm_component_libs) + message(SEND_ERROR "${name} specifies LINK_LIBS ${lib}, but LINK_LIBS cannot be used for LLVM libraries. Please use LINK_COMPONENTS instead.") + endif() + endforeach() +endfunction() + +function(add_mlir_example_library name) + cmake_parse_arguments(ARG + "SHARED;DISABLE_INSTALL" + "" + "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS" + ${ARGN}) + _set_mlir_additional_headers_as_srcs(${ARG_ADDITIONAL_HEADERS}) + if (ARG_SHARED) + set(LIBTYPE SHARED) + else() + if(BUILD_SHARED_LIBS) + set(LIBTYPE SHARED) + else() + set(LIBTYPE STATIC) + endif() + endif() + + # MLIR libraries uniformly depend on LLVMSupport. Just specify it once here. + list(APPEND ARG_LINK_COMPONENTS Support) + _check_llvm_components_usage(${name} ${ARG_LINK_LIBS}) + + list(APPEND ARG_DEPENDS mlir-generic-headers) + + llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs} DEPENDS ${ARG_DEPENDS} LINK_COMPONENTS ${ARG_LINK_COMPONENTS} LINK_LIBS ${ARG_LINK_LIBS}) + set_target_properties(${name} PROPERTIES FOLDER "Examples") + if (LLVM_BUILD_EXAMPLES AND NOT ${ARG_DISABLE_INSTALL}) + add_mlir_library_install(${name}) + else() + set_target_properties(${name} PROPERTIES EXCLUDE_FROM_ALL ON) + endif() +endfunction() + +# Declare an mlir library which can be compiled in libMLIR.so +# In addition to everything that llvm_add_library accepts, this +# also has the following option: +# EXCLUDE_FROM_LIBMLIR +# Don't include this library in libMLIR.so. This option should be used +# for test libraries, executable-specific libraries, or rarely used libraries +# with large dependencies. +# ENABLE_AGGREGATION +# Forces generation of an OBJECT library, exports additional metadata, +# and installs additional object files needed to include this as part of an +# aggregate shared library. +# TODO: Make this the default for all MLIR libraries once all libraries +# are compatible with building an object library. +function(add_mlir_library name) + cmake_parse_arguments(ARG + "SHARED;INSTALL_WITH_TOOLCHAIN;EXCLUDE_FROM_LIBMLIR;DISABLE_INSTALL;ENABLE_AGGREGATION" + "" + "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS" + ${ARGN}) + _set_mlir_additional_headers_as_srcs(${ARG_ADDITIONAL_HEADERS}) # Is an object library needed. set(NEEDS_OBJECT_LIB OFF) @@ -287,17 +341,7 @@ # MLIR libraries uniformly depend on LLVMSupport. Just specify it once here. list(APPEND ARG_LINK_COMPONENTS Support) - - # LINK_COMPONENTS is necessary to allow libLLVM.so to be properly - # substituted for individual library dependencies if LLVM_LINK_LLVM_DYLIB - # Perhaps this should be in llvm_add_library instead? However, it fails - # on libclang-cpp.so - get_property(llvm_component_libs GLOBAL PROPERTY LLVM_COMPONENT_LIBS) - foreach(lib ${ARG_LINK_LIBS}) - if(${lib} IN_LIST llvm_component_libs) - message(SEND_ERROR "${name} specifies LINK_LIBS ${lib}, but LINK_LIBS cannot be used for LLVM libraries. Please use LINK_COMPONENTS instead.") - endif() - endforeach() + _check_llvm_components_usage(${name} ${ARG_LINK_LIBS}) list(APPEND ARG_DEPENDS mlir-generic-headers) llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs} DEPENDS ${ARG_DEPENDS} LINK_COMPONENTS ${ARG_LINK_COMPONENTS} LINK_LIBS ${ARG_LINK_LIBS}) diff --git a/mlir/examples/transform/Ch2/lib/CMakeLists.txt b/mlir/examples/transform/Ch2/lib/CMakeLists.txt --- a/mlir/examples/transform/Ch2/lib/CMakeLists.txt +++ b/mlir/examples/transform/Ch2/lib/CMakeLists.txt @@ -1,14 +1,11 @@ -add_mlir_library( +# Outside examples, this should be `add_mlir_library`. +add_mlir_example_library( # Library called MyExtension. MyExtensionCh2 # Built from the following source files. MyExtension.cpp - # Do not make this part of the main library distribution. - # Needed for the example only. - EXCLUDE_FROM_LIBMLIR - # Make includes visible without top-level path. ADDITIONAL_HEADER_DIRS ${PROJECT_SOURCE_DIR}/examples/transform/Ch2/include @@ -18,7 +15,7 @@ MyExtensionCh2IncGen # Link in the transform dialect, an all generated dialects. - LINK_LIBS PUBLIC + LINK_LIBS PRIVATE MLIRTransformDialect MLIRFuncDialect MLIRSCFDialect diff --git a/mlir/examples/transform/Ch3/lib/CMakeLists.txt b/mlir/examples/transform/Ch3/lib/CMakeLists.txt --- a/mlir/examples/transform/Ch3/lib/CMakeLists.txt +++ b/mlir/examples/transform/Ch3/lib/CMakeLists.txt @@ -1,14 +1,11 @@ -add_mlir_library( +# Outside examples, this should be `add_mlir_library`. +add_mlir_example_library( # Library called MyExtension. MyExtensionCh3 # Built from the following source files. MyExtension.cpp - # Do not make this part of the main library distribution. - # Needed for the example only. - EXCLUDE_FROM_LIBMLIR - # Make includes visible without top-level path. ADDITIONAL_HEADER_DIRS ${PROJECT_SOURCE_DIR}/examples/transform/Ch3/include @@ -18,7 +15,7 @@ MyExtensionCh3IncGen # Link in the transform dialect, an all generated dialects. - LINK_LIBS PUBLIC + LINK_LIBS PRIVATE MLIRTransformDialect MLIRFuncDialect MLIRSCFDialect