-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathbuild.gradle
217 lines (184 loc) · 7.69 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Copyright (c) 2020 David Allison <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
apply plugin: 'java-library'
apply plugin: 'signing'
apply plugin: 'com.vanniktech.maven.publish'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// obtaining the OS
implementation 'org.apache.commons:commons-exec:1.3'
}
jar {
sourceSets {
main {
resources {
def assetsPath = new File("${project.rootDir}", "rsdroid-testing/assets")
println "assets path: ${assetsPath.absolutePath}"
srcDirs assetsPath.absolutePath
}
}
}
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
enum Platform {
LINUX, MACOS, WINDOWS
}
def Platform getCurrentPlatform() {
def currentOS = org.gradle.internal.os.OperatingSystem.current()
if (currentOS.isWindows()) return Platform.WINDOWS
if (currentOS.isMacOsX()) return Platform.MACOS
if (currentOS.isLinux()) return Platform.LINUX
throw "Unsuported OS"
}
def getInputDir() {
def path = "${project.sourceSets.main.java.srcDirs[0]}/../../../../rslib-bridge"
def canonicalPath = new File(path).getCanonicalPath()
if (!new File(canonicalPath).exists()) {
throw new FileNotFoundException("could not find working directory: ${canonicalPath}")
}
return canonicalPath
}
def getAssetsDir() {
return "${project.sourceSets.main.java.srcDirs[0]}/../../../assets";
}
def getTargetDir() {
return getInputDir() + "/target"
}
def toWindowsCommandLine(String command) {
def path = getAssetsDir()
def canonicalAssetsPath = new File(path).getCanonicalPath()
if (!new File(canonicalAssetsPath).exists()) {
throw new FileNotFoundException("could not find output path: ${canonicalAssetsPath}")
}
return ["bash", "--login", "-c", command]
}
def toCommandLine(Platform targetPlatform) {
// the --out-dir flag requires -Z unstable-options
// -Z unstable-options requires a 'nightly' toolchain
// TODO: Explain how to select the version
def useCross = System.getenv("NO_CROSS") != "true"
if (useCross && getCurrentPlatform() == targetPlatform) {
useCross = false
}
def standardArg = "${useCross ? "cross" : "cargo"} build --release --features no-android --verbose"
def assetsPath = new File("${project.sourceSets.main.java.srcDirs[0]}/../../../assets").getCanonicalPath()
String assetsSubPath = "/rsdroid-testing/assets"
File f = new File(project.rootDir, assetsSubPath)
// As we're running through WSL, we need a Unix path for windows:
// C:\GitHub\Rust-Test\rsdroid-testing\assets -> /mnt/c/GitHub/Rust-Test/rsdroid-testing/assets
String path = !org.gradle.internal.os.OperatingSystem.current().isWindows() ? f.absolutePath :
"/mnt/" + f.absolutePath[0].toLowerCase() + f.absolutePath.substring(1).replace(":\\", "\\").replace("\\", "/")
println "Outputting to: ${path}"
def outDir = ""
System.out.println(assetsPath)
def mainArg = standardArg + " " + outDir
if (useCross) {
mainArg = "DONT_RUSTFMT=1 " + mainArg //FIXME: try to mount $CARGO_HOME/bin and add it to path using cross
}
switch (targetPlatform) {
case Platform.LINUX: return toWindowsCommandLine("CC=\$ANKIDROID_LINUX_CC ${mainArg} --target x86_64-unknown-linux-gnu")
case Platform.MACOS:
/// x86_64-apple-darwin14-cc
return toWindowsCommandLine("CC=\$ANKIDROID_MACOS_CC ${mainArg} --target x86_64-apple-darwin")
case Platform.WINDOWS: return toWindowsCommandLine("${mainArg} --target x86_64-pc-windows-gnu")
}
throw new IllegalStateException(targetPlatform)
}
task preBuildWindows(type: Exec) {
doFirst {
logger.lifecycle(toCommandLine(Platform.WINDOWS).join(" "))
}
workingDir(getInputDir())
commandLine(toCommandLine(Platform.WINDOWS))
}
task preBuildMac(type: Exec) {
doFirst {
logger.lifecycle(toCommandLine(Platform.MACOS).join(" "))
}
if (!org.gradle.internal.os.OperatingSystem.current().isMacOsX()) {
return
/* def macCC = System.getenv("ANKIDROID_MACOS_CC")
println "ANKIDROID_MACOS_CC: ${System.getenv("ANKIDROID_MACOS_CC")}"
if (macCC == null || macCC == "") {
throw new IllegalStateException("ANKIDROID_MACOS_CC not set.\n" +
"Please install a MacOS toolchain.\n" +
"See folder: rsdroid-testing/tools and https://wapl.es/rust/2019/02/17/rust-cross-compile-linux-to-macos.html")
}*/
}
workingDir(getInputDir())
commandLine(toCommandLine(Platform.MACOS))
}
task preBuildLinux(type: Exec) {
doFirst {
logger.lifecycle(toCommandLine(Platform.LINUX).join(" "))
}
workingDir(getInputDir())
commandLine(toCommandLine(Platform.LINUX))
}
task copyLinuxOutput(type: Copy) {
dependsOn preBuildLinux
File linuxDir = new File(targetDir, "x86_64-unknown-linux-gnu/release/")
from linuxDir
include "*.so"
into assetsDir
}
task copyMacOutput(type: Copy) {
dependsOn preBuildMac
File macosDir = new File(targetDir, "x86_64-apple-darwin/release/")
from macosDir
include "*.dylib"
into assetsDir
}
task copyWindowsOutput(type: Copy) {
dependsOn preBuildWindows
File windowsDir = new File(targetDir, "x86_64-pc-windows-gnu/release/")
from windowsDir
include "*.dll"
include "*.dll.a"
into assetsDir
}
// TODO: check for cargo
// check for targets: x86_64-apple-darwin, x86_64-pc-windows-gnu, TODO: Linux
processResources.dependsOn preBuildWindows
processResources.dependsOn copyWindowsOutput
// To fix: "toolchain 'nightly-x86_64-unknown-linux-gnu' is not installed"
// execute in bash: rustup toolchain install nightly-x86_64-unknown-linux-gnu
// "linker `x86_64-unknown-linux-gnu-gcc` not found"
// sudo ln -s /usr/bin/cc /usr/local/bin/x86_64-unknown-linux-gnu-gcc
// rustup target add x86_64-pc-windows-gnu --toolchain nightly
// brew install mingw-w64 && x86_64-w64-mingw32-gcc -v
processResources.dependsOn preBuildLinux
processResources.dependsOn copyLinuxOutput
if (org.gradle.internal.os.OperatingSystem.current().isMacOsX()) {
// due to restrictions on downloading the MacOS SDK, we can only build for MacOS on MacOS
// > Install a reasonable number of copies of the Apple Software on Apple-branded computers
// https://www.apple.com/legal/sla/docs/xcode.pdf
processResources.dependsOn preBuildMac
processResources.dependsOn copyMacOutput
}
signing {
def hasPrivate = project.hasProperty('SIGNING_PRIVATE_KEY')
def hasPassword = project.hasProperty('SIGNING_PASSWORD')
if (hasPrivate && hasPassword) {
useInMemoryPgpKeys(project.getProperty('SIGNING_PRIVATE_KEY'), project.getProperty('SIGNING_PASSWORD'))
} else {
def message = "Publishing to Sonatype will not work - PGP keys not set for publishing"
def pk = System.getenv("ORG_GRADLE_PROJECT_SIGNING_PRIVATE_KEY")
def pwd = System.getenv("ORG_GRADLE_PROJECT_SIGNING_PASSWORD")
logger.warn("$message: ${hasPrivate}, ${hasPassword}, ${pk == null || "" == pk}, ${pwd == null || "" == pwd}")
}
}