|
| 1 | +"""Tests for `cargo::rustc-cdylib-link-arg` and `cargo::rustc-link-arg-bins`.""" |
| 2 | + |
| 3 | +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") |
| 4 | +load("//cargo:defs.bzl", "cargo_build_script") |
| 5 | +load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_shared_library") |
| 6 | +load("//test/unit:common.bzl", "assert_action_mnemonic") |
| 7 | + |
| 8 | +# The build script's `.cdyliblinkflags` / `.binlinkflags` outputs are passed to |
| 9 | +# rustc as `--arg-file <path>` (see `_process_build_scripts`). A flag only reaches |
| 10 | +# a consumer when its crate type matches, so the presence of a specific build |
| 11 | +# script's arg-file in the Rustc command line is what distinguishes a correctly |
| 12 | +# gated (and, for cdylibs, transitively propagated) consumer. |
| 13 | +def _has_arg_file(argv, suffix): |
| 14 | + for i in range(len(argv) - 1): |
| 15 | + if argv[i] == "--arg-file" and argv[i + 1].endswith(suffix): |
| 16 | + return True |
| 17 | + return False |
| 18 | + |
| 19 | +def _link_args_test_impl(ctx): |
| 20 | + env = analysistest.begin(ctx) |
| 21 | + tut = analysistest.target_under_test(env) |
| 22 | + |
| 23 | + action = None |
| 24 | + for a in tut.actions: |
| 25 | + if a.mnemonic == "Rustc": |
| 26 | + action = a |
| 27 | + break |
| 28 | + asserts.true(env, action != None, "Expected a Rustc action") |
| 29 | + assert_action_mnemonic(env, action, "Rustc") |
| 30 | + |
| 31 | + for suffix in ctx.attr.expect_arg_files: |
| 32 | + asserts.true(env, _has_arg_file(action.argv, suffix), "expected --arg-file ending with '{}'".format(suffix)) |
| 33 | + for suffix in ctx.attr.unexpected_arg_files: |
| 34 | + asserts.false(env, _has_arg_file(action.argv, suffix), "unexpected --arg-file ending with '{}'".format(suffix)) |
| 35 | + |
| 36 | + return analysistest.end(env) |
| 37 | + |
| 38 | +_link_args_test = analysistest.make( |
| 39 | + _link_args_test_impl, |
| 40 | + attrs = { |
| 41 | + "expect_arg_files": attr.string_list(), |
| 42 | + "unexpected_arg_files": attr.string_list(), |
| 43 | + }, |
| 44 | +) |
| 45 | + |
| 46 | +def cdylib_bin_link_args_test_suite(name): |
| 47 | + """Test that build-script cdylib/bin link args reach only the matching crate type. |
| 48 | +
|
| 49 | + Args: |
| 50 | + name: Name of the test suite. |
| 51 | + """ |
| 52 | + |
| 53 | + # A build script reached only transitively (via `dep_lib`), emitting a cdylib link arg. |
| 54 | + cargo_build_script( |
| 55 | + name = "transitive_build_script", |
| 56 | + srcs = ["build_transitive.rs"], |
| 57 | + tags = ["manual"], |
| 58 | + ) |
| 59 | + |
| 60 | + rust_library( |
| 61 | + name = "dep_lib", |
| 62 | + srcs = ["lib.rs"], |
| 63 | + deps = [":transitive_build_script"], |
| 64 | + tags = ["manual"], |
| 65 | + ) |
| 66 | + |
| 67 | + # The consumers' own build script, emitting both a cdylib and a bins link arg. |
| 68 | + cargo_build_script( |
| 69 | + name = "direct_build_script", |
| 70 | + srcs = ["build.rs"], |
| 71 | + tags = ["manual"], |
| 72 | + ) |
| 73 | + |
| 74 | + rust_shared_library( |
| 75 | + name = "cdylib", |
| 76 | + srcs = ["lib.rs"], |
| 77 | + deps = [":direct_build_script", ":dep_lib"], |
| 78 | + tags = ["manual"], |
| 79 | + ) |
| 80 | + |
| 81 | + rust_binary( |
| 82 | + name = "bin", |
| 83 | + srcs = ["bin.rs"], |
| 84 | + deps = [":direct_build_script", ":dep_lib"], |
| 85 | + tags = ["manual"], |
| 86 | + ) |
| 87 | + |
| 88 | + rust_library( |
| 89 | + name = "lib", |
| 90 | + srcs = ["lib.rs"], |
| 91 | + deps = [":direct_build_script"], |
| 92 | + tags = ["manual"], |
| 93 | + ) |
| 94 | + |
| 95 | + # A cdylib gets its own cdylib link args AND those of a transitive build |
| 96 | + # script, but not the bin link args. |
| 97 | + _link_args_test( |
| 98 | + name = "cdylib_consumer_test", |
| 99 | + target_under_test = ":cdylib", |
| 100 | + expect_arg_files = [ |
| 101 | + "direct_build_script.cdyliblinkflags", |
| 102 | + "transitive_build_script.cdyliblinkflags", |
| 103 | + ], |
| 104 | + unexpected_arg_files = ["direct_build_script.binlinkflags"], |
| 105 | + ) |
| 106 | + |
| 107 | + # A binary gets its own bin link args, but no cdylib link args (direct or |
| 108 | + # transitive). |
| 109 | + _link_args_test( |
| 110 | + name = "bin_consumer_test", |
| 111 | + target_under_test = ":bin", |
| 112 | + expect_arg_files = ["direct_build_script.binlinkflags"], |
| 113 | + unexpected_arg_files = [ |
| 114 | + "direct_build_script.cdyliblinkflags", |
| 115 | + "transitive_build_script.cdyliblinkflags", |
| 116 | + ], |
| 117 | + ) |
| 118 | + |
| 119 | + # An rlib gets neither. |
| 120 | + _link_args_test( |
| 121 | + name = "lib_consumer_test", |
| 122 | + target_under_test = ":lib", |
| 123 | + unexpected_arg_files = [ |
| 124 | + "direct_build_script.cdyliblinkflags", |
| 125 | + "direct_build_script.binlinkflags", |
| 126 | + ], |
| 127 | + ) |
| 128 | + |
| 129 | + native.test_suite( |
| 130 | + name = name, |
| 131 | + tests = [ |
| 132 | + ":cdylib_consumer_test", |
| 133 | + ":bin_consumer_test", |
| 134 | + ":lib_consumer_test", |
| 135 | + ], |
| 136 | + ) |
0 commit comments