Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 1 | # Copyright 2021 The IREE Authors |
Geoffrey Martin-Noble | 93a1ff1 | 2021-03-11 10:39:25 -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 |
Geoffrey Martin-Noble | 93a1ff1 | 2021-03-11 10:39:25 -0800 | [diff] [blame] | 6 | |
| 7 | """A utility to enforce that a list matches a glob expression. |
| 8 | |
| 9 | We use this primarily to enable the error-checking capabilities of globs of test |
| 10 | files in IREE while still allowing our Bazel to CMake conversion to not create |
| 11 | CMake globs (which are discouraged for collecting source files, see |
| 12 | https://cmake.org/cmake/help/latest/command/file.html#glob) and not be dependent |
| 13 | on any information outside of the BUILD file. |
| 14 | """ |
| 15 | |
| 16 | def enforce_glob(files, **kwargs): |
| 17 | """A utility to enforce that a list matches a glob expression. |
| 18 | |
| 19 | Note that the comparison is done in an order-independent fashion. |
| 20 | |
| 21 | Args: |
| 22 | files: a list that is expected to contain the same files as the |
| 23 | specified glob expression. |
| 24 | **kwargs: keyword arguments forwarded to the glob. |
| 25 | |
| 26 | Returns: |
| 27 | files. The input argument unchanged |
| 28 | """ |
| 29 | glob_result = native.glob(**kwargs) |
Stella Laurenzo | 8ef62de | 2023-11-03 16:04:57 -0700 | [diff] [blame] | 30 | for skip_file in ["CMakeLists.txt"]: |
| 31 | if skip_file in glob_result: |
| 32 | glob_result.remove(skip_file) |
Geoffrey Martin-Noble | 93a1ff1 | 2021-03-11 10:39:25 -0800 | [diff] [blame] | 33 | |
| 34 | # glob returns a sorted list. |
| 35 | if sorted(files) != glob_result: |
| 36 | glob_result_dict = {k: None for k in glob_result} |
| 37 | result_dict = {k: None for k in files} |
| 38 | missing = [k for k in glob_result if k not in files] |
| 39 | extra = [k for k in files if k not in glob_result] |
Geoffrey Martin-Noble | 9467a90 | 2021-03-11 11:57:11 -0800 | [diff] [blame] | 40 | expected_formatted = "\n".join(['"{}",'.format(file) for file in glob_result]) |
Geoffrey Martin-Noble | 93a1ff1 | 2021-03-11 10:39:25 -0800 | [diff] [blame] | 41 | fail(("Error in enforce_glob." + |
| 42 | "\nExpected {}." + |
| 43 | "\nGot {}." + |
| 44 | "\nMissing {}." + |
Geoffrey Martin-Noble | 9467a90 | 2021-03-11 11:57:11 -0800 | [diff] [blame] | 45 | "\nExtra {}" + |
| 46 | "\nPaste this into the first enforce_glob argument:" + |
| 47 | "\n{}").format( |
| 48 | glob_result, |
| 49 | files, |
| 50 | missing, |
| 51 | extra, |
| 52 | expected_formatted, |
| 53 | )) |
Geoffrey Martin-Noble | 93a1ff1 | 2021-03-11 10:39:25 -0800 | [diff] [blame] | 54 | return files |