|
| 1 | +# Copyright 2026 The Bazel Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Analysis test for verifying XCFramework include paths in CcInfo provider.""" |
| 16 | + |
| 17 | +load( |
| 18 | + "@bazel_skylib//lib:unittest.bzl", |
| 19 | + "analysistest", |
| 20 | + "unittest", |
| 21 | +) |
| 22 | +load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") |
| 23 | + |
| 24 | +def _analysis_xcframework_includes_test_impl(ctx): |
| 25 | + """Implementation of analysis_xcframework_includes_test.""" |
| 26 | + env = analysistest.begin(ctx) |
| 27 | + target_under_test = analysistest.target_under_test(env) |
| 28 | + |
| 29 | + # Get CcInfo provider from the target |
| 30 | + if not CcInfo in target_under_test: |
| 31 | + unittest.fail(env, "Target does not provide CcInfo") |
| 32 | + return analysistest.end(env) |
| 33 | + |
| 34 | + cc_info = target_under_test[CcInfo] |
| 35 | + compilation_context = cc_info.compilation_context |
| 36 | + |
| 37 | + # Get all include directories as strings |
| 38 | + includes = [inc for inc in compilation_context.includes.to_list()] |
| 39 | + system_includes = [inc for inc in compilation_context.system_includes.to_list()] |
| 40 | + quote_includes = [inc for inc in compilation_context.quote_includes.to_list()] |
| 41 | + |
| 42 | + all_includes = includes + system_includes + quote_includes |
| 43 | + |
| 44 | + # Check that expected includes are present |
| 45 | + for expected_include in ctx.attr.expected_includes: |
| 46 | + found = False |
| 47 | + for inc in all_includes: |
| 48 | + if expected_include in inc: |
| 49 | + found = True |
| 50 | + break |
| 51 | + if not found: |
| 52 | + unittest.fail( |
| 53 | + env, |
| 54 | + "Expected include path '{}' not found in CcInfo includes.\nActual includes: {}".format( |
| 55 | + expected_include, |
| 56 | + all_includes, |
| 57 | + ), |
| 58 | + ) |
| 59 | + return analysistest.end(env) |
| 60 | + |
| 61 | + # Check that not_expected includes are absent |
| 62 | + for not_expected_include in ctx.attr.not_expected_includes: |
| 63 | + for inc in all_includes: |
| 64 | + if not_expected_include in inc: |
| 65 | + unittest.fail( |
| 66 | + env, |
| 67 | + "Include path '{}' should NOT be present but was found in CcInfo includes.\nActual includes: {}".format( |
| 68 | + not_expected_include, |
| 69 | + all_includes, |
| 70 | + ), |
| 71 | + ) |
| 72 | + return analysistest.end(env) |
| 73 | + |
| 74 | + return analysistest.end(env) |
| 75 | + |
| 76 | +analysis_xcframework_includes_test = analysistest.make( |
| 77 | + _analysis_xcframework_includes_test_impl, |
| 78 | + attrs = { |
| 79 | + "expected_includes": attr.string_list( |
| 80 | + doc = """ |
| 81 | +A list of strings that should be substrings of paths in the CcInfo includes. |
| 82 | +Each expected include must match at least one path in the includes.""", |
| 83 | + ), |
| 84 | + "not_expected_includes": attr.string_list( |
| 85 | + doc = """ |
| 86 | +A list of strings that should NOT be substrings of any paths in the CcInfo includes. |
| 87 | +If any of these are found, the test fails.""", |
| 88 | + ), |
| 89 | + }, |
| 90 | +) |
0 commit comments