Skip to main content

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_RELEASE_CODEGEN_UNITS", "1");
111    let _e = sh.push_env("CARGO_PROFILE_DEV_REL_LTO", "thin");
112    let _e = sh.push_env("CARGO_PROFILE_DEV_REL_CODEGEN_UNITS", "1");
113
114    // Uncomment to enable debug info for releases. Note that:
115    //   * debug info is split on windows and macs, so it does nothing for those platforms,
116    //   * on Linux, this blows up the binary size from 8MB to 43MB, which is unreasonable.
117    // let _e = sh.push_env("CARGO_PROFILE_RELEASE_DEBUG", "1");
118
119    let linux_target = target.is_linux();
120    let target_name = match &target.libc_suffix {
121        Some(libc_suffix) if zig => format!("{}.{libc_suffix}", target.name),
122        _ => target.name.to_owned(),
123    };
124    let features = allocator.to_features();
125    let command = if linux_target && zig { "zigbuild" } else { "build" };
126
127    let pgo_profile = if let Some(train_crate) = pgo {
128        Some(crate::pgo::gather_pgo_profile(
129            sh,
130            crate::pgo::build_command(sh, command, &target_name, features),
131            &target_name,
132            train_crate,
133        )?)
134    } else {
135        None
136    };
137
138    let mut cmd = build_command(sh, command, &target_name, features, dev_rel);
139    let mut rustflags = Vec::new();
140
141    if let Some(profile) = pgo_profile {
142        rustflags.push(format!("-Cprofile-use={}", profile.to_str().unwrap()));
143    }
144
145    if target_name.ends_with("-windows-msvc") {
146        // https://github.com/rust-lang/rust-analyzer/issues/20970
147        rustflags.push("-Ctarget-feature=+crt-static".to_owned());
148    }
149
150    if !rustflags.is_empty() {
151        cmd = cmd.env("RUSTFLAGS", rustflags.join(" "));
152    }
153    cmd.run().context("cannot build Rust Analyzer")?;
154
155    let dst = Path::new("dist").join(&target.artifact_name);
156    if target_name.contains("-windows-") {
157        zip(&target.server_path, target.symbols_path.as_ref(), &dst.with_extension("zip"))?;
158    } else {
159        gzip(&target.server_path, &dst.with_extension("gz"))?;
160    }
161
162    Ok(())
163}
164
165fn build_command<'a>(
166    sh: &'a Shell,
167    command: &str,
168    target_name: &str,
169    features: &[&str],
170    dev_rel: bool,
171) -> Cmd<'a> {
172    let profile = if dev_rel { "dev-rel" } else { "release" };
173    cmd!(
174        sh,
175        "cargo {command} --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --target {target_name} {features...} --profile {profile}"
176    )
177}
178
179fn gzip(src_path: &Path, dest_path: &Path) -> anyhow::Result<()> {
180    let mut encoder = GzEncoder::new(File::create(dest_path)?, Compression::best());
181    let mut input = io::BufReader::new(File::open(src_path)?);
182    io::copy(&mut input, &mut encoder)?;
183    encoder.finish()?;
184    Ok(())
185}
186
187fn zip(src_path: &Path, symbols_path: Option<&PathBuf>, dest_path: &Path) -> anyhow::Result<()> {
188    let file = File::create(dest_path)?;
189    let mut writer = ZipWriter::new(BufWriter::new(file));
190    writer.start_file(
191        src_path.file_name().unwrap().to_str().unwrap(),
192        SimpleFileOptions::default()
193            .last_modified_time(
194                DateTime::try_from(OffsetDateTime::from(std::fs::metadata(src_path)?.modified()?))
195                    .unwrap(),
196            )
197            .unix_permissions(0o755)
198            .compression_method(zip::CompressionMethod::Deflated)
199            .compression_level(Some(9)),
200    )?;
201    let mut input = io::BufReader::new(File::open(src_path)?);
202    io::copy(&mut input, &mut writer)?;
203    if let Some(symbols_path) = symbols_path {
204        writer.start_file(
205            symbols_path.file_name().unwrap().to_str().unwrap(),
206            SimpleFileOptions::default()
207                .last_modified_time(
208                    DateTime::try_from(OffsetDateTime::from(
209                        std::fs::metadata(src_path)?.modified()?,
210                    ))
211                    .unwrap(),
212                )
213                .compression_method(zip::CompressionMethod::Deflated)
214                .compression_level(Some(9)),
215        )?;
216        let mut input = io::BufReader::new(File::open(symbols_path)?);
217        io::copy(&mut input, &mut writer)?;
218    }
219    writer.finish()?;
220    Ok(())
221}
222
223struct Target {
224    name: String,
225    libc_suffix: Option<String>,
226    server_path: PathBuf,
227    symbols_path: Option<PathBuf>,
228    artifact_name: String,
229}
230
231impl Target {
232    fn get(project_root: &Path, sh: &Shell) -> Self {
233        let name = detect_target(sh);
234        let (name, libc_suffix) = match name.split_once('.') {
235            Some((l, r)) => (l.to_owned(), Some(r.to_owned())),
236            None => (name, None),
237        };
238        let out_path = project_root.join("target").join(&name).join("release");
239        let (exe_suffix, symbols_path) = if name.contains("-windows-") {
240            (".exe".into(), Some(out_path.join("rust_analyzer.pdb")))
241        } else {
242            (String::new(), None)
243        };
244        let server_path = out_path.join(format!("rust-analyzer{exe_suffix}"));
245        let artifact_name = format!("rust-analyzer-{name}{exe_suffix}");
246        Self { name, libc_suffix, server_path, symbols_path, artifact_name }
247    }
248
249    fn is_linux(&self) -> bool {
250        self.name.contains("-linux-")
251    }
252}
253
254struct Patch {
255    path: PathBuf,
256    original_contents: String,
257    contents: String,
258}
259
260impl Patch {
261    fn new(sh: &Shell, path: impl Into<PathBuf>) -> anyhow::Result<Patch> {
262        let path = path.into();
263        let contents = sh.read_file(&path)?;
264        Ok(Patch { path, original_contents: contents.clone(), contents })
265    }
266
267    fn replace(&mut self, from: &str, to: &str) -> &mut Patch {
268        assert!(self.contents.contains(from));
269        self.contents = self.contents.replace(from, to);
270        self
271    }
272
273    fn commit(&self, sh: &Shell) -> anyhow::Result<()> {
274        sh.write_file(&self.path, &self.contents)?;
275        Ok(())
276    }
277}
278
279impl Drop for Patch {
280    fn drop(&mut self) {
281        // FIXME: find a way to bring this back
282        let _ = &self.original_contents;
283        // write_file(&self.path, &self.original_contents).unwrap();
284    }
285}