@@ -302,7 +302,7 @@ int cx::buildModule(Module& mainModule, BuildParams buildParams) {
302302 if (!remainingPrintOpts) return 0 ;
303303 }
304304
305- llvm::SmallString<128 > temporaryOutputFilePath ;
305+ llvm::SmallString<128 > tempIntermediateFilePath ;
306306 const char * outputFileExtension;
307307 // Prefer external C compiler for better system compatibility, fallback to embedded Clang.
308308 std::string ccPath = findExternalCCompiler ().value_or (buildParams.argv0 );
@@ -328,7 +328,7 @@ int cx::buildModule(Module& mainModule, BuildParams buildParams) {
328328 // TODO: emitAssembly not supported, report to user
329329 outputFileExtension = " c" ;
330330 int fileDescriptor;
331- if (auto error = llvm::sys::fs::createTemporaryFile (" cx" , outputFileExtension, fileDescriptor, temporaryOutputFilePath )) {
331+ if (auto error = llvm::sys::fs::createTemporaryFile (" cx" , outputFileExtension, fileDescriptor, tempIntermediateFilePath )) {
332332 ABORT (error.message ());
333333 }
334334
@@ -365,19 +365,14 @@ int cx::buildModule(Module& mainModule, BuildParams buildParams) {
365365 return 0 ;
366366 }
367367
368- if (emitAssembly) {
369- outputFileExtension = " s" ;
370- } else {
371- outputFileExtension = isWindows ? " obj" : " o" ;
372- }
373-
374- if (auto error = llvm::sys::fs::createTemporaryFile (" cx" , outputFileExtension, temporaryOutputFilePath)) {
368+ outputFileExtension = emitAssembly ? " s" : isWindows ? " obj" : " o" ;
369+ if (auto error = llvm::sys::fs::createTemporaryFile (" cx" , outputFileExtension, tempIntermediateFilePath)) {
375370 ABORT (error.message ());
376371 }
377372
378373 auto fileType = emitAssembly ? llvm::CodeGenFileType::AssemblyFile : llvm::CodeGenFileType::ObjectFile;
379374 auto relocModel = noPIE ? llvm::Reloc::Model::Static : llvm::Reloc::Model::PIC_;
380- emitLLVMModuleToMachineCode (linkedModule, temporaryOutputFilePath , fileType, relocModel);
375+ emitLLVMModuleToMachineCode (linkedModule, tempIntermediateFilePath , fileType, relocModel);
381376 break ;
382377 }
383378
@@ -393,20 +388,23 @@ int cx::buildModule(Module& mainModule, BuildParams buildParams) {
393388 if (compileOnly || emitAssembly) {
394389 llvm::SmallString<128 > outputFilePath = buildParams.outputDirectory ;
395390 llvm::sys::path::append (outputFilePath, llvm::Twine (" output." ) + outputFileExtension);
396- renameFile (temporaryOutputFilePath , outputFilePath);
391+ renameFile (tempIntermediateFilePath , outputFilePath);
397392 return 0 ;
398393 }
399394
400395 // Link the output:
401396
402- llvm::SmallString<128 > temporaryExecutablePath;
403- llvm::sys::fs::createUniquePath (" cx-%%%%%%%%" , temporaryExecutablePath, true );
397+ llvm::SmallString<128 > tempOutputFilePath;
398+ llvm::SmallString<128 > tempFileNamePattern (" cx-%%%%%%%%" );
399+ if (isMSVC) { // MSVC will append .exe to the output file anyway, so match that.
400+ tempFileNamePattern += (buildParams.createSharedLib ? " .dll" : " .exe" );
401+ }
402+ llvm::sys::fs::createUniquePath (tempFileNamePattern, tempOutputFilePath, true );
404403
405404 std::vector<const char *> ccArgs = {
406405 ccPath.c_str (),
407- temporaryOutputFilePath .c_str (),
406+ tempIntermediateFilePath .c_str (),
408407 };
409-
410408 if (buildParams.createSharedLib ) {
411409 ccArgs.push_back (isMSVC ? " -LD" : " -shared" );
412410 if (!isMSVC) {
@@ -415,7 +413,7 @@ int cx::buildModule(Module& mainModule, BuildParams buildParams) {
415413 }
416414 }
417415 ccArgs.push_back (isMSVC ? " -Fe:" : " -o" );
418- ccArgs.push_back (temporaryExecutablePath .c_str ());
416+ ccArgs.push_back (tempOutputFilePath .c_str ());
419417
420418 if (backend == Backend::C) {
421419 // TODO: remove these and fix errors
@@ -457,18 +455,18 @@ int cx::buildModule(Module& mainModule, BuildParams buildParams) {
457455
458456 std::vector<llvm::StringRef> ccArgStringRefs (ccArgs.begin (), ccArgs.end ());
459457 int ccExitStatus = useExternalCCompiler ? llvm::sys::ExecuteAndWait (ccArgs[0 ], ccArgStringRefs) : invokeClang (ccArgs);
460- llvm::sys::fs::remove (temporaryOutputFilePath );
458+ llvm::sys::fs::remove (tempIntermediateFilePath );
461459 if (ccExitStatus != 0 ) return ccExitStatus;
462460
463461 if (run) {
464- std::string command = (temporaryExecutablePath + " 2>&1" ).str ();
462+ std::string command = (tempOutputFilePath + " 2>&1" ).str ();
465463 std::string output;
466464 int executableExitStatus = exec (command.c_str (), output);
467465 llvm::outs () << output;
468- llvm::sys::fs::remove (temporaryExecutablePath );
466+ llvm::sys::fs::remove (tempOutputFilePath );
469467
470468 if (isMSVC) {
471- auto path = temporaryExecutablePath ;
469+ auto path = tempOutputFilePath ;
472470 llvm::sys::path::replace_extension (path, " ilk" );
473471 llvm::sys::fs::remove (path);
474472 llvm::sys::path::replace_extension (path, " pdb" );
@@ -498,10 +496,10 @@ int cx::buildModule(Module& mainModule, BuildParams buildParams) {
498496 }
499497 }
500498
501- renameFile (temporaryExecutablePath , outputPathPrefix + buildParams.outputFileName );
499+ renameFile (tempOutputFilePath , outputPathPrefix + buildParams.outputFileName );
502500
503501 if (isMSVC) {
504- auto path = temporaryExecutablePath ;
502+ auto path = tempOutputFilePath ;
505503 auto outputPath = outputPathPrefix;
506504 outputPath += buildParams.outputFileName ;
507505
0 commit comments