/
SliceExtension.groovy
428 lines (363 loc) · 14.1 KB
/
SliceExtension.groovy
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright (c) ZeroC, Inc. All rights reserved.
package com.zeroc.gradle.icebuilder.slice;
import org.gradle.api.GradleException
import org.gradle.api.logging.Logging
import org.gradle.api.NamedDomainObjectContainer
class SliceExtension {
final NamedDomainObjectContainer<Java> java;
private def iceHome = null
private def iceVersion = null
private def freezeHome = null
private def sliceDir = null
private def slice2java = null
private def slice2freezej = null
private def cppPlatform = null
private def cppConfiguration = null
private def compat = null
private def env = []
private def initialized = false
def output
private static Configuration configuration = null
private static final def LOGGER = Logging.getLogger(SliceExtension)
class Configuration {
def _iceHome = null
def _iceVersion = null
def _srcDist = false
def _freezeHome = null
def _sliceDir = null
def _slice2java = null
def _slice2freezej = null
def _cppPlatform = null
def _cppConfiguration = null
def _compat = null
def _env = []
Configuration(iceHome = null, freezeHome = null) {
_iceHome = iceHome ?: getIceHome();
_freezeHome = freezeHome
// Guess the cpp platform and cpp configuration to use with Windows source builds
_cppConfiguration = cppConfiguration ?: System.getenv("CPP_CONFIGURATION")
_cppPlatform = cppPlatform ?: System.getenv("CPP_PLATFORM")
def os = System.properties['os.name']
if(_iceHome != null) {
_srcDist = new File([_iceHome, "java", "build.gradle"].join(File.separator)).exists()
_slice2java = getSlice2java(_iceHome)
//
// If freezeHome is not set we assume slice2freezej resides in the same location as slice2java
// otherwise slice2freezej will be located in the freeze home bin directory.
//
_slice2freezej = getSlice2freezej(_freezeHome ? _freezeHome : _iceHome)
//
// Setup the environment required to run slice2java/slice2freezej commands
//
if(os.contains("Linux")) {
def cppDir = _srcDist ? "${_iceHome}/cpp" : _iceHome;
def libdir = new File("${cppDir}/lib/i386-linux-gnu").exists() ?
"${cppDir}/lib/i386-linux-gnu" : "${cppDir}/lib"
def lib64dir = new File("${cppDir}/lib/x86_64-linux-gnu").exists() ?
"${cppDir}/lib/x86_64-linux-gnu" : "${cppDir}/lib64"
def env = [libdir, lib64dir]
if(System.env.LD_LIBRARY_PATH) {
env.add(System.env.LD_LIBRARY_PATH)
}
_env = ["LD_LIBRARY_PATH=${env.join(File.pathSeparator)}"]
}
//
// Retrieve the version of the Ice distribution being used
//
_iceVersion = getIceVersion(_iceHome)
//
// This can only happen if iceHome is set to a source distribution. In this case we log a warning
// and return partially initialized. We DO NOT want to throw an exception because we could be in the
// middle of a clean, in which case slice2java will be missing.
//
if(!_iceVersion) {
LOGGER.warn("Unable to determine the Ice version using slice2java (${slice2java}) from " +
"iceHome (${iceHome}). This is expected when cleaning.")
return
}
//
// --compat only available for Ice 3.7 and higher
//
if(SliceExtension.compareVersions(_iceVersion, '3.7') >= 0) {
_compat = compat ?: false
} else if(compat != null) {
LOGGER.warn("Property \"slice.compat\" unavailable for Ice ${_iceVersion}.")
}
//
// Guess the slice and jar directories of the Ice distribution we are using
//
def sliceDirectories = [
[_iceHome, "share", "slice"], // Common shared slice directory
[_iceHome, "share", "ice", "slice"], // Ice >= 3.7
[_iceHome, "share", "Ice-${_iceVersion}", "slice"], // Ice < 3.7
[_iceHome, "slice"] // Opt/source installs & Windows distribution
]
def sliceDirCandidates = sliceDirectories.collect { it.join(File.separator) }
_sliceDir = sliceDirCandidates.find { new File(it).exists() }
if (!_sliceDir) {
LOGGER.warn("Unable to locate slice directory in iceHome (${iceHome})")
}
}
}
def getIceHome() {
if (System.env.ICE_HOME != null) {
return System.env.ICE_HOME
}
def os = System.properties['os.name']
if (os == "Mac OS X") {
return "/usr/local"
} else if (os.contains("Windows")) {
return getWin32IceHome()
} else {
return "/usr"
}
}
//
// Query Win32 registry key and return the InstallDir value for the given key
//
def getWin32InstallDir(key) {
def sout = new StringBuffer()
def serr = new StringBuffer()
def p = ["reg", "query", key, "/v", "InstallDir", "/reg:64"].execute()
p.waitForProcessOutput(sout, serr)
if (p.exitValue() != 0) {
return null
}
return sout.toString().split(" ")[3].trim()
}
//
// Query Win32 registry and return the path of the latest Ice version available.
//
def getWin32IceHome() {
def sout = new StringBuffer()
def serr = new StringBuffer()
def p = ["reg", "query", "HKLM\\Software\\ZeroC", "/reg:64"].execute()
p.waitForProcessOutput(sout, serr)
if (p.exitValue() != 0) {
//
// reg query will fail if Ice is not installed
//
return ""
}
def iceInstallDir = null
def iceVersion = null
sout.toString().split("\\r?\\n").each {
try{
if (it.indexOf("HKEY_LOCAL_MACHINE\\Software\\ZeroC\\Ice") != -1) {
def installDir = getWin32InstallDir(it)
if (installDir != null) {
def version = getIceVersion(installDir)
if(iceVersion == null || compareVersions(version, iceVersion) == 1) {
iceInstallDir = installDir
iceVersion = version
}
}
}
} catch(e){
}
}
return iceInstallDir
}
def getIceVersion(iceHome) {
def slice2java = getSlice2java(iceHome)
if(new File(slice2java).exists()) {
def command = [slice2java, "--version"]
def sout = new StringBuffer()
def serr = new StringBuffer()
def p = command.execute(_env, null)
p.waitForProcessOutput(sout, serr)
if (p.exitValue() != 0) {
println serr.toString()
throw new GradleException("${command[0]} command failed: ${p.exitValue()}")
}
return serr.toString().trim()
} else if(!_srcDist) {
// Only throw an exception if we are not using a source distribution. A binary distribution should
// always have slice2java, howerver a source distribution may not. For example, during a clean.
throw new GradleException("slice2java (${slice2java}) not found. Please ensure that Ice is installed " +
"and the iceHome property (${iceHome}) is correct.")
} else {
return null;
}
}
def getSlice2java(iceHome) {
return getSliceCompiler("slice2java", iceHome)
}
def getSlice2freezej(freezeHome) {
return getSliceCompiler("slice2freezej", freezeHome)
}
//
// Return the path to the specified slice compiler (slice2java|slice2freezej) with respect to
// the specified homeDir (iceHome|freezeHome)
//
def getSliceCompiler(name, homeDir) {
def os = System.properties['os.name']
//
// Check if we are using a Slice source distribution
//
def srcDist = new File([homeDir, "java", "build.gradle"].join(File.separator)).exists()
def compilerName = os.contains('Windows') ? "${name}.exe" : name
def sliceCompiler = null
//
// Set the location of the sliceCompiler executable
//
if (os.contains("Windows")) {
//
// For Windows source distribution we first check for <IceHome>\cpp\bin\<cppPlatform>\<cppConfiguration>
// that correspond with Ice 3.7 or greater source distribution.
///
// For Windows binary distribution we first check for <IceHome>\tools that correspond with NuGet package
// layout.
//
def basePath = srcDist ? [homeDir, "cpp", "bin", _cppPlatform, _cppConfiguration] : [homeDir, "tools"]
basePath = basePath.join(File.separator)
if(new File(basePath).exists()) {
sliceCompiler = [basePath, compilerName].join(File.separator)
}
}
if (sliceCompiler == null) {
sliceCompiler = srcDist ?
[homeDir, "cpp", "bin", compilerName].join(File.separator) :
[homeDir, "bin", compilerName].join(File.separator)
}
return sliceCompiler
}
}
SliceExtension(java) {
this.java = java
}
def java(Closure closure) {
try {
java.configure(closure)
} catch(MissingPropertyException ex) {
java.create('default', closure)
}
}
private def parseVersion(v) {
if(v) {
def vv = v.tokenize('.')
if(v.indexOf('a') != -1) {
return "${vv[0]}.${vv[1].replace('a', '.0-alpha')}"
} else if (v.indexOf('b') != -1) {
return "${vv[0]}.${vv[1].replace('b', '.0-beta')}"
} else {
return v
}
} else {
return null;
}
}
private void init() {
LOGGER.debug('Initializing configuration')
initialized = true // must happen before calling setters
Configuration c = new Configuration(iceHome, freezeHome)
iceHome = c._iceHome
iceVersion = c._iceVersion
freezeHome = c._freezeHome
sliceDir = c._sliceDir
slice2java = c._slice2java
slice2freezej = c._slice2freezej
cppPlatform = c._cppPlatform
cppConfiguration = c._cppConfiguration
compat = c._compat
env = c._env
LOGGER.debug("Property: iceHome = ${iceHome}")
LOGGER.debug("Property: iceVersion = ${iceVersion}")
LOGGER.debug("Property: freezeHome = ${freezeHome}")
LOGGER.debug("Property: sliceDir = ${sliceDir}")
LOGGER.debug("Property: slice2java = ${slice2java}")
LOGGER.debug("Property: slice2freezej = ${slice2freezej}")
LOGGER.debug("Property: cppPlatform = ${cppPlatform}")
LOGGER.debug("Property: cppConfiguration = ${cppConfiguration}")
LOGGER.debug("Property: compat = ${compat}")
LOGGER.debug("Property: env = ${env}")
assert initialized == true
}
// 1 is a > b
// 0 if a == b
// -1 if a < b
static def compareVersions(a, b) {
def verA = a.tokenize('.')
def verB = b.tokenize('.')
for (int i = 0; i < Math.min(verA.size(), verB.size()); ++i) {
if (verA[i] != verB[i]) {
return verA[i] <=> verB[i]
}
}
// Common indices match. Assume the longest version is the most recent
verA.size() <=> verB.size()
}
// Compare iceVersion with version
// 1 is iceVersion > version
// 0 if iceVersion == version
// -1 if iceVersion < version
def compareIceVersion(version) {
lazyInit()
return compareVersions(iceVersion, version)
}
def getIceHome() {
lazyInit()
return iceHome
}
def setIceHome(value) {
iceHome = value
initialized = false
}
def getIceVersion() {
lazyInit()
return iceVersion
}
def getFreezeHome() {
lazyInit()
return freezeHome
}
def setFreezeHome(value) {
freezeHome = value
initialized = false
}
def getSliceDir() {
lazyInit()
return sliceDir
}
def getSlice2java() {
lazyInit()
return slice2java
}
def getSlice2freezej() {
lazyInit()
return slice2freezej
}
def getCppPlatform() {
lazyInit()
return cppPlatform
}
def setCppPlatform(value) {
cppPlatform = value
initialized = false
}
def getCppConfiguration() {
lazyInit()
return cppConfiguration
}
def setCppConfiguration(value) {
cppConfiguration = value
initialized = false
}
def getCompat() {
lazyInit()
return compat
}
def setCompat(value) {
compat = value
initialized = false
}
def getEnv() {
lazyInit()
return env
}
def lazyInit() {
if(!initialized) {
init()
}
}
}