1*d4726bddSHONG Yifanload("@bazel_skylib//rules:common_settings.bzl", "bool_flag") 2*d4726bddSHONG Yifanload("@bazel_skylib//rules:write_file.bzl", "write_file") 3*d4726bddSHONG Yifanload("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test") 4*d4726bddSHONG Yifanload(":input_from_different_cfg.bzl", "input_from_different_cfg") 5*d4726bddSHONG Yifan 6*d4726bddSHONG Yifanwrite_file( 7*d4726bddSHONG Yifan name = "gen_src", 8*d4726bddSHONG Yifan out = "src.rs", 9*d4726bddSHONG Yifan content = """ 10*d4726bddSHONG Yifan#[cfg(not(generated_file_as_root))] 11*d4726bddSHONG Yifanpub fn forty_two() -> i32 { 42 } 12*d4726bddSHONG Yifan 13*d4726bddSHONG Yifan#[cfg(generated_file_as_root)] 14*d4726bddSHONG Yifanmod lib_for_src; 15*d4726bddSHONG Yifan 16*d4726bddSHONG Yifan#[cfg(generated_file_as_root)] 17*d4726bddSHONG Yifanpub fn get_forty_two_as_string() -> String { 18*d4726bddSHONG Yifan format!("{}", lib_for_src::forty_two()) 19*d4726bddSHONG Yifan} 20*d4726bddSHONG Yifan""".splitlines(), 21*d4726bddSHONG Yifan newline = "unix", 22*d4726bddSHONG Yifan) 23*d4726bddSHONG Yifan 24*d4726bddSHONG Yifanwrite_file( 25*d4726bddSHONG Yifan name = "gen_src_generated", 26*d4726bddSHONG Yifan out = "src/generated.rs", 27*d4726bddSHONG Yifan content = """ 28*d4726bddSHONG Yifanmod submodule; 29*d4726bddSHONG Yifan 30*d4726bddSHONG Yifan#[cfg(test)] 31*d4726bddSHONG Yifan#[test] 32*d4726bddSHONG Yifanfn test_foo() { 33*d4726bddSHONG Yifan assert_eq!(submodule::foo(), "foo"); 34*d4726bddSHONG Yifan} 35*d4726bddSHONG Yifan""".splitlines(), 36*d4726bddSHONG Yifan newline = "unix", 37*d4726bddSHONG Yifan) 38*d4726bddSHONG Yifan 39*d4726bddSHONG Yifanrust_library( 40*d4726bddSHONG Yifan name = "libgensrc", 41*d4726bddSHONG Yifan srcs = [ 42*d4726bddSHONG Yifan "lib.rs", 43*d4726bddSHONG Yifan "submodule/mod.rs", 44*d4726bddSHONG Yifan ":src.rs", 45*d4726bddSHONG Yifan ], 46*d4726bddSHONG Yifan edition = "2018", 47*d4726bddSHONG Yifan tags = ["norustfmt"], 48*d4726bddSHONG Yifan) 49*d4726bddSHONG Yifan 50*d4726bddSHONG Yifanrust_library( 51*d4726bddSHONG Yifan name = "libgensrc_with_crate_root", 52*d4726bddSHONG Yifan srcs = [ 53*d4726bddSHONG Yifan "lib.rs", 54*d4726bddSHONG Yifan "submodule/mod.rs", 55*d4726bddSHONG Yifan ":src.rs", 56*d4726bddSHONG Yifan ], 57*d4726bddSHONG Yifan crate_root = "lib.rs", 58*d4726bddSHONG Yifan edition = "2018", 59*d4726bddSHONG Yifan tags = ["norustfmt"], 60*d4726bddSHONG Yifan visibility = ["//visibility:public"], 61*d4726bddSHONG Yifan) 62*d4726bddSHONG Yifan 63*d4726bddSHONG Yifanrust_library( 64*d4726bddSHONG Yifan name = "libgensrc_as_crate_root", 65*d4726bddSHONG Yifan srcs = [ 66*d4726bddSHONG Yifan "lib.rs", 67*d4726bddSHONG Yifan "lib_for_src.rs", 68*d4726bddSHONG Yifan ":src.rs", 69*d4726bddSHONG Yifan ], 70*d4726bddSHONG Yifan crate_root = ":src.rs", 71*d4726bddSHONG Yifan edition = "2018", 72*d4726bddSHONG Yifan rustc_flags = ["--cfg=generated_file_as_root"], 73*d4726bddSHONG Yifan tags = ["norustfmt"], 74*d4726bddSHONG Yifan) 75*d4726bddSHONG Yifan 76*d4726bddSHONG Yifanrust_library( 77*d4726bddSHONG Yifan name = "lib_with_crate_root_in_subdir", 78*d4726bddSHONG Yifan srcs = [ 79*d4726bddSHONG Yifan "src/generated.rs", 80*d4726bddSHONG Yifan "src/generated/submodule.rs", 81*d4726bddSHONG Yifan "src/lib.rs", 82*d4726bddSHONG Yifan ], 83*d4726bddSHONG Yifan crate_root = "src/lib.rs", 84*d4726bddSHONG Yifan edition = "2018", 85*d4726bddSHONG Yifan tags = ["norustfmt"], 86*d4726bddSHONG Yifan) 87*d4726bddSHONG Yifan 88*d4726bddSHONG Yifanrust_test( 89*d4726bddSHONG Yifan name = "lib_with_srcs_in_subdir_test", 90*d4726bddSHONG Yifan srcs = [ 91*d4726bddSHONG Yifan "src/generated.rs", 92*d4726bddSHONG Yifan "src/generated/submodule.rs", 93*d4726bddSHONG Yifan "src/lib.rs", 94*d4726bddSHONG Yifan ], 95*d4726bddSHONG Yifan edition = "2018", 96*d4726bddSHONG Yifan tags = ["norustfmt"], 97*d4726bddSHONG Yifan) 98*d4726bddSHONG Yifan 99*d4726bddSHONG Yifanrust_test( 100*d4726bddSHONG Yifan name = "lib_with_crate_root_in_subdir_test", 101*d4726bddSHONG Yifan crate = "lib_with_crate_root_in_subdir", 102*d4726bddSHONG Yifan edition = "2018", 103*d4726bddSHONG Yifan tags = ["norustfmt"], 104*d4726bddSHONG Yifan) 105*d4726bddSHONG Yifan 106*d4726bddSHONG Yifan# When no lib.rs, main.rs file exists, we try to use the file that carries 107*d4726bddSHONG Yifan# the target's name as a crate_root. 108*d4726bddSHONG Yifanrust_library( 109*d4726bddSHONG Yifan name = "src", 110*d4726bddSHONG Yifan srcs = [ 111*d4726bddSHONG Yifan "lib_for_src.rs", 112*d4726bddSHONG Yifan ":src.rs", 113*d4726bddSHONG Yifan ], 114*d4726bddSHONG Yifan edition = "2018", 115*d4726bddSHONG Yifan rustc_flags = ["--cfg=generated_file_as_root"], 116*d4726bddSHONG Yifan tags = ["norustfmt"], 117*d4726bddSHONG Yifan) 118*d4726bddSHONG Yifan 119*d4726bddSHONG Yifanrust_test( 120*d4726bddSHONG Yifan name = "gensrc_test", 121*d4726bddSHONG Yifan crate = ":libgensrc_with_crate_root", 122*d4726bddSHONG Yifan edition = "2018", 123*d4726bddSHONG Yifan tags = ["norustfmt"], 124*d4726bddSHONG Yifan) 125*d4726bddSHONG Yifan 126*d4726bddSHONG Yifanrust_binary( 127*d4726bddSHONG Yifan name = "print_42", 128*d4726bddSHONG Yifan srcs = [ 129*d4726bddSHONG Yifan "main.rs", 130*d4726bddSHONG Yifan ":src.rs", 131*d4726bddSHONG Yifan ], 132*d4726bddSHONG Yifan edition = "2018", 133*d4726bddSHONG Yifan tags = ["norustfmt"], 134*d4726bddSHONG Yifan) 135*d4726bddSHONG Yifan 136*d4726bddSHONG Yifaninput_from_different_cfg( 137*d4726bddSHONG Yifan name = "generated_in_different_cfg", 138*d4726bddSHONG Yifan) 139*d4726bddSHONG Yifan 140*d4726bddSHONG Yifanfilegroup( 141*d4726bddSHONG Yifan name = "input_from_different_cfg", 142*d4726bddSHONG Yifan srcs = [":generated_in_different_cfg"], 143*d4726bddSHONG Yifan output_group = "generated_file", 144*d4726bddSHONG Yifan) 145*d4726bddSHONG Yifan 146*d4726bddSHONG Yifanrust_library( 147*d4726bddSHONG Yifan name = "gen_src_in_different_cfg_as_root", 148*d4726bddSHONG Yifan srcs = [":input_from_different_cfg"], 149*d4726bddSHONG Yifan edition = "2018", 150*d4726bddSHONG Yifan tags = ["norustfmt"], 151*d4726bddSHONG Yifan) 152*d4726bddSHONG Yifan 153*d4726bddSHONG Yifanrust_test( 154*d4726bddSHONG Yifan name = "gen_src_in_different_cfg_as_root_test", 155*d4726bddSHONG Yifan crate = "gen_src_in_different_cfg_as_root", 156*d4726bddSHONG Yifan edition = "2018", 157*d4726bddSHONG Yifan tags = ["norustfmt"], 158*d4726bddSHONG Yifan) 159*d4726bddSHONG Yifan 160*d4726bddSHONG Yifanrust_library( 161*d4726bddSHONG Yifan name = "gen_src_in_different_cfg", 162*d4726bddSHONG Yifan srcs = [ 163*d4726bddSHONG Yifan "root.rs", 164*d4726bddSHONG Yifan ":input_from_different_cfg", 165*d4726bddSHONG Yifan ], 166*d4726bddSHONG Yifan crate_root = "root.rs", 167*d4726bddSHONG Yifan edition = "2018", 168*d4726bddSHONG Yifan tags = ["norustfmt"], 169*d4726bddSHONG Yifan) 170*d4726bddSHONG Yifan 171*d4726bddSHONG Yifanrust_test( 172*d4726bddSHONG Yifan name = "gen_src_in_different_cfg_test", 173*d4726bddSHONG Yifan crate = "gen_src_in_different_cfg", 174*d4726bddSHONG Yifan edition = "2018", 175*d4726bddSHONG Yifan tags = ["norustfmt"], 176*d4726bddSHONG Yifan) 177*d4726bddSHONG Yifan 178*d4726bddSHONG Yifanbool_flag( 179*d4726bddSHONG Yifan name = "change_cfg", 180*d4726bddSHONG Yifan build_setting_default = False, 181*d4726bddSHONG Yifan) 182*d4726bddSHONG Yifan 183*d4726bddSHONG Yifanrust_test( 184*d4726bddSHONG Yifan name = "gen_inputs_external_repo_test", 185*d4726bddSHONG Yifan # This is regression testing a specific failure case for generated files _not_ in the root 186*d4726bddSHONG Yifan # of an external repository. 187*d4726bddSHONG Yifan crate = "@generated_inputs_in_external_repo//lib:generated_inputs_external_repo", 188*d4726bddSHONG Yifan edition = "2021", 189*d4726bddSHONG Yifan) 190