1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.kotlinxSerialization)
`maven-publish`
id("org.jetbrains.dokka") version "2.1.0"
id("com.gradleup.shadow") version "9.2.2"
}
group = "com.dropbear"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
val hostOs = providers.systemProperty("os.name").get()
val isArm64 = providers.systemProperty("os.arch").map { it == "aarch64" }.get()
val isMingwX64 = hostOs.startsWith("Windows")
val isLinux = hostOs == "Linux"
val isMacOs = hostOs == "Mac OS X"
val libName = when {
isMacOs -> "libeucalyptus_core.dylib"
isLinux -> "libeucalyptus_core.so"
isMingwX64 -> "eucalyptus_core.dll"
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
val libPathProvider = provider {
val candidates = listOf(
layout.projectDirectory.file("target/debug/$libName").asFile,
layout.projectDirectory.file("target/release/$libName").asFile,
layout.projectDirectory.file("libs/$libName").asFile
)
val foundFile = candidates.firstOrNull { it.exists() }
if (foundFile != null) {
foundFile.absolutePath
} else {
println("No Rust library exists")
""
}
}
kotlin {
jvm { }
val nativeTarget = when {
isMacOs && isArm64 -> macosArm64("nativeLib")
isMacOs && !isArm64 -> macosX64("nativeLib")
isLinux && isArm64 -> linuxArm64("nativeLib")
isLinux && !isArm64 -> linuxX64("nativeLib")
isMingwX64 -> mingwX64("nativeLib")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
val nativeLibPathRaw = libPathProvider.get()
if (nativeLibPathRaw.isNotBlank()) {
val nativeLibPath = file(nativeLibPathRaw)
val nativeLibDir = nativeLibPath.parentFile.absolutePath
val nativeLibFileName = nativeLibPath.name
val nativeLibNameForLinking = when {
isMacOs -> nativeLibFileName.removePrefix("lib").removeSuffix(".dylib")
isLinux -> nativeLibFileName.removePrefix("lib").removeSuffix(".so")
isMingwX64 -> nativeLibFileName.removeSuffix(".dll")
else -> throw GradleException("Unsupported OS for library name derivation.")
}
nativeTarget.apply {
compilations.getByName("main") {
cinterops {
val dropbear by creating {
defFile(project.file("scripting/dropbear.def"))
includeDirs.headerFilterOnly(project.file("include"))
compilerOpts("-I${project.file("include").absolutePath}")
}
}
}
binaries {
sharedLib {
baseName = "dropbear"
if (isLinux || isMacOs) {
linkerOpts("-L$nativeLibDir", "-l$nativeLibNameForLinking", "-Wl,-rpath,\\\$ORIGIN")
} else if (isMingwX64) {
val importLibName = "$nativeLibNameForLinking.dll.lib"
val importLibPath = file("$nativeLibDir/$importLibName").absolutePath
linkerOpts(importLibPath)
}
}
}
}
} else {
println("Skipping native target configuration due to missing library path.")
nativeTarget.apply {
compilations.getByName("main") {
cinterops {
val dropbear by creating {
defFile(project.file("scripting/dropbear.def"))
includeDirs.headerFilterOnly(project.file("include"))
}
}
}
}
}
sourceSets {
commonMain {
kotlin.srcDirs("scripting/commonMain/kotlin")
dependencies {
api("org.jetbrains.kotlinx:kotlinx-datetime:0.7.0")
}
}
nativeMain {
kotlin.srcDirs("scripting/nativeMain/kotlin")
dependencies {
implementation(libs.kotlinxSerializationJson)
}
}
jvmMain {
kotlin.srcDirs("scripting/jvmMain/kotlin", "scripting/jvmMain/java")
dependencies {
}
}
}
java {
sourceSets.getByName("jvmMain") {
java.srcDirs("scripting/jvmMain/java")
}
}
targets.all {
compilations.all {
compileTaskProvider.configure {
compilerOptions {
freeCompilerArgs.add("-Xexpect-actual-classes")
}
}
}
}
}
tasks.register<JavaCompile>("generateJniHeaders") {
val outputDir = layout.buildDirectory.dir("generated/jni-include")
options.headerOutputDirectory.set(outputDir.get().asFile)
destinationDirectory.set(layout.buildDirectory.dir("classes/java/jni"))
classpath = files(
tasks.named("compileKotlinJvm"),
)
source = fileTree("scripting/jvmMain/java") {
include("**/*.java")
}
dependsOn("compileKotlinJvm")
doFirst {
val javaFiles = source.files
if (javaFiles.isEmpty()) {
println("WARNING: No Java files found in scripting/jvmMain/kotlin for JNI header generation")
} else {
println("Generating JNI include for ${javaFiles.size} Java files:")
javaFiles.forEach { println(" - ${it.name}") }
}
}
doLast {
val headerDir = outputDir.get().asFile
val headers = headerDir.listFiles()?.filter { it.extension == "h" } ?: emptyList()
println("Generated ${headers.size} JNI include:")
headers.forEach { println(" - ${it.name}") }
}
}
publishing {
repositories {
maven {
name = "GitHubPages"
url = uri(layout.buildDirectory.dir("repo"))
}
}
publications.withType<MavenPublication> {
pom {
name.set("dropbear")
description.set("The dropbear scripting part of the engine... uhh yeah!")
url.set("https://github.com/tirbofish/dropbear")
licenses {
license {
name.set("dropbear engine License, Version 1.2")
url.set("https://raw.githubusercontent.com/tirbofish/dropbear/refs/heads/main/LICENSE.md")
}
}
developers {
developer {
id.set("tirbofish")
name.set("tk")
email.set("4tkbytes@pm.me")
}
}
scm {
url.set("https://github.com/tirbofish/dropbear")
connection.set("scm:git:git://github.com/tirbofish/dropbear.git")
developerConnection.set("scm:git:ssh://git@github.com/tirbofish/dropbear.git")
}
}
}
}