|
1 | | -#[cfg(feature = "graphing")] |
2 | | -mod graphing { |
3 | | - use proc_macro2::Span; |
4 | | - use quote::quote; |
5 | | - use std::env::var_os; |
6 | | - use std::ffi::OsStr; |
7 | | - use std::fs::{create_dir_all, read_dir, read_to_string, write}; |
8 | | - use std::path::{Path, PathBuf}; |
9 | | - use syn::fold::{fold_item_fn, fold_use_path, Fold}; |
10 | | - |
11 | | - use syn::{ |
12 | | - parse_file, Ident, ItemFn, ReturnType, Token, Type, TypeParamBound, UsePath, UseTree, |
13 | | - }; |
14 | | - |
15 | | - struct ReplaceActionExec<'a> { |
16 | | - actions: &'a mut Vec<String>, |
17 | | - } |
18 | | - |
19 | | - impl<'a> ReplaceActionExec<'a> { |
20 | | - fn new(actions: &'a mut Vec<String>) -> Self { |
21 | | - Self { actions } |
22 | | - } |
23 | | - } |
24 | | - |
25 | | - impl Fold for ReplaceActionExec<'_> { |
26 | | - // Replace every impl ActionExec with impl Action |
27 | | - // Uses GraphAction to always be sure Action is imported |
28 | | - // Also strips generics |
29 | | - fn fold_item_fn(&mut self, i: ItemFn) -> ItemFn { |
30 | | - let mut j = i.clone(); |
31 | | - if let ReturnType::Type(_, ref mut ret_type) = j.sig.output { |
32 | | - if let Type::ImplTrait(ref mut impl_trait) = **ret_type { |
33 | | - impl_trait.bounds.iter_mut().for_each(|bound| { |
34 | | - if let TypeParamBound::Trait(ref mut t) = bound { |
35 | | - t.path.segments.iter_mut().for_each(|seg| { |
36 | | - if seg.ident == "ActionExec" { |
37 | | - seg.ident = Ident::new("GraphActionExec", seg.ident.span()); |
38 | | - |
39 | | - if j.sig.inputs.len() == 1 { |
40 | | - self.actions.push(j.sig.ident.to_string()); |
41 | | - } |
42 | | - |
43 | | - /* |
44 | | - j.sig.generics.type_params_mut().for_each(|param| { |
45 | | - if param.ident == "Con" { |
46 | | - param.bounds = Punctuated::new() |
47 | | - } |
48 | | - }); |
49 | | - */ |
50 | | - } |
51 | | - }); |
52 | | - } |
53 | | - }); |
54 | | - } |
55 | | - }; |
56 | | - // Call on the regular recursion |
57 | | - fold_item_fn(self, j) |
58 | | - } |
59 | | - |
60 | | - // Replace local paths with global paths |
61 | | - fn fold_use_path(&mut self, i: syn::UsePath) -> syn::UsePath { |
62 | | - println!("Folding path segment: {:?}", i.ident); |
63 | | - let mut j = i.clone(); |
64 | | - match j.ident.to_string().as_str() { |
65 | | - "crate" => j.ident = Ident::new("sw8s_rust_lib", j.ident.span()), |
66 | | - "super" => { |
67 | | - j.ident = Ident::new("sw8s_rust_lib", j.ident.span()); |
68 | | - j.tree = Box::new(UseTree::Path(UsePath { |
69 | | - ident: Ident::new("missions", Span::call_site()), |
70 | | - colon2_token: Token), |
71 | | - tree: j.tree, |
72 | | - })); |
73 | | - } |
74 | | - _ => (), |
75 | | - } |
76 | | - // Call on the regular recursion |
77 | | - fold_use_path(self, j) |
78 | | - } |
79 | | - } |
80 | | - |
81 | | - fn get_files(file: PathBuf) -> Vec<PathBuf> { |
82 | | - if file.is_file() { |
83 | | - vec![file] |
84 | | - } else if file.is_dir() { |
85 | | - read_dir(file) |
86 | | - .unwrap() |
87 | | - .flat_map(|inner| get_files(inner.unwrap().path())) |
88 | | - .collect() |
89 | | - } else { |
90 | | - vec![] |
91 | | - } |
92 | | - } |
93 | | - |
94 | | - pub fn graphing_variants() { |
95 | | - // Command to cargo |
96 | | - println!("cargo:rerun-if-changed=src/missions"); |
97 | | - |
98 | | - // Calculating output dir |
99 | | - let out_dir = var_os("OUT_DIR").unwrap().to_str().unwrap().to_string(); |
100 | | - println!("out_dir: {out_dir}"); |
101 | | - let out_path_raw = out_dir + "/graph_missions"; |
102 | | - let out_path = Path::new(&out_path_raw); |
103 | | - |
104 | | - // Get parseable file clones |
105 | | - let mission_files = get_files("src/missions".into()) |
106 | | - .into_iter() |
107 | | - .filter(|path| path.extension().unwrap_or(OsStr::new("")) == "rs") |
108 | | - .map(|path| { |
109 | | - println!("Path: {:?}", path); |
110 | | - ( |
111 | | - path.clone(), |
112 | | - parse_file(&read_to_string(path).unwrap()).unwrap().clone(), |
113 | | - ) |
114 | | - }); |
115 | | - |
116 | | - // Modify clones and write to output dir |
117 | | - mission_files |
118 | | - .map(|(path, file)| { |
119 | | - let mut actions = vec![]; |
120 | | - ( |
121 | | - path, |
122 | | - ReplaceActionExec::new(&mut actions).fold_file(file), |
123 | | - actions, |
124 | | - ) |
125 | | - }) |
126 | | - .for_each(|(path, file, actions)| { |
127 | | - let actions_str = |
128 | | - "pub fn graph_actions<T: GraphActionContext::GetMainElectronicsBoard + GraphActionContext::GetControlBoard<tokio::io::WriteHalf<tokio_serial::SerialStream>> + GraphActionContext::GetFrontCamMat + GraphActionContext::GetBottomCamMat + Send + Sync + std::marker::Unpin>(context: &'static T) -> Vec<(String, Box<dyn GraphAction + '_>)> { vec![" |
129 | | - .to_string() |
130 | | - + &actions |
131 | | - .into_iter() |
132 | | - .fold("".to_string(), |acc, x| acc + &format!("(\"{x}\".to_string(), Box::new({x}(context))),")) |
133 | | - + "]}"; |
134 | | - let file_contents = |
135 | | - quote! { use sw8s_rust_lib::missions::action::Action as GraphAction; use sw8s_rust_lib::missions::action::ActionExec as GraphActionExec; use sw8s_rust_lib::missions::action_context as GraphActionContext; #file }; |
136 | | - let output_loc = out_path.join(path.strip_prefix::<PathBuf>("src/missions".into()).unwrap()); |
137 | | - create_dir_all(output_loc.parent().unwrap()).unwrap(); |
138 | | - write( |
139 | | - out_path.join(path.strip_prefix::<PathBuf>("src/missions".into()).unwrap()), |
140 | | - format!("{} {}", file_contents, actions_str), |
141 | | - ) |
142 | | - .unwrap() |
143 | | - }); |
144 | | - } |
145 | | -} |
146 | | - |
147 | | -fn main() { |
148 | | - #[cfg(feature = "graphing")] |
149 | | - graphing::graphing_variants(); |
150 | | - |
151 | | - #[cfg(feature = "cuda")] |
152 | | - { |
153 | | - // https://arnon.dk/matching-sm-architectures-arch-and-gencode-for-various-nvidia-cards/ |
154 | | - const COMPUTE_CODES: &[&str] = &[ |
155 | | - "52", "53", "60", "61", "62", "70", "72", "75", "80", "86", "87", "89", "90", "90a", |
156 | | - ]; |
157 | | - |
158 | | - // Rebuild on any kernel change |
159 | | - println!("cargo:rerun-if-changed=src/cuda_kernels"); |
160 | | - |
161 | | - // Rebuild for specific files that use kernels changing |
162 | | - println!("cargo:rerun-if-changed=src/vision/nn_cv2.rs"); |
163 | | - |
164 | | - let mut build = cc::Build::new(); |
165 | | - build.cuda(true).flag("-cudart=shared"); |
166 | | - |
167 | | - for code in COMPUTE_CODES { |
168 | | - build |
169 | | - .flag("-gencode") |
170 | | - .flag(&format!("arch=compute_{},code=sm_{}", code, code)); |
171 | | - } |
172 | | - |
173 | | - // Specify all cuda kernels that need to be built |
174 | | - build |
175 | | - .file("src/cuda_kernels/process_net.cu") |
176 | | - .compile("libsw8s_cuda.a"); |
177 | | - |
178 | | - println!("cargo:rustc-link-lib=cudart"); |
179 | | - } |
180 | | -} |
| 1 | +// fn main() { |
| 2 | +// // #[cfg(feature = "cuda")] |
| 3 | +// // { |
| 4 | +// // // https://arnon.dk/matching-sm-architectures-arch-and-gencode-for-various-nvidia-cards/ |
| 5 | +// // const COMPUTE_CODES: &[&str] = &[ |
| 6 | +// // "52", "53", "60", "61", "62", "70", "72", "75", "80", "86", "87", "89", "90", "90a", |
| 7 | +// // ]; |
| 8 | + |
| 9 | +// // // Rebuild on any kernel change |
| 10 | +// // println!("cargo:rerun-if-changed=src/cuda_kernels"); |
| 11 | + |
| 12 | +// // // Rebuild for specific files that use kernels changing |
| 13 | +// // println!("cargo:rerun-if-changed=src/vision/nn_cv2.rs"); |
| 14 | + |
| 15 | +// // let mut build = cc::Build::new(); |
| 16 | +// // build.cuda(true).flag("-cudart=shared"); |
| 17 | + |
| 18 | +// // for code in COMPUTE_CODES { |
| 19 | +// // build |
| 20 | +// // .flag("-gencode") |
| 21 | +// // .flag(&format!("arch=compute_{},code=sm_{}", code, code)); |
| 22 | +// // } |
| 23 | + |
| 24 | +// // println!("cargo:rustc-link-lib=cudart"); |
| 25 | +// // } |
| 26 | +// } |
0 commit comments