Kotlin/Native Tensorflow - tensorflow

I created a tensorflow library and trying get tensorflow version in my kotlin script like that
import tensorflow.TF_Version
fun main(args: Array<String>) {
TF_Version()
}
idea can link TF_Version() method to library with Ctrl+click
and there(in library) i have method
fun TF_Version(): CPointer<ByteVar>? {
return interpretCPointer<ByteVar>(kniBridge0())
}
#SymbolName("tensorflow_kniBridge0")
private external fun kniBridge0(): NativePtr
but when i try to build and start i see this error
/tmp/konan_temp8584442034275821874/combined.o:ld-temp.o:function Konan_start: error: undefined reference to 'tensorflow_kniBridge0'
error: /home/fnasibov/.konan/dependencies/target-gcc-toolchain-3-linux-x86-64/x86_64-unknown-linux-gnu/bin/ld.gold invocation reported errors
Please help me fix that

Found solution:
my .def file was
headers = /home/user/.konan/third--arty/tensorflow/include/tensorflow/c/c_api.h
And after i add Opts to def file:
headers = /home/fnasibov/.konan/third-party/tensorflow/include/tensorflow/c/c_api.h
compilerOpts.linux = -I/usr/include -I/usr/include/x86_64-linux-gnu
linkerOpts.linux = -L/home/user/.konan/third-party/tensorflow/lib -ltensorflow
and regenerate klib - thats worked!

Related

KotlinJs: Unable to run jsBrowserTest in Google Cloudbuild due to "ReferenceError: setMetadataFor is not defined"

We have a very simple test in kotlinJs to check that a function we have in Javascript is read into kotlin and returns something:
JS code:
function: makeKey = (num) => { return 1000 + num }
Kotlin FFI code:
#JsNonModule
external object MyApp {
fun makeKey(num: Int): Int
}
Test Code:
class TestFFI {
#Test
fun thingsShouldWork() {
println(MyApp.makeKey(5))
assertNotNull(MyApp.makeKey(5))
assertEquals(1005, MyApp.makeKey(5))
}
}
This works locally but when we put this into a google cloud run we get the following failure: ReferenceError: setMetadataFor is not defined
setMetadataFor appears to be a function in the testJs file that is produced by the kotlin compiler and is defined as
var setMetadataFor = kotlin_kotlin.$_$.t9;
We are wondering if the Kotlin code is getting packaged wrong and not including the relevant file
We are using Kotlin 1.8 and Gradle 7.4.

Why is the kotlin-gradle-plugin failing to create a PSIFile from CodeInsightTestFixture.configureByText?

I created an IntelliJ plugin using the template https://github.com/JetBrains/intellij-platform-plugin-template. The template comes with a test that runs on an XML file. I want to create a similar test for a Kotlin file. Here's the template test file plus my added test (test2):
package org.jetbrains.plugins.template
import com.intellij.ide.highlighter.XmlFileType
import com.intellij.psi.xml.XmlFile
import com.intellij.testFramework.TestDataPath
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import com.intellij.util.PsiErrorElementUtil
#TestDataPath("\$CONTENT_ROOT/src/test/testData")
class MyPluginTest : BasePlatformTestCase() {
fun testXMLFile() {
val psiFile = myFixture.configureByText(XmlFileType.INSTANCE, "<foo>bar</foo>")
val xmlFile = assertInstanceOf(psiFile, XmlFile::class.java)
assertFalse(PsiErrorElementUtil.hasErrors(project, xmlFile.virtualFile))
assertNotNull(xmlFile.rootTag)
xmlFile.rootTag?.let {
assertEquals("foo", it.name)
assertEquals("bar", it.value.text)
}
}
override fun getTestDataPath() = "src/test/testData/rename"
fun testRename() {
myFixture.testRename("foo.xml", "foo_after.xml", "a2")
}
// Here's my test
fun test2() {
val fileText: String = """
package com.loganmay.test
data class MyClass(val myString: String)
""".trimIndent()
val psiFile = myFixture.configureByText("a.kt", fileText)
val xmlFile = assertInstanceOf(psiFile, XmlFile::class.java)
}
}
Without changing the build.gradle file, that test fails with:
Expected instance of: com.intellij.psi.xml.XmlFile actual: com.intellij.psi.impl.source.PsiPlainTextFileImpl
I want it to parse the text as a PsiFile that's also a KtFile. From various sources, I've been led to believe that the fixture is parsing it as a plain text file because the test project doesn't have access to the Kotlin compiler. So, I added:
dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
}
to the build.gradle. Then, when I run the test, configureByText throws an exception with a big trace, the root exception of which is:
Caused by: java.lang.Throwable: 'filetype.archive.display.name' is not found in java.util.PropertyResourceBundle#4ecbb519(messages.CoreBundle)
... 53 more
org.jetbrains.plugins.template.MyPluginTest > test2 FAILED
com.intellij.diagnostic.PluginException at ComponentManagerImpl.kt:511
Caused by: java.util.MissingResourceException at Registry.java:164
Does anyone have any insight into what the issue is or know how to resolve it?
Notes:
I also tried importing the kotlin compiler and casting psiFile as KtFile, which produced the same error, an idea I got from here
This project has a test like this that may be working
This post and this post recommend adding the kotlin gradle plugin, which I did
This question seems similar
Yann Cebron replied on the jetbrains help forum with an answer for Java, which also worked for Kotlin.
The solution is to add a dependency to the IntelliJ gradle plugin. The template comes with these lines in the build.gradle:
intellij {
pluginName.set(properties("pluginName"))
version.set(properties("platformVersion"))
type.set(properties("platformType"))
// Plugin Dependencies. Uses `platformPlugins` property from the gradle.properties file.
plugins.set(properties("platformPlugins").split(',').map(String::trim).filter(String::isNotEmpty))
}
So, didn't need to do anything there. In my gradle.properties, I added
platformPlugins = com.intellij.java, org.jetbrains.kotlin
To my plugin.xml, I added:
<depends>com.intellij.modules.java</depends>
<depends>org.jetbrains.kotlin</depends>
I was able to remove
dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
}
from the build.gradle which I mentioned above.
Now, the test works for Java and Kotlin files.

