xref: /aosp_15_r20/external/bazelbuild-rules_rust/examples/cargo/build_script.rs (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1 // Copyright 2020 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 use std::env;
15 use std::fs::File;
16 use std::io::prelude::*;
17 
main()18 fn main() {
19     let bleh = env::var("CARGO_FEATURE_BLEH").unwrap();
20     let some_env = env::var("SOME_ENV").unwrap();
21     let out_dir = env::var("OUT_DIR").unwrap();
22     let data = std::fs::read("test.txt").unwrap();
23     assert!(!bleh.is_empty());
24     assert_eq!(some_env, "42");
25     println!(
26         r#"cargo:rustc-env=FOO=BAR
27 cargo:rustc-env=BAR=FOO
28 cargo:rustc-flags=--cfg=blah="bleh"
29 cargo:rustc-flags=--cfg=data="{}"
30 cargo:rustc-cfg=foobar"#,
31         std::str::from_utf8(&data).unwrap()
32     );
33     let mut file = File::create(format!("{}/hello.world.txt", out_dir)).unwrap();
34     file.write_all(b"Hello, world!").unwrap();
35 }
36