blob: 3ed32d9629612d325189013f8dd04a2ea55c57ff [file] [log] [blame]
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
load(":input_from_different_cfg.bzl", "input_from_different_cfg")
write_file(
name = "generate_src",
out = "src.rs",
content = """
#[cfg(not(generated_file_as_root))]
pub fn forty_two() -> i32 { 42 }
#[cfg(generated_file_as_root)]
mod lib_for_src;
#[cfg(generated_file_as_root)]
pub fn get_forty_two_as_string() -> String {
format!("{}", lib_for_src::forty_two())
}
""".splitlines(),
newline = "unix",
)
rust_library(
name = "use_generated_src",
srcs = [
"lib.rs",
":src.rs",
],
edition = "2018",
)
rust_library(
name = "use_generated_src_with_crate_root_defined",
srcs = [
"lib.rs",
":src.rs",
],
crate_root = "lib.rs",
edition = "2018",
)
rust_library(
name = "use_generated_src_as_crate_root",
srcs = [
"lib.rs",
"lib_for_src.rs",
":src.rs",
],
crate_root = ":src.rs",
edition = "2018",
rustc_flags = ["--cfg=generated_file_as_root"],
)
# When no lib.rs, main.rs file exists, we try to use the file that carries
# the target's name as a crate_root.
rust_library(
name = "src",
srcs = [
"lib_for_src.rs",
":src.rs",
],
edition = "2018",
rustc_flags = ["--cfg=generated_file_as_root"],
)
rust_test(
name = "generated_src_test",
crate = ":use_generated_src_with_crate_root_defined",
edition = "2018",
)
rust_binary(
name = "print_42",
srcs = [
"main.rs",
":src.rs",
],
edition = "2018",
)
input_from_different_cfg(
name = "generated_in_different_cfg",
)
filegroup(
name = "input_from_different_cfg",
srcs = [":generated_in_different_cfg"],
output_group = "generated_file",
)
rust_library(
name = "generated_src_in_different_cfg_as_root",
srcs = [":input_from_different_cfg"],
edition = "2018",
)
rust_test(
name = "generated_src_in_different_cfg_as_root_test",
crate = "generated_src_in_different_cfg_as_root",
edition = "2018",
)
rust_library(
name = "generated_src_in_different_cfg",
srcs = [
"root.rs",
":input_from_different_cfg",
],
crate_root = "root.rs",
edition = "2018",
)
rust_test(
name = "generated_src_in_different_cfg_test",
crate = "generated_src_in_different_cfg",
edition = "2018",
)
bool_flag(
name = "change_cfg",
build_setting_default = False,
)