-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.gradle
More file actions
300 lines (254 loc) · 9.73 KB
/
Copy pathbuild.gradle
File metadata and controls
300 lines (254 loc) · 9.73 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
//
// SPDX-License-Identifier: MPL-2.0
import javax.inject.Inject
plugins {
id 'java'
id 'maven-publish'
}
interface InjectedExecOps {
@Inject
ExecOperations getExecOperations()
}
group = 'org-power-grid-model'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
def pgmVersion = "1.13.109"
def pgmDir = layout.buildDirectory.dir("pgm").get()
def wheelDir = pgmDir.dir("download")
def headersDir = pgmDir.dir("headers")
def nativeLibDir = pgmDir.dir("lib")
def generatedJavaDir = pgmDir.dir("generated-src")
def architectures = [
"x86_64_macosx": [
url: "https://github.com/PowerGridModel/power-grid-model/releases/download/v$pgmVersion/power_grid_model-$pgmVersion-py3-none-macosx_13_0_x86_64.whl",
wheelFile: wheelDir.file("power_grid_model_x86_64_macosx.whl"),
nativeLibPathInWheel: "power_grid_model/_core/power_grid_model_c/lib/libpower_grid_model_c.dylib",
nativeLibFile: nativeLibDir.file("power_grid_model_c_x86_64_macosx.dylib")
],
"arm64_macosx": [
url: "https://github.com/PowerGridModel/power-grid-model/releases/download/v$pgmVersion/power_grid_model-$pgmVersion-py3-none-macosx_13_0_arm64.whl",
wheelFile: wheelDir.file("power_grid_model_arm64_macosx.whl"),
nativeLibPathInWheel: "power_grid_model/_core/power_grid_model_c/lib/libpower_grid_model_c.dylib",
nativeLibFile: nativeLibDir.file("power_grid_model_c_arm64_macosx.dylib")
],
"x86_64_windows": [
url: "https://github.com/PowerGridModel/power-grid-model/releases/download/v$pgmVersion/power_grid_model-$pgmVersion-py3-none-win_amd64.whl",
wheelFile: wheelDir.file("power_grid_model_x86_64_windows.whl"),
nativeLibPathInWheel: "power_grid_model/_core/power_grid_model_c/bin/power_grid_model_c.dll",
nativeLibFile: nativeLibDir.file("power_grid_model_c_x86_64_windows.dll")
],
"arm64_linux": [
url: "https://github.com/PowerGridModel/power-grid-model/releases/download/v$pgmVersion/power_grid_model-$pgmVersion-py3-none-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl",
wheelFile: wheelDir.file("power_grid_model_arm64_linux.whl"),
nativeLibPathInWheel: "power_grid_model/_core/power_grid_model_c/lib64/libpower_grid_model_c.so",
nativeLibFile: nativeLibDir.file("power_grid_model_c_arm64_linux.so")
],
"x86_64_linux": [
url: "https://github.com/PowerGridModel/power-grid-model/releases/download/v$pgmVersion/power_grid_model-$pgmVersion-py3-none-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl",
wheelFile: wheelDir.file("power_grid_model_x86_64_linux.whl"),
nativeLibPathInWheel: "power_grid_model/_core/power_grid_model_c/lib64/libpower_grid_model_c.so",
nativeLibFile: nativeLibDir.file("power_grid_model_c_x86_64_linux.so")
]
]
tasks.register("extractPGMNativeLibsFromWheels") {
doLast {
nativeLibDir.file("version").asFile.write(pgmVersion)
}
}
architectures.forEach { slug, architecture ->
def downloadWheelTaskName = "downloadPGMWheel_$slug"
def extractNativeLibTaskName = "extractPGMNativeLibFromWheel_$slug"
tasks.named("extractPGMNativeLibsFromWheels") {
dependsOn(tasks.named(extractNativeLibTaskName))
}
tasks.register(downloadWheelTaskName) {
GString url = architecture.url
RegularFile wheelFile = architecture.wheelFile
outputs.file(wheelFile)
doLast {
def target = wheelFile.asFile
target.parentFile.mkdirs()
URI.create(url).toURL().withInputStream { input ->
target.withOutputStream { output ->
output << input
}
}
}
}
tasks.register(extractNativeLibTaskName, Copy) {
dependsOn(tasks.named(downloadWheelTaskName))
RegularFile wheelFile = architecture.wheelFile
RegularFile nativeLibFile = architecture.nativeLibFile
String nativeLibFilename = nativeLibFile.asFile.name
String nativeLibPathInWheel = architecture.nativeLibPathInWheel
// Ensure the target directory exists before extracting the library
doFirst {
nativeLibDir.asFile.mkdirs()
}
// Register the file as an output of the task to ensure Gradle tracks it correctly
outputs.file(nativeLibFile)
from(zipTree(wheelFile)) {
include nativeLibPathInWheel
eachFile { FileCopyDetails details ->
if (details.path == null) {
return
}
println(details.path)
details.path = nativeLibFilename
}
}
includeEmptyDirs false
into(nativeLibDir)
}
}
tasks.register("extractPGMHeaderFiles", Copy) {
dependsOn(tasks.named("extractPGMNativeLibFromWheel_x86_64_linux"))
RegularFile wheelFile = architectures.x86_64_linux.wheelFile
doFirst {
headersDir.asFile.mkdirs()
}
def internalBaseDir = "power_grid_model/_core/power_grid_model_c/include"
from(zipTree(wheelFile)) {
include "$internalBaseDir/**/*.h"
eachFile { FileCopyDetails details ->
if (details.path == null) {
return
}
details.path = details.path.replace("$internalBaseDir/", "")
}
}
includeEmptyDirs false
into headersDir
}
def jextractDir = layout.buildDirectory.dir("jextract").get()
def jextractArchive = jextractDir.file("jextract.tar.gz")
def osName = System.getProperty("os.name").toLowerCase()
def osArch = System.getProperty("os.arch").toLowerCase()
def jextractUrlPropertyKey = {
if (osName.contains("linux")) {
return osArch in ["aarch64", "arm64"]
? "jextract_linux_aarch64_url"
: "jextract_linux_x64_url"
}
if (osName.contains("mac")) {
return osArch in ["aarch64", "arm64"]
? "jextract_macos_aarch64_url"
: "jextract_macos_x64_url"
}
if (osName.contains("windows")) {
if (!(osArch in ["amd64", "x86_64"])) {
throw new GradleException(
"Unsupported Windows architecture: $osArch"
)
}
return "jextract_windows_x64_url"
}
throw new GradleException(
"Unsupported platform: $osName / $osArch"
)
}()
def jextractUrl = project.property(jextractUrlPropertyKey)
tasks.register("downloadJextract") {
outputs.file(jextractArchive)
doLast {
def target = jextractArchive.asFile
target.parentFile.mkdirs()
URI.create(jextractUrl).toURL().withInputStream { input ->
target.withOutputStream { output ->
output << input
}
}
}
}
tasks.register("extractJextract", Copy) {
dependsOn(tasks.named("downloadJextract"))
from tarTree(resources.gzip(jextractArchive.asFile))
into jextractDir
}
tasks.register("generatePGMBindings") {
// Use a local jextract installation by setting jextract_bin in gradle.properties,
// otherwise jextract is downloaded and extracted automatically.
def localJextractBin = project.findProperty("jextract_bin")
if (!localJextractBin) {
dependsOn(tasks.named('extractJextract'))
}
dependsOn(tasks.named('extractPGMNativeLibsFromWheels'))
dependsOn(tasks.named('extractPGMHeaderFiles'))
def execOps = project.objects.newInstance(InjectedExecOps)
inputs.files(
headersDir.file("power_grid_model_c.h"),
headersDir.file("power_grid_model_c/dataset_definitions.h")
)
outputs.dir(generatedJavaDir)
def jextractExecutables = fileTree(jextractDir) {
include "**/bin/*"
}
doLast {
def jextractBin
if (localJextractBin) {
jextractBin = file(localJextractBin)
} else {
def executableName = osName.contains("windows")
? "jextract.exe"
: "jextract"
def jextractBins = jextractExecutables.files.findAll() {
it.name == executableName
}
if (jextractBins.size() != 1) {
throw new GradleException(
"Expected exactly one Jextract executable, " +
"but found ${jextractBins.size()}: $jextractBins"
)
}
jextractBin = jextractBins.first()
}
execOps.execOperations.exec {
commandLine jextractBin,
"--output", generatedJavaDir,
"--target-package", "org.lfenergy.pgm",
"--header-class-name", "PowerGridModelC",
headersDir.file("power_grid_model_c.h").asFile,
headersDir.file(
"power_grid_model_c/dataset_definitions.h"
).asFile
}
}
}
sourceSets {
main {
java {
srcDir generatedJavaDir
}
resources {
srcDir nativeLibDir
}
}
}
tasks.named('compileJava') {
dependsOn(tasks.named('generatePGMBindings'))
}
tasks.named('processResources') {
dependsOn(tasks.named('generatePGMBindings'))
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifactId = 'lib-power-grid-model'
}
}
}
dependencies {
testImplementation platform('org.junit:junit-bom:6.0.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testImplementation platform('org.mockito:mockito-bom:5.23.0')
testRuntimeOnly 'org.mockito:mockito-core'
testImplementation 'org.mockito:mockito-junit-jupiter'
}
test {
useJUnitPlatform()
}