Unnamed repository; edit this file 'description' to name the repository.
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
#![allow(unreachable_pub)]

use std::{fmt, str::FromStr};

use crate::install::{ClientOpt, ProcMacroServerOpt, ServerOpt};

#[derive(Debug, Clone)]
pub enum PgoTrainingCrate {
    // Use RA's own sources for PGO training
    RustAnalyzer,
    // Download a Rust crate from `https://github.com/{0}` and use it for PGO training.
    GitHub(String),
}

impl FromStr for PgoTrainingCrate {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "rust-analyzer" => Ok(Self::RustAnalyzer),
            url => Ok(Self::GitHub(url.to_owned())),
        }
    }
}

xflags::xflags! {
    src "./src/flags.rs"

    /// Run custom build command.
    cmd xtask {

        /// Install rust-analyzer server or editor plugin.
        cmd install {
            /// Install only VS Code plugin.
            optional --client
            /// One of `code`, `code-exploration`, `code-insiders`, `codium`, or `code-oss`.
            optional --code-bin name: String

            /// Install only the language server.
            optional --server
            /// Use mimalloc allocator for server.
            optional --mimalloc
            /// Use jemalloc allocator for server.
            optional --jemalloc
            // Enable memory profiling support.
            //
            // **Warning:** This will produce a slower build of rust-analyzer, use only for profiling.
            optional --enable-profiling

            /// Install the proc-macro server.
            optional --proc-macro-server

            /// build in release with debug info set to 2.
            optional --dev-rel

            /// Make `never!()`, `always!()` etc. panic instead of just logging an error.
            optional --force-always-assert

            /// Apply PGO optimizations
            optional --pgo pgo: PgoTrainingCrate
        }

        cmd fuzz-tests {}

        cmd release {
            optional --dry-run
        }

        cmd dist {
            /// Use mimalloc allocator for server
            optional --mimalloc
            /// Use jemalloc allocator for server
            optional --jemalloc
            // Enable memory profiling support.
            //
            // **Warning:** This will produce a slower build of rust-analyzer, use only for profiling.
            optional --enable-profiling
            optional --client-patch-version version: String
            /// Use cargo-zigbuild
            optional --zig
            /// Apply PGO optimizations
            optional --pgo pgo: PgoTrainingCrate
        }
        /// Read a changelog AsciiDoc file and update the GitHub Releases entry in Markdown.
        cmd publish-release-notes {
            /// Only run conversion and show the result.
            optional --dry-run
            /// Target changelog file.
            required changelog: String
        }
        cmd metrics {
            optional measurement_type: MeasurementType
        }
        /// Builds a benchmark version of rust-analyzer and puts it into `./target`.
        cmd bb {
            required suffix: String
        }

        cmd codegen {
            optional codegen_type: CodegenType
            optional --check
        }

        cmd tidy {}
    }
}

// generated start
// The following code is generated by `xflags` macro.
// Run `env UPDATE_XFLAGS=1 cargo build` to regenerate.
#[derive(Debug)]
pub struct Xtask {
    pub subcommand: XtaskCmd,
}

#[derive(Debug)]
pub enum XtaskCmd {
    Install(Install),
    FuzzTests(FuzzTests),
    Release(Release),
    Dist(Dist),
    PublishReleaseNotes(PublishReleaseNotes),
    Metrics(Metrics),
    Bb(Bb),
    Codegen(Codegen),
    Tidy(Tidy),
}

#[derive(Debug)]
pub struct Install {
    pub client: bool,
    pub code_bin: Option<String>,
    pub server: bool,
    pub mimalloc: bool,
    pub jemalloc: bool,
    pub enable_profiling: bool,
    pub proc_macro_server: bool,
    pub dev_rel: bool,
    pub force_always_assert: bool,
    pub pgo: Option<PgoTrainingCrate>,
}

#[derive(Debug)]
pub struct FuzzTests;

#[derive(Debug)]
pub struct Release {
    pub dry_run: bool,
}

#[derive(Debug)]
pub struct Dist {
    pub mimalloc: bool,
    pub jemalloc: bool,
    pub enable_profiling: bool,
    pub client_patch_version: Option<String>,
    pub zig: bool,
    pub pgo: Option<PgoTrainingCrate>,
}

#[derive(Debug)]
pub struct PublishReleaseNotes {
    pub changelog: String,

    pub dry_run: bool,
}

#[derive(Debug)]
pub struct Metrics {
    pub measurement_type: Option<MeasurementType>,
}

#[derive(Debug)]
pub struct Bb {
    pub suffix: String,
}

#[derive(Debug)]
pub struct Codegen {
    pub codegen_type: Option<CodegenType>,

    pub check: bool,
}

#[derive(Debug)]
pub struct Tidy;

impl Xtask {
    #[allow(dead_code)]
    pub fn from_env_or_exit() -> Self {
        Self::from_env_or_exit_()
    }

