kmath/kmath-torch/build.gradle.kts

194 lines
5.2 KiB
Plaintext
Raw Normal View History

2020-12-30 01:42:33 +03:00
import de.undercouch.gradle.tasks.download.Download
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
2021-01-17 00:03:11 +03:00
import org.gradle.api.JavaVersion.VERSION_11
2020-12-30 01:42:33 +03:00
plugins {
2021-03-01 17:59:34 +03:00
id("ru.mipt.npm.gradle.mpp")
2020-12-30 01:42:33 +03:00
id("de.undercouch.download")
}
2021-01-17 00:03:11 +03:00
java {
sourceCompatibility = VERSION_11
targetCompatibility = VERSION_11
}
2020-12-30 01:42:33 +03:00
val home = System.getProperty("user.home")
2021-01-17 00:03:11 +03:00
val javaHome = System.getProperty("java.home")
2020-12-30 01:42:33 +03:00
val thirdPartyDir = "$home/.konan/third-party/kmath-torch-${project.property("version")}"
val cppBuildDir = "$thirdPartyDir/cpp-build"
2021-01-17 00:03:11 +03:00
val cppSources = projectDir.resolve("src/cppMain")
2020-12-30 01:42:33 +03:00
2021-01-05 00:25:04 +03:00
val cudaHome: String? = System.getenv("CUDA_HOME")
val cudaDefault = file("/usr/local/cuda").exists()
val cudaFound = cudaHome?.isNotEmpty() ?: false or cudaDefault
2020-12-30 01:42:33 +03:00
val cmakeArchive = "cmake-3.19.2-Linux-x86_64"
val torchArchive = "libtorch"
val cmakeCmd = "$thirdPartyDir/$cmakeArchive/bin/cmake"
val ninjaCmd = "$thirdPartyDir/ninja"
val downloadCMake by tasks.registering(Download::class) {
val tarFile = "$cmakeArchive.tar.gz"
src("https://github.com/Kitware/CMake/releases/download/v3.19.2/$tarFile")
dest(File(thirdPartyDir, tarFile))
overwrite(false)
}
val downloadNinja by tasks.registering(Download::class) {
src("https://github.com/ninja-build/ninja/releases/download/v1.10.2/ninja-linux.zip")
dest(File(thirdPartyDir, "ninja-linux.zip"))
overwrite(false)
}
val downloadTorch by tasks.registering(Download::class) {
2021-01-05 00:25:04 +03:00
val abiMeta = "$torchArchive-cxx11-abi-shared-with-deps-1.7.1%2B"
val cudaUrl = "https://download.pytorch.org/libtorch/cu110/${abiMeta}cu110.zip"
val cpuUrl = "https://download.pytorch.org/libtorch/cpu/${abiMeta}cpu.zip"
val url = if (cudaFound) cudaUrl else cpuUrl
src(url)
2020-12-30 01:42:33 +03:00
dest(File(thirdPartyDir, "$torchArchive.zip"))
overwrite(false)
}
val extractCMake by tasks.registering(Copy::class) {
dependsOn(downloadCMake)
from(tarTree(resources.gzip(downloadCMake.get().dest)))
into(thirdPartyDir)
}
val extractTorch by tasks.registering(Copy::class) {
dependsOn(downloadTorch)
from(zipTree(downloadTorch.get().dest))
into(thirdPartyDir)
}
val extractNinja by tasks.registering(Copy::class) {
dependsOn(downloadNinja)
from(zipTree(downloadNinja.get().dest))
into(thirdPartyDir)
}
val configureCpp by tasks.registering {
dependsOn(extractCMake)
dependsOn(extractNinja)
dependsOn(extractTorch)
onlyIf { !file(cppBuildDir).exists() }
doLast {
exec {
workingDir(thirdPartyDir)
commandLine("mkdir", "-p", cppBuildDir)
}
exec {
workingDir(cppBuildDir)
commandLine(
cmakeCmd,
2021-01-17 00:03:11 +03:00
cppSources,
2020-12-30 01:42:33 +03:00
"-GNinja",
"-DCMAKE_MAKE_PROGRAM=$ninjaCmd",
"-DCMAKE_PREFIX_PATH=$thirdPartyDir/$torchArchive",
2021-01-17 00:03:11 +03:00
"-DJAVA_HOME=$javaHome",
2020-12-30 01:42:33 +03:00
"-DCMAKE_BUILD_TYPE=Release"
)
}
}
}
val cleanCppBuild by tasks.registering {
onlyIf { file(cppBuildDir).exists() }
doLast {
exec {
workingDir(thirdPartyDir)
commandLine("rm", "-rf", cppBuildDir)
}
}
}
val buildCpp by tasks.registering {
dependsOn(configureCpp)
doLast {
exec {
workingDir(cppBuildDir)
commandLine(cmakeCmd, "--build", ".", "--config", "Release")
}
}
}
2021-01-17 00:03:11 +03:00
val generateJNIHeader by tasks.registering {
doLast {
exec {
2021-03-01 17:59:34 +03:00
workingDir(projectDir.resolve("src/jvmMain/java/space/kscience/kmath/torch"))
2021-01-17 00:03:11 +03:00
commandLine("$javaHome/bin/javac", "-h", cppSources.resolve("include") , "JTorch.java")
}
}
}
2020-12-30 01:42:33 +03:00
kotlin {
explicitApiWarning()
2021-01-17 00:03:11 +03:00
jvm {
withJava()
}
val nativeTarget = linuxX64("native")
2020-12-30 01:42:33 +03:00
nativeTarget.apply {
binaries {
all {
linkerOpts(
"-L$cppBuildDir",
"-Wl,-rpath=$cppBuildDir",
"-lctorch"
)
optimized = true
debuggable = false
2020-12-30 01:42:33 +03:00
}
}
}
val main by nativeTarget.compilations.getting {
cinterops {
val libctorch by creating {
2021-01-17 00:03:11 +03:00
includeDirs(cppSources.resolve("include"))
2020-12-30 01:42:33 +03:00
}
}
}
val test by nativeTarget.compilations.getting
sourceSets {
2021-01-17 00:03:11 +03:00
val commonMain by getting {
2020-12-30 01:42:33 +03:00
dependencies {
api(project(":kmath-core"))
}
}
2021-01-06 16:20:48 +03:00
2021-01-17 00:03:11 +03:00
val nativeMain by getting {
dependencies {
api(project(":kmath-core"))
}
2021-01-06 16:20:48 +03:00
}
2021-01-19 12:58:47 +03:00
val jvmMain by getting {
dependencies {
api(project(":kmath-core"))
}
}
2020-12-30 01:42:33 +03:00
}
}
2021-01-17 00:03:11 +03:00
val native: KotlinNativeTarget by kotlin.targets
tasks[native.compilations["main"].cinterops["libctorch"].interopProcessingTaskName]
2021-01-06 16:20:48 +03:00
.dependsOn(buildCpp)
2021-01-17 00:03:11 +03:00
tasks["jvmProcessResources"].dependsOn(buildCpp)
tasks {
withType<Test>{
2021-03-01 17:59:34 +03:00
systemProperty("java.library.path", cppBuildDir)
2021-01-17 00:03:11 +03:00
}
2021-01-20 17:01:45 +03:00
}
// No JS implementation
project.gradle.startParameter.excludedTaskNames.add("jsTest")
project.gradle.startParameter.excludedTaskNames.add("jsBrowserTest")