Native: delete konan_temp right after the compilation instead of on exit
The Kotlin/Native compiler uses `.deleteOnExit()` as a substitute for "delete after the compilation". But when the compiler runs in a Gradle daemon, `.deleteOnExit()` means "delete on Gradle daemon exit", which might be not soon. If a single Gradle daemon runs the compiler many times, the remaining temporary files can consume quite a lot of disk space. For example, this is the case for the Kotlin build. Replace some of `.deleteOnExit()` calls with an explicit removal of temporary files at the end of a compilation session.
This commit is contained in:
committed by
Space
parent
ea8a05ec02
commit
4cf10686d6
@@ -85,8 +85,12 @@ class K2Native : CLICompiler<K2NativeCompilerArguments>() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
val konanConfig = KonanConfig(project, configuration)
|
val konanConfig = KonanConfig(project, configuration)
|
||||||
ensureModuleName(konanConfig, environment)
|
try {
|
||||||
runTopLevelPhases(konanConfig, environment)
|
ensureModuleName(konanConfig, environment)
|
||||||
|
runTopLevelPhases(konanConfig, environment)
|
||||||
|
} finally {
|
||||||
|
konanConfig.dispose()
|
||||||
|
}
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
if (e is KonanCompilationException || e is CompilationErrorException)
|
if (e is KonanCompilationException || e is CompilationErrorException)
|
||||||
return ExitCode.COMPILATION_ERROR
|
return ExitCode.COMPILATION_ERROR
|
||||||
|
|||||||
+4
@@ -29,6 +29,10 @@ import org.jetbrains.kotlin.utils.addToStdlib.cast
|
|||||||
|
|
||||||
class KonanConfig(val project: Project, val configuration: CompilerConfiguration) {
|
class KonanConfig(val project: Project, val configuration: CompilerConfiguration) {
|
||||||
|
|
||||||
|
fun dispose() {
|
||||||
|
tempFiles.dispose()
|
||||||
|
}
|
||||||
|
|
||||||
internal val distribution = run {
|
internal val distribution = run {
|
||||||
val overridenProperties = mutableMapOf<String, String>().apply {
|
val overridenProperties = mutableMapOf<String, String>().apply {
|
||||||
configuration.get(KonanConfigKeys.OVERRIDE_KONAN_PROPERTIES)?.let(this::putAll)
|
configuration.get(KonanConfigKeys.OVERRIDE_KONAN_PROPERTIES)?.let(this::putAll)
|
||||||
|
|||||||
@@ -23,6 +23,13 @@ import org.jetbrains.kotlin.konan.file.*
|
|||||||
* If pathToTemporaryDir is given and is not empty then temporary outputs will be preserved
|
* If pathToTemporaryDir is given and is not empty then temporary outputs will be preserved
|
||||||
*/
|
*/
|
||||||
class TempFiles(outputPath: String, pathToTemporaryDir: String? = null) {
|
class TempFiles(outputPath: String, pathToTemporaryDir: String? = null) {
|
||||||
|
fun dispose() {
|
||||||
|
if (deleteOnExit) {
|
||||||
|
// Note: this can throw an exception if a file deletion is failed for some reason (e.g. OS is Windows and the file is in use).
|
||||||
|
dir.deleteRecursively()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private val outputName = File(outputPath).name
|
private val outputName = File(outputPath).name
|
||||||
val deleteOnExit = pathToTemporaryDir == null || pathToTemporaryDir.isEmpty()
|
val deleteOnExit = pathToTemporaryDir == null || pathToTemporaryDir.isEmpty()
|
||||||
|
|
||||||
@@ -36,7 +43,7 @@ class TempFiles(outputPath: String, pathToTemporaryDir: String? = null) {
|
|||||||
|
|
||||||
private val dir by lazy {
|
private val dir by lazy {
|
||||||
if (deleteOnExit) {
|
if (deleteOnExit) {
|
||||||
createTempDir("konan_temp").deleteOnExit()
|
createTempDir("konan_temp")
|
||||||
} else {
|
} else {
|
||||||
createDirForTemporaryFiles(pathToTemporaryDir!!)
|
createDirForTemporaryFiles(pathToTemporaryDir!!)
|
||||||
}
|
}
|
||||||
@@ -55,8 +62,6 @@ class TempFiles(outputPath: String, pathToTemporaryDir: String? = null) {
|
|||||||
* Create file named {name}{suffix} inside temporary dir
|
* Create file named {name}{suffix} inside temporary dir
|
||||||
*/
|
*/
|
||||||
fun create(prefix: String, suffix: String = ""): File =
|
fun create(prefix: String, suffix: String = ""): File =
|
||||||
File(dir, "$prefix$suffix").also {
|
File(dir, "$prefix$suffix")
|
||||||
if (deleteOnExit) it.deleteOnExit()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user