Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 1 | # Copyright 2019 The IREE Authors |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 2 | # |
Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 3 | # Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 | # See https://llvm.org/LICENSE.txt for license information. |
| 5 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 6 | |
| 7 | include(CMakeParseArguments) |
| 8 | |
| 9 | # iree_tablegen_library() |
| 10 | # |
| 11 | # Runs iree-tablegen to produce some artifacts. |
| 12 | function(iree_tablegen_library) |
| 13 | cmake_parse_arguments( |
Ben Vanik | 6b112ef | 2019-10-03 10:45:14 -0700 | [diff] [blame] | 14 | _RULE |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 15 | "TESTONLY" |
Marius Brehler | 80d9abe | 2019-12-11 08:44:38 -0800 | [diff] [blame] | 16 | "NAME;TBLGEN" |
Marius Brehler | fa1dbd8 | 2020-01-28 14:45:50 -0800 | [diff] [blame] | 17 | "TD_FILE;OUTS" |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 18 | ${ARGN} |
| 19 | ) |
| 20 | |
Lei Zhang | d454ee4 | 2020-06-02 11:48:57 -0700 | [diff] [blame] | 21 | if(_RULE_TESTONLY AND NOT IREE_BUILD_TESTS) |
| 22 | return() |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 23 | endif() |
Lei Zhang | d454ee4 | 2020-06-02 11:48:57 -0700 | [diff] [blame] | 24 | |
| 25 | # Prefix the library with the package name, so we get: iree_package_name |
| 26 | iree_package_name(_PACKAGE_NAME) |
| 27 | set(_NAME "${_PACKAGE_NAME}_${_RULE_NAME}") |
| 28 | |
| 29 | if(${_RULE_TBLGEN} MATCHES "IREE") |
| 30 | set(_TBLGEN "IREE") |
| 31 | else() |
| 32 | set(_TBLGEN "MLIR") |
| 33 | endif() |
| 34 | |
| 35 | set(LLVM_TARGET_DEFINITIONS ${_RULE_TD_FILE}) |
Ben Vanik | 6bc6f90 | 2020-11-16 05:37:08 -0800 | [diff] [blame] | 36 | set(_INCLUDE_DIRS |
| 37 | "${MLIR_INCLUDE_DIRS}" |
Stella Laurenzo | 41a2ceb | 2022-04-29 12:49:36 -0700 | [diff] [blame] | 38 | "${IREE_SOURCE_DIR}/compiler/src" |
| 39 | "${IREE_BINARY_DIR}/compiler/src" |
Ben Vanik | 6bc6f90 | 2020-11-16 05:37:08 -0800 | [diff] [blame] | 40 | ) |
Stella Laurenzo | be0f1e1 | 2023-04-03 13:34:38 -0700 | [diff] [blame] | 41 | if(DEFINED IREE_COMPILER_TABLEGEN_INCLUDE_DIRS) |
| 42 | list(APPEND _INCLUDE_DIRS ${IREE_COMPILER_TABLEGEN_INCLUDE_DIRS}) |
| 43 | endif() |
Lei Zhang | d454ee4 | 2020-06-02 11:48:57 -0700 | [diff] [blame] | 44 | list(APPEND _INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}) |
| 45 | list(TRANSFORM _INCLUDE_DIRS PREPEND "-I") |
| 46 | set(_OUTPUTS) |
| 47 | while(_RULE_OUTS) |
Ben Vanik | 7bc983c | 2022-07-14 09:39:19 -0700 | [diff] [blame] | 48 | # Eat any number of flags (--a --b ...) and a single file. |
| 49 | # Flags only impact the successively declared file. |
| 50 | set(_COMMAND) |
| 51 | set(_FILE) |
| 52 | while(_RULE_OUTS AND NOT _FILE) |
| 53 | list(GET _RULE_OUTS 0 _PART) |
| 54 | list(REMOVE_AT _RULE_OUTS 0) |
| 55 | if(${_PART} MATCHES "^-.*") |
| 56 | # Flag (- or --). |
| 57 | list(APPEND _COMMAND ${_PART}) |
| 58 | else() |
| 59 | # File path. |
| 60 | set(_FILE ${_PART}) |
| 61 | endif() |
| 62 | endwhile() |
Lei Zhang | d454ee4 | 2020-06-02 11:48:57 -0700 | [diff] [blame] | 63 | tablegen(${_TBLGEN} ${_FILE} ${_COMMAND} ${_INCLUDE_DIRS}) |
| 64 | list(APPEND _OUTPUTS ${CMAKE_CURRENT_BINARY_DIR}/${_FILE}) |
| 65 | endwhile() |
| 66 | add_custom_target(${_NAME}_target DEPENDS ${_OUTPUTS}) |
| 67 | set_target_properties(${_NAME}_target PROPERTIES FOLDER "Tablegenning") |
| 68 | |
| 69 | add_library(${_NAME} INTERFACE) |
| 70 | add_dependencies(${_NAME} ${_NAME}_target) |
| 71 | |
| 72 | # Alias the iree_package_name library to iree::package::name. |
| 73 | iree_package_ns(_PACKAGE_NS) |
| 74 | add_library(${_PACKAGE_NS}::${_RULE_NAME} ALIAS ${_NAME}) |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 75 | endfunction() |