cargo-skyline 3.1.0

A cargo subcommand for working with Skyline plugins written in Rust
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};

use crate::build::get_rustup_home;
use crate::Error;

use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::{env, fs};

fn get_cargo_dir() -> PathBuf {
    env::var("CARGO_HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|_| {
            dirs::home_dir()
                .expect("No home directory found")
                .push_join(".cargo")
        })
        .ensure_exists()
}

const ORG: &str = "skyline-rs";
const REPO: &str = "rust-src";
const BRANCH: &str = "skyline";

fn url() -> String {
    format!("https://github.com/{}/{}", ORG, REPO)
}

#[tokio::main(flavor = "current_thread")]
async fn get_base_nightly() -> Result<String, Error> {
    let octocrab = octocrab::instance();

    let commit = octocrab
        .repos(ORG, REPO)
        .list_commits()
        .branch(BRANCH)
        .author("bors")
        .per_page(1)
        .send()
        .await?
        .into_iter()
        .next()
        .ok_or(Error::NoBaseCommit)?;

    Ok(format!(
        "nightly-{}",
        commit
            .commit
            .author
            .expect("No author for the last bors commit")
            .date
            .expect("No date for last bors commit")
            .format("%Y-%m-%d")
    ))
}

fn get_original_toolchain(
    base_nightly_progress: &ProgressBar,
    progress: &ProgressBar,
    success_style: ProgressStyle,
    failed_style: ProgressStyle,
) -> Result<PathBuf, Error> {
    let base_nightly = std::thread::spawn(get_base_nightly);

    while !base_nightly.is_finished() {
        base_nightly_progress.tick();
    }

    let base_nightly = base_nightly.join().unwrap().map_err(|err| {
        base_nightly_progress.set_style(failed_style.clone());
        base_nightly_progress.finish_with_message("Failed to get find base nightly");

        err
    })?;

    let toolchain = get_rustup_home()?
        .push_join("toolchains")
        .push_join(format!("{}-{}", base_nightly, TARGET));

    progress.println(format!(
        "Using {base_nightly} as a base for installation..."
    ));

    base_nightly_progress.set_style(success_style.clone());
    base_nightly_progress.finish_with_message("Base nightly found");

    if toolchain.exists() {
        Ok(toolchain)
    } else {
        let mut rustup_cmd = Command::new("rustup")
            .args(&["toolchain", "add", &base_nightly])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .stdin(Stdio::null())
            .spawn()
            .map_err(|_| Error::RustupToolchainAddFailed)?;

        let err = |_| Error::RustupToolchainAddFailed;

        let status = loop {
            progress.tick();

            if let Some(status) = rustup_cmd.try_wait().map_err(err)? {
                break status;
            }
        };

        let install_succeed = status.success();

        if install_succeed {
            progress.set_style(success_style);
            progress.finish_with_message("Base toolchain downloaded");
        } else {
            progress.set_style(failed_style);
            progress.finish_with_message("Failed to get find base nightly");
        }

        (install_succeed && toolchain.exists())
            .then(|| toolchain)
            .ok_or(Error::RustupToolchainAddFailed)
    }
}

fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> {
    fs::create_dir_all(&dst)?;

    for entry in fs::read_dir(src)? {
        let entry = entry?;
        let ty = entry.file_type()?;

        if ty.is_dir() {
            copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
        } else {
            fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
        }
    }
    Ok(())
}

pub fn target_json_path() -> PathBuf {
    get_cargo_skyline_dir().push_join("aarch64-skyline-switch.json")
}

fn linker_script_path() -> PathBuf {
    get_cargo_skyline_dir().push_join("link.T")
}

const LINKER_SCRIPT: &str = include_str!("link.T");

fn ensure_target_json_exists() {
    let target_json_path = target_json_path();

    let link_script_path = linker_script_path();
    if !link_script_path.exists() {
        fs::write(&link_script_path, LINKER_SCRIPT).expect("Failed to create link.T linker script");
    }

    if !target_json_path.exists() {
        fs::write(&target_json_path, target_json())
            .expect("Failed to create aarch64-skyline-switch target json");
    }
}

fn target_json() -> String {
    let linker_script = if cfg!(windows) {
        linker_script_path()
            .to_str()
            .unwrap()
            .replace('\\', "/")
            .into()
    } else {
        linker_script_path()
    };

    format!(
        r#"{{
        "arch": "aarch64",
        "crt-static-default": false,
        "crt-static-respected": false,
        "data-layout": "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128",
        "dynamic-linking": true,
        "executables": true,
        "has-rpath": false,
        "linker": "rust-lld",
        "linker-flavor": "ld.lld",
        "llvm-target": "aarch64-unknown-none",
        "max-atomic-width": 128,
        "os": "switch",
        "panic-strategy": "abort",
        "position-independent-executables": true,
        "pre-link-args": {{
          "ld.lld": [
            "-T{linker_script}",
            "-init=__custom_init",
            "-fini=__custom_fini",
            "--export-dynamic"
          ]
        }},
        "post-link-args": {{
          "ld.lld": [
            "--no-gc-sections",
            "--eh-frame-hdr"
          ]
        }},
        "relro-level": "off",
        "target-c-int-width": "32",
        "target-endian": "little",
        "target-pointer-width": "64",
        "vendor": "jam1garner"
    }}"#,
        linker_script = linker_script.to_string_lossy()
    )
}

