Skip to content
This repository was archived by the owner on Sep 1, 2022. It is now read-only.

Commit e4d72c9

Browse files
committed
Check that libtorch build-version matches Java version
Tested with valid libtorch, wrong libtorch version, and invalid libtorch directory.
1 parent dfaf4c1 commit e4d72c9

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

build.gradle

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ plugins {
55

66
def USE_NIGHTLY = System.getenv('USE_LIBTORCH_NIGHTLY')?.toBoolean()
77

8+
def LIBTORCH_VERSION = USE_NIGHTLY ? "1.5.0-SNAPSHOT" : "1.4.0";
9+
810
repositories {
911
jcenter()
1012

@@ -14,20 +16,26 @@ repositories {
1416
}
1517

1618
dependencies {
17-
if (USE_NIGHTLY) {
18-
implementation 'org.pytorch:pytorch_java_only:1.5.0-SNAPSHOT'
19-
} else {
20-
implementation 'org.pytorch:pytorch_java_only:1.4.0'
21-
}
19+
implementation "org.pytorch:pytorch_java_only:${LIBTORCH_VERSION}"
2220
}
2321

2422
def LIBTORCH_HOME = System.getenv('LIBTORCH_HOME')
2523
if (!LIBTORCH_HOME) {
2624
throw new RuntimeException('LIBTORCH_HOME not present in environment.');
2725
}
28-
if (!file(LIBTORCH_HOME).isDirectory()) {
29-
throw new RuntimeException('LIBTORCH_HOME does not refer to a directory.');
26+
def BUILD_VERSION_FILE = new File(LIBTORCH_HOME, "build-version");
27+
if (!BUILD_VERSION_FILE.isFile()) {
28+
throw new RuntimeException(
29+
"Cannot find ${BUILD_VERSION_FILE}. " +
30+
"Make sure LIBTORCH_HOME refers to the root of the libtorch distribution.");
3031
}
32+
def installedVersion = BUILD_VERSION_FILE.readLines();
33+
def versionPattern = "^" + java.util.regex.Pattern.quote(LIBTORCH_VERSION) + "\\b.*";
34+
if (!USE_NIGHTLY && !(installedVersion[0] ==~ versionPattern)) {
35+
throw new RuntimeException(
36+
"Found libtorch version ${installedVersion}, but build.gradle expects ${LIBTORCH_VERSION}.");
37+
}
38+
3139

3240
application {
3341
mainClassName = 'demo.App'

0 commit comments

Comments
 (0)