kmath/kmath-noa/build.gradle.kts

134 lines
4.0 KiB
Plaintext
Raw Normal View History

2021-06-26 00:36:01 +03:00
/*
* Copyright 2018-2021 KMath contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
import de.undercouch.gradle.tasks.download.Download
plugins {
kotlin("jvm")
id("ru.mipt.npm.gradle.common")
id("de.undercouch.download")
}
description = "Wrapper for the Bayesian Computation library NOA on top of LibTorch"
dependencies {
implementation(project(":kmath-tensors"))
2021-06-26 00:36:01 +03:00
}
2021-06-27 18:28:28 +03:00
val home: String = System.getProperty("user.home")
val javaHome: String = System.getProperty("java.home")
2021-06-26 00:36:01 +03:00
val thirdPartyDir = "$home/.konan/third-party/kmath-noa-${project.property("version")}"
val cppBuildDir = "$thirdPartyDir/cpp-build"
val cppSources = projectDir.resolve("src/main/cpp")
val cudaHome: String? = System.getenv("CUDA_HOME")
val cudaDefault = file("/usr/local/cuda").exists()
val cudaFound = cudaHome?.isNotEmpty() ?: false or cudaDefault
2021-06-28 00:12:02 +03:00
val cmakeArchive = "cmake-3.20.5-linux-x86_64"
2021-06-26 00:36:01 +03:00
val torchArchive = "libtorch"
2021-06-28 00:12:02 +03:00
val cmakeCmd = "$thirdPartyDir/cmake/$cmakeArchive/bin/cmake"
val ninjaCmd = "$thirdPartyDir/ninja/ninja"
val generateJNIHeader by tasks.registering {
println(cmakeCmd)
doLast {
exec {
workingDir(projectDir.resolve("src/main/java/space/kscience/kmath/noa"))
commandLine("$javaHome/bin/javac", "-h", cppSources , "JNoa.java")
}
}
}
2021-06-26 00:36:01 +03:00
val downloadCMake by tasks.registering(Download::class) {
val tarFile = "$cmakeArchive.tar.gz"
src("https://github.com/Kitware/CMake/releases/download/v3.20.5/$tarFile")
2021-06-28 00:12:02 +03:00
dest(File("$thirdPartyDir/cmake", tarFile))
2021-06-26 00:36:01 +03:00
overwrite(false)
}
val downloadNinja by tasks.registering(Download::class) {
src("https://github.com/ninja-build/ninja/releases/download/v1.10.2/ninja-linux.zip")
2021-06-28 00:12:02 +03:00
dest(File("$thirdPartyDir/ninja", "ninja-linux.zip"))
2021-06-26 00:36:01 +03:00
overwrite(false)
}
val downloadTorch by tasks.registering(Download::class) {
val torchVersion = "$torchArchive-shared-with-deps-1.9.0%2B"
val cudaUrl = "https://download.pytorch.org/libtorch/cu111/${torchVersion}cu111.zip"
val cpuUrl = "https://download.pytorch.org/libtorch/cpu/${torchVersion}cpu.zip"
val url = if (cudaFound) cudaUrl else cpuUrl
src(url)
2021-06-28 00:12:02 +03:00
dest(File("$thirdPartyDir/torch", "$torchArchive.zip"))
2021-06-26 00:36:01 +03:00
overwrite(false)
}
val extractCMake by tasks.registering(Copy::class) {
dependsOn(downloadCMake)
from(tarTree(resources.gzip(downloadCMake.get().dest)))
2021-06-28 00:12:02 +03:00
into("$thirdPartyDir/cmake")
2021-06-26 00:36:01 +03:00
}
val extractNinja by tasks.registering(Copy::class) {
dependsOn(downloadNinja)
from(zipTree(downloadNinja.get().dest))
2021-06-28 00:12:02 +03:00
into("$thirdPartyDir/ninja")
2021-06-26 00:36:01 +03:00
}
val extractTorch by tasks.registering(Copy::class) {
dependsOn(downloadTorch)
from(zipTree(downloadTorch.get().dest))
2021-06-28 00:12:02 +03:00
into("$thirdPartyDir/torch")
}
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,
cppSources,
"-GNinja",
"-DCMAKE_MAKE_PROGRAM=$ninjaCmd",
"-DCMAKE_PREFIX_PATH=$thirdPartyDir/torch/$torchArchive",
"-DJAVA_HOME=$javaHome",
"-DCMAKE_BUILD_TYPE=Release",
"-DBUILD_NOA_CUDA=${if(!cudaFound) "ON" else "OFF"}"
)
}
}
}
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")
}
}
}
tasks["compileJava"].dependsOn(buildCpp)