xtask/
dist.rs

1use anyhow::Context;
2use flate2::{Compression, write::GzEncoder};
3use std::{
4    fs::File,
5    io::{self, BufWriter},
6    path::{Path, PathBuf},
7};
8use time::OffsetDateTime;
9use xshell::{Cmd, Shell, cmd};
10use zip::{DateTime, ZipWriter, write::SimpleFileOptions};
11
12use crate::{
13    date_iso,
14    flags::{self, Malloc, PgoTrainingCrate},
15    project_root,
16    util::detect_target,
17};
18
19const VERSION_STABLE: &str = "0.3";
20const VERSION_NIGHTLY: &str = "0.4";
21const VERSION_DEV: &str = "0.5"; // keep this one in sync with `package.json`
22
23impl flags::Dist {
24    pub(crate) fn run(self, sh: &Shell) -> anyhow::Result<()> {
25        let stable = sh.var("GITHUB_REF").unwrap_or_default().as_str() == "refs/heads/release";
26
27        let project_root = project_root();
28        let target = Target::get(&project_root, sh);
29        let allocator = self.allocator();
30        let dist = project_root.join("dist");
31        sh.remove_path(&dist)?;
32        sh.create_dir(&dist)?;
33
34        if let Some(patch_version) = self.client_patch_version {
35            let version = if stable {
36                format!("{VERSION_STABLE}.{patch_version}")
37            } else {
38                // A hack to make VS Code prefer nightly over stable.
39                format!("{VERSION_NIGHTLY}.{patch_version}")
40            };
41            dist_server(
42                sh,
43                &format!("{version}-standalone"),
44                &target,
45                allocator,
46                self.zig,
47                self.pgo,
48                // Profiling requires debug information.
49                self.enable_profiling,
50            )?;
51            let release_tag = if stable { date_iso(sh)? } else { "nightly".to_owned() };
52            dist_client(sh, &version, &release_tag, &target)?;
53        } else {
54            dist_server(
55                sh,
56                "0.0.0-standalone",
57                &target,
58                allocator,
59                self.zig,
60                self.pgo,
61                // Profiling requires debug information.
62                self.enable_profiling,
63            )?;
64        }
65        Ok(())
66    }
67}
68
69fn dist_client(
70    sh: &Shell,
71    version: &str,
72    release_tag: &str,
73    target: &Target,
74) -> anyhow::Result<()> {
75    let bundle_path = Path::new("editors").join("code").join("server");
76    sh.create_dir(&bundle_path)?;
77    sh.copy_file(&target.server_path, &bundle_path)?;
78    if let Some(symbols_path) = &target.symbols_path {
79        sh.copy_file(symbols_path, &bundle_path)?;
80    }
81
82    let _d = sh.push_dir("./editors/code");
83
84    let mut patch = Patch::new(sh, "./package.json")?;
85    patch
86        .replace(
87            &format!(r#""version": "{VERSION_DEV}.0-dev""#),
88            &format!(r#""version": "{version}""#),
89        )
90        .replace(r#""releaseTag": null"#, &format!(r#""releaseTag": "{release_tag}""#))
91        .replace(r#""title": "$generated-start""#, "")
92        .replace(r#""title": "$generated-end""#, "")
93        .replace(r#""enabledApiProposals": [],"#, r#""#);
94    patch.commit(sh)?;
95
96    Ok(())
97}
98
99fn dist_server(
100    sh: &Shell,
101    release: &str,
102    target: &Target,
103    allocator: Malloc,
104    zig: bool,
105    pgo: Option<PgoTrainingCrate>,
106    dev_rel: bool,
107) -> anyhow::Result<()> {
108    let _e = sh.push_env("CFG_RELEASE", release);
109    let _e = sh.push_env("CARGO_PROFILE_RELEASE_LTO", "thin");
110    let _e = sh.push_env("CARGO_PROFILE_DEV_REL_LTO", "thin");
111
112    // Uncomment to enable debug info for releases. Note that:
113    //   * debug info is split on windows and macs, so it does nothing for those platforms,
114    //   * on Linux, this blows up the binary size from 8MB to 43MB, which is unreasonable.
115    // let _e = sh.push_env("CARGO_PROFILE_RELEASE_DEBUG", "1");
116
117    let linux_target = target.is_linux();
118    let target_name = match &target.libc_suffix {
119        Some(libc_suffix) if zig => format!("{}.{libc_suffix}", target.name),
120        _ => target.name.to_owned(),
121    };
122    let features = allocator.to_features();
123    let command = if linux_target && zig { "zigbuild" } else { "build" };
124
125    let pgo_profile = if let Some(train_crate) = pgo {
126        Some(crate::pgo::gather_pgo_profile(
127            sh,
128            crate::pgo::build_command(sh, command, &target_name, features),
129            &target_name,
130            train_crate,
131        )?)
132    } else {
133        None
134    };
135
136    let mut cmd = build_command(sh, command, &target_name, features, dev_rel);
137    let mut rustflags = Vec::new();
138
139    if let Some(profile) = pgo_profile {
140        rustflags.push(format!("-Cprofile-use={}", profile.to_str().unwrap()));
141    }
142
143    if target_name.ends_with("-windows-msvc") {
144        // https://github.com/rust-lang/rust-analyzer/issues/20970
145        rustflags.push("-Ctarget-feature=+crt-static".to_owned());
146    }
147
148    if !rustflags.is_empty() {
149        cmd = cmd.env("RUSTFLAGS", rustflags.join(" "));
150    }
151    cmd.run().context("cannot build Rust Analyzer")?;
152
153    let dst = Path::new("dist").join(&target.artifact_name);
154    if target_name.contains("-windows-") {
155        zip(&target.server_path, target.symbols_path.as_ref(), &dst.with_extension("zip"))?;
156    } else {
157        gzip(&target.server_path, &dst.with_extension("gz"))?;
158    }
159
160    Ok(())
161}
162
163fn build_command<'a>(
164    sh: &'a Shell,
165    command: &str,
166    target_name: &str,
167    features: &[&str],
168    dev_rel: bool,
169) -> Cmd<'a> {
170    let profile = if dev_rel { "dev-rel" } else { "release" };
171    cmd!(
172        sh,
173        "cargo {command} --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --target {target_name} {features...} --profile {profile}"
174    )
175}
176
177fn gzip(src_path: &Path, dest_path: &Path) -> anyhow::Result<()> {
178    let mut encoder = GzEncoder::new(File::create(dest_path)?, Compression::best());
179    let mut input = io::BufReader::new(File::open(src_path)?);
180    io::copy(&mut input, &mut encoder)?;
181    encoder.finish()?;
182    Ok(())
183}
184
185fn zip(src_path: &Path, symbols_path: Option<&PathBuf>, dest_path: &Path) -> anyhow::Result<()> {
186    let file = File::create(dest_path)?;
187    let mut writer = ZipWriter::new(BufWriter::new(file));
188    writer.start_file(
189        src_path.file_name().unwrap().to_str().unwrap(),
190        SimpleFileOptions::default()
191            .last_modified_time(
192                DateTime::try_from(OffsetDateTime::from(std::fs::metadata(src_path)?.modified()?))
193                    .unwrap(),
194            )
195            .unix_permissions(0o755)
196            .compression_method(zip::CompressionMethod::Deflated)
197            .compression_level(Some(9)),
198    )?;
199    let mut input = io::BufReader::new(File::open(src_path)?);
200    io::copy(&mut input, &mut writer)?;
201    if let Some(symbols_path) = symbols_path {
202        writer.start_file(
203            symbols_path.file_name().unwrap().to_str().unwrap(),
204            SimpleFileOptions::default()
205                .last_modified_time(
206                    DateTime::try_from(OffsetDateTime::from(
207                        std::fs::metadata(src_path)?.modified()?,
208                    ))
209                    .unwrap(),
210                )
211                .compression_method(zip::CompressionMethod::Deflated)
212                .compression_level(Some(9)),
213        )?;
214        let mut input = io::BufReader::new(File::open(symbols_path)?);
215        io::copy(&mut input, &mut writer)?;
216    }
217    writer.finish()?;
218    Ok(())
219}
220
221struct Target {
222    name: String,
223    libc_suffix: Option<String>,
224    server_path: PathBuf,
225    symbols_path: Option<PathBuf>,
226    artifact_name: String,
227}
228
229impl Target {
230    fn get(project_root: &Path, sh: &Shell) -> Self {
231        let name = detect_target(sh);
232        let (name, libc_suffix) = match name.split_once('.') {
233            Some((l, r)) => (l.to_owned(), Some(r.to_owned())),
234            None => (name, None),
235        };
236        let out_path = project_root.join("target").join(&name).join("release");
237        let (exe_suffix, symbols_path) = if name.contains("-windows-") {
238            (".exe".into(), Some(out_path.join("rust_analyzer.pdb")))
239        } else {
240            (String::new(), None)
241        };
242        let server_path = out_path.join(format!("rust-analyzer{exe_suffix}"));
243        let artifact_name = format!("rust-analyzer-{name}{exe_suffix}");
244        Self { name, libc_suffix, server_path, symbols_path, artifact_name }
245    }
246
247    fn is_linux(&self) -> bool {
248        self.name.contains("-linux-")
249    }
250}
251
252struct Patch {
253    path: PathBuf,
254    original_contents: String,
255    contents: String,
256}
257
258impl Patch {
259    fn new(sh: &Shell, path: impl Into<PathBuf>) -> anyhow::Result<Patch> {
260        let path = path.into();
261        let contents = sh.read_file(&path)?;
262        Ok(Patch { path, original_contents: contents.clone(), contents })
263    }
264
265    fn replace(&mut self, from: &str, to: &str) -> &mut Patch {
266        assert!(self.contents.contains(from));
267        self.contents = self.contents.replace(from, to);
268        self
269    }
270
271    fn commit(&self, sh: &Shell) -> anyhow::Result<()> {
272        sh.write_file(&self.path, &self.contents)?;
273        Ok(())
274    }
275}
276
277impl Drop for Patch {
278    fn drop(&mut self) {
279        // FIXME: find a way to bring this back
280        let _ = &self.original_contents;
281        // write_file(&self.path, &self.original_contents).unwrap();
282    }
283}