forked from LabyMod/ultralight-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathci.gradle
More file actions
154 lines (133 loc) · 5.9 KB
/
ci.gradle
File metadata and controls
154 lines (133 loc) · 5.9 KB
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
import org.apache.tools.ant.taskdefs.condition.Os
// This is the directory the library headers will be generated in
ext.generatedHeadersDir = new File(buildDir, "generatedHeaders")
if(!ext.generatedHeadersDir.exists() && !ext.generatedHeadersDir.mkdirs()) {
throw new GradleException("Failed to create directory ${ext.generatedHeadersDir.absolutePath}")
}
// This is the directory where the native binaries can be found after compilation
ext.nativeBinariesDir = new File(buildDir, "nativeBinaries")
if(!ext.nativeBinariesDir.exists() && !ext.nativeBinariesDir.mkdirs()) {
throw new GradleException("Failed to create directory ${ext.nativeBinariesDir.absolutePath}")
}
// Properties determining the build type
def isExternalBuild = false
def ciBuild = project.hasProperty("CI") && Boolean.parseBoolean(project.property("CI").toString())
if(!project.hasProperty("nativeBinaryExternalDir")) {
// The binaries will possibly be need to be built locally
if(Os.isFamily(Os.FAMILY_WINDOWS)) {
// WARNING, this assumes CMake uses visual studio
// For GCC or Clang the output should be $nativeBinariesDir/libultralight-java.dll
ext.nativeBinaries = Arrays.asList(new File(nativeBinariesDir, "Debug/ultralight-java.dll"))
} else if(Os.isFamily(Os.FAMILY_MAC)) {
ext.nativeBinaries = Arrays.asList(new File(nativeBinariesDir, "libultralight-java.dylib"))
} else if(Os.isFamily(Os.FAMILY_UNIX)) {
ext.nativeBinaries = Arrays.asList(new File(nativeBinariesDir, "libultralight-java.so"))
} else {
if(!project.hasProperty("nativeBinaryLibrary")) {
// Unknown OS and missing binary, bail out
throw new GradleException("Unable to determine native output library, " +
"pass -PnativeBinaryLibrary=/path/to/native/binary to gradle")
} else {
// Unknown OS but the user specified the output binary
ext.nativeBinaries = [new File(project.property("nativeBinaryLibrary").toString())]
}
}
} else {
// The binaries have been built by an external process and can be used now
def nativeBinaryExternalDir = file(project.property("nativeBinaryExternalDir").toString())
if(!nativeBinaryExternalDir.exists()) {
// The directory where the binaries are supposed to be is missing, bail out
throw new GradleException("nativeBinaryExternalDir ${nativeBinaryExternalDir.absolutePath} does not exist")
}
ext.nativeBinaries = new ArrayList<File>()
// Collect all native binaries built externally
for(def file in nativeBinaryExternalDir.listFiles()) {
println "Found native binary ${file.absolutePath}"
ext.nativeBinaries.add(file)
}
// Flag the build
isExternalBuild = true
}
project(":ultralight-java-base").afterEvaluate { javaProject ->
project(":ultralight-java-native").afterEvaluate { nativeProject ->
if(!isExternalBuild) {
// If the binaries have not been built externally, building the java project requires
// building OS dependent binary locally
javaProject.tasks["processResources"].dependsOn(nativeProject.tasks["build"])
}
}
}
/**
* Helper function to retrieve the file extensions from a file.
*
* @param file The file to retrieve the extension from
* @return The extension of the file, or an empty string, if the file does not have an extension
*/
static def getExtension(File file) {
String fileName = file.getName()
if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
return fileName.substring(fileName.lastIndexOf(".") + 1)
} else {
return ""
}
}
/**
* Helper function to retrieve the absolute path of a file without its extension.
*
* @param file The file to retrieve the path from
* @return The absolute path of the file without its extension, or the path itself,
* if the file does not have an extension
*/
static def getPathWithoutExtension(File file) {
String path = file.getAbsolutePath()
if(path.lastIndexOf(".") != -1 && path.lastIndexOf(".") != 0) {
return path.substring(0, path.lastIndexOf("."))
} else {
return path
}
}
/**
* Processes a binary file by calculating its new path and moving it if desired.
*
* @param binary The binary to process
* @param doMove If {@code true}, the file will be moved to the new path
* @return The new file of the binary
*/
def processBinary(File binary, boolean doMove) {
String extension = getExtension(binary)
String path = getPathWithoutExtension(binary)
File newPath = new File(path + "-64." + extension)
if(doMove) {
println "Moving ${binary.absolutePath} to ${newPath.absolutePath}"
binary.renameTo(newPath)
}
return newPath
}
if(ciBuild) {
// We are running a CI build
def ciDir = file("ci")
if(!ciDir.exists() && !ciDir.mkdirs()) {
// Failed to create a necessary directory, bail out
throw new GradleException("Failed to create directory ${ciDir.absolutePath}")
}
new File(ciDir, "binaries").withWriter {
// Write a file with paths to the native binaries, required by our script
// at .github/workflows/ci.yml to create an archive with natives
for(File binary in ext.nativeBinaries) {
it.write(processBinary(binary, false).absolutePath)
}
}
def binaries = ext.nativeBinaries
task moveNativeBinaries {
doLast {
for(File binary in binaries) {
// Move the binaries to their new locations
processBinary(binary, true)
}
}
}
project(":ultralight-java-native").afterEvaluate { nativeProject ->
// Make sure the binaries are moved after the build
nativeProject.tasks["build"].finalizedBy(moveNativeBinaries)
}
}