    #[allow(dead_code)]
    pub fn from_env() -> xflags::Result<Self> {
        Self::from_env_()
    }

    #[allow(dead_code)]
    pub fn from_vec(args: Vec<std::ffi::OsString>) -> xflags::Result<Self> {
        Self::from_vec_(args)
    }
}
// generated end

#[derive(Debug, Default)]
pub enum CodegenType {
    #[default]
    All,
    Grammar,
    AssistsDocTests,
    DiagnosticsDocs,
    LintDefinitions,
    ParserTests,
    FeatureDocs,
}

impl fmt::Display for CodegenType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::All => write!(f, "all"),
            Self::Grammar => write!(f, "grammar"),
            Self::AssistsDocTests => write!(f, "assists-doc-tests"),
            Self::DiagnosticsDocs => write!(f, "diagnostics-docs"),
            Self::LintDefinitions => write!(f, "lint-definitions"),
            Self::ParserTests => write!(f, "parser-tests"),
            Self::FeatureDocs => write!(f, "feature-docs"),
        }
    }
}

impl FromStr for CodegenType {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "all" => Ok(Self::All),
            "grammar" => Ok(Self::Grammar),
            "assists-doc-tests" => Ok(Self::AssistsDocTests),
            "diagnostics-docs" => Ok(Self::DiagnosticsDocs),
            "lint-definitions" => Ok(Self::LintDefinitions),
            "parser-tests" => Ok(Self::ParserTests),
            "feature-docs" => Ok(Self::FeatureDocs),
            _ => Err("Invalid option".to_owned()),
        }
    }
}

#[derive(Debug)]
pub enum MeasurementType {
    Build,
    RustcTests,
    AnalyzeSelf,
    AnalyzeRipgrep,
    AnalyzeWebRender,
    AnalyzeDiesel,
    AnalyzeHyper,
}

impl FromStr for MeasurementType {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "build" => Ok(Self::Build),
            "rustc_tests" => Ok(Self::RustcTests),
            "self" => Ok(Self::AnalyzeSelf),
            "ripgrep-13.0.0" => Ok(Self::AnalyzeRipgrep),
            "webrender-2022" => Ok(Self::AnalyzeWebRender),
            "diesel-1.4.8" => Ok(Self::AnalyzeDiesel),
            "hyper-0.14.18" => Ok(Self::AnalyzeHyper),
            _ => Err("Invalid option".to_owned()),
        }
    }
}
impl AsRef<str> for MeasurementType {
    fn as_ref(&self) -> &str {
        match self {
            Self::Build => "build",
            Self::RustcTests => "rustc_tests",
            Self::AnalyzeSelf => "self",
            Self::AnalyzeRipgrep => "ripgrep-13.0.0",
            Self::AnalyzeWebRender => "webrender-2022",
            Self::AnalyzeDiesel => "diesel-1.4.8",
            Self::AnalyzeHyper => "hyper-0.14.18",
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub(crate) enum Malloc {
    System,
    Mimalloc,
    Jemalloc,
    Dhat,
}

impl Malloc {
    pub(crate) fn to_features(self) -> &'static [&'static str] {
        match self {
            Malloc::System => &[][..],
            Malloc::Mimalloc => &["--features", "mimalloc"],
            Malloc::Jemalloc => &["--features", "jemalloc"],
            Malloc::Dhat => &["--features", "dhat"],
        }
    }
}

impl Install {
    pub(crate) fn server(&self) -> Option<ServerOpt> {
        if (self.client || self.proc_macro_server) && !self.server {
            return None;
        }
        let malloc = if self.mimalloc {
            Malloc::Mimalloc
        } else if self.jemalloc {
            Malloc::Jemalloc
        } else if self.enable_profiling {
            Malloc::Dhat
        } else {
            Malloc::System
        };
        Some(ServerOpt {
            malloc,
            // Profiling requires debug information.
            dev_rel: self.dev_rel || self.enable_profiling,
            pgo: self.pgo.clone(),
            force_always_assert: self.force_always_assert,
        })
    }
    pub(crate) fn proc_macro_server(&self) -> Option<ProcMacroServerOpt> {
        if !self.proc_macro_server {
            return None;
        }
        Some(ProcMacroServerOpt { dev_rel: self.dev_rel })
    }
    pub(crate) fn client(&self) -> Option<ClientOpt> {
        if (self.server || self.proc_macro_server) && !self.client {
            return None;
        }
        Some(ClientOpt { code_bin: self.code_bin.clone() })
    }
}

impl Dist {
    pub(crate) fn allocator(&self) -> Malloc {
        if self.mimalloc {
            Malloc::Mimalloc
        } else if self.jemalloc {
            Malloc::Jemalloc
        } else if self.enable_profiling {
            Malloc::Dhat
        } else {
            Malloc::System
        }
    }
}