Auto merge of #133540 - ehuss:compiletest-proc-macro, r=jieyouxu · rust-lang/rust@a2545fd (original) (raw)

`@@ -102,7 +102,7 @@ fn get_lib_name(name: &str, aux_type: AuxType) -> Option {

`

102

102

`// In this case, the only path we can pass

`

103

103

`// with '--extern-meta' is the '.rlib' file

`

104

104

`AuxType::Lib => Some(format!("lib{name}.rlib")),

`

105

``

`-

AuxType::Dylib => Some(dylib_name(name)),

`

``

105

`+

AuxType::Dylib | AuxType::ProcMacro => Some(dylib_name(name)),

`

106

106

`}

`

107

107

`}

`

108

108

``

`@@ -1097,7 +1097,9 @@ impl<'test> TestCx<'test> {

`

1097

1097

`}

`

1098

1098

``

1099

1099

`fn has_aux_dir(&self) -> bool {

`

1100

``

`-

!self.props.aux.builds.is_empty() || !self.props.aux.crates.is_empty()

`

``

1100

`+

!self.props.aux.builds.is_empty()

`

``

1101

`+

|| !self.props.aux.crates.is_empty()

`

``

1102

`+

|| !self.props.aux.proc_macros.is_empty()

`

1101

1103

`}

`

1102

1104

``

1103

1105

`fn aux_output_dir(&self) -> PathBuf {

`

`@@ -1118,31 +1120,48 @@ impl<'test> TestCx<'test> {

`

1118

1120

``

1119

1121

`fn build_all_auxiliary(&self, of: &TestPaths, aux_dir: &Path, rustc: &mut Command) {

`

1120

1122

`for rel_ab in &self.props.aux.builds {

`

1121

``

`-

self.build_auxiliary(of, rel_ab, &aux_dir, false /* is_bin */);

`

``

1123

`+

self.build_auxiliary(of, rel_ab, &aux_dir, None);

`

1122

1124

`}

`

1123

1125

``

1124

1126

`for rel_ab in &self.props.aux.bins {

`

1125

``

`-

self.build_auxiliary(of, rel_ab, &aux_dir, true /* is_bin */);

`

``

1127

`+

self.build_auxiliary(of, rel_ab, &aux_dir, Some(AuxType::Bin));

`

1126

1128

`}

`

1127

1129

``

``

1130

`+

let path_to_crate_name = |path: &str| -> String {

`

``

1131

`+

path.rsplit_once('/')

`

``

1132

`+

.map_or(path, |(_, tail)| tail)

`

``

1133

`+

.trim_end_matches(".rs")

`

``

1134

`+

.replace('-', "_")

`

``

1135

`+

};

`

``

1136

+

``

1137

`+

let add_extern =

`

``

1138

`+

|rustc: &mut Command, aux_name: &str, aux_path: &str, aux_type: AuxType| {

`

``

1139

`+

let lib_name = get_lib_name(&path_to_crate_name(aux_path), aux_type);

`

``

1140

`+

if let Some(lib_name) = lib_name {

`

``

1141

`+

rustc.arg("--extern").arg(format!(

`

``

1142

`+

"{}={}/{}",

`

``

1143

`+

aux_name,

`

``

1144

`+

aux_dir.display(),

`

``

1145

`+

lib_name

`

``

1146

`+

));

`

``

1147

`+

}

`

``

1148

`+

};

`

``

1149

+

1128

1150

`for (aux_name, aux_path) in &self.props.aux.crates {

`

1129

``

`-

let aux_type = self.build_auxiliary(of, &aux_path, &aux_dir, false /* is_bin */);

`

1130

``

`-

let lib_name =

`

1131

``

`-

get_lib_name(&aux_path.trim_end_matches(".rs").replace('-', "_"), aux_type);

`

1132

``

`-

if let Some(lib_name) = lib_name {

`

1133

``

`-

rustc.arg("--extern").arg(format!(

`

1134

``

`-

"{}={}/{}",

`

1135

``

`-

aux_name,

`

1136

``

`-

aux_dir.display(),

`

1137

``

`-

lib_name

`

1138

``

`-

));

`

1139

``

`-

}

`

``

1151

`+

let aux_type = self.build_auxiliary(of, &aux_path, &aux_dir, None);

`

``

1152

`+

add_extern(rustc, aux_name, aux_path, aux_type);

`

``

1153

`+

}

`

``

1154

+

``

1155

`+

for proc_macro in &self.props.aux.proc_macros {

`

``

1156

`+

self.build_auxiliary(of, proc_macro, &aux_dir, Some(AuxType::ProcMacro));

`

``

1157

`+

let crate_name = path_to_crate_name(proc_macro);

`

