|
1 | 1 | #include <stdio.h> |
2 | 2 |
|
| 3 | +#include <iomanip> |
3 | 4 | #include <vector> |
4 | 5 | #include <set> |
5 | 6 |
|
@@ -306,44 +307,55 @@ static size_t getLongestEntryLength(ArrayRef<KV> Table) { |
306 | 307 | return MaxLen; |
307 | 308 | } |
308 | 309 |
|
309 | | -extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM, const char* TargetCPU) { |
| 310 | +using PrintBackendInfo = void(void*, const char* Data, size_t Len); |
| 311 | + |
| 312 | +extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM, |
| 313 | + const char* TargetCPU, |
| 314 | + PrintBackendInfo Print, |
| 315 | + void* Out) { |
310 | 316 | const TargetMachine *Target = unwrap(TM); |
311 | 317 | const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo(); |
312 | 318 | const Triple::ArchType HostArch = Triple(sys::getDefaultTargetTriple()).getArch(); |
313 | 319 | const Triple::ArchType TargetArch = Target->getTargetTriple().getArch(); |
314 | 320 |
|
| 321 | + std::ostringstream Buf; |
| 322 | + |
315 | 323 | #if LLVM_VERSION_GE(17, 0) |
316 | 324 | const ArrayRef<SubtargetSubTypeKV> CPUTable = MCInfo->getAllProcessorDescriptions(); |
317 | 325 | #elif defined(LLVM_RUSTLLVM) |
318 | 326 | const ArrayRef<SubtargetSubTypeKV> CPUTable = MCInfo->getCPUTable(); |
319 | 327 | #else |
320 | | - printf("Full target CPU help is not supported by this LLVM version.\n\n"); |
| 328 | + Buf << "Full target CPU help is not supported by this LLVM version.\n\n"; |
321 | 329 | SubtargetSubTypeKV TargetCPUKV = { TargetCPU, {{}}, {{}} }; |
322 | 330 | const ArrayRef<SubtargetSubTypeKV> CPUTable = TargetCPUKV; |
323 | 331 | #endif |
324 | 332 | unsigned MaxCPULen = getLongestEntryLength(CPUTable); |
325 | 333 |
|
326 | | - printf("Available CPUs for this target:\n"); |
| 334 | + Buf << "Available CPUs for this target:\n"; |
327 | 335 | // Don't print the "native" entry when the user specifies --target with a |
328 | 336 | // different arch since that could be wrong or misleading. |
329 | 337 | if (HostArch == TargetArch) { |
330 | 338 | MaxCPULen = std::max(MaxCPULen, (unsigned) std::strlen("native")); |
331 | 339 | const StringRef HostCPU = sys::getHostCPUName(); |
332 | | - printf(" %-*s - Select the CPU of the current host (currently %.*s).\n", |
333 | | - MaxCPULen, "native", (int)HostCPU.size(), HostCPU.data()); |
| 340 | + Buf << " " << std::left << std::setw(MaxCPULen) << "native" |
| 341 | + << " - Select the CPU of the current host " |
| 342 | + "(currently " << HostCPU.str() << ").\n"; |
334 | 343 | } |
335 | 344 | for (auto &CPU : CPUTable) { |
336 | 345 | // Compare cpu against current target to label the default |
337 | 346 | if (strcmp(CPU.Key, TargetCPU) == 0) { |
338 | | - printf(" %-*s - This is the default target CPU" |
339 | | - " for the current build target (currently %s).", |
340 | | - MaxCPULen, CPU.Key, Target->getTargetTriple().str().c_str()); |
| 347 | + Buf << " " << std::left << std::setw(MaxCPULen) << CPU.Key |
| 348 | + << " - This is the default target CPU for the current build target " |
| 349 | + "(currently " << Target->getTargetTriple().str() << ")."; |
341 | 350 | } |
342 | 351 | else { |
343 | | - printf(" %-*s", MaxCPULen, CPU.Key); |
| 352 | + Buf << " " << CPU.Key; |
344 | 353 | } |
345 | | - printf("\n"); |
| 354 | + Buf << "\n"; |
346 | 355 | } |
| 356 | + |
| 357 | + const auto &BufString = Buf.str(); |
| 358 | + Print(Out, BufString.data(), BufString.size()); |
347 | 359 | } |
348 | 360 |
|
349 | 361 | extern "C" size_t LLVMRustGetTargetFeaturesCount(LLVMTargetMachineRef TM) { |
|
0 commit comments