blob: b41e9249af68578cb30528a8b3b444ad3d54fc71 [file] [log] [blame]
Brian Silverman4e662aa2022-05-11 23:10:19 -07001// Copyright 2021 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
9fn main() -> miette::Result<()> {
10 let path = std::path::PathBuf::from("src");
11 let mut b = autocxx_build::Builder::new("src/main.rs", &[&path])
12 .auto_allowlist(true)
13 .build()?;
14 b.flag_if_supported("-std=c++17")
15 .file("src/messages.cc")
16 .compile("autocxx-subclass-example");
17 println!("cargo:rerun-if-changed=src/main.rs");
18 println!("cargo:rerun-if-changed=src/messages.cc");
19 println!("cargo:rerun-if-changed=src/messages.h");
20
21 // The following line is *unrelated* to autocxx builds and is
22 // just designed to ensure that example code doesn't get out of sync
23 // from copies in comments.
24 ensure_comments_match_real_code(&std::path::PathBuf::from("src/main.rs"));
25 Ok(())
26}
27
28use std::fs::File;
29use std::io::BufRead;
30use std::io::BufReader;
31use std::io::Lines;
32use std::path::Path;
33
34enum CommentMatcherState {
35 Searching,
36 EatingBacktickLine(Lines<BufReader<File>>),
37 SearchingForFirstLine(Lines<BufReader<File>>),
38 Found(Lines<BufReader<File>>),
39}
40
41fn ensure_comments_match_real_code(rs_file: &Path) {
42 use regex::Regex;
43 let start_re = Regex::new(r"// .*from ([\w/]+\.\w+).*").unwrap();
44 let strip_comment_re = Regex::new(r"// (.*)").unwrap();
45 let file = File::open(rs_file).unwrap();
46 let lines = BufReader::new(file).lines();
47 let mut state = CommentMatcherState::Searching;
48 for line in lines {
49 let line = line.unwrap();
50 state = match state {
51 CommentMatcherState::Searching => match start_re.captures(&line) {
52 Some(captures) => {
53 let fname = captures.get(1).unwrap().as_str();
54 let srcfile = File::open(fname).unwrap();
55 let srclines = BufReader::new(srcfile).lines();
56 CommentMatcherState::EatingBacktickLine(srclines)
57 }
58 None => CommentMatcherState::Searching,
59 },
60 CommentMatcherState::EatingBacktickLine(srclines) => {
61 CommentMatcherState::SearchingForFirstLine(srclines)
62 }
63 CommentMatcherState::SearchingForFirstLine(mut srclines) => {
64 match strip_comment_re.captures(&line) {
65 Some(captures) => {
66 let mut found = false;
67 while !found {
68 let srcline = srclines.next().unwrap().unwrap();
69 found = captures.get(1).unwrap().as_str() == srcline;
70 }
71 CommentMatcherState::Found(srclines)
72 }
73 None => CommentMatcherState::Searching,
74 }
75 }
76 CommentMatcherState::Found(mut srclines) => {
77 if line == "// ```" {
78 CommentMatcherState::Searching
79 } else {
80 match strip_comment_re.captures(&line) {
81 Some(captures) => {
82 let actual = captures.get(1).unwrap().as_str();
83 let expected = srclines.next().unwrap().unwrap();
84 assert_eq!(expected, actual);
85 CommentMatcherState::Found(srclines)
86 }
87 None => CommentMatcherState::Searching,
88 }
89 }
90 }
91 }
92 }
93}