Cannot use Duration.seconds method from kotlin.time

I'm trying to use the Duration class from kotlin.time package like this:
Duration.seconds(5)
But I see Expression 'seconds' cannot be invoked as a function. The function 'invoke()' is not found in my intelliJ.
Not sure, how should I call this function then? Or is there something I'm missing?
For time being I have to use 5.toDuration(DurationUnit.SECONDS)
Snippet:
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
#ExperimentalTime
class Temp {
fun test() {
println(Duration.seconds(5))
}
}
I have kotlin ref in build.gradle.kts file
plugins {
id("org.jetbrains.kotlin.jvm") version "1.5.21"
}
Using it with kotest.
Here are my project dependencies.
dependencies {
testImplementation(kotlin("test-junit5"))
testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
testImplementation("io.kotest:kotest-runner-junit5-jvm:$kotestVersion")
testImplementation("io.rest-assured:kotlin-extensions:4.3.0")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.12.+")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.12.+")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.+")
implementation("io.kotest.extensions:kotest-extensions-allure:1.0.1")
}
Make sure you are importing the correct Duration
import kotlin.time.Duration
as there are other Durations.
You can also use the extension function on Int,
import kotlin.time.toDuration
5.toDuration(DurationUnit.SECONDS)
For those looking to use Java Duration, you can do this using #Francesc answer and the information in the link below is (1.toDuration(DurationUnit.SECONDS)).toJavaDuration()
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.time/to-java-duration.html

Kotlin compiler argument not applied to scratch file

I wanted to use kotlin.time.measureTime in a Kotlin scratch file but it doesn't work.
...
kotlinOptions {
...
freeCompilerArgs = ["-Xopt-in=kotlin.time.ExperimentalTime"]
}
Of course in my scratch file I "use classpath of module" which defines the compiler argument. When I use measureTime in regular code it works fine, but in the scratch file doesn't.
#OptIn works for me (see alternative recommendation by IntelliJ IDEA).
#OptIn(ExperimentalTime::class)
val s = measureTime { sleep(10) }
println(s)
Try to use explicit declaration of experimental API usage inside scratch file:
#ExperimentalTime
val s = measureTime {
}

How to Debug NoClassDefFoundError (Gradle, Kotlin)

With my kotlin project at Runtime I get the following error:
java.lang.NoClassDefFoundError: com/julianjarecki/latextemplates/lib/jtwig/RequireNodeParser
at com.julianjarecki.latextemplates.lib.jtwig.RequireNodeParserProvider.<init>(RequireNodeParserProvider.kt:8)
at com.julianjarecki.latextemplates.lib.jtwig.RequireExtension.configure(RequireExtension.kt:11)
at org.jtwig.environment.EnvironmentFactory.create(EnvironmentFactory.java:60)
at com.julianjarecki.latextemplates.lib.jtwig.LatexTemplate.getLatexRequireEnvironment(LatexParser.kt:76)
at com.julianjarecki.latextemplates.lib.jtwig.LatexTemplate.access$getLatexRequireEnvironment$p(LatexParser.kt:68)
at com.julianjarecki.latextemplates.lib.jtwig.LatexTemplate$template$2.invoke(LatexParser.kt:73)
at com.julianjarecki.latextemplates.lib.jtwig.LatexTemplate$template$2.invoke(LatexParser.kt:68)
at com.julianjarecki.latextemplates.lib.UpdatableLazy.getValue(UpdatableLazy.kt:17)
at com.julianjarecki.latextemplates.lib.jtwig.LatexTemplate.getTemplate(LatexParser.kt)
at com.julianjarecki.latextemplates.view.MainView$root$1$4$1$1$2.invoke(MainView.kt:133)
at com.julianjarecki.latextemplates.view.MainView$root$1$4$1$1$2.invoke(MainView.kt:35)
at tornadofx.NodesKt$onDoubleClick$1.handle(Nodes.kt:484)
at tornadofx.NodesKt$onDoubleClick$1.handle(Nodes.kt)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
Indeed, when I look into my buil folder, RequireNodeParser.class is not where it is supposed to be.
The class is referenced in RequireNodeParserProvider like this:
package com.julianjarecki.latextemplates.lib.jtwig
import org.jtwig.parser.addon.AddonParserProvider
import org.jtwig.parser.parboiled.node.AddonParser
import com.julianjarecki.latextemplates.lib.jtwig.RequireNodeParser
class RequireNodeParserProvider : AddonParserProvider {
override fun keywords(): MutableCollection<String> = mutableListOf(RequireKeywords.REQUIRE.token)
override fun parser(): Class<out AddonParser> = RequireNodeParser::class.java
}
How do I even start debugging this Problem?
Edit 1 (Stacktrace of the ClassNotFoundException cause):