From dc80ba34929c47de2933af02f3d0a57ad59e88c3 Mon Sep 17 00:00:00 2001 From: Alexander Gorshenev Date: Wed, 17 May 2017 20:00:31 +0300 Subject: [PATCH] Moved KonanLibrary to org.jetbrains.kotlin.backend.konan.util.File instead of java.io.File. --- .../kotlin/backend/konan/library/FileUtil.kt | 70 --------------- .../backend/konan/library/KonanLibrary.kt | 19 ++-- .../backend/konan/llvm/MetadataGenerator.kt | 6 +- .../kotlin/backend/konan/util/NoJavaUtil.kt | 90 ++++++++++++++++++- 4 files changed, 98 insertions(+), 87 deletions(-) delete mode 100644 backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/library/FileUtil.kt diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/library/FileUtil.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/library/FileUtil.kt deleted file mode 100644 index c9fac613301..00000000000 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/library/FileUtil.kt +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.backend.konan - -import java.io.File -import java.net.URI -import java.nio.file.FileSystems -import java.nio.file.Files -import java.nio.file.Path -import java.nio.file.StandardCopyOption - -val File.zipUri: URI - get() = URI.create("jar:file:${this.absolutePath}") - -val File.zipRootPath: Path - get() { - val zipUri = this.zipUri - val allowCreation = hashMapOf("create" to "true") - val zipfs = FileSystems.newFileSystem(zipUri, allowCreation, null) - return zipfs.getPath("/") - } - -fun File.zipDirAs(zipFile: File) { - val zipPath = zipFile.zipRootPath - this.toPath().recursiveCopyTo(zipPath) - zipPath.fileSystem.close() -} - -fun File.unzipAs(directory: File) { - val zipPath = this.zipRootPath - zipPath.recursiveCopyTo(directory.toPath()) - zipPath.fileSystem.close() -} - -fun Path.recursiveCopyTo(destPath: Path) { - val sourcePath = this - Files.walk(sourcePath).forEach next@ { oldPath -> - - val relative = sourcePath.relativize(oldPath) - val destFs = destPath.getFileSystem() - // We are copying files between file systems, - // so pass the relative path through the Sting. - val newPath = destFs.getPath(destPath.toString(), relative.toString()) - - // File systems don't allow replacing an existing root. - if (newPath == newPath.getRoot()) return@next - Files.copy(oldPath, newPath, - StandardCopyOption.REPLACE_EXISTING) - - } -} - -fun File.copyTo(destination: File) { - Files.copy(this.toPath(), destination.toPath()) -} - diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/library/KonanLibrary.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/library/KonanLibrary.kt index 31b08f70f11..090ce757861 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/library/KonanLibrary.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/library/KonanLibrary.kt @@ -22,9 +22,12 @@ import llvm.LLVMWriteBitcodeToFile import org.jetbrains.kotlin.backend.konan.llvm.* import org.jetbrains.kotlin.backend.konan.serialization.Base64 import org.jetbrains.kotlin.backend.konan.serialization.deserializeModule +import org.jetbrains.kotlin.backend.konan.util.File +import org.jetbrains.kotlin.backend.konan.util.copyTo +import org.jetbrains.kotlin.backend.konan.util.unzipAs +import org.jetbrains.kotlin.backend.konan.util.zipDirAs import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl -import java.io.File interface KonanLibraryReader { val libraryName: String @@ -87,23 +90,23 @@ class SplitLibraryReader(val libDir: File, configuration: CompilerConfiguration) // TODO: Search path processing is also goes somewhere around here. fun unpackIfNeeded() { // TODO: Clarify the policy here. - if (libDir.exists()) { - if (libDir.isDirectory()) return + if (libDir.exists) { + if (libDir.isDirectory) return } - if (!klibFile.exists()) { + if (!klibFile.exists) { error("Could not find neither $libDir nor $klibFile.") } - if (klibFile.isFile()) { + if (klibFile.isFile) { klibFile.unzipAs(libDir) - if (!libDir.exists()) error("Could not unpack $klibFile as $libDir.") + if (!libDir.exists) error("Could not unpack $klibFile as $libDir.") } else { error("Expected $klibFile to be a regular libDir.") } } private val File.dirAbsolutePaths: List - get() = this.listFiles()!!.toList()!!.map{it->it.absolutePath} + get() = this.listFiles!!.toList()!!.map{it->it.absolutePath} private val targetDir: File get() { @@ -209,7 +212,7 @@ class SplitLibraryWriter(val libDir: File, target: String, val nopack: Boolean = } override fun addNativeBitcode(library: String) { - val basename = File(library).getName() + val basename = File(library).name File(library).copyTo(File(nativeDir, basename)) } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/MetadataGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/MetadataGenerator.kt index 95cb633ab5b..85357b539f5 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/MetadataGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/MetadataGenerator.kt @@ -18,13 +18,9 @@ package org.jetbrains.kotlin.backend.konan.llvm import kotlinx.cinterop.* import llvm.* -import org.jetbrains.kotlin.backend.konan.Context -import org.jetbrains.kotlin.backend.konan.KonanConfigKeys import org.jetbrains.kotlin.backend.konan.LinkData -import org.jetbrains.kotlin.ir.declarations.IrModuleFragment +import org.jetbrains.kotlin.backend.konan.util.File import java.io.Closeable -import java.io.File -import kotlin.io.readText import java.util.Properties class NamedModuleData(val name:String, val base64: String) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/util/NoJavaUtil.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/util/NoJavaUtil.kt index 66753d71b7f..aa206a93991 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/util/NoJavaUtil.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/util/NoJavaUtil.kt @@ -16,12 +16,94 @@ package org.jetbrains.kotlin.backend.konan.util -class File(val path:String) { +import java.net.URI +import java.nio.file.FileSystems +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.Paths +import java.nio.file.StandardCopyOption + +class File(val path: String) { + private constructor(file: java.io.File): this(file.path) + constructor(parent: String, child: String): this(java.io.File(parent, child)) + constructor(parent: File, child: String): this(java.io.File(parent.path, child)) + private val javaFile = java.io.File(path) - val absoluteFile: File - get() = File(javaFile.absolutePath) + val absolutePath: String + get() = javaFile.absolutePath + val absoluteFile: File + get() = File(absolutePath) val name: String get() = javaFile.name val parent: String get() = javaFile.parent -} \ No newline at end of file + + val exists + get() = javaFile.exists() + val isDirectory + get() = javaFile.isDirectory() + val isFile + get() = javaFile.isFile() + val listFiles + get() = javaFile.listFiles() + + fun mkdirs() = javaFile.mkdirs() + fun delete() = javaFile.delete() + fun deleteRecursively() = javaFile.deleteRecursively() + fun readText() = javaFile.readText() + fun writeText(text: String) = javaFile.writeText(text) + + override fun toString() = path + + // TODO: Consider removeing these after konanazing java.util.Properties. + fun bufferedReader() = javaFile.bufferedReader() + fun outputStream() = javaFile.outputStream() +} + + +private val File.zipUri: URI + get() = URI.create("jar:file:${this.absolutePath}") + +private val File.zipRootPath: Path + get() { + val zipUri = this.zipUri + val allowCreation = hashMapOf("create" to "true") + val zipfs = FileSystems.newFileSystem(zipUri, allowCreation, null) + return zipfs.getPath("/") + } + +private fun File.toPath() = Paths.get(this.path) + +fun File.zipDirAs(zipFile: File) { + val zipPath = zipFile.zipRootPath + this.toPath().recursiveCopyTo(zipPath) + zipPath.fileSystem.close() +} + +fun File.unzipAs(directory: File) { + val zipPath = this.zipRootPath + zipPath.recursiveCopyTo(directory.toPath()) + zipPath.fileSystem.close() +} + +fun Path.recursiveCopyTo(destPath: Path) { + val sourcePath = this + Files.walk(sourcePath).forEach next@ { oldPath -> + + val relative = sourcePath.relativize(oldPath) + val destFs = destPath.getFileSystem() + // We are copying files between file systems, + // so pass the relative path through the Sting. + val newPath = destFs.getPath(destPath.toString(), relative.toString()) + + // File systems don't allow replacing an existing root. + if (newPath == newPath.getRoot()) return@next + Files.copy(oldPath, newPath, + StandardCopyOption.REPLACE_EXISTING) + } +} + +fun File.copyTo(destination: File) { + Files.copy(this.toPath(), destination.toPath()) +} +