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
mod image;
mod opencv;
mod test;
use crate::{commands::fetch_online, Arch, PROJECT_DIR, REPOS};
use os_xtask_utils::{dir, CommandExt, Ext, Git, Make};
use std::{
env,
ffi::OsString,
fs,
os::unix,
path::{Path, PathBuf},
};
pub(crate) struct LinuxRootfs(Arch);
impl LinuxRootfs {
#[inline]
pub const fn new(arch: Arch) -> Self {
Self(arch)
}
pub fn make(&self, clear: bool) {
let dir = self.path();
if dir.is_dir() && !clear {
return;
}
let musl = self.0.linux_musl_cross();
let busybox = self.busybox(&musl);
let bin = dir.join("bin");
let lib = dir.join("lib");
dir::clear(&dir).unwrap();
fs::create_dir(&bin).unwrap();
fs::create_dir(&lib).unwrap();
fs::copy(busybox, bin.join("busybox")).unwrap();
let from = musl
.join(format!("{}-linux-musl", self.0.name()))
.join("lib")
.join("libc.so");
let to = lib.join(format!("ld-musl-{arch}.so.1", arch = self.0.name()));
fs::copy(from, &to).unwrap();
Ext::new(self.strip(musl)).arg("-s").arg(to).invoke();
const SH: &[&str] = &[
"cat", "cp", "echo", "false", "grep", "gzip", "kill", "ln", "ls", "mkdir", "mv",
"pidof", "ping", "ping6", "printenv", "ps", "pwd", "rm", "rmdir", "sh", "sleep",
"stat", "tar", "touch", "true", "uname", "usleep", "watch",
];
let bin = dir.join("bin");
for sh in SH {
unix::fs::symlink("busybox", bin.join(sh)).unwrap();
}
}
pub fn put_musl_libs(&self) -> PathBuf {
self.make(false);
let dir = self.0.linux_musl_cross();
self.put_libs(&dir, dir.join(format!("{}-linux-musl", self.0.name())));
dir
}
#[inline]
pub fn path(&self) -> PathBuf {
PROJECT_DIR.join("rootfs").join(self.0.name())
}
fn busybox(&self, musl: impl AsRef<Path>) -> PathBuf {
let target = self.0.target().join("busybox");
let executable = target.join("busybox");
if executable.is_file() {
return executable;
}
let source = REPOS.join("busybox");
if !source.is_dir() {
fetch_online!(source, |tmp| {
Git::clone("https://git.busybox.net/busybox.git")
.dir(tmp)
.single_branch()
.depth(1)
.done()
});
}
dir::rm(&target).unwrap();
dircpy::copy_dir(source, &target).unwrap();
Make::new().current_dir(&target).arg("defconfig").invoke();
let musl = musl.as_ref();
Make::new()
.current_dir(&target)
.arg(format!(
"CROSS_COMPILE={musl}/{arch}-linux-musl-",
musl = musl.canonicalize().unwrap().join("bin").display(),
arch = self.0.name(),
))
.invoke();
Ext::new(self.strip(musl))
.arg("-s")
.arg(&executable)
.invoke();
executable
}
fn strip(&self, musl: impl AsRef<Path>) -> PathBuf {
musl.as_ref()
.join("bin")
.join(format!("{}-linux-musl-strip", self.0.name()))
}
fn put_libs(&self, musl: impl AsRef<Path>, dir: impl AsRef<Path>) {
let lib = self.path().join("lib");
let musl_libc_protected = format!("ld-musl-{}.so.1", self.0.name());
let musl_libc_ignored = "libc.so";
let strip = self.strip(musl);
dir.as_ref()
.join("lib")
.read_dir()
.unwrap()
.filter_map(|res| res.map(|e| e.path()).ok())
.filter(|path| check_so(path))
.for_each(|source| {
let name = source.file_name().unwrap();
let target = lib.join(name);
if source.is_symlink() {
if name != musl_libc_protected.as_str() {
dir::rm(&target).unwrap();
unix::fs::symlink(source.read_link().unwrap(), target).unwrap();
}
} else if name != musl_libc_ignored {
dir::rm(&target).unwrap();
fs::copy(source, &target).unwrap();
Ext::new(&strip).arg("-s").arg(target).status();
}
});
}
}
fn join_path_env<I, S>(paths: I) -> OsString
where
I: IntoIterator<Item = S>,
S: AsRef<Path>,
{
let mut path = OsString::new();
let mut first = true;
if let Ok(current) = env::var("PATH") {
path.push(current);
first = false;
}
for item in paths {
if first {
first = false;
} else {
path.push(":");
}
path.push(item.as_ref().canonicalize().unwrap().as_os_str());
}
path
}
fn check_so<P: AsRef<Path>>(path: P) -> bool {
let path = path.as_ref();
if !path.is_symlink() && !path.is_file() {
return false;
}
let name = path.file_name().unwrap().to_string_lossy();
let mut seg = name.split('.');
if matches!(seg.next(), Some("") | None) {
return false;
}
if !matches!(seg.next(), Some("so")) {
return false;
}
!seg.any(|it| !it.chars().all(|ch| ch.is_ascii_digit()))
}