summaryrefslogtreecommitdiffstats
path: root/src/context.rs
blob: 983aed795368a93a6c0ff3e82a39c7fd9b33f2b0 (plain)
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
use std::{
	borrow::Cow,
	collections::{HashMap, HashSet},
	fmt::Display,
	hash::Hash,
	ops::Index,
	rc::Rc,
};

use serde::Serialize;

use crate::{
	args::{self, arg, TaskArgs},
	task::*,
	types::TaskID,
};

#[derive(Clone, Debug, Serialize, PartialEq, Eq, Hash)]
pub struct TaskRef<'ctx> {
	pub id: &'ctx TaskID,
	pub args: TaskArgs,
}

impl<'ctx> Display for TaskRef<'ctx> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		self.id.fmt(f)
	}
}

#[derive(Clone, Debug, Serialize, PartialEq, Eq, Hash)]
pub struct OutputRef<'ctx> {
	pub task: TaskRef<'ctx>,
	pub output: &'ctx str,
}

const BUILD_PLATFORM: args::Platform = args::Platform {
	gnu_triplet: "x86_64-linux-gnu",
	karch: "x86",
	prefix: "/opt/toolchain",
};
const HOST_PLATFORM: args::Platform = args::Platform {
	gnu_triplet: "aarch64-linux-gnu",
	karch: "arm64",
	prefix: "/usr",
};

#[derive(Debug)]
pub struct Context {
	tasks: HashMap<TaskID, TaskDef>,
	default_args: HashMap<String, Rc<serde_json::Value>>,
}

impl Context {
	pub fn new(tasks: HashMap<TaskID, TaskDef>) -> Self {
		let default_args = [arg("build", BUILD_PLATFORM), arg("host", HOST_PLATFORM)]
			.iter()
			.cloned()
			.collect();

		Context {
			tasks,
			default_args,
		}
	}

	pub fn get(&self, id: &TaskID) -> Option<&TaskDef> {
		self.tasks.get(id)
	}

	fn task_ref<'a>(&'a self, id: &'a TaskID, args: Cow<'_, TaskArgs>) -> TaskRef {
		TaskRef {
			id,
			args: args.into_owned(),
		}
	}

	pub fn parse(&self, task: &str) -> Option<TaskRef> {
		let (id, _) = self.tasks.get_key_value(task)?;
		Some(self.task_ref(id, Cow::Owned(TaskArgs(self.default_args.clone()))))
	}

	fn inherit_ref<'a>(&'a self, dep: &'a InheritDep, args: &TaskArgs) -> TaskRef {
		self.task_ref(&dep.task, Cow::Borrowed(args))
	}

	pub fn output_ref<'a>(&'a self, dep: &'a OutputDep, args: &TaskArgs) -> OutputRef<'a> {
		OutputRef {
			task: self.task_ref(&dep.task, Cow::Borrowed(args)),
			output: &dep.output,
		}
	}

	fn build_depend_args(args: &TaskArgs) -> TaskArgs {
		let mut ret = args.clone();

		ret.0.insert("host".to_string(), args.0["build"].clone());

		// TODO: Hack for correct target of toolchain build depends (gcc -> binutils)
		if !ret.0.contains_key("target") {
			ret.0.insert("target".to_string(), args.0["host"].clone());
		}

		ret
	}

	pub fn get_inherit_depend(&self, task_ref: &TaskRef) -> Option<TaskRef> {
		let task = &self[task_ref.id];
		Some(self.inherit_ref(task.inherit.as_ref()?, &task_ref.args))
	}

	pub fn get_build_depends(&self, task_ref: &TaskRef) -> HashSet<OutputRef> {
		let task = &self[task_ref.id];
		task.build_depends
			.iter()
			.map(|dep| self.output_ref(dep, &Context::build_depend_args(&task_ref.args)))
			.collect()
	}

	pub fn get_host_depends(&self, task_ref: &TaskRef) -> HashSet<OutputRef> {
		let task = &self[task_ref.id];
		task.depends
			.iter()
			.map(|dep| self.output_ref(dep, &task_ref.args))
			.collect()
	}

	pub fn get_dependent_outputs(&self, task_ref: &TaskRef) -> HashSet<OutputRef> {
		self.get_build_depends(task_ref)
			.into_iter()
			.chain(self.get_host_depends(task_ref).into_iter())
			.collect()
	}

	pub fn get_dependent_tasks(&self, task_ref: &TaskRef) -> HashSet<TaskRef> {
		self.get_inherit_depend(task_ref)
			.into_iter()
			.chain(
				self.get_dependent_outputs(task_ref)
					.into_iter()
					.map(|dep| dep.task),
			)
			.collect()
	}
}

impl Index<&TaskID> for Context {
	type Output = TaskDef;

	fn index(&self, index: &TaskID) -> &TaskDef {
		self.tasks.get(index).expect("Invalid TaskID")
	}
}