| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 1 | use std::fmt::{self, Display}; |
| 2 | |
| David Tolnay | 736cbca | 2020-03-11 16:49:18 -0700 | [diff] [blame] | 3 | pub static HEADER: &str = include_str!("include/cxx.h"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 4 | |
| 5 | pub fn get(guard: &str) -> &'static str { |
| David Tolnay | 2a1eaac | 2020-02-24 02:01:47 -0800 | [diff] [blame] | 6 | let ifndef = format!("#ifndef {}", guard); |
| 7 | let endif = format!("#endif // {}", guard); |
| David Tolnay | 26ad0bd | 2020-03-17 21:34:05 -0700 | [diff] [blame] | 8 | let begin = find_line(&ifndef); |
| 9 | let end = find_line(&endif); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 10 | if let (Some(begin), Some(end)) = (begin, end) { |
| 11 | &HEADER[begin..end + endif.len()] |
| 12 | } else { |
| David Tolnay | 736cbca | 2020-03-11 16:49:18 -0700 | [diff] [blame] | 13 | panic!("not found in cxx.h header: {}", guard) |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 14 | } |
| 15 | } |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 16 | |
| David Tolnay | 26ad0bd | 2020-03-17 21:34:05 -0700 | [diff] [blame] | 17 | fn find_line(line: &str) -> Option<usize> { |
| 18 | let mut offset = 0; |
| 19 | loop { |
| 20 | offset += HEADER[offset..].find(line)?; |
| 21 | let rest = &HEADER[offset + line.len()..]; |
| 22 | if rest.starts_with('\n') || rest.starts_with('\r') { |
| 23 | return Some(offset); |
| 24 | } |
| 25 | offset += line.len(); |
| 26 | } |
| 27 | } |
| 28 | |
| David Tolnay | 3577d45 | 2020-03-17 21:48:13 -0700 | [diff] [blame] | 29 | #[derive(Default, PartialEq)] |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 30 | pub struct Includes { |
| 31 | custom: Vec<String>, |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 32 | pub array: bool, |
| David Tolnay | 30430f1 | 2020-03-19 20:49:00 -0700 | [diff] [blame] | 33 | pub cstddef: bool, |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 34 | pub cstdint: bool, |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 35 | pub cstring: bool, |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 36 | pub exception: bool, |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 37 | pub memory: bool, |
| 38 | pub string: bool, |
| 39 | pub type_traits: bool, |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 40 | pub utility: bool, |
| David Tolnay | 8b7f899 | 2020-04-25 13:06:08 -0700 | [diff] [blame^] | 41 | pub vector: bool, |
| David Tolnay | 59b5ba1 | 2020-04-10 11:32:19 -0700 | [diff] [blame] | 42 | pub base_tsd: bool, |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | impl Includes { |
| 46 | pub fn new() -> Self { |
| 47 | Includes::default() |
| 48 | } |
| 49 | |
| 50 | pub fn insert(&mut self, include: String) { |
| 51 | self.custom.push(include); |
| 52 | } |
| 53 | } |
| 54 | |
| David Tolnay | 33d3029 | 2020-03-18 18:02:02 -0700 | [diff] [blame] | 55 | impl Extend<String> for Includes { |
| 56 | fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) { |
| 57 | self.custom.extend(iter); |
| 58 | } |
| 59 | } |
| 60 | |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 61 | impl Display for Includes { |
| 62 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 63 | for include in &self.custom { |
| 64 | writeln!(f, "#include \"{}\"", include.escape_default())?; |
| 65 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 66 | if self.array { |
| 67 | writeln!(f, "#include <array>")?; |
| 68 | } |
| David Tolnay | 30430f1 | 2020-03-19 20:49:00 -0700 | [diff] [blame] | 69 | if self.cstddef { |
| 70 | writeln!(f, "#include <cstddef>")?; |
| 71 | } |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 72 | if self.cstdint { |
| 73 | writeln!(f, "#include <cstdint>")?; |
| 74 | } |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 75 | if self.cstring { |
| 76 | writeln!(f, "#include <cstring>")?; |
| 77 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 78 | if self.exception { |
| 79 | writeln!(f, "#include <exception>")?; |
| 80 | } |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 81 | if self.memory { |
| 82 | writeln!(f, "#include <memory>")?; |
| 83 | } |
| 84 | if self.string { |
| 85 | writeln!(f, "#include <string>")?; |
| 86 | } |
| 87 | if self.type_traits { |
| 88 | writeln!(f, "#include <type_traits>")?; |
| 89 | } |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 90 | if self.utility { |
| 91 | writeln!(f, "#include <utility>")?; |
| 92 | } |
| David Tolnay | 59b5ba1 | 2020-04-10 11:32:19 -0700 | [diff] [blame] | 93 | if self.base_tsd { |
| 94 | writeln!(f, "#if defined(_WIN32)")?; |
| 95 | writeln!(f, "#include <BaseTsd.h>")?; |
| 96 | writeln!(f, "#endif")?; |
| 97 | } |
| David Tolnay | 3577d45 | 2020-03-17 21:48:13 -0700 | [diff] [blame] | 98 | if *self != Self::default() { |
| 99 | writeln!(f)?; |
| 100 | } |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 101 | Ok(()) |
| 102 | } |
| 103 | } |