``

1158

`+

add_extern(rustc, &crate_name, proc_macro, AuxType::ProcMacro);

`

1140

1159

`}

`

1141

1160

``

1142

1161

`` // Build any //@ aux-codegen-backend, and pass the resulting library

``

1143

1162

`` // to -Zcodegen-backend when compiling the test file.

``

1144

1163

`if let Some(aux_file) = &self.props.aux.codegen_backend {

`

1145

``

`-

let aux_type = self.build_auxiliary(of, aux_file, aux_dir, false);

`

``

1164

`+

let aux_type = self.build_auxiliary(of, aux_file, aux_dir, None);

`

1146

1165

`if let Some(lib_name) = get_lib_name(aux_file.trim_end_matches(".rs"), aux_type) {

`

1147

1166

`let lib_path = aux_dir.join(&lib_name);

`

1148

1167

` rustc.arg(format!("-Zcodegen-backend={}", lib_path.display()));

`

`@@ -1209,17 +1228,23 @@ impl<'test> TestCx<'test> {

`

1209

1228

`}

`

1210

1229

``

1211

1230

`/// Builds an aux dependency.

`

``

1231

`+

///

`

``

1232

`` +

/// If aux_type is None, then this will determine the aux-type automatically.

``

1212

1233

`fn build_auxiliary(

`

1213

1234

`&self,

`

1214

1235

`of: &TestPaths,

`

1215

1236

`source_path: &str,

`

1216

1237

`aux_dir: &Path,

`

1217

``

`-

is_bin: bool,

`

``

1238

`+

aux_type: Option,

`

1218

1239

`) -> AuxType {

`

1219

1240

`let aux_testpaths = self.compute_aux_test_paths(of, source_path);

`

1220

``

`-

let aux_props = self.props.from_aux_file(&aux_testpaths.file, self.revision, self.config);

`

``

1241

`+

let mut aux_props =

`

``

1242

`+

self.props.from_aux_file(&aux_testpaths.file, self.revision, self.config);

`

``

1243

`+

if aux_type == Some(AuxType::ProcMacro) {

`

``

1244

`+

aux_props.force_host = true;

`

``

1245

`+

}

`

1221

1246

`let mut aux_dir = aux_dir.to_path_buf();

`

1222

``

`-

if is_bin {

`

``

1247

`+

if aux_type == Some(AuxType::Bin) {

`

1223

1248

`` // On unix, the binary of auxiliary/foo.rs will be named

``

1224

1249

`` // auxiliary/foo which clashes with the dir auxiliary/foo, so

``

1225

1250

`` // put bins in a bin subfolder.

``

`@@ -1250,8 +1275,12 @@ impl<'test> TestCx<'test> {

`

1250

1275

` aux_rustc.env_remove(key);

`

1251

1276

`}

`

1252

1277

``

1253

``

`-

let (aux_type, crate_type) = if is_bin {

`

``

1278

`+

let (aux_type, crate_type) = if aux_type == Some(AuxType::Bin) {

`

1254

1279

`(AuxType::Bin, Some("bin"))

`

``

1280

`+

} else if aux_type == Some(AuxType::ProcMacro) {

`

``

1281

`+

(AuxType::ProcMacro, Some("proc-macro"))

`

``

1282

`+

} else if aux_type.is_some() {

`

``

1283

`+

panic!("aux_type {aux_type:?} not expected");

`

1255

1284

`} else if aux_props.no_prefer_dynamic {

`

1256

1285

`(AuxType::Dylib, None)

`

1257

1286

`} else if self.config.target.contains("emscripten")

`

`@@ -1287,6 +1316,11 @@ impl<'test> TestCx<'test> {

`

1287

1316

` aux_rustc.args(&["--crate-type", crate_type]);

`

1288

1317

`}

`

1289

1318

``

``

1319

`+

if aux_type == AuxType::ProcMacro {

`

``

1320

`+

// For convenience, but this only works on 2018.

`

``

1321

`+

aux_rustc.args(&["--extern", "proc_macro"]);

`

``

1322

`+

}

`

``

1323

+

1290

1324

` aux_rustc.arg("-L").arg(&aux_dir);

`

1291

1325

``

1292

1326

`let auxres = aux_cx.compose_and_run(

`

`@@ -2768,8 +2802,10 @@ enum LinkToAux {

`

2768

2802

`No,

`

2769

2803

`}

`

2770

2804

``

``

2805

`+

#[derive(Debug, PartialEq)]

`

2771

2806

`enum AuxType {

`

2772

2807

`Bin,

`

2773

2808

`Lib,

`

2774

2809

`Dylib,

`

``

2810

`+

ProcMacro,

`

2775

2811

`}

`