Skip to content

Commit 784ce4a

Browse files
committed
Auto merge of rust-lang#130446 - durin42:llvm-20-fix-CommandLineArgs, r=<try>
rustc_llvm: adapt to flattened CLI args in LLVM This changed in llvm/llvm-project@e190d07. I decided to stick with more duplication between the ifdef blocks to make the code easier to read for the next two years before we can plausibly drop LLVM 19. `@rustbot` label: +llvm-main try-job: x86_64-msvc
2 parents e2dc1a1 + 86d67b7 commit 784ce4a

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,22 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
498498
Options.EmitStackSizeSection = EmitStackSizeSection;
499499

500500
if (ArgsCstrBuff != nullptr) {
501+
#if LLVM_VERSION_GE(20, 0)
502+
int buffer_offset = 0;
503+
assert(ArgsCstrBuff[ArgsCstrBuffLen - 1] == '\0');
504+
auto Arg0 = std::string(ArgsCstrBuff);
505+
buffer_offset = Arg0.size() + 1;
506+
auto ArgsCppStr =
507+
std::string(ArgsCstrBuff + buffer_offset, ArgsCstrBuffLen - 1);
508+
auto i = 0;
509+
while (i != std::string::npos) {
510+
i = ArgsCppStr.find('\0', i + 1);
511+
if (i != std::string::npos)
512+
ArgsCppStr.replace(i, i + 1, " ");
513+
}
514+
Options.MCOptions.Argv0 = Arg0;
515+
Options.MCOptions.CommandlineArgs = ArgsCppStr;
516+
#else
501517
int buffer_offset = 0;
502518
assert(ArgsCstrBuff[ArgsCstrBuffLen - 1] == '\0');
503519

@@ -523,6 +539,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
523539
Options.MCOptions.Argv0 = arg0;
524540
Options.MCOptions.CommandLineArgs =
525541
llvm::ArrayRef<std::string>(cmd_arg_strings, num_cmd_arg_strings);
542+
#endif
526543
}
527544

528545
TargetMachine *TM = TheTarget->createTargetMachine(
@@ -531,10 +548,11 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
531548
}
532549

533550
extern "C" void LLVMRustDisposeTargetMachine(LLVMTargetMachineRef TM) {
534-
551+
#if LLVM_VERSION_LT(20, 0)
535552
MCTargetOptions &MCOptions = unwrap(TM)->Options.MCOptions;
536553
delete[] MCOptions.Argv0;
537554
delete[] MCOptions.CommandLineArgs.data();
555+
#endif
538556

539557
delete unwrap(TM);
540558
}

0 commit comments

Comments
 (0)