Skip to content

Commit

Permalink
cleanup and arm64
Browse files Browse the repository at this point in the history
Signed-off-by: Martmists <[email protected]>
  • Loading branch information
Martmists-GH committed Jun 2, 2024
1 parent 1c4afdf commit 7bc4def
Show file tree
Hide file tree
Showing 30 changed files with 80 additions and 62 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ Enable the plugin in your build.gradle.kts file:
```kotlin
plugins {
kotlin("multiplatform") version "2.0.0"
id("com.martmists.kpy.kpy-plugin") version "1.0.0"
id("com.martmists.kpy.kpy-plugin") version "1.0.1"
}

kotlin {
val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
val isArm64 = System.getProperty("os.arch") == "aarch64"
// You can rename the target from `native` to something else,
// but make sure to also change setup.py to match this change!
// ARM targets are currently unsupported
val nativeTarget = when {
hostOs == "Mac OS X" -> macosX64("native")
hostOs == "Linux" -> linuxX64("native")
hostOs == "Mac OS X" && !isArm64 -> macosX64("native")
hostOs == "Linux" && !isArm64 -> linuxX64("native")
hostOs == "Mac OS X" && isArm64 -> macosArm64("native")
hostOs == "Linux" && isArm64 -> linuxArm64("native")
isMingwX64 -> mingwX64("native")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repositories {

allprojects {
group = "com.martmists.kpy"
version = "1.0.0"
version = "1.0.1"

tasks.withType<DependencyUpdatesTask> {
rejectVersionIf {
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/DownloadPythonTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.gradle.kotlin.dsl.*
import java.io.File

abstract class DownloadPythonTask : DefaultTask() {
@Suppress("EnumEntryName")
enum class Version(val str: String) {
Python3_9("3.9"),
Python3_10("3.10"),
Expand Down
5 changes: 0 additions & 5 deletions buildSrc/src/main/kotlin/Versions.kt

This file was deleted.

3 changes: 1 addition & 2 deletions kpy-library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ kotlin {
val extractSourcesTask = tasks.register("extractPython${targetName.capitalized()}", Exec::class) {
dependsOn(downloadSourcesTask)

val outDir =
project.layout.buildDirectory.dir("python-${pythonVersion.str}-${targetName}").get().asFile
val outDir = project.layout.buildDirectory.dir("python-${pythonVersion.str}-${targetName}").get().asFile

executable = "tar"
args(
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ package kpy.annotations
*/
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
@Suppress("unused")
annotation class PyDictClass
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ package kpy.annotations
*/
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS, AnnotationTarget.PROPERTY)
@Retention(AnnotationRetention.SOURCE)
@Suppress("unused")
annotation class PyExport(val name: String = "", val priority: Int = 9999)
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ package kpy.annotations
*/
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.SOURCE)
@Suppress("unused")
annotation class PyMagic(val key: PyMagicMethod)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kpy.annotations

// See: https://docs.python.org/3/c-api/typeobj.html
@Suppress("unused")
enum class PyMagicMethod {
// tp
TP_GETATTRO,
Expand Down
1 change: 1 addition & 0 deletions kpy-library/src/commonMain/kotlin/kpy/builders/module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.cinterop.nativeHeap
import kpy.internals.PyModuleDef_HEAD_INIT
import python.*

@Suppress("LocalVariableName")
fun makeModule(
km_name: String,
km_methods: List<CValue<PyMethodDef>>? = null,
Expand Down
2 changes: 2 additions & 0 deletions kpy-library/src/commonMain/kotlin/kpy/builders/type.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import python.*

val Py_TPFLAGS_DEFAULT = Py_TPFLAGS_HAVE_STACKLESS_EXTENSION.toULong() or Py_TPFLAGS_HAVE_VERSION_TAG.toULong()

@Suppress("unused", "LocalVariableName")
inline fun <reified T> makePyType(
ktp_dealloc: destructor? = null,
ktp_vectorcall_offset: Py_ssize_t = 0,
Expand Down Expand Up @@ -86,6 +87,7 @@ inline fun <reified T> makePyType(
ktp_has_dictoffset
)

@Suppress("RemoveRedundantCallsOfConversionMethods", "LocalVariableName")
fun makePyType(
name: String,
ktp_dealloc: destructor? = null,
Expand Down
1 change: 0 additions & 1 deletion kpy-library/src/commonMain/kotlin/kpy/ext/ktypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package kpy.ext
import kotlinx.cinterop.*
import kpy.wrappers.PyObjectT
import python.*
import kotlin.native.ref.WeakReference

val CPointer<PyObject>?.kt
get(): CPointer<KtPyObject> = this!!.reinterpret()
Expand Down
1 change: 1 addition & 0 deletions kpy-library/src/commonMain/kotlin/kpy/ext/method_defs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kpy.wrappers.PyMethodKwargsT
import kpy.wrappers.PyMethodT
import python.*

@Suppress("unused")
inline fun FuncPtr<PyMethodT>.pydef(
name: String,
doc: String,
Expand Down
2 changes: 1 addition & 1 deletion kpy-library/src/commonMain/kotlin/kpy/ext/ops.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ operator fun PyObjectT.rem(other: PyObjectT) = PyNumber_Remainder(this, other)
operator fun PyObjectT.rem(other: Any) = this.rem(other.toPython())

operator fun PyObjectT.get(key: PyObjectT) = PyObject_GetItem(this, key)
operator fun PyObjectT.get(key: Any) = this.get(key.toPython())
operator fun PyObjectT.get(key: Any) = this[key.toPython()]

operator fun PyObjectT.set(key: PyObjectT, value: PyObjectT) = PyObject_SetItem(this, key, value)
operator fun PyObjectT.set(key: PyObjectT, value: Any) = this.set(key, value.toPython())
Expand Down
2 changes: 2 additions & 0 deletions kpy-library/src/commonMain/kotlin/kpy/internals/macros.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("FunctionName")

package kpy.internals

import kotlinx.cinterop.CPointer
Expand Down
8 changes: 4 additions & 4 deletions kpy-library/src/commonMain/kotlin/kpy/utilities/convert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal val typeMap = mutableMapOf<KType, PyTypeObject>()
internal val instanceMap = mutableMapOf<Any, PyObjectT>()

// Avoid using these
@Suppress("FunctionName")
inline fun <reified K> PyTypeObject._registerType() : PyTypeObject {
typeMap[typeOf<K>()] = this
return this
Expand All @@ -29,7 +30,7 @@ fun Any.forget() {
inline fun <reified R : Any> PyObjectT.toKotlin() : R = toKotlin(typeOf<R>())
inline fun <reified R : Any> PyObjectT.toKotlinNullable() : R? = toKotlinNullable(typeOf<R>())

@Suppress("IMPLICIT_CAST_TO_ANY", "UNCHECKED_CAST")
@Suppress("UNCHECKED_CAST", "DuplicatedCode")
fun <R : Any> PyObjectT.toKotlin(type: KType?) : R {
val clazz = type?.classifier

Expand Down Expand Up @@ -178,14 +179,12 @@ fun <R : Any> PyObjectT.toKotlin(type: KType?) : R {
} as R
}

@Suppress("IMPLICIT_CAST_TO_ANY", "UNCHECKED_CAST")
fun <R : Any> PyObjectT.toKotlinNullable(type: KType?) : R? {
return if (type?.isMarkedNullable == true && this == Py_None) null else toKotlin(type)
return if (type?.isMarkedNullable == true && this === Py_None) null else toKotlin(type)
}

inline fun <reified T> T.toPython() = toPython(typeOf<T>())

@Suppress("UNNECESSARY_NOT_NULL_ASSERTION") // False positive
fun <T> T.toPython(type: KType) : PyObjectT {
return when (this) {
null, is Unit -> Py_None.incref()
Expand Down Expand Up @@ -259,6 +258,7 @@ fun <T> T.toPython(type: KType) : PyObjectT {
tuple
}
is CPointer<*> -> {
@Suppress("UNCHECKED_CAST")
this as PyObjectT
}
else -> {
Expand Down
2 changes: 1 addition & 1 deletion kpy-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
id("com.github.gmazzo.buildconfig")
}

buildDir = file("../build/kpy-plugin")
layout.buildDirectory = rootProject.layout.buildDirectory.dir("kpy-plugin")

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ abstract class KPyExtension {
// Name of the root native package
abstract val moduleName: Property<String?>

@Suppress("unused")
fun metadata(key: String, value: String) {
props.put(key, value)
}

@Suppress("unused")
fun metadata(vararg entries: Pair<String, String>) {
props.putAll(entries.toMap())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

@Suppress("unused")
open class KPyPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ open class MetadataTask : DefaultTask() {
|project_name = "${project.name}"
|module_name = "${ext.moduleName.get() ?: project.name}"
|project_version = "${project.version}"
|build_dir = "${project.buildDir.absolutePath}"
|build_dir = "${project.layout.buildDirectory.get().asFile.absolutePath}"
|target = "$targetName"
|has_stubs = ${if (ext.generateStubs.get()) "True" else "False"}
${ext.props.get().map { (key, value) -> "|$key = $value" }.joinToString { "\n" }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.martmists.kpy.plugin

@Suppress("unused")
enum class PythonVersion(val value: String) {
Py39("3.9"),
Py310("3.10"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.martmists.kpy.processor.generation.KPyStubGenerator
@AutoService(SymbolProcessor::class)
class KPySymbolProcessor(private val projectName: String, private val generateStubs: Boolean, private val codeGenerator: CodeGenerator) : SymbolProcessor {
override fun process(resolver: Resolver): List<KSAnnotated> {
val modules = KPyCodeAnalyzer(projectName, codeGenerator).collect(resolver)
val modules = KPyCodeAnalyzer().collect(resolver)

val rootModule = modules.all().filter {
it.isRoot()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.processing.SymbolProcessorProvider
import com.martmists.kpy.processor.ext.toSnakeCase

@Suppress("unused")
@AutoService(SymbolProcessorProvider::class)
class KPySymbolProcessorProvider : SymbolProcessorProvider {
override fun create(environment: SymbolProcessorEnvironment): KPySymbolProcessor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.martmists.kpy.processor.analysis

import com.google.devtools.ksp.getAllSuperTypes
import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.*
import com.martmists.kpy.processor.collected.KPyClass
import com.martmists.kpy.processor.collected.KPyFunction
import com.martmists.kpy.processor.collected.KPyProperty

class KPyCodeAnalyzer(private val projectName: String, private val codeGenerator: CodeGenerator) {
class KPyCodeAnalyzer {
fun collect(resolver: Resolver) : KPyModuleCache {
val modules = KPyModuleCache()

Expand Down Expand Up @@ -52,28 +51,28 @@ class KPyCodeAnalyzer(private val projectName: String, private val codeGenerator

private fun KSClassDeclaration.asKPy() : KPyClass {
val funcs = getAllFunctions().filter {
it.parentDeclaration == this && it.annotations.any {
it.shortName.asString() == "PyExport"
it.parentDeclaration == this && it.annotations.any { ann ->
ann.shortName.asString() == "PyExport"
}
}

val magic = getAllFunctions().filter {
it.parentDeclaration == this && it.annotations.any {
it.shortName.asString() == "PyMagic"
it.parentDeclaration == this && it.annotations.any { ann ->
ann.shortName.asString() == "PyMagic"
}
}

val props = getAllProperties().filter {
it.parentDeclaration == this && it.annotations.any {
it.shortName.asString() == "PyHint"
it.parentDeclaration == this && it.annotations.any { ann ->
ann.shortName.asString() == "PyHint"
}
}

val parent = this.getAllSuperTypes().firstOrNull() {
val parent = this.getAllSuperTypes().firstOrNull {
val decl = it.declaration
decl is KSClassDeclaration &&
decl.classKind == ClassKind.CLASS && decl.annotations.any {
it.shortName.asString() == "PyExport"
decl.classKind == ClassKind.CLASS && decl.annotations.any { ann ->
ann.shortName.asString() == "PyExport"
}
}?.declaration as? KSClassDeclaration

Expand Down Expand Up @@ -136,11 +135,12 @@ class KPyCodeAnalyzer(private val projectName: String, private val codeGenerator
val byName = mutableMapOf<String, T>()
val byIndex = mutableMapOf<Int, T>()

arguments.forEachIndexed { index, argument ->
@Suppress("UNCHECKED_CAST")
arguments.forEachIndexed { idx, argument ->
if (argument.name != null) {
byName[argument.name!!.asString()] = argument.value as T
} else {
byIndex[index] = argument.value as T
byIndex[idx] = argument.value as T
}
}

Expand Down
Loading

0 comments on commit 7bc4def

Please sign in to comment.