1# 2# Copyright 2020 The TensorFlow Authors. All Rights Reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16cmake_minimum_required(VERSION 3.13.4) 17 18if(POLICY CMP0068) 19 cmake_policy(SET CMP0068 NEW) 20 set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON) 21endif() 22 23if(POLICY CMP0075) 24 cmake_policy(SET CMP0075 NEW) 25endif() 26 27if(POLICY CMP0077) 28 cmake_policy(SET CMP0077 NEW) 29endif() 30 31#------------------------------------------------------------------------------- 32# Options and settings 33#------------------------------------------------------------------------------- 34option(MHLO_BUILD_EMBEDDED "Build MHLO as part of another project" OFF) 35option(MHLO_ENABLE_BINDINGS_PYTHON "Enables MHLO python bindings" OFF) 36 37#------------------------------------------------------------------------------- 38# Project setup and globals 39#------------------------------------------------------------------------------- 40set(MHLO_EXTERNAL_PROJECT_BUILD OFF) 41 42if(PROJECT_NAME STREQUAL "LLVM") 43 # Building as part of LLVM via the external project mechanism. 44 set(MHLO_EXTERNAL_PROJECT_BUILD ON) 45else() 46 # Building standalone. 47 project(mlir-hlo LANGUAGES CXX C) 48 set(CMAKE_C_STANDARD 11) 49 set(CMAKE_CXX_STANDARD 14) 50 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") 51endif() 52 53if(MHLO_ENABLE_BINDINGS_PYTHON AND NOT MHLO_EXTERNAL_PROJECT_BUILD) 54 message(WARNING "MHLO python bindings are only supported in unified, external project builds") 55endif() 56 57#------------------------------------------------------------------------------- 58# MLIR/LLVM Configuration 59#------------------------------------------------------------------------------- 60 61# Find MLIR to install if we are building standalone. If building as part of 62# another project, let it handle the MLIR dependency. The dependent project 63# might use a bundled version of MLIR instead of installing, for instance. 64if(MHLO_EXTERNAL_PROJECT_BUILD) 65 message(STATUS "Building MHLO as an external LLVM project") 66 set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir ) # --src-root 67 set(MLIR_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR}/include ) # --includedir 68 set(MLIR_GENERATED_INCLUDE_DIR ${LLVM_BINARY_DIR}/tools/mlir/include) 69 set(MLIR_TABLEGEN_EXE $<TARGET_FILE:mlir-tblgen>) 70 include_directories(SYSTEM ${MLIR_INCLUDE_DIR}) 71 include_directories(SYSTEM ${MLIR_GENERATED_INCLUDE_DIR}) 72 include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR}) 73 74 set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}") 75 list(APPEND CMAKE_MODULE_PATH "${MLIR_MAIN_SRC_DIR}/cmake/modules") 76elseif(NOT MHLO_BUILD_EMBEDDED) 77 message(STATUS "Building MHLO with an installed MLIR") 78 find_package(MLIR REQUIRED CONFIG) 79 message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}") 80 message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") 81 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin) 82 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib) 83 list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}") 84 list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}") 85else() 86 message(STATUS "Building MHLO embedded in another project") 87endif() 88 89if(LLVM_ENABLE_ZLIB) 90 find_package(ZLIB) 91endif() 92 93include(TableGen) 94include(AddLLVM) 95include(AddMLIR) 96include(HandleLLVMOptions) 97include_directories(${LLVM_INCLUDE_DIRS}) 98include_directories(${MLIR_INCLUDE_DIRS}) 99include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) 100include_directories(${CMAKE_CURRENT_BINARY_DIR}/include) 101include_directories(${CMAKE_CURRENT_BINARY_DIR}/) 102link_directories(${LLVM_BUILD_LIBRARY_DIR}) 103add_definitions(${LLVM_DEFINITIONS}) 104 105#------------------------------------------------------------------------------- 106# Python configuration 107#------------------------------------------------------------------------------- 108 109if(MHLO_ENABLE_BINDINGS_PYTHON) 110 include(MLIRDetectPythonEnv) 111 mlir_detect_pybind11_install() 112 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} 113 COMPONENTS Interpreter Development NumPy REQUIRED) 114 find_package(pybind11 2.6 CONFIG REQUIRED) 115endif() 116 117#------------------------------------------------------------------------------- 118# Directory setup 119#------------------------------------------------------------------------------- 120 121set(MLIR_HLO_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 122set(MLIR_HLO_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) 123 124add_custom_target(check-mlir-hlo) 125 126add_subdirectory(include/mlir-hlo) 127add_subdirectory(lib) 128add_subdirectory(tools) 129add_subdirectory(tests) 130 131if(MHLO_ENABLE_BINDINGS_PYTHON) 132 add_subdirectory(python) 133endif() 134