Skip to content

Commit

Permalink
compile proc-macro crates and fill dylibs for ra
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <[email protected]>
  • Loading branch information
onur-ozkan committed Feb 5, 2024
1 parent 8bbdb07 commit 9219a80
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 23 deletions.
11 changes: 5 additions & 6 deletions src/bootstrap/src/core/build_steps/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,16 +648,15 @@ impl Step for RustProjectJson {
}
}
fn run(self, builder: &Builder<'_>) -> Self::Output {
let config = &builder.config;
if config.dry_run() {
if builder.config.dry_run() {
return;
}

while !t!(create_ra_project_json_maybe(&config)) {}
while !t!(create_ra_project_json_maybe(builder)) {}
}
}

fn create_ra_project_json_maybe(config: &Config) -> io::Result<bool> {
fn create_ra_project_json_maybe(builder: &Builder<'_>) -> io::Result<bool> {
println!("\nx.py can automatically generate `rust-project.json` file for rust-analyzer");

let should_create = match prompt_user("Would you like to create rust-project.json?: [y/N]")? {
Expand All @@ -669,8 +668,8 @@ fn create_ra_project_json_maybe(config: &Config) -> io::Result<bool> {
};

if should_create {
let ra_project = RustAnalyzerProject::collect_ra_project_data(&config);
ra_project.generate_file(&config.src.join("rust-project.json"))?;
let ra_project = RustAnalyzerProject::collect_ra_project_data(builder);
ra_project.generate_file(&builder.config.src.join("rust-project.json"))?;
println!("Created `rust-project.json`");
}

Expand Down
14 changes: 13 additions & 1 deletion src/bootstrap/src/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2274,7 +2274,7 @@ impl<'a> Builder<'a> {
///
/// `-Z crate-attr` flags will be applied recursively on the target code using the `rustc_parse::parser::Parser`.
/// See `rustc_builtin_macros::cmdline_attrs::inject` for more information.
#[derive(Debug, Clone)]
#[derive(Debug, Default, Clone)]
struct Rustflags(String, TargetSelection);

impl Rustflags {
Expand Down Expand Up @@ -2429,3 +2429,15 @@ impl From<Cargo> for Command {
cargo.command
}
}

impl From<Command> for Cargo {
fn from(command: Command) -> Cargo {
Cargo {
command,
rustflags: Default::default(),
rustdocflags: Default::default(),
hostflags: Default::default(),
allow_features: Default::default(),
}
}
}
66 changes: 50 additions & 16 deletions src/bootstrap/src/utils/ra_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ use serde_derive::Serialize;
use std::collections::{BTreeMap, BTreeSet};
use std::io;
use std::path::Path;
use std::process::Command;

use crate::core::build_steps::compile::{stream_cargo, CargoMessage};
use crate::core::builder::Builder;
use crate::core::metadata::{project_metadata, workspace_members, Dependency};
use crate::Config;

#[derive(Debug, Serialize)]
/// FIXME(before-merge): doc-comment
Expand Down Expand Up @@ -47,8 +49,9 @@ struct Dep {
}

impl RustAnalyzerProject {
#[allow(dead_code)] // FIXME(before-merge): remove this
pub(crate) fn collect_ra_project_data(config: &Config) -> Self {
pub(crate) fn collect_ra_project_data(builder: &Builder<'_>) -> Self {
let config = &builder.config;

let mut ra_project = RustAnalyzerProject {
crates: vec![],
sysroot: format!("{}", config.out.join("host").join("stage0").display()),
Expand Down Expand Up @@ -86,11 +89,6 @@ impl RustAnalyzerProject {
krate.is_workspace_member = workspace_members.iter().any(|p| p.name == target.name);
krate.is_proc_macro = target.crate_types.contains(&"proc-macro".to_string());

// FIXME(before-merge): We need to figure out how to find proc-macro dylibs.
// if krate.is_proc_macro {
// krate.proc_macro_dylib_path =
// }

krate.env.insert("RUSTC_BOOTSTRAP".into(), "1".into());

if target
Expand All @@ -109,14 +107,51 @@ impl RustAnalyzerProject {

// Find and fill dependencies of crates.
for package in packages {
if package.dependencies.is_empty() {
continue;
}
if let Some(index) =
ra_project.crates.iter().position(|c| c.display_name == package.name)
{
assert!(
!package.manifest_path.is_empty(),
"manifest_path must be valid for proc-macro crates."
);

let mut cargo = Command::new(&builder.initial_cargo);
cargo
.env("RUSTC_BOOTSTRAP", "1")
.arg("build")
.arg("--manifest-path")
.arg(package.manifest_path);

if ra_project.crates[index].is_proc_macro {
// FIXME(before-merge): use `CARGO_TARGET_DIR` to place shared libraries in the build output directory.
let ok = stream_cargo(builder, cargo.into(), vec![], &mut |msg| {
let filenames = match msg {
CargoMessage::CompilerArtifact { filenames, .. } => filenames,
_ => return,
};

for filename in filenames {
let snake_case_name = ra_project.crates[index]
.display_name
.replace('-', "_")
.to_lowercase();

if filename.ends_with(".so")
&& (filename.contains(&format!(
"lib{}",
ra_project.crates[index].display_name
)) || filename.contains(&format!("lib{}", snake_case_name)))
{
ra_project.crates[index].proc_macro_dylib_path =
Some(filename.to_string());
}
}
});

for dependency in package.dependencies {
if let Some(index) =
ra_project.crates.iter().position(|c| c.display_name == package.name)
{
assert!(ok);
}

for dependency in package.dependencies {
if let Some(dependency_index) =
ra_project.crates.iter().position(|c| c.display_name == dependency.name)
{
Expand All @@ -138,7 +173,6 @@ impl RustAnalyzerProject {
ra_project
}

#[allow(dead_code)] // FIXME(before-merge): remove this
pub(crate) fn generate_file(&self, path: &Path) -> io::Result<()> {
if path.exists() {
return Err(io::Error::new(
Expand Down

0 comments on commit 9219a80

Please sign in to comment.