/
build.gradle
121 lines (102 loc) · 4.78 KB
/
build.gradle
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
import org.apache.commons.compress.archivers.tar.TarArchiveEntry
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream
import org.apache.commons.compress.utils.IOUtils
import org.apache.commons.io.FileUtils
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.attribute.FileTime
import java.nio.file.attribute.PosixFilePermission
buildscript {
dependencies {
classpath "org.apache.commons:commons-compress:1.16.1"
classpath "commons-io:commons-io:2.6"
classpath "org.tukaani:xz:1.0"
}
repositories {
mavenCentral()
}
}
class DownloadToolChainTask extends DefaultTask {
@Input // URL is assumed to always point at the same file
final Property<URL> fromUrl = project.objects.property(URL)
@OutputFile
final RegularFileProperty outputFile = project.objects.fileProperty()
@TaskAction
private void download() {
IOUtils.copy(fromUrl.get().newInputStream(), outputFile.get().asFile.newOutputStream())
}
}
class ExtractToolChainTask extends DefaultTask {
@InputFile
final RegularFileProperty toolChainArchive = project.objects.fileProperty()
@OutputDirectory
final DirectoryProperty toolChainLocation = project.objects.directoryProperty()
@TaskAction
private void extract() {
FileUtils.deleteDirectory(toolChainLocation.get().asFile)
InputStream fileStream = Files.newInputStream(toolChainArchive.get().asFile.toPath());
TarArchiveInputStream tarStream = new TarArchiveInputStream(createDecompressStream(fileStream))
File outputDir = toolChainLocation.asFile.get()
try {
for (TarArchiveEntry entry = tarStream.getNextTarEntry(); entry != null; entry = tarStream.getNextTarEntry()) {
String entryPath = entry.getName().split("[\\/]").drop(1).join("/")
final File file = new File(outputDir, entryPath)
if (entry.isDirectory()) {
continue
}
file.getParentFile().mkdirs()
if (entry.isSymbolicLink()) {
Files.createSymbolicLink(file.toPath(), Paths.get(entry.getLinkName()))
} else if (entry.isFile()) {
OutputStream outStream = file.newOutputStream()
IOUtils.copy(tarStream, outStream)
IOUtils.closeQuietly(outStream)
Files.setLastModifiedTime(file.toPath(), FileTime.from(entry.getLastModifiedDate().toInstant()))
file.setExecutable((entry.getMode() & toModeBit(PosixFilePermission.OWNER_EXECUTE)) > 0, true)
} else {
continue
}
}
} catch (EOFException ex) {
// Ignore EOF exception
}
IOUtils.closeQuietly(tarStream)
}
private InputStream createDecompressStream(InputStream inputStream) {
String archiveName = toolChainArchive.get().asFile.name
if (archiveName.endsWith('.tar.xz')) {
return new XZCompressorInputStream(inputStream)
} else if (archiveName.endsWith('.tar.gz')) {
return new GzipCompressorInputStream(inputStream)
}
throw new IllegalArgumentException('Tool chain archive uses unsupported compression')
}
private static int toModeBit(PosixFilePermission permission) {
return 1 << permission.ordinal()
}
}
def downloadClangToolChain = tasks.register("downloadClangToolChain", DownloadToolChainTask) {
String osName = System.getProperty('os.name').toLowerCase()
if (osName.contains('mac')) {
fromUrl = new URL("https://releases.llvm.org/5.0.1/clang+llvm-5.0.1-x86_64-apple-darwin.tar.xz")
} else if (osName.contains('linux')) {
fromUrl = new URL("https://releases.llvm.org/5.0.1/clang+llvm-5.0.1-x86_64-linux-gnu-ubuntu-14.04.tar.xz")
} else {
throw new UnsupportedOperationException("Clang tool chain provisioning doesn't support operating system '${System.getProperty('os.name')}'")
}
outputFile = layout.buildDirectory.file('clang-5.0.1.tar.xz')
}
def extractClangToolChain = tasks.register("extractClangToolChain", ExtractToolChainTask) {
toolChainArchive = downloadClangToolChain.flatMap { it.outputFile }
toolChainLocation = layout.buildDirectory.dir('toolChains/clang-5.0.1')
}
def provisionClangToolChain = tasks.register("provisionClangToolChain") {
dependsOn extractClangToolChain
group = 'Tool Chain Provisioning'
description = 'Provision Clang 5.0.1 tool chain'
}
tasks.named("build") { dependsOn provisionClangToolChain }