@@ -406,7 +406,8 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
406406    bool  RelaxELFRelocations,
407407    bool  UseInitArray,
408408    const  char  *SplitDwarfFile,
409-     bool  ForceEmulatedTls) {
409+     bool  ForceEmulatedTls,
410+     const  char  *ArgsCstrBuff, size_t  ArgsCstrBuffLen) {
410411
411412  auto  OptLevel = fromRust (RustOptLevel);
412413  auto  RM = fromRust (RustReloc);
@@ -462,12 +463,48 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
462463
463464  Options.EmitStackSizeSection  = EmitStackSizeSection;
464465
466+ 
467+   if  (ArgsCstrBuff != nullptr )
468+   {
469+     int  buffer_offset = 0 ;
470+     assert (ArgsCstrBuff[ArgsCstrBuffLen - 1 ] == ' \0 ' 
471+ 
472+     const  size_t  arg0_len = std::strlen (ArgsCstrBuff);
473+     char * arg0 = new  char [arg0_len + 1 ];
474+     memcpy (arg0, ArgsCstrBuff, arg0_len);
475+     arg0[arg0_len] = ' \0 ' 
476+     buffer_offset += arg0_len + 1 ;
477+ 
478+     const  int  num_cmd_arg_strings =
479+       std::count (&ArgsCstrBuff[buffer_offset], &ArgsCstrBuff[ArgsCstrBuffLen], ' \0 ' 
480+ 
481+     std::string* cmd_arg_strings = new  std::string[num_cmd_arg_strings];
482+     for  (int  i = 0 ; i < num_cmd_arg_strings; ++i)
483+     {
484+       assert (buffer_offset < ArgsCstrBuffLen);
485+       const  int  len = std::strlen (ArgsCstrBuff + buffer_offset);
486+       cmd_arg_strings[i] = std::string (&ArgsCstrBuff[buffer_offset], len);
487+       buffer_offset += len + 1 ;
488+     }
489+ 
490+     assert (buffer_offset == ArgsCstrBuffLen);
491+ 
492+     Options.MCOptions .Argv0  = arg0;
493+     Options.MCOptions .CommandLineArgs  =
494+       llvm::ArrayRef<std::string>(cmd_arg_strings, num_cmd_arg_strings);
495+   }
496+ 
465497  TargetMachine *TM = TheTarget->createTargetMachine (
466498      Trip.getTriple (), CPU, Feature, Options, RM, CM, OptLevel);
467499  return  wrap (TM);
468500}
469501
470502extern  " C" void  LLVMRustDisposeTargetMachine (LLVMTargetMachineRef TM) {
503+ 
504+   MCTargetOptions& MCOptions = unwrap (TM)->Options .MCOptions ;
505+   delete[]  MCOptions.Argv0 ;
506+   delete[]  MCOptions.CommandLineArgs .data ();
507+ 
471508  delete  unwrap (TM);
472509}
473510
0 commit comments