From 0bc941e324f239b2801029acc9374b1576138053 Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Sat, 7 Dec 2019 21:36:11 +0300 Subject: [PATCH] Called system for atomic moving of compiler's output --- backend.native/build.gradle | 18 ++++++++++ .../jetbrains/kotlin/backend/konan/Linker.kt | 3 +- common/build.gradle | 8 +++++ common/src/files/cpp/Files.cpp | 34 +++++++++++++++++++ common/src/files/headers/Files.h | 20 +++++++++++ 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 common/src/files/cpp/Files.cpp create mode 100644 common/src/files/headers/Files.h diff --git a/backend.native/build.gradle b/backend.native/build.gradle index cb9a4d7ee1a..8481d3d9841 100644 --- a/backend.native/build.gradle +++ b/backend.native/build.gradle @@ -105,6 +105,22 @@ kotlinNativeInterop { pkg 'org.jetbrains.kotlin.backend.konan.hash' } + + files { + if (!project.parent.convention.plugins.platformInfo.isWindows()) { + compilerOpts '-fPIC' + linkerOpts '-fPIC' + } + linker 'clang++' + linkOutputs ":common:${hostName}Files" + + headers fileTree('../common/src/files/headers') { + include '**/*.h' + include '**/*.hpp' + } + + pkg 'org.jetbrains.kotlin.backend.konan.files' + } } @@ -144,6 +160,7 @@ dependencies { compilerCompile kotlinCompilerModule compilerCompile kotlinNativeInterop['llvm'].configuration compilerCompile kotlinNativeInterop['hash'].configuration + compilerCompile kotlinNativeInterop['files'].configuration compilerCompile "org.jetbrains.kotlin:kotlin-native-shared:$konanVersion" compilerCompile "org.jetbrains.kotlin:konan.serializer:$konanVersion" @@ -229,6 +246,7 @@ jar { from sourceSets.cli_bc.output, sourceSets.compiler.output, sourceSets.hashInteropStubs.output, + sourceSets.filesInteropStubs.output, sourceSets.llvmInteropStubs.output dependsOn ':runtime:hostRuntime', 'external_jars' diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Linker.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Linker.kt index fddb170a575..462cdb89818 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Linker.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Linker.kt @@ -5,6 +5,7 @@ import org.jetbrains.kotlin.konan.file.File import org.jetbrains.kotlin.konan.target.CompilerOutputKind import org.jetbrains.kotlin.konan.target.Family import org.jetbrains.kotlin.konan.target.LinkerOutputKind +import org.jetbrains.kotlin.backend.konan.files.renameAtomic internal fun determineLinkerOutput(context: Context): LinkerOutputKind = when (context.config.produce) { @@ -45,7 +46,7 @@ internal class Linker(val context: Context) { val outputFiles = context.config.outputFiles val outputFile = java.io.File(outputFiles.mainFileMangled) val outputDsymBundle = java.io.File(outputFiles.mainFileMangled + ".dSYM") - if (outputFile.renameTo(java.io.File(outputFiles.mainFile))) + if (renameAtomic(outputFile.absolutePath, outputFiles.mainFile, /* replaceExisting = */ false)) outputDsymBundle.renameTo(java.io.File(outputFiles.mainFile + ".dSYM")) else { outputFile.delete() diff --git a/common/build.gradle b/common/build.gradle index 02e7594975c..c20d29ee972 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -13,8 +13,16 @@ targetList.each { targetName -> } } +targetList.each { targetName -> + task ("${targetName}Files", type: CompileCppToBitcode) { + name 'files' + target targetName + } +} + task build { dependsOn "${hostName}Hash" + dependsOn "${hostName}Files" } task clean { diff --git a/common/src/files/cpp/Files.cpp b/common/src/files/cpp/Files.cpp new file mode 100644 index 00000000000..fab901711ed --- /dev/null +++ b/common/src/files/cpp/Files.cpp @@ -0,0 +1,34 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ +#include "Files.h" + +#include + +#ifdef __APPLE__ + +bool renameAtomic(const char* from, const char* to, bool replaceExisting) { + return renamex_np(from, to, replaceExisting ? 0 : RENAME_EXCL) == 0; +} + +#elif _WIN32 + +#include + +bool renameAtomic(const char* from, const char* to, bool replaceExisting) { + return MoveFileEx(from, to, replaceExisting ? MOVEFILE_REPLACE_EXISTING : 0) != 0; +} + +#else + +#include +#include +#include + +bool renameAtomic(const char* from, const char* to, bool replaceExisting) { + return syscall(SYS_renameat2, 0, from, 0, to, replaceExisting ? 0 : RENAME_NOREPLACE) == 0; +} + + +#endif \ No newline at end of file diff --git a/common/src/files/headers/Files.h b/common/src/files/headers/Files.h new file mode 100644 index 00000000000..9fe41524868 --- /dev/null +++ b/common/src/files/headers/Files.h @@ -0,0 +1,20 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ +#ifndef COMMON_FILES_H +#define COMMON_FILES_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +bool renameAtomic(const char* from, const char* to, bool replaceExisting); + +#ifdef __cplusplus +} +#endif + +#endif // COMMON_FILES_H