Brian Silverman | 4e662aa | 2022-05-11 23:10:19 -0700 | [diff] [blame^] | 1 | // Copyright 2020 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
| 9 | #![forbid(unsafe_code)] |
| 10 | |
| 11 | use autocxx_engine::{BuilderContext, RebuildDependencyRecorder}; |
| 12 | use indexmap::set::IndexSet as HashSet; |
| 13 | use std::{io::Write, sync::Mutex}; |
| 14 | |
| 15 | pub type Builder = autocxx_engine::Builder<'static, CargoBuilderContext>; |
| 16 | |
| 17 | #[doc(hidden)] |
| 18 | pub struct CargoBuilderContext; |
| 19 | |
| 20 | impl BuilderContext for CargoBuilderContext { |
| 21 | fn setup() { |
| 22 | env_logger::builder() |
| 23 | .format(|buf, record| writeln!(buf, "cargo:warning=MESSAGE:{}", record.args())) |
| 24 | .init(); |
| 25 | } |
| 26 | fn get_dependency_recorder() -> Option<Box<dyn RebuildDependencyRecorder>> { |
| 27 | Some(Box::new(CargoRebuildDependencyRecorder::new())) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | #[derive(Debug)] |
| 32 | struct CargoRebuildDependencyRecorder { |
| 33 | printed_already: Mutex<HashSet<String>>, |
| 34 | } |
| 35 | |
| 36 | impl CargoRebuildDependencyRecorder { |
| 37 | fn new() -> Self { |
| 38 | Self { |
| 39 | printed_already: Mutex::new(HashSet::new()), |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl RebuildDependencyRecorder for CargoRebuildDependencyRecorder { |
| 45 | fn record_header_file_dependency(&self, filename: &str) { |
| 46 | let mut already = self.printed_already.lock().unwrap(); |
| 47 | if already.insert(filename.into()) { |
| 48 | println!("cargo:rerun-if-changed={}", filename); |
| 49 | } |
| 50 | } |
| 51 | } |