Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 1 | # Copyright 2020 The IREE Authors |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [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 |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 6 | |
| 7 | include(CMakeParseArguments) |
| 8 | |
| 9 | ############################################################################### |
| 10 | # Main user rules |
| 11 | ############################################################################### |
| 12 | |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 13 | # iree_py_library() |
| 14 | # |
| 15 | # CMake function to imitate Bazel's iree_py_library rule. |
| 16 | # |
| 17 | # Parameters: |
| 18 | # NAME: name of target |
| 19 | # SRCS: List of source files for the library |
| 20 | # DEPS: List of other targets the test python libraries require |
| 21 | # PYEXT_DEPS: List of deps of extensions built with iree_pyext_module |
| 22 | function(iree_py_library) |
| 23 | cmake_parse_arguments( |
Scott Todd | 1a1aea6 | 2022-04-28 09:12:11 -0700 | [diff] [blame] | 24 | _RULE |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 25 | "" |
| 26 | "NAME" |
| 27 | "SRCS;DEPS;PYEXT_DEPS" |
| 28 | ${ARGN} |
| 29 | ) |
| 30 | |
| 31 | iree_package_ns(_PACKAGE_NS) |
| 32 | # Replace dependencies passed by ::name with ::iree::package::name |
Scott Todd | 1a1aea6 | 2022-04-28 09:12:11 -0700 | [diff] [blame] | 33 | list(TRANSFORM _RULE_DEPS REPLACE "^::" "${_PACKAGE_NS}::") |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 34 | |
| 35 | iree_package_name(_PACKAGE_NAME) |
Scott Todd | 1a1aea6 | 2022-04-28 09:12:11 -0700 | [diff] [blame] | 36 | set(_NAME "${_PACKAGE_NAME}_${_RULE_NAME}") |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 37 | |
| 38 | add_custom_target(${_NAME} ALL |
Scott Todd | 1a1aea6 | 2022-04-28 09:12:11 -0700 | [diff] [blame] | 39 | DEPENDS ${_RULE_DEPS} |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 40 | ) |
| 41 | |
| 42 | # Symlink each file as its own target. |
Scott Todd | 1a1aea6 | 2022-04-28 09:12:11 -0700 | [diff] [blame] | 43 | foreach(_SRC_FILE ${_RULE_SRCS}) |
| 44 | # _SRC_FILE could have other path components in it, so we need to make a |
Geoffrey Martin-Noble | fd8a769 | 2021-08-20 05:54:39 -0700 | [diff] [blame] | 45 | # directory for it. Ninja does this automatically, but make doesn't. See |
Scott Todd | 3f51a55 | 2024-04-19 11:00:27 -0700 | [diff] [blame] | 46 | # https://github.com/iree-org/iree/issues/6801 |
Scott Todd | 1a1aea6 | 2022-04-28 09:12:11 -0700 | [diff] [blame] | 47 | set(_SRC_BIN_PATH "${CMAKE_CURRENT_BINARY_DIR}/${_SRC_FILE}") |
Geoffrey Martin-Noble | fd8a769 | 2021-08-20 05:54:39 -0700 | [diff] [blame] | 48 | get_filename_component(_SRC_BIN_DIR "${_SRC_BIN_PATH}" DIRECTORY) |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 49 | add_custom_command( |
| 50 | TARGET ${_NAME} |
Geoffrey Martin-Noble | fd8a769 | 2021-08-20 05:54:39 -0700 | [diff] [blame] | 51 | COMMAND |
| 52 | ${CMAKE_COMMAND} -E make_directory "${_SRC_BIN_DIR}" |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 53 | COMMAND ${CMAKE_COMMAND} -E create_symlink |
Scott Todd | 1a1aea6 | 2022-04-28 09:12:11 -0700 | [diff] [blame] | 54 | "${CMAKE_CURRENT_SOURCE_DIR}/${_SRC_FILE}" "${_SRC_BIN_PATH}" |
Geoffrey Martin-Noble | fd8a769 | 2021-08-20 05:54:39 -0700 | [diff] [blame] | 55 | BYPRODUCTS "${_SRC_BIN_PATH}" |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 56 | ) |
| 57 | endforeach() |
| 58 | |
Lei Zhang | 739a596 | 2021-05-17 15:12:58 -0400 | [diff] [blame] | 59 | # Add PYEXT_DEPS if any. |
Scott Todd | 1a1aea6 | 2022-04-28 09:12:11 -0700 | [diff] [blame] | 60 | if(_RULE_PYEXT_DEPS) |
| 61 | list(TRANSFORM _RULE_PYEXT_DEPS REPLACE "^::" "${_PACKAGE_NS}::") |
| 62 | add_dependencies(${_NAME} ${_RULE_PYEXT_DEPS}) |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 63 | endif() |
| 64 | endfunction() |
| 65 | |
Jerry Wu | 5a8e738 | 2022-04-20 20:20:40 +0000 | [diff] [blame] | 66 | # iree_local_py_test() |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 67 | # |
Jerry Wu | 5a8e738 | 2022-04-20 20:20:40 +0000 | [diff] [blame] | 68 | # CMake function to run python test with provided python package paths. |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 69 | # |
| 70 | # Parameters: |
| 71 | # NAME: name of test |
Jerry Wu | 5a8e738 | 2022-04-20 20:20:40 +0000 | [diff] [blame] | 72 | # SRC: Test source file |
Geoffrey Martin-Noble | f9cda5a | 2021-03-16 22:18:01 -0700 | [diff] [blame] | 73 | # ARGS: Command line arguments to the Python source file. |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 74 | # LABELS: Additional labels to apply to the test. The package path is added |
| 75 | # automatically. |
Stella Laurenzo | 0a8a944 | 2021-06-14 22:42:37 -0700 | [diff] [blame] | 76 | # GENERATED_IN_BINARY_DIR: If present, indicates that the srcs have been |
| 77 | # in the CMAKE_CURRENT_BINARY_DIR. |
Jerry Wu | 5a8e738 | 2022-04-20 20:20:40 +0000 | [diff] [blame] | 78 | # PACKAGE_DIRS: Python package paths to be added to PYTHONPATH. |
| 79 | function(iree_local_py_test) |
Scott Todd | a6b96c1 | 2022-06-06 11:10:11 -0700 | [diff] [blame] | 80 | if(NOT IREE_BUILD_TESTS OR ANDROID OR EMSCRIPTEN) |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 81 | return() |
| 82 | endif() |
| 83 | |
| 84 | cmake_parse_arguments( |
| 85 | _RULE |
Stella Laurenzo | 0a8a944 | 2021-06-14 22:42:37 -0700 | [diff] [blame] | 86 | "GENERATED_IN_BINARY_DIR" |
Jerry Wu | 5a8e738 | 2022-04-20 20:20:40 +0000 | [diff] [blame] | 87 | "NAME;SRC" |
Rob Suderman | f900f50 | 2022-06-07 12:26:06 -0700 | [diff] [blame] | 88 | "ARGS;LABELS;PACKAGE_DIRS;TIMEOUT" |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 89 | ${ARGN} |
| 90 | ) |
| 91 | |
Stella Laurenzo | 0a8a944 | 2021-06-14 22:42:37 -0700 | [diff] [blame] | 92 | # Switch between source and generated tests. |
| 93 | set(_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}") |
| 94 | if(_RULE_GENERATED_IN_BINARY_DIR) |
| 95 | set(_SRC_DIR "${CMAKE_CURRENT_BINARY_DIR}") |
| 96 | endif() |
| 97 | |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 98 | iree_package_name(_PACKAGE_NAME) |
| 99 | set(_NAME "${_PACKAGE_NAME}_${_RULE_NAME}") |
| 100 | |
| 101 | iree_package_ns(_PACKAGE_NS) |
| 102 | string(REPLACE "::" "/" _PACKAGE_PATH ${_PACKAGE_NS}) |
| 103 | set(_NAME_PATH "${_PACKAGE_PATH}/${_RULE_NAME}") |
| 104 | list(APPEND _RULE_LABELS "${_PACKAGE_PATH}") |
| 105 | |
Stella Laurenzo | 2d82fb7 | 2021-10-21 20:52:33 -0700 | [diff] [blame] | 106 | add_test( |
| 107 | NAME ${_NAME_PATH} |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 108 | COMMAND |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 109 | "${Python3_EXECUTABLE}" |
Jerry Wu | 5a8e738 | 2022-04-20 20:20:40 +0000 | [diff] [blame] | 110 | "${CMAKE_CURRENT_SOURCE_DIR}/${_RULE_SRC}" |
Geoffrey Martin-Noble | 801c108 | 2021-08-18 18:50:48 -0700 | [diff] [blame] | 111 | ${_RULE_ARGS} |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 112 | ) |
| 113 | |
Scott Todd | a29e67a | 2022-11-07 17:00:33 -0800 | [diff] [blame] | 114 | set_property(TEST ${_NAME_PATH} PROPERTY LABELS "${_RULE_LABELS}") |
| 115 | set_property(TEST ${_NAME_PATH} PROPERTY TIMEOUT ${_RULE_ARGS}) |
| 116 | |
| 117 | # Extend the PYTHONPATH environment variable with _RULE_PACKAGE_DIRS. |
Jerry Wu | 5a8e738 | 2022-04-20 20:20:40 +0000 | [diff] [blame] | 118 | list(APPEND _RULE_PACKAGE_DIRS "$ENV{PYTHONPATH}") |
Scott Todd | a29e67a | 2022-11-07 17:00:33 -0800 | [diff] [blame] | 119 | if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") |
| 120 | # Windows uses semi-colon delimiters, but so does CMake, so escape them. |
| 121 | list(JOIN _RULE_PACKAGE_DIRS "\\;" _PYTHONPATH) |
| 122 | else() |
| 123 | list(JOIN _RULE_PACKAGE_DIRS ":" _PYTHONPATH) |
| 124 | endif() |
| 125 | set_property(TEST ${_NAME_PATH} PROPERTY ENVIRONMENT |
| 126 | "PYTHONPATH=${_PYTHONPATH}" |
| 127 | ) |
Jerry Wu | 5a8e738 | 2022-04-20 20:20:40 +0000 | [diff] [blame] | 128 | |
Rob Suderman | f900f50 | 2022-06-07 12:26:06 -0700 | [diff] [blame] | 129 | if (NOT DEFINED _RULE_TIMEOUT) |
| 130 | set(_RULE_TIMEOUT 60) |
| 131 | endif() |
| 132 | |
Geoffrey Martin-Noble | 10f1822 | 2022-06-09 16:00:13 -0700 | [diff] [blame] | 133 | iree_configure_test(${_NAME_PATH}) |
Geoffrey Martin-Noble | 801c108 | 2021-08-18 18:50:48 -0700 | [diff] [blame] | 134 | |
Stella Laurenzo | 94363e2 | 2020-12-15 13:46:14 -0800 | [diff] [blame] | 135 | # TODO(marbre): Find out how to add deps to tests. |
| 136 | # Similar to _RULE_DATA in iree_lit_test(). |
| 137 | endfunction() |
Jerry Wu | 5a8e738 | 2022-04-20 20:20:40 +0000 | [diff] [blame] | 138 | |
| 139 | # iree_py_test() |
| 140 | # |
| 141 | # CMake function to imitate Bazel's iree_py_test rule. |
| 142 | # |
| 143 | # Parameters: |
| 144 | # NAME: name of test |
| 145 | # SRCS: Test source file (single file only, despite name) |
| 146 | # ARGS: Command line arguments to the Python source file. |
| 147 | # LABELS: Additional labels to apply to the test. The package path is added |
| 148 | # automatically. |
| 149 | # GENERATED_IN_BINARY_DIR: If present, indicates that the srcs have been |
| 150 | # in the CMAKE_CURRENT_BINARY_DIR. |
| 151 | function(iree_py_test) |
| 152 | cmake_parse_arguments( |
| 153 | _RULE |
| 154 | "GENERATED_IN_BINARY_DIR" |
| 155 | "NAME;SRCS" |
Rob Suderman | f900f50 | 2022-06-07 12:26:06 -0700 | [diff] [blame] | 156 | "ARGS;LABELS;TIMEOUT" |
Jerry Wu | 5a8e738 | 2022-04-20 20:20:40 +0000 | [diff] [blame] | 157 | ${ARGN} |
| 158 | ) |
Geoffrey Martin-Noble | 69c35cb | 2023-03-07 12:05:07 -0800 | [diff] [blame] | 159 | if(NOT IREE_BUILD_PYTHON_BINDINGS) |
| 160 | return() |
| 161 | endif() |
Jerry Wu | 5a8e738 | 2022-04-20 20:20:40 +0000 | [diff] [blame] | 162 | |
| 163 | iree_local_py_test( |
| 164 | NAME |
| 165 | "${_RULE_NAME}" |
| 166 | SRC |
| 167 | "${_RULE_SRCS}" |
| 168 | ARGS |
| 169 | ${_RULE_ARGS} |
| 170 | LABELS |
| 171 | ${_RULE_LABELS} |
| 172 | PACKAGE_DIRS |
Stella Laurenzo | 41a2ceb | 2022-04-29 12:49:36 -0700 | [diff] [blame] | 173 | "${IREE_BINARY_DIR}/compiler/bindings/python" |
Jerry Wu | 5a8e738 | 2022-04-20 20:20:40 +0000 | [diff] [blame] | 174 | "${IREE_BINARY_DIR}/runtime/bindings/python" |
| 175 | GENERATED_IN_BINARY_DIR |
| 176 | "${_RULE_GENERATED_IN_BINARY_DIR}" |
Rob Suderman | f900f50 | 2022-06-07 12:26:06 -0700 | [diff] [blame] | 177 | TIMEOUT |
| 178 | ${_RULE_TIMEOUT} |
Jerry Wu | 5a8e738 | 2022-04-20 20:20:40 +0000 | [diff] [blame] | 179 | ) |
| 180 | endfunction() |