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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//! ARM GNU toolchain

use std::borrow::Cow;
use std::ffi::OsStr;
use std::io::ErrorKind;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::io::Error as IoError;
use std::process::Stdio;

use self::err::Error;


/// Env var name that points to the arm-gcc executable.
pub const ARM_GCC_PATH_ENV_VAR: &'static str = "ARM_GCC_PATH";

/// Variants of the compile's name - actual and old.
pub const ARM_NONE_EABI_GCC: &[&str] = &["arm-none-eabi-gcc", "gcc-arm-none-eabi"];


pub struct Gcc {
	path: PathBuf,
}

pub struct ArmToolchain {
	gcc: Gcc,
	sysroot: PathBuf,
}


impl Gcc {
	pub fn path(&self) -> &Path { self.path.as_path() }


	/// Automatically determine the gcc
	pub fn try_new() -> Result<Self, Error> {
		let try_with = |f: fn() -> Result<Self, Error>| {
			move |err: Error| {
				let result = f();
				if result.is_err() {
					crate::error!("{err}");
				}
				result
			}
		};

		Self::try_from_default_env().or_else(try_with(Self::try_from_env_path))
		                            .or_else(try_with(Self::try_from_default_path))
	}


	pub fn try_new_exact_path<P: Into<PathBuf>>(path: P) -> Result<Self, Error> {
		let path = path.into().canonicalize()?;
		if path.try_exists()? {
			Ok(Self { path })
		} else {
			Err(IoError::new(
				ErrorKind::NotFound,
				format!("Could not find ARM GCC at '{}'", path.display()),
			).into())
		}
	}


	/// Create new with default env var
	pub fn try_from_default_env() -> Result<Self, Error> {
		let res = std::env::var_os(ARM_GCC_PATH_ENV_VAR).map(PathBuf::from)
		                                                .map(Self::try_new_exact_path);
		res.ok_or(IoError::new(ErrorKind::NotFound, format!("Missed env {ARM_GCC_PATH_ENV_VAR}")))?
	}

	/// Create new with executable in PATH
	pub fn try_from_env_path() -> Result<Self, Error> {
		for name in ARM_NONE_EABI_GCC {
			if let Ok(result) = Self::try_from_path(name) {
				return Ok(result);
			}
		}

		return Err(Error::Err("Could not find ARM GCC in PATH"));
	}

	/// Create new with executable name or path
	pub fn try_from_path<S: AsRef<OsStr>>(path: S) -> Result<Self, Error> {
		let mut proc = Command::new(path.as_ref());
		proc.arg("--version");
		let output = proc.output()?;
		if !output.status.success() {
			return Err(Error::exit_status_error(&proc, output.stderr, output.status));
		}
		Ok(Self { path: path.as_ref().into() })
	}

