1load("@bazel_skylib//rules:build_test.bzl", "build_test") 2load("@rules_rust//rust:defs.bzl", "rust_library") 3load("defs.bzl", "with_exec_cfg", "with_extra_exec_rustc_flags_cfg") 4 5package(default_visibility = ["//test:__subpackages__"]) 6 7# Checks that extra_exec_rustc_flags are passed in exec configuration. 8# lib.rs is a sample source file that requires a `--cfg=bazel_exec` flag to build. 9# These targets set up transitions so that building :lib triggers building 10# lib.rs in exec configuration with //:extra_exec_rustc_flags=[--cfg=bazel_exec]. 11# The intermediate targets are tagged "manual" as they are not meant to be built 12# on their own. 13 14rust_library( 15 name = "lib_do_not_build_directly", 16 srcs = ["lib.rs"], 17 edition = "2018", 18 tags = ["manual"], 19) 20 21with_extra_exec_rustc_flags_cfg( 22 name = "lib_with_exec_flags_do_not_build_directly", 23 srcs = ["lib_do_not_build_directly"], 24 extra_exec_rustc_flag = [], 25 extra_exec_rustc_flags = ["--cfg=bazel_exec"], 26 tags = ["manual"], 27) 28 29with_extra_exec_rustc_flags_cfg( 30 name = "lib_with_singular_exec_flags_do_not_build_directly", 31 srcs = ["lib_do_not_build_directly"], 32 extra_exec_rustc_flag = ["--cfg=bazel_exec"], 33 extra_exec_rustc_flags = [], 34 tags = ["manual"], 35) 36 37with_exec_cfg( 38 name = "lib", 39 srcs = ["lib_with_exec_flags_do_not_build_directly"], 40) 41 42with_exec_cfg( 43 name = "lib_singular", 44 srcs = ["lib_with_singular_exec_flags_do_not_build_directly"], 45) 46 47# Checks that extra_exec_rustc_flags are not passed in non-exec configurations. 48# lib_no_exec.rs is a sample source file that fails to build if 49# `--cfg=bazel_exec` is present. The targets below are built in non-exec configurations, 50# so they should build just fine with //:extra_exec_rustc_flags=[--cfg=bazel_exec]. 51rust_library( 52 name = "lib_no_exec", 53 srcs = ["lib_no_exec.rs"], 54 edition = "2018", 55) 56 57with_extra_exec_rustc_flags_cfg( 58 name = "lib_no_exec_with_extra_build_flags", 59 srcs = ["lib_no_exec"], 60 extra_exec_rustc_flag = [], 61 extra_exec_rustc_flags = ["--cfg=bazel_exec"], 62) 63 64with_extra_exec_rustc_flags_cfg( 65 name = "lib_no_exec_with_singular_extra_build_flags", 66 srcs = ["lib_no_exec"], 67 extra_exec_rustc_flag = ["--cfg=bazel_exec"], 68 extra_exec_rustc_flags = [], 69) 70 71build_test( 72 name = "lib_build", 73 targets = [ 74 ":lib", 75 ":lib_singular", 76 ":lib_no_exec", 77 ":lib_no_exec_with_extra_build_flags", 78 ":lib_no_exec_with_singular_extra_build_flags", 79 ], 80) 81