blob: c1df58030271d0bcfb05b70abae2342501892f8b [file] [log] [blame]
Brian Silverman4e662aa2022-05-11 23:10:19 -07001// 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
11use autocxx_engine::{BuilderContext, RebuildDependencyRecorder};
12use indexmap::set::IndexSet as HashSet;
13use std::{io::Write, sync::Mutex};
14
15pub type Builder = autocxx_engine::Builder<'static, CargoBuilderContext>;
16
17#[doc(hidden)]
18pub struct CargoBuilderContext;
19
20impl 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)]
32struct CargoRebuildDependencyRecorder {
33 printed_already: Mutex<HashSet<String>>,
34}
35
36impl CargoRebuildDependencyRecorder {
37 fn new() -> Self {
38 Self {
39 printed_already: Mutex::new(HashSet::new()),
40 }
41 }
42}
43
44impl 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}