	/// Create new with default path of executable
	pub fn try_from_default_path() -> Result<Self, Error> {
		#[cfg(unix)]
		{
			let paths = ["/usr/local/bin/", "/usr/bin/"].into_iter()
			                                            .map(Path::new)
			                                            .flat_map(|p| ARM_NONE_EABI_GCC.into_iter().map(|name| p.join(name)))
			                                            .filter(|p| p.try_exists().ok().unwrap_or_default());
			for path in paths {
				match Self::try_from_path(&path) {
					Ok(gcc) => return Ok(gcc),
					Err(err) => crate::debug!("{}: {err:?}", path.display()),
				}
			}

			// Not found, so err:
			return Err(Error::Err("Could not find ARM toolchain in default paths"));
		}

		#[cfg(windows)]
		{
			println!("TRY_FROM_DEFAULT_PATH:: WIN...");
			let path =
				PathBuf::from(r"C:\Program Files (x86)\GNU Tools Arm Embedded\9 2019-q4-major\bin\").join(ARM_NONE_EABI_GCC[0])
				                                                                                    .with_extension("exe");
			println!(
			         "TRY_FROM_DEFAULT_PATH:: WIN:: {} - {}",
			         path.exists(),
			         path.display()
			);
			Self::try_from_path(path).map_err(|_| Error::Err("Could not find ARM toolchain in default paths"))
		}
	}


	/// Determine sysroot.
	// There're another ways to do this.
	// For example we can parse makefile in PlaydateSDK to get the path, but that's ugly way.
	fn sysroot(&self) -> Result<PathBuf, Error> { self.sysroot_by_output().or_else(|_| self.sysroot_fallback()) }

	/// Determine by asking gcc.
	fn sysroot_by_output(&self) -> Result<PathBuf, Error> {
		let mut proc = Command::new(&self.path);
		proc.arg("-print-sysroot");

		let output = proc.output()?;
		if !output.status.success() {
			return Err(Error::exit_status_error(&proc, output.stderr, output.status));
		}
		let path = std::str::from_utf8(&output.stdout).map(str::trim)
		                                              .map(PathBuf::from)?;

		if path.as_os_str().is_empty() {
			Err(Error::Err("gcc returns empty string for sysroot"))
		} else {
			Ok(path.canonicalize()?)
		}
	}

	/// Determine by path, relative to the gcc path.
	fn sysroot_fallback(&self) -> Result<PathBuf, Error> {
		// just name in PATH | full path
		let path = if self.path.is_relative() || self.path.components().count() == 1 {
			           let mut proc = Command::new("which");
			           proc.arg(&self.path);
			           let output = proc.output()?;
			           if !output.status.success() {
				           return Err(Error::exit_status_error(&proc, output.stderr, output.status));
			           }
			           crate::debug!("path by which: {:?}", std::str::from_utf8(&output.stdout));
			           Cow::from(std::str::from_utf8(&output.stdout).map(str::trim)
			                                                        .map(PathBuf::from)?)
		           } else {
			           Cow::from(self.path.as_path())
		           }.canonicalize()?;


		let path = path.parent()
		               .and_then(|p| p.parent())
		               .map(|p| p.join("arm-none-eabi"))
		               .ok_or(IoError::new(ErrorKind::NotFound, "GCC sysroot not found"))?;

		if !path.exists() && path == PathBuf::from("/usr/arm-none-eabi") {
			let path = PathBuf::from("/usr/lib/arm-none-eabi");
			if path.exists() {
				return Ok(path);
			}
		}

		crate::trace!("trying canonicalize this: {}", path.display());
		let path = path.canonicalize()?;
		Ok(path)
	}
}


impl ArmToolchain {
	pub fn gcc(&self) -> &Gcc { &self.gcc }
	pub fn bin(&self) -> PathBuf { self.sysroot.join("bin") }
	pub fn lib(&self) -> PathBuf { self.sysroot.join("lib") }
	pub fn include(&self) -> PathBuf { self.sysroot.join("include") }
	pub fn sysroot(&self) -> &Path { self.sysroot.as_ref() }


	/// Specialized search-path for target
	// e.g.: arm-none-eabi-gcc -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -print-search-dirs
	pub fn lib_search_paths_for<S: AsRef<OsStr>, I: IntoIterator<Item = S>>(&self,
	                                                                        args: I)
	                                                                        -> Result<Vec<PathBuf>, Error> {
		let mut proc = Command::new(self.gcc().path());
		proc.args(args);
		proc.arg("-print-search-dirs");
		proc.stderr(Stdio::inherit());
		proc.stdout(Stdio::piped());

		let output = proc.output()?;
		if !output.status.success() {
			return Err(Error::exit_status_error(&proc, output.stderr, output.status));
		}

		#[cfg(not(windows))]
		const SEP: &str = ":";
		#[cfg(windows)]
		const SEP: &str = ";";

		Ok(std::str::from_utf8(&output.stdout)?.lines()
		                                       .filter_map(|s| s.strip_prefix("libraries: ="))
		                                       .flat_map(|s| s.split(SEP).map(|s| s.trim()).map(PathBuf::from))
		                                       .collect())
	}

	pub fn lib_search_paths_for_playdate(&self) -> Result<Vec<PathBuf>, Error> {
		self.lib_search_paths_for([
			"-mthumb",
			"-mcpu=cortex-m7",
			"-mfloat-abi=hard",
			"-mfpu=fpv5-sp-d16",
		])
	}

	pub fn lib_search_paths_default(&self) -> Result<Vec<PathBuf>, Error> {
		match self.lib_search_paths_for::<&str, _>([]) {
			Ok(paths) if !paths.is_empty() => Ok(paths),
			Ok(_) | Err(_) => Ok(vec![self.gcc().sysroot().and_then(|p| Ok(p.join("lib")))?]),
		}
	}


