Skip to content

Commit ec575b4

Browse files
committed
adapt to llvm debuginfo changes
1 parent f7b5e54 commit ec575b4

File tree

4 files changed

+50
-20
lines changed

4 files changed

+50
-20
lines changed

compiler/rustc_codegen_llvm/src/back/owned_target_machine.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ impl OwnedTargetMachine {
4040
debug_info_compression: &CStr,
4141
use_emulated_tls: bool,
4242
args_cstr_buff: &[u8],
43+
compiler_path: &CStr,
44+
commandline_args: &CStr,
4345
) -> Result<Self, LlvmError<'static>> {
4446
assert!(args_cstr_buff.len() > 0);
4547
assert!(
@@ -73,6 +75,8 @@ impl OwnedTargetMachine {
7375
use_emulated_tls,
7476
args_cstr_buff.as_ptr() as *const c_char,
7577
args_cstr_buff.len(),
78+
compiler_path.as_ptr(),
79+
commandline_args.as_ptr(),
7680
)
7781
};
7882

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,29 @@ fn to_llvm_relocation_model(relocation_model: RelocModel) -> llvm::RelocModel {
167167
}
168168
}
169169

170+
fn flatten_commandline_args(args: &[String]) -> CString {
171+
let mut result: String = Default::default();
172+
173+
let mut did_print_arg = false;
174+
175+
for arg in args {
176+
if arg.ends_with(".rs") {
177+
continue;
178+
}
179+
180+
if did_print_arg {
181+
result.push(' ');
182+
}
183+
184+
result.push('"');
185+
result.push_str(arg);
186+
result.push('"');
187+
did_print_arg = true;
188+
}
189+
190+
CString::new(result).unwrap()
191+
}
192+
170193
pub(crate) fn to_llvm_code_model(code_model: Option<CodeModel>) -> llvm::CodeModel {
171194
match code_model {
172195
Some(CodeModel::Tiny) => llvm::CodeModel::Tiny,
@@ -247,6 +270,16 @@ pub(crate) fn target_machine_factory(
247270
args_cstr_buff
248271
};
249272

273+
let commandline_args = flatten_commandline_args(&sess.expanded_args);
274+
let compiler_path = CString::new(
275+
std::env::current_exe()
276+
.unwrap_or_default()
277+
.into_os_string()
278+
.into_string()
279+
.unwrap_or_default(),
280+
)
281+
.unwrap();
282+
250283
let debuginfo_compression = sess.opts.debuginfo_compression.to_string();
251284
match sess.opts.debuginfo_compression {
252285
rustc_session::config::DebugInfoCompression::Zlib => {
@@ -302,6 +335,8 @@ pub(crate) fn target_machine_factory(
302335
&debuginfo_compression,
303336
use_emulated_tls,
304337
&args_cstr_buff,
338+
&compiler_path,
339+
&commandline_args,
305340
)
306341
})
307342
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,6 +2176,8 @@ unsafe extern "C" {
21762176
UseEmulatedTls: bool,
21772177
ArgsCstrBuff: *const c_char,
21782178
ArgsCstrBuffLen: usize,
2179+
CompilerPath: *const c_char,
2180+
CommandlineArgs: *const c_char,
21792181
) -> *mut TargetMachine;
21802182

21812183
pub fn LLVMRustDisposeTargetMachine(T: *mut TargetMachine);

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,8 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
398398
bool EmitStackSizeSection, bool RelaxELFRelocations, bool UseInitArray,
399399
const char *SplitDwarfFile, const char *OutputObjFile,
400400
const char *DebugInfoCompression, bool UseEmulatedTls,
401-
const char *ArgsCstrBuff, size_t ArgsCstrBuffLen) {
401+
const char *ArgsCstrBuff, size_t ArgsCstrBuffLen,
402+
const char *CompilerPath, const char *CommandlineArgs) {
402403

403404
auto OptLevel = fromRust(RustOptLevel);
404405
auto RM = fromRust(RustReloc);
@@ -484,23 +485,8 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
484485

485486
Options.EmitStackSizeSection = EmitStackSizeSection;
486487

488+
#if LLVM_VERSION_LT(20, 0)
487489
if (ArgsCstrBuff != nullptr) {
488-
#if LLVM_VERSION_GE(20, 0)
489-
int buffer_offset = 0;
490-
assert(ArgsCstrBuff[ArgsCstrBuffLen - 1] == '\0');
491-
auto Arg0 = std::string(ArgsCstrBuff);
492-
buffer_offset = Arg0.size() + 1;
493-
auto ArgsCppStr = std::string(ArgsCstrBuff + buffer_offset,
494-
ArgsCstrBuffLen - buffer_offset);
495-
auto i = 0;
496-
while (i != std::string::npos) {
497-
i = ArgsCppStr.find('\0', i + 1);
498-
if (i != std::string::npos)
499-
ArgsCppStr.replace(i, 1, " ");
500-
}
501-
Options.MCOptions.Argv0 = Arg0;
502-
Options.MCOptions.CommandlineArgs = ArgsCppStr;
503-
#else
504490
int buffer_offset = 0;
505491
assert(ArgsCstrBuff[ArgsCstrBuffLen - 1] == '\0');
506492

@@ -525,9 +511,13 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
525511

526512
Options.MCOptions.Argv0 = arg0;
527513
Options.MCOptions.CommandLineArgs =
528-
llvm::ArrayRef<std::string>(cmd_arg_strings, num_cmd_arg_strings);
529-
#endif
514+
llvm::ArrayRef<std::string>(cmd_arg_strings, num_cmd_arg_strings);
530515
}
516+
#else // LLVM_VERSION_GE(20, 0)
517+
Options.MCOptions.CommandlineArgs = CommandlineArgs != nullptr ? CommandlineArgs : "";
518+
Options.MCOptions.Argv0 = CompilerPath != nullptr ? CompilerPath : "";
519+
#endif
520+
531521

532522
TargetMachine *TM = TheTarget->createTargetMachine(
533523
Trip.getTriple(), CPU, Feature, Options, RM, CM, OptLevel);
@@ -540,7 +530,6 @@ extern "C" void LLVMRustDisposeTargetMachine(LLVMTargetMachineRef TM) {
540530
delete[] MCOptions.Argv0;
541531
delete[] MCOptions.CommandLineArgs.data();
542532
#endif
543-
544533
delete unwrap(TM);
545534
}
546535

0 commit comments

Comments
 (0)