Skip to content

Commit 702b665

Browse files
committed
Update Gradle, Kotlin, KMath; fix build problems; drop PSI in code generation
1 parent 840649b commit 702b665

24 files changed

+691
-893
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
# Based on https://github.com/touchlab/Stately/blob/main/.github/workflows/build.yml
1+
name: Gradle build
22

3-
name: build
43
on: [ push ]
54

65
jobs:
76
build:
87
runs-on: ubuntu-20.04
8+
timeout-minutes: 30
99
steps:
1010
- name: Checkout the repo
1111
uses: actions/checkout@v2
1212
- name: Set up JDK 11
13-
uses: actions/setup-java@v1
13+
uses: DeLaGuardo/setup-graalvm@4.0
1414
with:
15-
java-version: 11
15+
graalvm: 21.1.0
16+
java: java11
17+
arch: amd64
1618
- name: Cache gradle
1719
uses: actions/cache@v2
1820
with:

.github/workflows/pages.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,25 @@ jobs:
1212
- name: Checkout the repo
1313
uses: actions/checkout@v2
1414
- name: Set up JDK 11
15-
uses: actions/setup-java@v1
15+
uses: DeLaGuardo/setup-graalvm@4.0
1616
with:
17-
java-version: 11
17+
graalvm: 21.1.0
18+
java: java11
19+
arch: amd64
20+
- name: Cache gradle
21+
uses: actions/cache@v2
22+
with:
23+
path: ~/.gradle/caches
24+
key: ubuntu-20.04-gradle-${{ hashFiles('*.gradle.kts') }}
25+
restore-keys: |
26+
ubuntu-20.04-gradle-
27+
- name: Cache konan
28+
uses: actions/cache@v2
29+
with:
30+
path: ~/.konan
31+
key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
32+
restore-keys: |
33+
${{ runner.os }}-gradle-
1834
- name: Build
1935
run: ./gradlew dokkaHtml --no-daemon --no-parallel --stacktrace
2036
- name: Deploy to GitHub Pages

