diff --git a/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/ModuleSupport.kt b/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/ModuleSupport.kt index b603ae69c2e..a9b7a03501f 100644 --- a/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/ModuleSupport.kt +++ b/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/ModuleSupport.kt @@ -2,6 +2,7 @@ package org.jetbrains.kotlin.native.interop.indexer import clang.* import kotlinx.cinterop.* +import java.nio.file.Files data class ModulesInfo(val topLevelHeaders: List, val ownHeaders: Set) @@ -39,13 +40,13 @@ internal open class ModularCompilation(compilation: Compilation): Compilation by } private val moduleCacheDirectory = if (compilation.compilerArgs.none { it.startsWith(moduleCacheFlag) }) { - createTempDir("ModuleCache") + Files.createTempDirectory("ModuleCache").toAbsolutePath().toFile() } else { null } override val compilerArgs: List = compilation.compilerArgs + - listOfNotNull("-fmodules", moduleCacheDirectory?.let { "$moduleCacheFlag${it.absolutePath}" }) + listOfNotNull("-fmodules", moduleCacheDirectory?.let { "$moduleCacheFlag${it}" }) override fun dispose() { moduleCacheDirectory?.deleteRecursively() diff --git a/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt b/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt index 371bc3c6b63..5ea12ca88d4 100644 --- a/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt +++ b/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt @@ -100,7 +100,7 @@ internal fun parseTranslationUnit( ) if (errorCode != CXErrorCode.CXError_Success) { - val copiedSourceFile = sourceFile.copyTo(createTempFile(suffix = sourceFile.name), overwrite = true) + val copiedSourceFile = sourceFile.copyTo(Files.createTempFile(null, sourceFile.name).toFile(), overwrite = true) error(""" clang_parseTranslationUnit2 failed with $errorCode; @@ -241,7 +241,7 @@ internal fun Appendable.appendPreamble(compilation: Compilation) = this.apply { * Creates temporary source file which includes the library. */ internal fun Compilation.createTempSource(): File { - val result = createTempFile(suffix = ".${language.sourceFileExtension}") + val result = Files.createTempFile(null, ".${language.sourceFileExtension}").toFile() result.deleteOnExit() result.bufferedWriter().use { writer -> @@ -292,7 +292,7 @@ fun Compilation.precompileHeaders(): CompilationWithPCH = withIndex { index -> } internal fun Compilation.withPrecompiledHeader(translationUnit: CXTranslationUnit): CompilationWithPCH { - val precompiledHeader = createTempFile(suffix = ".pch").apply { this.deleteOnExit() } + val precompiledHeader = Files.createTempFile(null, ".pch").toFile().apply { this.deleteOnExit() } clang_saveTranslationUnit(translationUnit, precompiledHeader.absolutePath, 0) return CompilationWithPCH(this.compilerArgs, precompiledHeader.absolutePath, this.language) @@ -740,12 +740,10 @@ private fun createVfsOverlayFileContents(virtualPathToReal: Map): By fun createVfsOverlayFile(virtualPathToReal: Map): Path { val bytes = createVfsOverlayFileContents(virtualPathToReal) - return createTempFile(prefix = "konan", suffix = ".vfsoverlay").apply { - bufferedWriter().use { - writeBytes(bytes) - } - deleteOnExit() - }.toPath() + return Files.createTempFile("konan", ".vfsoverlay").also { + Files.write(it, bytes) + it.toFile().deleteOnExit() + } } tailrec fun Type.unwrapTypedefs(): Type = if (this is Typedef) { diff --git a/kotlin-native/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmUtils.kt b/kotlin-native/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmUtils.kt index d1943be26b7..f99ac1fb915 100644 --- a/kotlin-native/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmUtils.kt +++ b/kotlin-native/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmUtils.kt @@ -98,7 +98,7 @@ fun loadKonanLibrary(name: String) { try { System.load("$dir/$fullLibraryName") } catch (e: UnsatisfiedLinkError) { - val tempDir = createTempDir(directory = File(dir)).absolutePath + val tempDir = Files.createTempDirectory(Paths.get(dir), null).toAbsolutePath().toString() Files.createLink(Paths.get(tempDir, fullLibraryName), Paths.get(dir, fullLibraryName)) // TODO: Does not work on Windows. May be use FILE_FLAG_DELETE_ON_CLOSE? File(tempDir).deleteOnExit() diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/VerifyModule.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/VerifyModule.kt index b67b3bfd674..9ecc32c4f61 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/VerifyModule.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/VerifyModule.kt @@ -3,6 +3,7 @@ package org.jetbrains.kotlin.backend.konan.llvm import kotlinx.cinterop.* import llvm.* import java.io.File +import java.nio.file.Files internal fun verifyModule(llvmModule: LLVMModuleRef, current: String = "") = memScoped { val errorRef = allocPointerTo() @@ -49,7 +50,7 @@ private fun StringBuilder.appendModuleVerificationFailureDetails( appendVerificationError(verificationError) - val moduleDumpFile = createTempFile("kotlin_native_llvm_module_dump", ".ll") + val moduleDumpFile = Files.createTempFile("kotlin_native_llvm_module_dump", ".ll").toFile() dumpModuleAndAppendDetails(llvmModule, moduleDumpFile) diff --git a/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/exec/ExecuteCommand.kt b/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/exec/ExecuteCommand.kt index 47ae615b5b7..f2db585c3bf 100644 --- a/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/exec/ExecuteCommand.kt +++ b/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/exec/ExecuteCommand.kt @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.konan.KonanExternalToolFailure import java.io.BufferedReader import java.io.InputStreamReader import java.lang.ProcessBuilder.Redirect +import java.nio.file.Files open class Command(initialCommand: List) { @@ -83,7 +84,7 @@ open class Command(initialCommand: List) { fun getResult(withErrors: Boolean, handleError: Boolean = false): Result { log() - val outputFile = createTempFile() + val outputFile = Files.createTempFile(null, null).toFile() outputFile.deleteOnExit() try { diff --git a/kotlin-native/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/konan/KonanToolRunner.kt b/kotlin-native/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/konan/KonanToolRunner.kt index b26a217fd81..64fcbef9392 100644 --- a/kotlin-native/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/konan/KonanToolRunner.kt +++ b/kotlin-native/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/konan/KonanToolRunner.kt @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.konan.target.Family import org.jetbrains.kotlin.konan.target.HostManager import org.jetbrains.kotlin.konan.target.KonanTarget import org.jetbrains.kotlin.konan.util.DependencyProcessor +import java.nio.file.Files internal interface KonanToolRunner: Named { val mainClass: String @@ -135,16 +136,12 @@ internal class KonanCompilerRunner( return args } - val argFile = createTempFile(prefix = "konancArgs", suffix = ".lst").apply { - deleteOnExit() - } - argFile.printWriter().use { writer -> - args.forEach { - writer.println(it) - } + val argFile = Files.createTempFile("konancArgs", ".lst").toAbsolutePath().apply { + toFile().deleteOnExit() } + Files.write(argFile, args) - return listOf("@${argFile.absolutePath}") + return listOf("@${argFile}") } } diff --git a/kotlin-native/utilities/cli-runner/src/main/kotlin/org/jetbrains/kotlin/cli/utilities/GeneratePlatformLibraries.kt b/kotlin-native/utilities/cli-runner/src/main/kotlin/org/jetbrains/kotlin/cli/utilities/GeneratePlatformLibraries.kt index 47220bbabe0..e86c72d9c7b 100644 --- a/kotlin-native/utilities/cli-runner/src/main/kotlin/org/jetbrains/kotlin/cli/utilities/GeneratePlatformLibraries.kt +++ b/kotlin-native/utilities/cli-runner/src/main/kotlin/org/jetbrains/kotlin/cli/utilities/GeneratePlatformLibraries.kt @@ -25,6 +25,8 @@ import org.jetbrains.kotlin.native.interop.tool.CommonInteropArguments.Companion import org.jetbrains.kotlin.native.interop.tool.SHORT_MODULE_NAME import java.io.PrintWriter import java.io.StringWriter +import java.nio.file.Files +import java.nio.file.Paths import java.util.concurrent.atomic.AtomicInteger import kotlin.system.exitProcess import java.io.File as JFile @@ -175,11 +177,11 @@ private class DefFile(val name: String, val depends: MutableList) { } private fun createTempDir(prefix: String, parent: File): File = - File(createTempDir(prefix, directory = JFile(parent.absolutePath)).absolutePath) + File(Files.createTempDirectory(Paths.get(parent.absolutePath), prefix).toString()) private fun File.deleteAtomicallyIfPossible(tmpDirectory: File) { // Try to atomically delete the old directory. - val tmpToDelete = createTempFile(directory = JFile(tmpDirectory.absolutePath)) + val tmpToDelete = Files.createTempFile(Paths.get(tmpDirectory.absolutePath), null, null).toFile() if (renameAtomic(this.absolutePath, tmpToDelete.absolutePath, replaceExisting = true)) { tmpToDelete.deleteRecursively() } else {