Debian Codegraph @

src/main/groovy/com/zeroc/gradle/icebuilder/slice/SliceExtension.groovy

raw
1 // **********************************************************************
2 //
3 // Copyright (c) 2014-2017 ZeroC, Inc. All rights reserved.
4 //
5 // **********************************************************************
6
7 package com.zeroc.gradle.icebuilder.slice;
8
9 import org.gradle.api.GradleException
10 import org.gradle.api.logging.Logging
11 import org.gradle.api.NamedDomainObjectContainer
12
13 class SliceExtension {
14
15 final NamedDomainObjectContainer<Java> java;
16
17 private def iceHome = null
18 private def iceVersion = null
19 private def iceArtifactVersion = null
20 private def srcDist = false
21 private def freezeHome = null
22 private def sliceDir = null
23 private def slice2java = null
24 private def slice2freezej = null
25 private def jarDir = null
26 private def cppPlatform = null
27 private def cppConfiguration = null
28 private def compat = null
29
30 private def env = []
31 private def initialized = false
32 def output
33
34 private static Configuration configuration = null
35 private static final def LOGGER = Logging.getLogger(SliceExtension)
36
37 class Configuration {
38
39 def _iceHome = null
40 def _iceVersion = null
41 def _srcDist = false
42 def _freezeHome = null
43 def _sliceDir = null
44 def _slice2java = null
45 def _slice2freezej = null
46 def _jarDir = null
47 def _cppPlatform = null
48 def _cppConfiguration = null
49 def _compat = null
50 def _env = []
51
52 Configuration(iceHome = null, freezeHome = null) {
53 _iceHome = iceHome ?: getIceHome();
54 _freezeHome = freezeHome
55
56 // Guess the cpp platform and cpp configuration to use with Windows source builds
57 _cppConfiguration = cppConfiguration ?: System.getenv("CPP_CONFIGURATION")
58 _cppPlatform = cppPlatform ?: System.getenv("CPP_PLATFORM")
59
60 def os = System.properties['os.name']
61
62 if(_iceHome != null) {
63 _srcDist = new File([_iceHome, "java", "build.gradle"].join(File.separator)).exists()
64 _slice2java = getSlice2java(_iceHome)
65
66 //
67 // If freezeHome is not set we assume slice2freezej resides in the same location as slice2java
68 // otherwise slice2freezej will be located in the freeze home bin directory.
69 //
70 _slice2freezej = getSlice2freezej(_freezeHome ? _freezeHome : _iceHome)
71
72 //
73 // Setup the environment required to run slice2java/slice2freezej commands
74 //
75 if(os.contains("Linux")) {
76 def cppDir = _srcDist ? "${_iceHome}/cpp" : _iceHome;
77
78 def libdir = new File("${cppDir}/lib/i386-linux-gnu").exists() ?
79 "${cppDir}/lib/i386-linux-gnu" : "${cppDir}/lib"
80 def lib64dir = new File("${cppDir}/lib/x86_64-linux-gnu").exists() ?
81 "${cppDir}/lib/x86_64-linux-gnu" : "${cppDir}/lib64"
82 def env = [libdir, lib64dir]
83 if(System.env.LD_LIBRARY_PATH) {
84 env.add(System.env.LD_LIBRARY_PATH)
85 }
86 _env = ["LD_LIBRARY_PATH=${env.join(File.pathSeparator)}"]
87 }
88
89 //
90 // Retrieve the version of the Ice distribution being used
91 //
92 _iceVersion = getIceVersion(_iceHome)
93
94 //
95 // This can only happen if iceHome is set to a source distribution. In this case we log a warning
96 // and return partially initialized. We DO NOT want to throw an exception because we could be in the
97 // middle of a clean, in which case slice2java will be missing.
98 //
99 if(!_iceVersion) {
100 LOGGER.warn("Unable to determine the Ice version using slice2java (${slice2java}) from " +
101 "iceHome (${iceHome}). This is expected when cleaning.")
102 return
103 }
104
105 //
106 // --compat only available for Ice 3.7 and higher
107 //
108 if(SliceExtension.compareVersions(_iceVersion, '3.7') >= 0) {
109 _compat = compat ?: false
110 } else if(compat != null) {
111 LOGGER.warn("Property \"slice.compat\" unavailable for Ice ${_iceVersion}.")
112 }
113
114 //
115 // Guess the slice and jar directories of the Ice distribution we are using
116 //
117 def sliceDirectories = [
118 [_iceHome, "share", "slice"], // Common shared slice directory
119 [_iceHome, "share", "ice", "slice"], // Ice >= 3.7
120 [_iceHome, "share", "Ice-${_iceVersion}", "slice"], // Ice < 3.7
121 [_iceHome, "slice"] // Opt/source installs & Windows distribution
122 ]
123
124 def jarDirectories = [
125 [_iceHome, "share", "java"], // Default usr install
126 [_iceHome, _compat ? "java-compat" : "java", "lib"], // Source distribution
127 [_iceHome, "lib"] // Opt style install & Windows distribution
128 ]
129
130 def sliceDirCandidates = sliceDirectories.collect { it.join(File.separator) }
131 def jarDirCandidates = jarDirectories.collect { it.join(File.separator) }
132
133 _sliceDir = sliceDirCandidates.find { new File(it).exists() }
134 _jarDir = jarDirCandidates.find { new File(it).exists() }
135
136 if (!_sliceDir) {
137 LOGGER.warn("Unable to locate slice directory in iceHome (${iceHome})")
138 }
139 }
140 }
141
142 def getIceHome() {
143 if (System.env.ICE_HOME != null) {
144 return System.env.ICE_HOME
145 }
146
147 def os = System.properties['os.name']
148 if (os == "Mac OS X") {
149 return "/usr/local"
150 } else if (os.contains("Windows")) {
151 return getWin32IceHome()
152 } else {
153 return "/usr"
154 }
155 }
156
157 //
158 // Query Win32 registry key and return the InstallDir value for the given key
159 //
160 def getWin32InstallDir(key) {
161 def sout = new StringBuffer()
162 def serr = new StringBuffer()
163 def p = ["reg", "query", key, "/v", "InstallDir"].execute()
164 p.waitForProcessOutput(sout, serr)
165 if (p.exitValue() != 0) {
166 return null
167 }
168 return sout.toString().split(" ")[3].trim()
169 }
170
171 //
172 // Query Win32 registry and return the path of the latest Ice version available.
173 //
174 def getWin32IceHome() {
175 def sout = new StringBuffer()
176 def serr = new StringBuffer()
177
178 def p = ["reg", "query", "HKLM\\Software\\ZeroC"].execute()
179 p.waitForProcessOutput(sout, serr)
180 if (p.exitValue() != 0) {
181 //
182 // reg query will fail if Ice is not installed
183 //
184 return ""
185 }
186
187 def iceInstallDir = null
188 def iceVersion = null
189
190 sout.toString().split("\\r?\\n").each {
191 try{
192 if (it.indexOf("HKEY_LOCAL_MACHINE\\Software\\ZeroC\\Ice") != -1) {
193 def installDir = getWin32InstallDir(it)
194 if (installDir != null) {
195 def version = getIceVersion(installDir)
196 if(iceVersion == null || compareVersions(version, iceVersion) == 1) {
197 iceInstallDir = installDir
198 iceVersion = version
199 }
200 }
201 }
202 } catch(e){
203 }
204 }
205 return iceInstallDir
206 }
207
208 def getIceVersion(iceHome) {
209 def slice2java = getSlice2java(iceHome)
210 if(new File(slice2java).exists()) {
211 def command = [slice2java, "--version"]
212 def sout = new StringBuffer()
213 def serr = new StringBuffer()
214 def p = command.execute(_env, null)
215 p.waitForProcessOutput(sout, serr)
216 if (p.exitValue() != 0) {
217 println serr.toString()
218 throw new GradleException("${command[0]} command failed: ${p.exitValue()}")
219 }
220 return serr.toString().trim()
221 } else if(!_srcDist) {
222 // Only throw an exception if we are not using a source distribution. A binary distribution should
223 // always have slice2java, howerver a source distribution may not. For example, during a clean.
224 throw new GradleException("slice2java (${slice2java}) not found. Please ensure that Ice is installed " +
225 "and the iceHome property (${iceHome}) is correct.")
226 } else {
227 return null;
228 }
229 }
230
231 def getSlice2java(iceHome) {
232 return getSliceCompiler("slice2java", iceHome)
233 }
234
235 def getSlice2freezej(freezeHome) {
236 return getSliceCompiler("slice2freezej", freezeHome)
237 }
238
239 //
240 // Return the path to the specified slice compiler (slice2java|slice2freezej) with respect to
241 // the specified homeDir (iceHome|freezeHome)
242 //
243 def getSliceCompiler(name, homeDir) {
244 def os = System.properties['os.name']
245 //
246 // Check if we are using a Slice source distribution
247 //
248 def srcDist = new File([homeDir, "java", "build.gradle"].join(File.separator)).exists()
249 def compilerName = os.contains('Windows') ? "${name}.exe" : name
250 def sliceCompiler = null
251
252 //
253 // Set the location of the sliceCompiler executable
254 //
255 if (os.contains("Windows")) {
256
257 //
258 // For Windows source distribution we first check for <IceHome>\cpp\bin\<cppPlatform>\<cppConfiguration>
259 // that correspond with Ice 3.7 or greater source distribution.
260 ///
261 // For Windows binary distribution we first check for <IceHome>\tools that correspond with NuGet package
262 // layout.
263 //
264 def basePath = srcDist ? [homeDir, "cpp", "bin", _cppPlatform, _cppConfiguration] : [homeDir, "tools"]
265 basePath = basePath.join(File.separator)
266 if(new File(basePath).exists()) {
267 sliceCompiler = [basePath, compilerName].join(File.separator)
268 }
269 }
270
271 if (sliceCompiler == null) {
272 sliceCompiler = srcDist ?
273 [homeDir, "cpp", "bin", compilerName].join(File.separator) :
274 [homeDir, "bin", compilerName].join(File.separator)
275 }
276
277 return sliceCompiler
278 }
279 }
280
281 SliceExtension(java) {
282 this.java = java
283 }
284
285 def java(Closure closure) {
286 try {
287 java.configure(closure)
288 } catch(MissingPropertyException ex) {
289 java.create('default', closure)
290 }
291 }
292
293 private def parseVersion(v) {
294 if(v) {
295 def vv = v.tokenize('.')
296 if(v.indexOf('a') != -1) {
297 return "${vv[0]}.${vv[1].replace('a', '.0-alpha')}"
298 } else if (v.indexOf('b') != -1) {
299 return "${vv[0]}.${vv[1].replace('b', '.0-beta')}"
300 } else {
301 return v
302 }
303 } else {
304 return null;
305 }
306 }
307
308 private void init() {
309 LOGGER.debug('Initializing configuration')
310 initialized = true // must happen before calling setters
311
312 Configuration c = new Configuration(iceHome, freezeHome)
313
314 iceHome = c._iceHome
315 iceVersion = c._iceVersion
316 iceArtifactVersion = parseVersion(c._iceVersion)
317 srcDist = c._srcDist
318 freezeHome = c._freezeHome
319 sliceDir = c._sliceDir
320 slice2java = c._slice2java
321 slice2freezej = c._slice2freezej
322 jarDir = c._jarDir
323 cppPlatform = c._cppPlatform
324 cppConfiguration = c._cppConfiguration
325 compat = c._compat
326 env = c._env
327
328 LOGGER.debug("Property: iceHome = ${iceHome}")
329 LOGGER.debug("Property: iceVersion = ${iceVersion}")
330 LOGGER.debug("Property: srcDist = ${srcDist}")
331 LOGGER.debug("Property: freezeHome = ${freezeHome}")
332 LOGGER.debug("Property: sliceDir = ${sliceDir}")
333 LOGGER.debug("Property: slice2java = ${slice2java}")
334 LOGGER.debug("Property: slice2freezej = ${slice2freezej}")
335 LOGGER.debug("Property: jarDir = ${jarDir}")
336 LOGGER.debug("Property: cppPlatform = ${cppPlatform}")
337 LOGGER.debug("Property: cppConfiguration = ${cppConfiguration}")
338 LOGGER.debug("Property: compat = ${compat}")
339 LOGGER.debug("Property: env = ${env}")
340
341 assert initialized == true
342 }
343
344 // 1 is a > b
345 // 0 if a == b
346 // -1 if a < b
347 static def compareVersions(a, b) {
348 def verA = a.tokenize('.')
349 def verB = b.tokenize('.')
350
351 for (int i = 0; i < Math.min(verA.size(), verB.size()); ++i) {
352 if (verA[i] != verB[i]) {
353 return verA[i] <=> verB[i]
354 }
355 }
356 // Common indices match. Assume the longest version is the most recent
357 verA.size() <=> verB.size()
358 }
359
360 // Compare iceVersion with version
361 // 1 is iceVersion > version
362 // 0 if iceVersion == version
363 // -1 if iceVersion < version
364 def compareIceVersion(version) {
365 lazyInit()
366 return compareVersions(iceVersion, version)
367 }
368
369 def getIceHome() {
370 lazyInit()
371 return iceHome
372 }
373
374 def setIceHome(value) {
375 iceHome = value
376 initialized = false
377 }
378
379 def getIceVersion() {
380 lazyInit()
381 return iceVersion
382 }
383
384 def getIceArtifactVersion() {
385 lazyInit()
386 return iceArtifactVersion
387 }
388
389 def getSrcDist() {
390 lazyInit()
391 return srcDist
392 }
393
394 def getFreezeHome() {
395 lazyInit()
396 return freezeHome
397 }
398
399 def setFreezeHome(value) {
400 freezeHome = value
401 initialized = false
402 }
403
404 def getSliceDir() {
405 lazyInit()
406 return sliceDir
407 }
408
409 def getSlice2java() {
410 lazyInit()
411 return slice2java
412 }
413
414 def getSlice2freezej() {
415 lazyInit()
416 return slice2freezej
417 }
418
419 def getJarDir() {
420 lazyInit()
421 return jarDir
422 }
423
424 def getCppPlatform() {
425 lazyInit()
426 return cppPlatform
427 }
428
429 def setCppPlatform(value) {
430 cppPlatform = value
431 initialized = false
432 }
433
434 def getCppConfiguration() {
435 lazyInit()
436 return cppConfiguration
437 }
438
439 def setCppConfiguration(value) {
440 cppConfiguration = value
441 initialized = false
442 }
443
444 def getCompat() {
445 lazyInit()
446 return compat
447 }
448
449 def setCompat(value) {
450 compat = value
451 initialized = false
452 }
453
454 def getEnv() {
455 lazyInit()
456 return env
457 }
458
459 def lazyInit() {
460 if(!initialized) {
461 init()
462 }
463 }
464 }