.github/workflows/publish.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ jobs:
1010
publish:
1111
environment:
1212
name: publish
13-
runs-on: ubuntu-latest
13+
runs-on: ubuntu-20.04
1414
steps:
1515
- name: Checkout the repo
1616
uses: actions/checkout@v2
1717
- name: Set up JDK 11
18-
uses: actions/setup-java@v1
18+
uses: DeLaGuardo/setup-graalvm@4.0
1919
with:
20-
java-version: 11
20+
graalvm: 21.1.0
21+
java: java11
22+
arch: amd64
2123
- name: Cache gradle
2224
uses: actions/cache@v2
2325
with:

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
[![Gradle build](https://github.com/mipt-npm/kmath-gsl/workflows/build/badge.svg)](https://github.com/mipt-npm/kmath-gsl/actions/workflows/build.yml)
77
[![Space](https://img.shields.io/maven-metadata/v?label=Space&metadataUrl=https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven/space/kscience/kmath-gsl/maven-metadata.xml)](https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven/space/kscience/kmath-gsl/0.2.1-dev-1/)
88

9-
- [matrix-contexts](src/nativeMain/kotlin/GslMatrixContext.kt) : Matrix Contexts over Double, Float, and Complex implemented with GSL
9+
- [matrix-contexts](src/nativeMain/kotlin/GslLinearSpace.kt) : LinearSpace implementations for Double, Float, and Complex matrices and vectors implemented with GSL
1010

1111

1212
## Artifact:
1313

14-
The Maven coordinates of this project are `space.kscience:kmath-gsl:0.2.1-dev-1`.
14+
The Maven coordinates of this project are `space.kscience:kmath-gsl:0.3.0-dev-1`.
1515

1616
**Gradle:**
1717
```gradle
@@ -21,7 +21,7 @@ repositories {
2121
}
2222
2323
dependencies {
24-
implementation 'space.kscience:kmath-gsl:0.2.1-dev-1'
24+
implementation 'space.kscience:kmath-gsl:0.3.0-dev-1'
2525
}
2626
```
2727
**Gradle Kotlin DSL:**
@@ -32,6 +32,6 @@ repositories {
3232
}
3333

3434
dependencies {
35-
implementation("space.kscience:kmath-gsl:0.2.1-dev-1")
35+
implementation("space.kscience:kmath-gsl:0.3.0-dev-1")
3636
}
3737
```

build.gradle.kts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ plugins {
1414
}
1515

1616
group = "space.kscience"
17-
version = "0.2.1-dev-1"
17+
version = "0.3.0-dev-1"
1818

1919
repositories.mavenCentral()
2020

@@ -23,10 +23,8 @@ kotlin {
2323
data class DownloadLinks(val gsl: String)
2424

2525
val (nativeTarget, downloadLinks) = when (System.getProperty("os.name")) {
26-
// "Mac OS X" -> macosX64()
27-
2826
"Linux" -> linuxX64() to DownloadLinks(
29-
gsl = "https://anaconda.org/conda-forge/gsl/2.6/download/linux-64/gsl-2.6-he838d99_2.tar.bz2"
27+
gsl = "https://anaconda.org/conda-forge/gsl/2.6/download/linux-64/gsl-2.6-he838d99_2.tar.bz2",
3028
)
3129

3230
else -> {
@@ -38,14 +36,12 @@ kotlin {
3836
val thirdPartyDir =
3937
File("${System.getProperty("user.home")}/.konan/third-party/kmath-gsl-${project.property("version")}")
4038

41-
val main by nativeTarget.compilations.getting {
42-
cinterops {
43-
val libgsl by creating
44-
}
45-
}
4639

40+
val main by nativeTarget.compilations.getting
4741
val test by nativeTarget.compilations.getting
4842

43+
val libgsl by main.cinterops.creating
44+
4945
val nativeMain by sourceSets.creating {
5046
val codegen by tasks.creating {
5147
matricesCodegen(kotlin.srcDirs.first().absolutePath + "/_Matrices.kt")
@@ -55,7 +51,7 @@ kotlin {
5551
kotlin.srcDirs(files().builtBy(codegen))
5652

5753
dependencies {
58-
api("space.kscience:kmath-complex:0.2.1")
54+
api("space.kscience:kmath-complex:0.3.0-dev-12")
5955
}
6056
}
6157

@@ -67,8 +63,7 @@ kotlin {
6763
test.defaultSourceSet.dependsOn(nativeTest)
6864

6965
val downloadGsl by tasks.creating(Download::class) {
70-
val url = downloadLinks.gsl
71-
src(url)
66+
src(downloadLinks.gsl)
7267
dest(File(thirdPartyDir, "libgsl.tar.bz2"))
7368
overwrite(false)
7469
}
@@ -80,13 +75,14 @@ kotlin {
8075
}
8176

8277
val writeDefFile by tasks.creating {
83-
val file = main.cinterops["libgsl"].defFile
78+
val file = libgsl.defFile
8479
file.parentFile.mkdirs()
85-
file.createNewFile()
80+
if (!file.exists()) file.createNewFile()
8681

8782
file.writeText("""
8883
package=org.gnu.gsl
8984
headers=gsl/gsl_blas.h gsl/gsl_linalg.h gsl/gsl_permute_matrix.h gsl/gsl_matrix.h gsl/gsl_vector.h gsl/gsl_errno.h
85+
linkerOpts = -L/usr/lib64 -L/usr/lib/x86_64-linux-gnu -lblas
9086
staticLibraries=libgsl.a libgslcblas.a
9187
compilerOpts=-I${thirdPartyDir}/include/
9288
libraryPaths=${thirdPartyDir}/lib/
@@ -114,8 +110,9 @@ readme {
114110

115111
feature(
116112
id = "matrix-contexts",
117-
description = "Matrix Contexts over Double, Float, and Complex implemented with GSL",
118-
ref = "src/nativeMain/kotlin/GslMatrixContext.kt"
113+
description =
114+
"LinearSpace implementations for Double, Float, and Complex matrices and vectors implemented with GSL",
115+
ref = "src/nativeMain/kotlin/GslLinearSpace.kt"
119116
)
120117
}
121118

buildSrc/build.gradle.kts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,3 @@ plugins {
33
}
44

55
repositories.mavenCentral()
6-
7-
dependencies {
8-
implementation(kotlin("compiler-embeddable", "1.4.32"))
9-
}

buildSrc/src/main/kotlin/codegenUtilities.kt

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,5 @@
11
package space.kscience.kmath.gsl.codegen
22

3-
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
4-
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
5-
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
6-
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
7-
import org.jetbrains.kotlin.com.intellij.mock.MockProject
8-
import org.jetbrains.kotlin.com.intellij.openapi.extensions.ExtensionPoint
9-
import org.jetbrains.kotlin.com.intellij.openapi.extensions.Extensions
10-
import org.jetbrains.kotlin.com.intellij.openapi.util.UserDataHolderBase
11-
import org.jetbrains.kotlin.com.intellij.pom.PomModel
12-
import org.jetbrains.kotlin.com.intellij.pom.PomModelAspect
13-
import org.jetbrains.kotlin.com.intellij.pom.PomTransaction
14-
import org.jetbrains.kotlin.com.intellij.pom.impl.PomTransactionBase
15-
import org.jetbrains.kotlin.com.intellij.pom.tree.TreeAspect
16-
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
17-
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.TreeCopyHandler
18-
import org.jetbrains.kotlin.config.CompilerConfiguration
19-
import sun.reflect.ReflectionFactory
20-
21-
internal fun createProject(): MockProject {
22-
val project = KotlinCoreEnvironment.createForProduction(
23-
{},
24-
CompilerConfiguration().apply { put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE) },
25-
EnvironmentConfigFiles.JVM_CONFIG_FILES
26-
).project as MockProject
27-
28-
val extensionPoint = "org.jetbrains.kotlin.com.intellij.treeCopyHandler"
29-
30-
arrayOf(project.extensionArea, Extensions.getRootArea())
31-
.asSequence()
32-
.filterNot { it.hasExtensionPoint(extensionPoint) }
33-
.forEach {
34-
it.registerExtensionPoint(extensionPoint, TreeCopyHandler::class.java.name, ExtensionPoint.Kind.INTERFACE)
35-
}
36-
37-
project.registerService(PomModel::class.java, object : UserDataHolderBase(), PomModel {
38-
override fun runTransaction(transaction: PomTransaction) = (transaction as PomTransactionBase).run()
39-
40-
@Suppress("UNCHECKED_CAST")
41-
override fun <T : PomModelAspect> getModelAspect(aspect: Class<T>): T? {
42-
if (aspect == TreeAspect::class.java) {
43-
val constructor = ReflectionFactory.getReflectionFactory().newConstructorForSerialization(
44-
aspect,
45-
Any::class.java.getDeclaredConstructor(*arrayOfNulls(0))
46-
)
47-
48-
return constructor.newInstance() as T
49-
}
50-
51-
return null
52-
}
53-
})
54-
55-
return project
56-
}
57-
58-
internal operator fun PsiElement.plusAssign(e: PsiElement) {
59-
add(e)
60-
}
61-
623
internal fun fn(pattern: String, type: String): String {
634
if (type == "double") return pattern.replace("R", "_")
645
return pattern.replace("R", "_${type}_")
Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
package space.kscience.kmath.gsl.codegen
22

33
import org.intellij.lang.annotations.Language
4-
import org.jetbrains.kotlin.com.intellij.openapi.project.Project
5-
import org.jetbrains.kotlin.name.FqName
6-
import org.jetbrains.kotlin.psi.KtFile
7-
import org.jetbrains.kotlin.psi.KtPsiFactory
8-
import org.jetbrains.kotlin.resolve.ImportPath
94
import java.io.File
105

11-
private fun KtPsiFactory.createMatrixClass(
12-
f: KtFile,
6+
private fun Appendable.createMatrixClass(
137
cTypeName: String,
148
kotlinTypeName: String,
15-
kotlinTypeAlias: String = kotlinTypeName
169
) {
1710
fun fn(pattern: String) = fn(pattern, cTypeName)
18-
val className = "Gsl${kotlinTypeAlias}Matrix"
11+
val className = "Gsl${kotlinTypeName}Matrix"
1912
val structName = sn("gsl_matrixR", cTypeName)
2013

2114
@Language("kotlin") val text = """internal class $className(
@@ -29,18 +22,20 @@ private fun KtPsiFactory.createMatrixClass(
2922
override val colNum: Int
3023
get() = nativeHandle.pointed.size2.toInt()
3124
32-
override val rows: Buffer<Buffer<$kotlinTypeName>>
33-
get() = VirtualBuffer(rowNum) { r ->
34-
Gsl${kotlinTypeAlias}Vector(
25+
@PerformancePitfall
26+
override val rows: List<Buffer<$kotlinTypeName>>
27+
get() = List(rowNum) { r ->
28+
Gsl${kotlinTypeName}Vector(
3529
${fn("gsl_matrixRrow")}(nativeHandle, r.toULong()).placeTo(scope).pointed.vector.ptr,
3630
scope,
3731
false,
3832
)
3933
}
4034
41-
override val columns: Buffer<Buffer<$kotlinTypeName>>
42-
get() = VirtualBuffer(rowNum) { c ->
43-
Gsl${kotlinTypeAlias}Vector(
35+
@PerformancePitfall
36+
override val columns: List<Buffer<$kotlinTypeName>>
37+
get() = List(rowNum) { c ->
38+
Gsl${kotlinTypeName}Vector(
4439
${fn("gsl_matrixRcolumn")}(nativeHandle, c.toULong()).placeTo(scope).pointed.vector.ptr,
4540
scope,
4641
false,
@@ -60,45 +55,31 @@ private fun KtPsiFactory.createMatrixClass(
6055
}
6156
6257
override fun close(): Unit = ${fn("gsl_matrixRfree")}(nativeHandle)
63-
64-
override fun equals(other: Any?): Boolean {
65-
if (other is $className)
66-
return ${fn("gsl_matrixRequal")}(nativeHandle, other.nativeHandle) == 1
67-
68-
return super.equals(other)
69-
}
7058
}"""
71-
f += createClass(text)
72-
f += createNewLine(2)
59+
appendLine(text)
60+
appendLine()
7361
}
7462

7563
/**
7664
* Generates matrices source code for kmath-gsl.
7765
*/
78-
fun matricesCodegen(outputFile: String, project: Project = createProject()) {
79-
val f = KtPsiFactory(project, true).run {
80-
createFile("").also { f ->
81-
f += createPackageDirective(FqName("space.kscience.kmath.gsl"))
82-
f += createNewLine(2)
83-
f += createImportDirective(ImportPath.fromString("kotlinx.cinterop.*"))
84-
f += createNewLine(1)
85-
f += createImportDirective(ImportPath.fromString("space.kscience.kmath.structures.*"))
86-
f += createNewLine(1)
87-
f += createImportDirective(ImportPath.fromString("org.gnu.gsl.*"))
88-
f += createNewLine(2)
89-
createMatrixClass(f, "double", "Double", "Real")
90-
createMatrixClass(f, "float", "Float")
91-
createMatrixClass(f, "short", "Short")
92-
createMatrixClass(f, "ushort", "UShort")
93-
createMatrixClass(f, "long", "Long")
94-
createMatrixClass(f, "ulong", "ULong")
95-
createMatrixClass(f, "int", "Int")
96-
createMatrixClass(f, "uint", "UInt")
97-
}
98-
}
99-
100-
File(outputFile).apply {
101-
parentFile.mkdirs()
102-
writeText(f.text)
66+
fun matricesCodegen(outputFile: String): Unit = File(outputFile).run {
67+
parentFile.mkdirs()
68+
writer().use {
69+
it.appendLine("package space.kscience.kmath.gsl")
70+
it.appendLine()
71+
it.appendLine("import kotlinx.cinterop.*")
72+
it.appendLine("import org.gnu.gsl.*")
73+
it.appendLine("import space.kscience.kmath.misc.PerformancePitfall")
74+
it.appendLine("import space.kscience.kmath.structures.*")
75+
it.appendLine()
76+
it.createMatrixClass("double", "Double")
77+
it.createMatrixClass("float", "Float")
78+
it.createMatrixClass("short", "Short")
79+
it.createMatrixClass("ushort", "UShort")
80+
it.createMatrixClass("long", "Long")
81+
it.createMatrixClass("ulong", "ULong")
82+
it.createMatrixClass("int", "Int")
83+
it.createMatrixClass("uint", "UInt")
10384
}
10485
}

0 commit comments

Comments
 (0)