	/// Create auto-determine the toolchain
	pub fn try_new() -> Result<Self, Error> { Self::try_new_with(Gcc::try_new()?) }

	/// Create auto-determine the toolchain by specified gcc
	pub fn try_new_with(gcc: Gcc) -> Result<Self, Error> {
		let sysroot = gcc.sysroot()?;
		let bin = sysroot.join("bin");
		let lib = sysroot.join("lib");
		let include = sysroot.join("include");

		if !bin.try_exists()? || !lib.try_exists()? || !include.try_exists()? {
			Err(IoError::new(
				ErrorKind::NotFound,
				format!("ARM toolchain not found in '{}'", sysroot.display()),
			).into())
		} else {
			Ok(Self { gcc, sysroot })
		}
	}
}


pub mod err {
	use std::io::Error as IoError;
	use std::process::Command;
	use std::process::ExitStatus;
	use std::str::Utf8Error;

	#[derive(Debug)]
	pub enum Error {
		Io(IoError),
		Utf8(Utf8Error),
		Err(&'static str),
		ExitStatusError {
			cmd: String,
			stderr: Vec<u8>,
			status: ExitStatus,
		},
		// TODO: from `std::process::ExitStatusError` when stabilized `exit_status_error`
	}

	impl From<&'static str> for Error {
		fn from(s: &'static str) -> Self { Self::Err(s) }
	}

	impl From<IoError> for Error {
		fn from(err: IoError) -> Self { Self::Io(err) }
	}
	impl From<Utf8Error> for Error {
		fn from(err: Utf8Error) -> Self { Self::Utf8(err) }
	}

	impl Error {
		pub fn exit_status_error(cmd: &Command, stderr: Vec<u8>, status: ExitStatus) -> Self {
			let cmd = format!(
			                  "{} {}",
			                  cmd.get_program().to_string_lossy(),
			                  cmd.get_args()
			                     .map(|s| s.to_string_lossy())
			                     .collect::<Vec<_>>()
			                     .join(" ")
			);
			Self::ExitStatusError { cmd, stderr, status }
		}
	}

	impl std::error::Error for Error {
		fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
			match self {
				Error::Io(err) => Some(err),
				Error::Utf8(err) => Some(err),
				Error::Err(_) => None,
				Error::ExitStatusError { .. } => None,
			}
		}
	}

	impl std::fmt::Display for Error {
		fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
			match self {
				Error::Io(err) => err.fmt(f),
				Error::Utf8(err) => err.fmt(f),
				Error::Err(err) => err.fmt(f),
				Error::ExitStatusError { cmd, status, stderr } => {
					let stderr = std::str::from_utf8(&stderr).map(str::trim)
					                                         .map(|s| format!("with output: {s}"))
					                                         .ok()
					                                         .unwrap_or_else(|| {
						                                         if stderr.is_empty() {
							                                         "without output".into()
						                                         } else {
							                                         "with not decodable output".into()
						                                         }
					                                         });
					write!(f, "ExitStatusError: ({status}) {cmd} {stderr}.",)
				},
			}
		}
	}
}


// TODO: Maybe move this tests to integration tests dir and run if arm-gcc exists only.
#[cfg(test)]
mod tests {
	use super::*;


	#[test]
	fn gcc_from_env_path() { Gcc::try_from_env_path().unwrap(); }

	#[test]
	#[cfg(unix)]
	fn gcc_from_default_path() { Gcc::try_from_default_path().unwrap(); }


	#[test]
	#[cfg(unix)]
	fn gcc_sysroot_fallback() {
		let gcc = Gcc::try_new().unwrap();
		let res = gcc.sysroot_fallback().unwrap();
		assert!(res.exists());
	}

	#[test]
	#[ignore = "sysroot can be empty"]
	fn gcc_sysroot_by_output() {
		let gcc = Gcc::try_new().unwrap();
		let res = gcc.sysroot_by_output().unwrap();
		assert!(res.exists());
	}


	#[test]
	fn toolchain_new() {
		let toolchain = ArmToolchain::try_new().unwrap();
		assert!(toolchain.bin().exists());
		assert!(toolchain.lib().exists());
		assert!(toolchain.include().exists());
	}
}