pub fn create_modified_toolchain(deep: bool, pull: bool) -> Result<(), Error> {
    let multiprogress = MultiProgress::new();
    let style =
        ProgressStyle::default_spinner().template("{prefix:.bold.dim} {spinner} {wide_msg}");
    let finished_style =
        ProgressStyle::default_spinner().template("{prefix:.bold.dim} ✔️ {wide_msg}");
    let failed_style =
        ProgressStyle::default_spinner().template("{prefix:.bold.dim} ❌ {wide_msg}");

    let get_base_nightly_pb = multiprogress.add(
        ProgressBar::new_spinner()
            .with_message("Searching git history for base nightly")
            .with_prefix("[1/3]")
            .with_style(style.clone()),
    );

    let base_chain_pb = multiprogress.add(
        ProgressBar::new_spinner()
            .with_message("Downloading base toolchain")
            .with_prefix("[2/3]")
            .with_style(style.clone()),
    );

    let std_clone_pb = multiprogress.add(
        ProgressBar::new_spinner()
            .with_message("Downloading custom Rust standard library")
            .with_prefix("[3/3]")
            .with_style(style),
    );

    std::thread::spawn(move || multiprogress.join());

    let toolchain = get_toolchain();

    if pull {
        let pull_success = Command::new("git")
            .current_dir(toolchain.join("lib/rustlib/src/rust"))
            .args(&["pull", "--recurse-submodules", "-q"])
            .status()
            .map_err(|_| Error::GitNotInstalled)?
            .success();

        return if pull_success {
            Ok(())
        } else {
            Err(Error::StdCloneFailed)
        };
    }

    let _ = fs::remove_dir_all(&toolchain);

    let original_toolchain = get_original_toolchain(
        &get_base_nightly_pb,
        &base_chain_pb,
        finished_style.clone(),
        failed_style.clone(),
    )?;

    copy_dir_all(&original_toolchain, &toolchain).map_err(|_| Error::ToolchainCopyFailed)?;

    let src_dir = toolchain.join("lib/rustlib/src");

    if src_dir.exists() {
        let _ = fs::remove_dir_all(&src_dir);
    }

    if fs::create_dir_all(&src_dir).is_err() {
        panic!("Failed to create {:?}", &src_dir);
    }

    let src_dir = src_dir.push_join("rust");

    let mut clone_cmd = Command::new("git")
        .args(&["clone", "--recurse-submodules"])
        .args(if deep {
            &[]
        } else {
            &["--shallow-submodules", "--depth", "1"][..]
        })
        .arg("--branch")
        .arg(BRANCH)
        .arg(url())
        .arg(&src_dir)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .stdin(Stdio::null())
        .spawn()
        .map_err(|_| Error::GitNotInstalled)?;

    let clone_status = loop {
        std_clone_pb.tick();

        if let Some(status) = clone_cmd.try_wait()? {
            break status;
        }
    };

    std_clone_pb.set_style(if clone_status.success() {
        finished_style
    } else {
        failed_style
    });
    std_clone_pb.finish_with_message(if clone_status.success() {
        "Finished downloading custom Rust standard library"
    } else {
        "Failed to download custom Rust standard library"
    });

    rustup_toolchain_link("skyline-v3", &toolchain)?;

    if clone_status.success() {
        Ok(())
    } else {
        Err(Error::StdCloneFailed)
    }
}

fn get_cargo_skyline_dir() -> PathBuf {
    get_cargo_dir().push_join("skyline").ensure_exists()
}

fn get_skyline_toolchain_dir() -> PathBuf {
    get_cargo_skyline_dir()
        .push_join("toolchain")
        .ensure_exists()
}

fn get_toolchain() -> PathBuf {
    get_skyline_toolchain_dir()
        .push_join("skyline")
        .ensure_exists()
}

const TARGET: &str = env!("TARGET");

pub fn check_std_installed() -> Result<(), Error> {
    ensure_target_json_exists();

    if get_rustup_home()?
        .push_join("toolchains/skyline-v3")
        .exists()
    {
        Ok(())
    } else {
        let should_install = dialoguer::Confirm::new()
            .with_prompt("The skyline-rs toolchain is not installed. Would you like to install it?")
            .default(true)
            .interact()
            .unwrap();

        if should_install {
            create_modified_toolchain(false, false)
        } else {
            std::process::exit(1);
        }
    }
}

fn rustup_toolchain_link(name: &str, path: &Path) -> Result<(), Error> {
    let status = Command::new("rustup")
        .args(&["toolchain", "link", name])
        .arg(path)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .map_err(|_| Error::RustupNotFound)?;

    if status.success() {
        Ok(())
    } else {
        Err(Error::RustupLinkFailed)
    }
}

pub fn update_std(_repo: &str, _tag: Option<&str>, deep: bool, pull: bool) -> Result<(), Error> {
    create_modified_toolchain(deep, pull)?;

    Ok(())
}

pub(crate) trait PathExt: Sized {
    fn ensure_exists(self) -> Self;
    fn push_join<P: AsRef<Path>>(self, join: P) -> Self;
    fn if_exists(self) -> Option<Self>;
}

impl PathExt for PathBuf {
    fn ensure_exists(self) -> Self {
        if !self.exists() {
            fs::create_dir_all(&self).unwrap();
        }

        self
    }

    fn push_join<P: AsRef<Path>>(mut self, join: P) -> Self {
        self.push(join);

        self
    }

    fn if_exists(self) -> Option<Self> {
        if self.exists() {
            Some(self)
        } else {
            None
        }
    }
}