-
Notifications
You must be signed in to change notification settings - Fork 3
/
merge.gradle
166 lines (136 loc) · 4.93 KB
/
merge.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
import java.security.MessageDigest
def downloadsFolder = project.file("build/downloads")
def toUpload = project.file("build/toUpload")
def extractFolder = project.file('build/extract')
def archivesFolder = project.file('build/archive')
def files = project.files(downloadsFolder)
def constantNames = [
'version.txt',
'ceres-cpp_CLS-headers.zip'
]
def constantFiles = []
def mergeFiles = []
downloadsFolder.eachFile {
def found = false
for (element in constantNames) {
System.out.println(it)
if (it.name.endsWith(element)) {
constantFiles << it
found = true
break
}
}
if (!found) {
mergeFiles << it
}
}
def mergePairs = [:]
// Match all to extract
mergeFiles.removeAll {
it.name.contains('osxarm64')
}
mergeFiles.each {
mergePairs.put(it.name, it.name.replace('osxx86-64', 'osxarm64'))
}
def mergeTasks = []
mergePairs.each {
def intelArchive = it.key
def armArchive = it.value
def extractIntelTask = project.tasks.create("extract_$intelArchive", Copy) {
def withoutExt = intelArchive.substring(0, intelArchive.length() - 4);
def folder = "$extractFolder/$withoutExt"
def f = "$downloadsFolder/$intelArchive"
inputs.file f
from zipTree(f)
into folder
}
def extractArmTask = project.tasks.create("extract_$armArchive", Copy) {
def withoutExt = armArchive.substring(0, armArchive.length() - 4);
def folder = "$extractFolder/$withoutExt"
def f = "$downloadsFolder/$armArchive"
inputs.file f
from zipTree(f)
into folder
}
def copyAndMerge = project.tasks.create("merge_$intelArchive", Copy) {
def withoutExt = intelArchive.substring(0, intelArchive.length() - 4);
def replaced = withoutExt.replace('osxx86-64', 'osxuniversal')
def replacedFolder = "$extractFolder/$replaced"
dependsOn extractIntelTask
dependsOn extractArmTask
from (extractIntelTask) {
exclude '**/*.dylib'
exclude '**/*.a'
exclude '**/*.hash'
includeEmptyDirs = false
}
into replacedFolder
doLast {
def doHash = null
extractIntelTask.destinationDir.eachFileRecurse {
if (it.name.endsWith('.hash')) {
doHash = it
}
if (it.name.endsWith('.dylib') || it.name.endsWith('.a')) {
def intelFile = it.toString()
def armFile = it.toString().replace('x86-64', 'arm64')
def universalFile = it.toString().replace('x86-64', 'universal')
project.file(universalFile).parentFile.mkdirs()
exec {
executable 'lipo'
args = [
'-create',
intelFile,
armFile,
'-output',
universalFile
]
}
}
if (it.name.endsWith('.dylib') && project.hasProperty("developerID")) {
def universalFile = it.toString().replace('x86-64', 'universal')
// Get path to binary.
exec {
workingDir rootDir
def args = [
"sh",
"-c",
"codesign --force --strict --deep " +
"--timestamp --options=runtime " +
"--verbose -s ${project.findProperty("developerID")} " +
"$universalFile"
]
commandLine args
}
}
}
if (doHash != null) {
// Find dylib file
def dylibName = 'lib' + doHash.name.replace('.hash', '.dylib')
def dylibPath = project.file(doHash.parentFile.absolutePath.replace('x86-64', 'universal') + "/$dylibName")
def hashFile = project.file(doHash.absolutePath.replace('x86-64', 'universal'))
hashFile.text = MessageDigest.getInstance("MD5").digest(dylibPath.bytes).encodeHex().toString()
}
}
}
def cls = intelArchive.endsWith('.jar') ? Jar.class : Zip.class
def archive = project.tasks.create("archive_$intelArchive", cls) {
dependsOn copyAndMerge
def withoutExt = intelArchive.substring(0, intelArchive.length() - 4);
def replaced = withoutExt.replace('osxx86-64', 'osxuniversal')
archiveBaseName = replaced
destinationDirectory = archivesFolder
from copyAndMerge
}
mergeTasks << archive
}
project.tasks.register('copyToUpload', Copy) {
mergeTasks.each {
dependsOn it
from it
}
constantFiles.each {
from it
}
into toUpload
}