Skip to content

Instantly share code, notes, and snippets.

@bric3
Last active November 4, 2020 08:31
Show Gist options
  • Save bric3/a08dbb6d76c8f947e53d55b94236092a to your computer and use it in GitHub Desktop.
Save bric3/a08dbb6d76c8f947e53d55b94236092a to your computer and use it in GitHub Desktop.
Gradle script that downloads prebuilt libsodium for macOS or Linux
apply plugin: 'de.undercouch.download'
OperatingSystem os = org.gradle.nativeplatform.platform.internal.DefaultNativePlatform.currentOperatingSystem;
if(os.isLinux()) {
ext {
debFileName = "libsodium-dev_1.0.17-1_amd64.deb"
debSha256 = "f72e5b1e3a716154c284d98969bb698701daa30b02d755a78d10d460c582d48b"
}
task downloadLibsodiumDeb(type: Download) {
description "Download Libsodium Debian package into $buildDir directory"
// link found via
// https://packages.debian.org/stable/libsodium23 => 1.0.17
// https://packages.debian.org/unstable/libsodium23 => 1.0.18
src "http://ftp.fr.debian.org/debian/pool/main/libs/libsodium/libsodium23_1.0.18-1_amd64.deb"
// src "http://ftp.fr.debian.org/debian/pool/main/libs/libsodium/libsodium23_1.0.17-1_amd64.deb"
dest new File(buildDir, debFileName)
onlyIfModified true
}
task verifyLibsodiumDeb(type: Verify, dependsOn: downloadLibsodiumDeb) {
src downloadLibsodiumDeb.dest
algorithm 'SHA-256'
checksum debSha256
}
downloadLibsodiumDeb.finalizedBy verifyLibsodiumDeb
task unpackLibsodium(dependsOn: downloadLibsodiumDeb) {
description "Extract Libsodium Debian package file into $buildDir/libsodium directory"
inputs.property('libFilename', 'libsodium.so.23.3.0')
inputs.property('debFile', downloadLibsodiumDeb.dest)
outputs.file("${buildDir}/libsodium/${inputs.properties.libFilename}").withPropertyName('libraryFile')
// Using command line as gradle doesn't support some compression algorithm like LZMA (used for tar.xz)
// https://github.com/gradle/gradle/issues/15065
doLast {
mkdir("${buildDir}/libsodium")
exec {
commandLine('sh', '-c', "tar xJfO ${inputs.properties.debFile} data.tar.xz | tar xJ --strip-components=4 -C ${buildDir}/libsodium usr/lib/x86_64-linux-gnu/${inputs.properties.libFilename}")
}
}
}
}
if(os.isMacOsX()) {
ext {
bottleFileName = "libsodium-1.0.18_1.catalina.bottle.tar.gz"
bottleSha256 = "db372521cd0b1861a5b578bee22426f3a1f4f7cb3c382be1f842da4715dc65bd"
}
task downloadLibsodiumBrewBottle(type: Download) {
description "Download Libsodium bottle package into $buildDir directory"
// link found via https://formulae.brew.sh/formula/libsodium#default
// https://formulae.brew.sh/api/formula/libsodium.json => 1.0.18
src "https://homebrew.bintray.com/bottles/libsodium-1.0.18_1.catalina.bottle.tar.gz"
dest new File(buildDir, bottleFileName)
onlyIfModified true
}
task verifyLibsodiumBrewBottle(type: Verify, dependsOn: downloadLibsodiumBrewBottle) {
src downloadLibsodiumBrewBottle.dest
algorithm 'SHA-256'
checksum bottleSha256
}
downloadLibsodiumBrewBottle.finalizedBy verifyLibsodiumBrewBottle
task unpackLibsodium(type: Copy, dependsOn: downloadLibsodiumBrewBottle) {
description "Extract Libsodium bottle tar file into $buildDir/libsodium directory"
inputs.property('libFilename', 'libsodium.23.dylib')
inputs.property('debFile', downloadLibsodiumBrewBottle.dest)
outputs.file("${buildDir}/libsodium/${inputs.properties.libFilename}").withPropertyName("libraryFile")
from (tarTree(resources.gzip(downloadLibsodiumBrewBottle.dest))) {
include("libsodium/1.0.18_1/lib/${inputs.properties.libFilename}")
eachFile { fcd -> fcd.relativePath = new RelativePath(true, fcd.relativePath.segments.drop(3)) }
includeEmptyDirs false
}
into "${buildDir}/libsodium"
}
}
test.dependsOn unpackLibsodium
build.dependsOn unpackLibsodium
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment