diff --git a/llvm/cmake/modules/LLVMProjectOptions.cmake b/llvm/cmake/modules/LLVMProjectOptions.cmake new file mode 100644 --- /dev/null +++ b/llvm/cmake/modules/LLVMProjectOptions.cmake @@ -0,0 +1,68 @@ +# LLVM-style projects generally have the same directory structure. This file +# provides some bolierplate cmake support for projects that supports this +# directory structure. Note that generally speaking, projects should prefer +# to use their own rules for these rather than relying on the core llvm build +# targets. + +# Generally name should be lower case. +function(add_llvm_project_options name) + string(TOUPPER "${name}" uppername) + + # Define options to control the inclusion and default build behavior for + # components which may not strictly be necessary (tools, examples, and tests). + # + # This is primarily to support building smaller or faster project files. + option(${uppername}_INCLUDE_TOOLS + "Generate build targets for the ${uppername} tools." + ${LLVM_INCLUDE_TOOLS}) + option(${uppername}_BUILD_TOOLS + "Build the ${uppername} tools. If OFF, just generate build targets." + ${LLVM_BUILD_TOOLS}) + + option(${uppername}_INCLUDE_UTILS + "Generate build targets for the ${uppername} utils." + ${LLVM_INCLUDE_UTILS}) + option(${uppername}_BUILD_UTILS + "Build ${uppername} utility binaries. If OFF, just generate build targets." + ${LLVM_BUILD_UTILS}) + option(${uppername}_INSTALL_UTILS + "Include utility binaries in the 'install' target." + ${LLVM_INSTALL_UTILS}) + + # i.e. Don't install headers, for instance. + option(${uppername}_INSTALL_TOOLCHAIN_ONLY + "Only include toolchain files in the 'install' target." + ${LLVM_INSTALL_TOOLCHAIN_ONLY}) + + option(${uppername}_BUILD_EXAMPLES + "Build the ${uppername} example programs. If OFF, just generate build targets." + ${LLVM_BUILD_EXAMPLES}) + option(${uppername}_INCLUDE_EXAMPLES + "Generate build targets for the ${uppername} examples" + ${LLVM_INCLUDE_EXAMPLES}) + if(${uppername}_BUILD_EXAMPLES) + add_definitions(-DBUILD_EXAMPLES) + endif(${uppername}_BUILD_EXAMPLES) + + option(${uppername}_BUILD_TESTS + "Build ${uppername} unit tests. If OFF, just generate build targets." + ${LLVM_BUILD_TESTS}) + option(${uppername}_INCLUDE_TESTS + "Generate build targets for the ${uppername} unit tests." + ${LLVM_INCLUDE_TESTS}) + if (${uppername}_INCLUDE_TESTS) + add_definitions(-D${uppername}_INCLUDE_TESTS) + endif() + + option(${uppername}_INCLUDE_INTEGRATION_TESTS + "Generate build targets for the ${uppername} integration tests." + ${LLVM_INCLUDE_INTEGRATION_TESTS}) + if (${uppername}_INCLUDE_INTEGRATION_TESTS) + add_definitions(-D${uppername}_INCLUDE_INTEGRATION_TESTS) + endif() + + option(${uppername}_INCLUDE_DOCS + "Generate build targets for the ${uppername} docs." + ${LLVM_INCLUDE_DOCS}) + +endfunction(add_llvm_project_options) diff --git a/llvm/cmake/modules/LLVMProjectTargets.cmake b/llvm/cmake/modules/LLVMProjectTargets.cmake new file mode 100644 --- /dev/null +++ b/llvm/cmake/modules/LLVMProjectTargets.cmake @@ -0,0 +1,109 @@ +# For project foo, this function generates: +# add_foo_tool(name) (An executable installed by default) +# add_foo_utility(name) (An executable *not* installed by default) +# add_foo_example(name) (An executable which is built, but never installed) +# add_foo_example_library(name) (A library to go along with an example) + +# It also assumes the following configuration environment variables +# (see LLVMProjectOptions.cmake) +# FOO_TOOLS_INSTALL_DIR +# FOO_BUILD_TOOLS +# FOO_BUILD_UTILS +# FOO_INSTALL_UTILS +# FOO_BUILD_EXAMPLES +# FOO_HAS_EXPORTS +# FOO_INSTALL_TOOLCHAIN_ONLY + +function(add_llvm_project_targets projectname) + string(TOUPPER "${name}" upperprojectname) + + macro(add_${projectname}_tool name) + if( NOT ${upperprojectname}_BUILD_TOOLS ) + set(EXCLUDE_FROM_ALL ON) + endif() + add_llvm_executable(${name} ${ARGN}) + + if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT ${upperprojectname}_INSTALL_TOOLCHAIN_ONLY) + if( ${upperprojectname}_BUILD_TOOLS ) + set(export_to_${projectname}exports) + if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR + NOT LLVM_DISTRIBUTION_COMPONENTS) + set(export_to_${projectname}exports EXPORT ${upperprojectname}Exports) + set_property(GLOBAL PROPERTY ${upperprojectname}_HAS_EXPORTS True) + endif() + + install(TARGETS ${name} + ${export_to_${projectname}exports} + RUNTIME DESTINATION ${${upperprojectname}_TOOLS_INSTALL_DIR} + COMPONENT ${name}) + + if (NOT LLVM_ENABLE_IDE) + add_llvm_install_targets(install-${name} + DEPENDS ${name} + COMPONENT ${name}) + endif() + endif() + endif() + if( ${upperprojectname}_BUILD_TOOLS ) + set_property(GLOBAL APPEND PROPERTY ${upperprojectname}_EXPORTS ${name}) + endif() + set_target_properties(${name} PROPERTIES FOLDER "Tools") + endmacro(add_${projectname}_tool name) + + macro(add_${projectname}_example name) + if( NOT ${upperprojectname}_BUILD_EXAMPLES ) + set(EXCLUDE_FROM_ALL ON) + endif() + add_llvm_executable(${name} ${ARGN}) + if( ${upperprojectname}_BUILD_EXAMPLES ) + install(TARGETS ${name} RUNTIME DESTINATION examples) + endif() + set_target_properties(${name} PROPERTIES FOLDER "Examples") + endmacro(add_${projectname}_example name) + + macro(add_${projectname}_example_library name) + if( NOT ${upperprojectname}_BUILD_EXAMPLES ) + set(EXCLUDE_FROM_ALL ON) + add_llvm_library(${name} BUILDTREE_ONLY ${ARGN}) + else() + add_llvm_library(${name} ${ARGN}) + endif() + + set_target_properties(${name} PROPERTIES FOLDER "Examples") + endmacro(add_${projectname}_example_library name) + + # This is a macro that is used to create targets for executables that are needed + # for development, but that are not intended to be installed by default. + macro(add_${projectname}_utility name) + if ( NOT ${upperprojectname}_BUILD_UTILS ) + set(EXCLUDE_FROM_ALL ON) + endif() + + add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN}) + set_target_properties(${name} PROPERTIES FOLDER "Utils") + if (NOT ${upperprojectname}_INSTALL_TOOLCHAIN_ONLY) + if (${upperprojectname}_INSTALL_UTILS AND ${upperprojectname}_BUILD_UTILS) + set(export_to_${projectname}exports) + if (${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR + NOT LLVM_DISTRIBUTION_COMPONENTS) + set(export_to_${projectname}exports EXPORT ${upperprojectname}Exports) + set_property(GLOBAL PROPERTY ${upperprojectname}_HAS_EXPORTS True) + endif() + + install(TARGETS ${name} + ${export_to_${projectname}exports} + RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR} + COMPONENT ${name}) + + if (NOT LLVM_ENABLE_IDE) + add_llvm_install_targets(install-${name} + DEPENDS ${name} + COMPONENT ${name}) + endif() + set_property(GLOBAL APPEND PROPERTY ${upperprojectname}_EXPORTS ${name}) + elseif(${upperprojectname}_BUILD_UTILS) + set_property(GLOBAL APPEND PROPERTY ${upperprojectname}_EXPORTS_BUILDTREE_ONLY ${name}) + endif() + endif() + endmacro(add_${projectname}_utility name) +endfunction(add_llvm_project_targets) diff --git a/mlir/CMakeLists.txt b/mlir/CMakeLists.txt --- a/mlir/CMakeLists.txt +++ b/mlir/CMakeLists.txt @@ -21,6 +21,10 @@ add_dependencies(mlir-headers mlir-generic-headers) add_custom_target(mlir-doc) +# Get a bunch of LLVM-style default options. +include(LLVMProjectOptions) +add_llvm_project_options(mlir) + # Build the CUDA conversions and run according tests if the NVPTX backend # is available if ("NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD) @@ -44,13 +48,6 @@ set(MLIR_ROCM_RUNNER_ENABLED 0 CACHE BOOL "Enable building the mlir ROCm runner") set(MLIR_VULKAN_RUNNER_ENABLED 0 CACHE BOOL "Enable building the mlir Vulkan runner") -option(MLIR_INCLUDE_TESTS - "Generate build targets for the MLIR unit tests." - ${LLVM_INCLUDE_TESTS}) - -option(MLIR_INCLUDE_INTEGRATION_TESTS - "Generate build targets for the MLIR integration tests.") - #------------------------------------------------------------------------------- # Python Bindings Configuration # Requires: @@ -83,40 +80,44 @@ "extension = '${PYTHON_MODULE_EXTENSION}") endif() +# Get a bunch of default targets +include(LLVMProjectTargets) +add_llvm_project_targets(mlir) + include_directories( "include") include_directories( ${MLIR_INCLUDE_DIR}) # Adding tools/mlir-tblgen here as calling add_tablegen sets some variables like # MLIR_TABLEGEN_EXE in PARENT_SCOPE which gets lost if that folder is included # from another directory like tools -add_subdirectory(tools/mlir-tblgen) +if (MLIR_INCLUDE_TOOLS) + add_subdirectory(tools/mlir-tblgen) +endif() add_subdirectory(include/mlir) add_subdirectory(lib) if (MLIR_INCLUDE_TESTS) - add_definitions(-DMLIR_INCLUDE_TESTS) add_subdirectory(unittests) add_subdirectory(test) endif() if (MLIR_INCLUDE_INTEGRATION_TESTS) - add_definitions(-DMLIR_INCLUDE_INTEGRATION_TESTS) add_subdirectory(integration_test) endif() # Tools needs to come late to ensure that MLIR_ALL_LIBS is populated. # Generally things after this point may depend on MLIR_ALL_LIBS or libMLIR.so. -add_subdirectory(tools) +if (MLIR_INCLUDE_TOOLS) + add_subdirectory(tools) +endif() -if( LLVM_INCLUDE_EXAMPLES ) +if (MLIR_INCLUDE_EXAMPLES) add_subdirectory(examples) endif() -option(MLIR_INCLUDE_DOCS "Generate build targets for the MLIR docs." - ${LLVM_INCLUDE_DOCS}) if (MLIR_INCLUDE_DOCS) add_subdirectory(docs) endif() -if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY) +if (NOT MLIR_INSTALL_TOOLCHAIN_ONLY) install(DIRECTORY include/mlir include/mlir-c DESTINATION include COMPONENT mlir-headers 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 @@ -40,7 +40,7 @@ endfunction() # Declare an mlir library which can be compiled in libMLIR.so -# In addition to everything that llvm_add_librar accepts, this +# 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 diff --git a/mlir/examples/standalone/CMakeLists.txt b/mlir/examples/standalone/CMakeLists.txt --- a/mlir/examples/standalone/CMakeLists.txt +++ b/mlir/examples/standalone/CMakeLists.txt @@ -32,8 +32,17 @@ include(TableGen) include(AddLLVM) include(AddMLIR) + +# Get a bunch of LLVM-style default options. +include(LLVMProjectOptions) +add_llvm_project_options(standalone) + include(HandleLLVMOptions) +# Get a bunch of default targets +include(LLVMProjectTargets) +add_llvm_project_targets(standalone) + include_directories(${LLVM_INCLUDE_DIRS}) include_directories(${MLIR_INCLUDE_DIRS}) include_directories(${PROJECT_SOURCE_DIR}/include) diff --git a/mlir/examples/standalone/standalone-opt/CMakeLists.txt b/mlir/examples/standalone/standalone-opt/CMakeLists.txt --- a/mlir/examples/standalone/standalone-opt/CMakeLists.txt +++ b/mlir/examples/standalone/standalone-opt/CMakeLists.txt @@ -6,7 +6,7 @@ MLIROptLib MLIRStandalone ) -add_llvm_executable(standalone-opt standalone-opt.cpp) +add_standalone_tool(standalone-opt standalone-opt.cpp) llvm_update_compile_flags(standalone-opt) target_link_libraries(standalone-opt PRIVATE ${LIBS}) diff --git a/mlir/examples/standalone/standalone-translate/CMakeLists.txt b/mlir/examples/standalone/standalone-translate/CMakeLists.txt --- a/mlir/examples/standalone/standalone-translate/CMakeLists.txt +++ b/mlir/examples/standalone/standalone-translate/CMakeLists.txt @@ -5,7 +5,7 @@ get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) get_property(translation_libs GLOBAL PROPERTY MLIR_TRANSLATION_LIBS) -add_llvm_executable(standalone-translate +add_standalone_tool(standalone-translate standalone-translate.cpp ) llvm_update_compile_flags(standalone-translate) diff --git a/mlir/examples/toy/CMakeLists.txt b/mlir/examples/toy/CMakeLists.txt --- a/mlir/examples/toy/CMakeLists.txt +++ b/mlir/examples/toy/CMakeLists.txt @@ -3,7 +3,7 @@ macro(add_toy_chapter name) add_dependencies(Toy ${name}) - add_llvm_example(${name} ${ARGN}) + add_mlir_example(${name} ${ARGN}) endmacro(add_toy_chapter name) add_subdirectory(Ch1) diff --git a/mlir/tools/mlir-cpu-runner/CMakeLists.txt b/mlir/tools/mlir-cpu-runner/CMakeLists.txt --- a/mlir/tools/mlir-cpu-runner/CMakeLists.txt +++ b/mlir/tools/mlir-cpu-runner/CMakeLists.txt @@ -4,7 +4,7 @@ nativecodegen ) -add_llvm_tool(mlir-cpu-runner +add_mlir_tool(mlir-cpu-runner mlir-cpu-runner.cpp ) llvm_update_compile_flags(mlir-cpu-runner) diff --git a/mlir/tools/mlir-cuda-runner/CMakeLists.txt b/mlir/tools/mlir-cuda-runner/CMakeLists.txt --- a/mlir/tools/mlir-cuda-runner/CMakeLists.txt +++ b/mlir/tools/mlir-cuda-runner/CMakeLists.txt @@ -68,7 +68,7 @@ LIST(APPEND targets_to_link "LLVM${t}") ENDFOREACH(t) - add_llvm_tool(mlir-cuda-runner + add_mlir_tool(mlir-cuda-runner mlir-cuda-runner.cpp DEPENDS diff --git a/mlir/tools/mlir-linalg-ods-gen/CMakeLists.txt b/mlir/tools/mlir-linalg-ods-gen/CMakeLists.txt --- a/mlir/tools/mlir-linalg-ods-gen/CMakeLists.txt +++ b/mlir/tools/mlir-linalg-ods-gen/CMakeLists.txt @@ -2,7 +2,7 @@ Core Support ) -add_llvm_tool(mlir-linalg-ods-gen +add_mlir_tool(mlir-linalg-ods-gen mlir-linalg-ods-gen.cpp ) llvm_update_compile_flags(mlir-linalg-ods-gen) diff --git a/mlir/tools/mlir-opt/CMakeLists.txt b/mlir/tools/mlir-opt/CMakeLists.txt --- a/mlir/tools/mlir-opt/CMakeLists.txt +++ b/mlir/tools/mlir-opt/CMakeLists.txt @@ -50,7 +50,7 @@ ${LIBS} ) -add_llvm_tool(mlir-opt +add_mlir_tool(mlir-opt mlir-opt.cpp DEPENDS diff --git a/mlir/tools/mlir-reduce/CMakeLists.txt b/mlir/tools/mlir-reduce/CMakeLists.txt --- a/mlir/tools/mlir-reduce/CMakeLists.txt +++ b/mlir/tools/mlir-reduce/CMakeLists.txt @@ -31,7 +31,7 @@ MLIRTransformUtils ) -add_llvm_tool(mlir-reduce +add_mlir_tool(mlir-reduce mlir-reduce.cpp ) diff --git a/mlir/tools/mlir-rocm-runner/CMakeLists.txt b/mlir/tools/mlir-rocm-runner/CMakeLists.txt --- a/mlir/tools/mlir-rocm-runner/CMakeLists.txt +++ b/mlir/tools/mlir-rocm-runner/CMakeLists.txt @@ -104,7 +104,7 @@ LIST(APPEND targets_to_link "LLVM${t}") ENDFOREACH(t) - add_llvm_tool(mlir-rocm-runner + add_mlir_tool(mlir-rocm-runner mlir-rocm-runner.cpp DEPENDS diff --git a/mlir/tools/mlir-translate/CMakeLists.txt b/mlir/tools/mlir-translate/CMakeLists.txt --- a/mlir/tools/mlir-translate/CMakeLists.txt +++ b/mlir/tools/mlir-translate/CMakeLists.txt @@ -5,7 +5,7 @@ get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) get_property(translation_libs GLOBAL PROPERTY MLIR_TRANSLATION_LIBS) -add_llvm_tool(mlir-translate +add_mlir_tool(mlir-translate mlir-translate.cpp ) llvm_update_compile_flags(mlir-translate) diff --git a/mlir/tools/mlir-vulkan-runner/CMakeLists.txt b/mlir/tools/mlir-vulkan-runner/CMakeLists.txt --- a/mlir/tools/mlir-vulkan-runner/CMakeLists.txt +++ b/mlir/tools/mlir-vulkan-runner/CMakeLists.txt @@ -85,7 +85,7 @@ LIST(APPEND targets_to_link "LLVM${t}") ENDFOREACH(t) - add_llvm_tool(mlir-vulkan-runner + add_mlir_tool(mlir-vulkan-runner mlir-vulkan-runner.cpp ) add_dependencies(mlir-vulkan-runner vulkan-runtime-wrappers)