This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
90 lines (77 loc) · 3.47 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
apply plugin: 'base'
import static groovy.io.FileType.FILES
version = externalVersion
File sourcesDir = file(projectDir.absolutePath + '/cmd/mtls')
task buildApplication(type: Task) {
description 'Builds the application for all supported operating systems, or for a target OS using "-Ptarget".'
group 'build'
doLast {
List<String> ldFlags = ['-X main.version=' + version.toString(), '-s', '-w']
if ('' == target) {
if (goBuild(sourcesDir, buildDir, project.name + '-linux-amd64', 'linux', 'amd64', ldFlags) > 0) {
throw new GradleException('Failed to compile the application for Linux 64-bit')
}
if (goBuild(sourcesDir, buildDir, project.name + '-linux-386', 'linux', '386', ldFlags) > 0) {
throw new GradleException('Failed to compile the application for Linux 32-bit')
}
if (goBuild(sourcesDir, buildDir, project.name + '-macos', 'darwin', 'amd64', ldFlags) > 0) {
throw new GradleException('Failed to compile the application for macOS')
}
if (goBuild(sourcesDir, buildDir, project.name + '-windows-amd64', 'windows', 'amd64', ldFlags) > 0) {
throw new GradleException('Failed to compile the application for Windows 64-bit')
}
if (goBuild(sourcesDir, buildDir, project.name + '-windows-386', 'windows', '386', ldFlags) > 0) {
throw new GradleException('Failed to compile the application for Windows 32-bit')
}
if (markBinariesAsExecutable(project.name + '-', buildDir) > 0) {
throw new GradleException('Failed to set binaries as executable')
}
} else {
if (goBuild(sourcesDir, buildDir, project.name, target, 'amd64', ldFlags) > 0) {
throw new GradleException('Failed to compile the application for the current OS')
}
}
}
}
int goBuild(File sourceCodeDir, File outputDir, String binaryName, String os, String arch, List<String> ldFlags) {
List<String> command = ['go', 'build']
if (!arch?.trim()) {
throw new GradleException('Please specify the target arch')
}
if (!os?.trim()) {
throw new GradleException('Please specify the target operating system')
}
if (!ldFlags?.isEmpty()) {
command.add('-ldflags')
command.add(ldFlags.join(' '))
}
command.add('-o')
String outputPath = outputDir.absolutePath + File.separator + binaryName
if (os == 'windows') {
outputPath = outputPath + ".exe"
}
command.add(outputPath)
logger.quiet('Compiling ' + sourceCodeDir.absolutePath + ' for ' + os + ', ' + arch + '...')
return exec {
environment GOARCH: arch, GOOS: os
workingDir sourceCodeDir.absolutePath
commandLine command
}.exitValue
}
int markBinariesAsExecutable(String nameMatch, File directory) {
int exitValue = 0
String osName = System.getProperty('os.name').toLowerCase()
if (osName.contains('linux') || osName.contains('mac')) {
directory.eachFileRecurse(FILES) {
File someFile = file(it)
if (someFile.name.contains(nameMatch)) {
exitValue = exec {
commandLine 'chmod', '-v', '+x', someFile.absolutePath
}.exitValue
}
}
} else if (osName.contains('windows')) {
logger.quiet('[WARN] Unable to set Linux and macOS binaries as executable')
}
return exitValue
}