1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use super::error::Error;

use regex::Regex;
use std::{
    fmt::Debug,
    path::{Path, PathBuf},
    sync::Arc,
};
use tokio::{
    fs::File,
    io::{AsyncBufReadExt as _, BufReader},
    task::JoinSet,
};
use tracing::{error, info, trace};

/// Attempt to make the full path of head::tail
/// returns None if that fails (e.g. path does not exist)
fn try_resolve(head: &Path, tail: &PathBuf) -> Option<PathBuf> {
    head.join(tail).canonicalize().ok()
}

#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub enum FileType {
    Header,
    Source,
    Unknown,
}

impl FileType {
    pub fn of(path: &Path) -> Self {
        let ext = path
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("")
            .to_lowercase();
        match ext.as_str() {
            "h" | "hpp" => FileType::Header,
            "c" | "cpp" | "cc" | "cxx" => FileType::Source,
            _ => FileType::Unknown,
        }
    }
}

/// Given a C-like source, try to resolve includes.
///
/// Includes are generally of the form `#include <name>` or `#include "name"`
pub async fn extract_includes(
    path: &PathBuf,
    include_dirs: &[PathBuf],
) -> Result<Vec<PathBuf>, Error> {
    let f = File::open(path).await.map_err(|source| Error::IOError {
        source,
        path: path.clone(),
        message: "open",
    })?;

    let reader = BufReader::new(f);

    let inc_re = Regex::new(r##"^\s*#include\s*(["<])([^">]*)[">]"##).unwrap();

    let mut result = Vec::new();
    let parent_dir = PathBuf::from(path.parent().unwrap());

    let mut lines = reader.lines();

    loop {
        let line = lines.next_line().await.map_err(|source| Error::IOError {
            source,
            path: path.clone(),
            message: "line read",
        })?;

        let line = match line {
            Some(value) => value,
            None => break,
        };

        if let Some(captures) = inc_re.captures(&line) {
            let inc_type = captures.get(1).unwrap().as_str();
            let relative_path = PathBuf::from(captures.get(2).unwrap().as_str());

            trace!("Possible include: {:?}", relative_path);

            if inc_type == "\"" {
                if let Some(p) = try_resolve(&parent_dir, &relative_path) {
                    result.push(p);
                    continue;
                }
            }

            if let Some(p) = include_dirs
                .iter()
                .filter_map(|i| try_resolve(i, &relative_path))
                .next()
            {
                result.push(p);
            } else {
                // Debug only as this is VERY common due to C++ and system inclues,
                // like "list", "vector", "string" or even platform specific like "jni.h"
                // or non-enabled things (like openthread on a non-thread platform)
                trace!("Include {:?} could not be resolved", relative_path);
            }
        }
    }

    info!(target: "include-extract",
          "Includes for:\n  {:?}: {:#?}", path, result);

    Ok(result)
}

#[derive(Debug, PartialEq, PartialOrd)]
pub struct SourceWithIncludes {
    pub path: PathBuf,
    pub includes: Vec<PathBuf>,
}

/// Given a list of paths, figure out their dependencies
pub async fn all_sources_and_includes<I, E>(
    paths: I,
    includes: &[PathBuf],
) -> Result<Vec<SourceWithIncludes>, Error>
where
    I: Iterator<Item = Result<PathBuf, E>>,
    E: Debug,
{
    let includes = Arc::new(Vec::from(includes));

    let mut join_set = JoinSet::new();

    for entry in paths {
        let path = match entry {
            Ok(value) => value,
            Err(e) => {
                return Err(Error::Internal {
                    message: format!("{:?}", e),
                })
            }
        };

        if FileType::of(&path) == FileType::Unknown {
            trace!("Skipping non-source: {:?}", path);
            continue;
        }

        // prepare data to mve into sub-task
        let includes = includes.clone();

        join_set.spawn(async move {
            trace!("PROCESS: {:?}", path);
            let includes = match extract_includes(&path, &includes).await {
                Ok(value) => value,
                Err(e) => {
                    error!("Error extracing includes: {:?}", e);
                    return Err(e);
                }
            };

            Ok(SourceWithIncludes { path, includes })
        });
    }

    let mut results = Vec::new();
    while let Some(h) = join_set.join_next().await {
        let r = h.map_err(Error::JoinError)?;
        results.push(r?)
    }

    Ok(results)
}