-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
130 lines (117 loc) · 4.32 KB
/
build.gradle.kts
File metadata and controls
130 lines (117 loc) · 4.32 KB
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
import java.net.URI
plugins {
java
}
val slangVersion: String by project
fun slangPlatform(): String {
val os = System.getProperty("os.name", "").lowercase()
val arch = System.getProperty("os.arch", "").lowercase()
val osName = when {
"linux" in os -> "linux"
"mac" in os || "darwin" in os -> "macos"
"win" in os -> "windows"
else -> error("Unsupported OS: $os")
}
val archName = when (arch) {
"amd64", "x86_64" -> "x86_64"
"aarch64", "arm64" -> "aarch64"
else -> error("Unsupported arch: $arch")
}
return "$osName-$archName"
}
val slangNativesDir = layout.buildDirectory.dir("slang-natives")
val downloadSlang by tasks.registering {
val outputDir = slangNativesDir.get().asFile
val marker = File(outputDir, ".slang-$slangVersion")
outputs.dir(outputDir)
onlyIf { !marker.exists() }
doLast {
val platform = slangPlatform()
val archiveName = "slang-$slangVersion-$platform"
val url = "https://github.com/shader-slang/slang/releases/download/v$slangVersion/$archiveName.zip"
val zipFile = File(outputDir, "$archiveName.zip")
outputDir.mkdirs()
logger.lifecycle("Downloading Slang $slangVersion for $platform...")
val uri = URI.create(url)
uri.toURL().openStream().use { input: java.io.InputStream ->
zipFile.outputStream().use { output -> input.copyTo(output) }
}
logger.lifecycle("Extracting...")
copy {
from(zipTree(zipFile))
into(outputDir)
}
zipFile.delete()
// Zip doesn't preserve symlinks — fix up stub files that should point to versioned .so files
val libDir = File(outputDir, "lib")
if (libDir.exists()) {
libDir.listFiles()?.filter { it.length() < 200 && it.name.endsWith(".so") }?.forEach { stub ->
val target = stub.readText().trim()
val targetFile = File(libDir, target)
if (targetFile.exists() && targetFile != stub) {
stub.delete()
targetFile.copyTo(stub)
logger.lifecycle(" Fixed symlink: ${stub.name} -> $target")
}
}
}
marker.createNewFile()
}
}
val slangLibPath: String
get() {
val platform = slangPlatform()
val base = slangNativesDir.get().asFile
// Archive extracts to slang-<version>-<platform>/lib/
val libDir = File(base, "slang-$slangVersion-$platform/lib")
if (libDir.exists()) return libDir.absolutePath
// Fallback: check for lib/ directly
val fallback = File(base, "lib")
if (fallback.exists()) return fallback.absolutePath
return libDir.absolutePath
}
subprojects {
apply(plugin = "java")
apply(plugin = "maven-publish")
afterEvaluate {
extensions.configure<PublishingExtension> {
publications {
create<MavenPublication>("maven") {
from(components["java"])
groupId = project.group.toString()
artifactId = project.name
version = project.version.toString()
}
}
}
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(property("javaVersion").toString().toInt()))
}
}
repositories {
mavenCentral()
}
tasks.withType<JavaCompile> {
options.compilerArgs.addAll(listOf("--enable-preview"))
}
tasks.withType<Test> {
dependsOn(downloadSlang)
useJUnitPlatform()
jvmArgs("--enable-preview", "--enable-native-access=ALL-UNNAMED",
"-Djava.library.path=$slangLibPath")
val envPath = System.getenv("LD_LIBRARY_PATH") ?: ""
val fullPath = if (envPath.isNotEmpty()) "$slangLibPath:$envPath" else slangLibPath
environment("LD_LIBRARY_PATH", fullPath)
// Fork per class to isolate native library lifecycle
forkEvery = 1
}
tasks.withType<JavaExec> {
dependsOn(downloadSlang)
jvmArgs("-Djava.library.path=$slangLibPath")
val envPath = System.getenv("LD_LIBRARY_PATH") ?: ""
val fullPath = if (envPath.isNotEmpty()) "$slangLibPath:$envPath" else slangLibPath
environment("LD_LIBRARY_PATH", fullPath)
}
}