Skip to content

Commit

Permalink
A bit of infrastructure for the klib pretty printer.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-gorshenev committed Jun 8, 2017
1 parent 9e8ca0f commit a23c640
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 30 deletions.
8 changes: 4 additions & 4 deletions LIBRARIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ The **klib** library management utility allows one to inspect and install the li

The following commands are available.

To ask the details of the library
To list library contents:

$ klib info foo
$ klib contents foo

To list library contents:
To ask the details of the library

$ klib list foo
$ klib info foo

To install the library to the default location use

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ abstract open class FileBasedLibraryReader(
override val moduleName: String
get() = namedModuleData.name

protected val tableOfContentsAsString : String
val tableOfContents : Base64
get() = namedModuleData.base64

protected fun packageMetadata(fqName: String): Base64 =
fun packageMetadata(fqName: String): Base64 =
reader.loadSerializedPackageFragment(fqName)

override fun moduleDescriptor(specifics: LanguageVersionSettings)
= deserializeModule(specifics, {packageMetadata(it)},
tableOfContentsAsString, moduleName)
tableOfContents, moduleName)
}

// This scheme describes the Konan Library (klib) layout.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ override fun loadSerializedModule(currentAbiVersion: Int): NamedModuleData {
throw Error("Expected ABI version ${currentAbiVersion}, but the binary is ${abiVersion}")
}
val moduleName = string(nameNode)
val tableOfContentsAsString = string(dataNode)
return NamedModuleData(moduleName, tableOfContentsAsString)
val tableOfContents = string(dataNode)
return NamedModuleData(moduleName, tableOfContents)
}

override fun loadSerializedPackageFragment(fqName: String): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.serialization.KonanDescriptorSerializer
import org.jetbrains.kotlin.serialization.KonanLinkData
import org.jetbrains.kotlin.serialization.KonanLinkData.*
import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.*
import org.jetbrains.kotlin.storage.LockBasedStorageManager
Expand Down Expand Up @@ -123,11 +124,13 @@ fun createKonanPackageFragmentProvider(
return provider
}

internal fun deserializePackageFragment(base64: Base64): KonanLinkData.PackageFragment {
return KonanLinkData.PackageFragment
.parseFrom(base64ToStream(base64),
KonanSerializerProtocol.extensionRegistry)
}
public fun parsePackageFragment(base64: Base64): PackageFragment =
PackageFragment.parseFrom(base64ToStream(base64),
KonanSerializerProtocol.extensionRegistry)

public fun parseModuleHeader(base64: Base64): Library =
Library.parseFrom(base64ToStream(base64),
KonanSerializerProtocol.extensionRegistry)

internal fun deserializeModule(languageVersionSettings: LanguageVersionSettings,
packageLoader:(String)->Base64, library: Base64, moduleName: String): ModuleDescriptorImpl {
Expand All @@ -139,13 +142,11 @@ internal fun deserializeModule(languageVersionSettings: LanguageVersionSettings,
builtIns.builtInsModule = moduleDescriptor
val deserializationConfiguration = CompilerDeserializationConfiguration(languageVersionSettings)

val libraryProto = KonanLinkData.Library
.parseFrom(base64ToStream(library),
KonanSerializerProtocol.extensionRegistry)
val libraryProto = parseModuleHeader(library)

val provider = createKonanPackageFragmentProvider(
libraryProto.packageFragmentNameList,
{it -> deserializePackageFragment(packageLoader(it))},
{it -> parsePackageFragment(packageLoader(it))},
storageManager,
moduleDescriptor, deserializationConfiguration)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.cli.klib

import org.jetbrains.kotlin.serialization.KonanLinkData
import org.jetbrains.kotlin.backend.konan.serialization.Base64
import org.jetbrains.kotlin.backend.konan.serialization.parseModuleHeader
import org.jetbrains.kotlin.backend.konan.serialization.parsePackageFragment

class PrettyPrinter(val library: Base64, val packageLoader: (String) -> Base64) {

private val moduleHeader: KonanLinkData.Library
get() = parseModuleHeader(library)

fun packageFragment(fqname: String): KonanLinkData.PackageFragment
= parsePackageFragment(packageLoader(fqname))

val packageFragmentNameList: List<String>
get() = moduleHeader.packageFragmentNameList

fun printPackageFragment(fqname: String) {
if (fqname.isNotEmpty()) println("package $fqname" )
println("\tHere goes the \"$fqname\" package body.\n\tIt is not implemented yet.\n")
// TODO: implement deserialized package protobuf print out.
}
}

29 changes: 17 additions & 12 deletions klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fun printUsage() {
println("where the commands are:")
println("\tinfo\tgeneral information about the library")
println("\tinstall\tinstall the library to the local repository")
println("\tlist\tcontents of the library")
println("\tcontents\tlist contents of the library")
println("\tremove\tremove the library from the local repository")
println("and the options are:")
println("\t-repository <path>\twork with the specified repository")
Expand Down Expand Up @@ -66,8 +66,8 @@ class Library(val name: String, val repository: String, val target: String) {
// TODO: need to do something here.
val currentAbiVersion = 1

val splitLibrary = SplitLibraryReader(file, currentAbiVersion, target)
val manifestFile = splitLibrary.manifestFile
val library = SplitLibraryReader(file, currentAbiVersion, target)
val manifestFile = library.manifestFile

fun info() {
val header = Properties()
Expand All @@ -78,29 +78,34 @@ class Library(val name: String, val repository: String, val target: String) {
val moduleName = header.getProperty("module_name")!!
println("Module name: $moduleName")
println("ABI version: $headerAbiVersion")
val targets = splitLibrary.targetsDir.listFiles.map{it.name}.joinToString(", ")
val targets = library.targetsDir.listFiles.map{it.name}.joinToString(", ")
print("Available targets: $targets\n")
}

fun install() {
remove()
val baseName = splitLibrary.klibFile.name
val baseName = library.klibFile.name
val newKlibName = File(repositoryFile, baseName)
splitLibrary.klibFile.copyTo(newKlibName)
library.klibFile.copyTo(newKlibName)
}

fun remove() {
repositoryFile.mkdirs()
val baseName = splitLibrary.klibFile.name
val newDirName = File(repositoryFile, splitLibrary.libDir.name)
val baseName = library.klibFile.name
val newDirName = File(repositoryFile, library.libDir.name)
val newKlibName = File(repositoryFile, baseName)
newKlibName.deleteRecursively()
newDirName.deleteRecursively()
}

fun list() {
// TODO: implement printing deserialized module protobuf.
println("Module listing not implemented yet.")
fun contents() {
val moduleName = library.moduleName
val printer = PrettyPrinter(
library.tableOfContents, {name -> library.packageMetadata(name)})

printer.packageFragmentNameList.forEach{
printer.printPackageFragment(it)
}
}
}

Expand All @@ -122,9 +127,9 @@ fun main(args: Array<String>) {
warn("IMPORTANT: the library format is unstable now. It can change with any new git commit without warning!")

when (command.verb) {
"contents" -> library.contents()
"info" -> library.info()
"install" -> library.install()
"list" -> library.list()
"remove" -> library.remove()
else -> error("Unknown command ${command.verb}.")
}
Expand Down

0 comments on commit a23c640

Please sign in to comment.