| 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.DefaultTask |
| 10 |
import org.gradle.api.tasks.TaskAction |
| 11 |
import org.gradle.api.tasks.InputFiles |
| 12 |
import org.gradle.api.tasks.OutputDirectory |
| 13 |
import org.gradle.api.GradleException |
| 14 |
import java.io.* |
| 15 |
import org.slf4j.Logger |
| 16 |
import org.slf4j.LoggerFactory |
| 17 |
|
| 18 |
import groovy.util.XmlSlurper |
| 19 |
import groovy.xml.MarkupBuilder |
| 20 |
|
| 21 |
class SliceTask extends DefaultTask { |
| 22 |
private static final Logger LOGGER = LoggerFactory.getLogger(SliceTask) |
| 23 |
|
| 24 |
SliceTask() { |
| 25 |
// This forces the task to run on each build rather than relying on a hash |
| 26 |
// of the input files. |
| 27 |
outputs.upToDateWhen { false } |
| 28 |
} |
| 29 |
|
| 30 |
@TaskAction |
| 31 |
def action() { |
| 32 |
if(!project.slice.output.isDirectory()) { |
| 33 |
if(!project.slice.output.mkdirs()) { |
| 34 |
throw new GradleException("could not create slice output directory: ${project.slice.output}") |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
// |
| 39 |
// In case the slice output directory is not inside the buildDir, as we still store |
| 40 |
// dependency files here |
| 41 |
// |
| 42 |
if(!project.buildDir.isDirectory()) { |
| 43 |
if(!project.buildDir.mkdirs()) { |
| 44 |
throw new GradleException("could not create build output directory: ${project.buildDir}") |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// Make sure default source set is present |
| 49 |
if(project.slice.java.isEmpty()) { |
| 50 |
project.slice.java.create("default") |
| 51 |
} |
| 52 |
|
| 53 |
processJava() |
| 54 |
processFreezeJ() |
| 55 |
} |
| 56 |
|
| 57 |
@InputFiles |
| 58 |
def getInputFiles() { |
| 59 |
return [] |
| 60 |
} |
| 61 |
|
| 62 |
@OutputDirectory |
| 63 |
def getOutputDirectory() { |
| 64 |
return project.slice.output |
| 65 |
} |
| 66 |
|
| 67 |
def processFreezeJ() { |
| 68 |
def freezej = project.slice.freezej |
| 69 |
|
| 70 |
// Set of source files and all dependencies. |
| 71 |
Set files = [] |
| 72 |
if(freezej.files) { |
| 73 |
files.addAll(freezej.files) |
| 74 |
getS2FDependencies(freezej).values().each({ files.addAll(it) }) |
| 75 |
} |
| 76 |
|
| 77 |
def state = new FreezeJBuildState() |
| 78 |
def stateFile = new File(project.slice.output, "slice2freezej.df.xml") |
| 79 |
state.read(stateFile) |
| 80 |
|
| 81 |
def rebuild = false |
| 82 |
|
| 83 |
def args = buildS2FCommandLine(freezej) |
| 84 |
// If the command line changes or the slice2freezej compiler (always the first argument) |
| 85 |
// has been updated then rebuild. |
| 86 |
if(args != state.args || getTimestamp(new File(args[0])) > state.timestamp) { |
| 87 |
rebuild = true |
| 88 |
} |
| 89 |
|
| 90 |
// Rebuild if the set of slice files has changed, or if one of the slice files has a timestamp newer |
| 91 |
// than the last build. |
| 92 |
if(!rebuild && state.slice.size() != files.size()) { |
| 93 |
rebuild = true |
| 94 |
} |
| 95 |
|
| 96 |
// If the build timestamp is older than the slice file timestamp then we need to build the source file. |
| 97 |
if(!rebuild) { |
| 98 |
rebuild = files.any { getTimestamp(it) > state.timestamp } |
| 99 |
} |
| 100 |
|
| 101 |
// Check if the prior set of slice files has changed. |
| 102 |
if(!rebuild) { |
| 103 |
rebuild = state.slice.any { !files.contains(it) } |
| 104 |
} |
| 105 |
|
| 106 |
// Check that any previously generated files still exist |
| 107 |
if(!rebuild) { |
| 108 |
rebuild = state.generated.any { generatedFile -> |
| 109 |
!generatedFile.isFile() || getTimestamp(generatedFile) > state.timestamp |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// Bail out if there is nothing to do (in theory this should not occur). |
| 114 |
if(!rebuild) { |
| 115 |
LOGGER.info("nothing to do") |
| 116 |
return |
| 117 |
} |
| 118 |
|
| 119 |
if(freezej.files) { |
| 120 |
LOGGER.info("running slice2freezej on the following slice files") |
| 121 |
freezej.files.each { LOGGER.info(" ${it}") } |
| 122 |
} else if (!stateFile.isFile()) { |
| 123 |
// The state file has never been written and and we have no |
| 124 |
// files to process. No reason to continue as it will only |
| 125 |
// write an empty dependency file. |
| 126 |
LOGGER.info("nothing to do") |
| 127 |
return |
| 128 |
} |
| 129 |
|
| 130 |
// List of generated java source files. |
| 131 |
def generated = executeS2F(freezej) |
| 132 |
|
| 133 |
// Gather up the list of source files that we previously built for those files which are building. |
| 134 |
Set oldGenerated = state.generated |
| 135 |
|
| 136 |
// Remove all source files that we have generated from the list of previously built sources. |
| 137 |
oldGenerated.removeAll(generated) |
| 138 |
|
| 139 |
// Update the build state. |
| 140 |
def newState = new FreezeJBuildState() |
| 141 |
newState.timestamp = System.currentTimeMillis() |
| 142 |
newState.slice = files |
| 143 |
newState.generated = generated |
| 144 |
newState.args = args |
| 145 |
|
| 146 |
// Write the new dependencies file. |
| 147 |
newState.write(stateFile) |
| 148 |
|
| 149 |
deleteFiles(oldGenerated) |
| 150 |
} |
| 151 |
|
| 152 |
def getS2FGenerated(freezej) { |
| 153 |
def files = [] |
| 154 |
freezej.dict.each { |
| 155 |
files.add(it.javaType) |
| 156 |
} |
| 157 |
|
| 158 |
freezej.index.each { |
| 159 |
files.add(it.javaType) |
| 160 |
} |
| 161 |
|
| 162 |
// Convert each file name from java package convention to file name. |
| 163 |
files = files.collect { |
| 164 |
it.tr('.', '/') + ".java" |
| 165 |
} |
| 166 |
// Convert to a file with the generated path. |
| 167 |
files = files.collect { |
| 168 |
new File(project.slice.output, it) |
| 169 |
} |
| 170 |
|
| 171 |
return files |
| 172 |
} |
| 173 |
|
| 174 |
// Executes slice2freezej to determine the slice file dependencies. |
| 175 |
// Returns a dictionary of A -> [B] where A depends on B. |
| 176 |
def getS2FDependencies(freezej) { |
| 177 |
if((freezej.dict == null || freezej.dict.isEmpty()) && (freezej.index == null || freezej.index.isEmpty())) { |
| 178 |
// No build artifacts. |
| 179 |
return [:] |
| 180 |
} |
| 181 |
|
| 182 |
def command = buildS2FCommandLine(freezej) |
| 183 |
command.add("--depend-xml") |
| 184 |
|
| 185 |
freezej.files.each { |
| 186 |
command.add(it.getAbsolutePath() ) |
| 187 |
} |
| 188 |
|
| 189 |
LOGGER.info("processing dependencies:\n${command}") |
| 190 |
|
| 191 |
def sout = new StringBuffer() |
| 192 |
def serr = new StringBuffer() |
| 193 |
|
| 194 |
def p = command.execute(project.slice.env, null) |
| 195 |
p.waitForProcessOutput(sout, serr) |
| 196 |
|
| 197 |
printWarningsAndErrors(serr) |
| 198 |
|
| 199 |
if (p.exitValue() != 0) { |
| 200 |
throw new GradleException("${command[0]} failed with exit code: ${p.exitValue()}") |
| 201 |
} |
| 202 |
|
| 203 |
return parseSliceDependencyXML(new XmlSlurper().parseText(sout.toString())) |
| 204 |
} |
| 205 |
|
| 206 |
// Run slice2freezej. Returns a dictionary of A -> [B] where A is a slice file, and B is the list of |
| 207 |
// generated java source files. |
| 208 |
def executeS2F(freezej) { |
| 209 |
if((freezej.dict == null || freezej.dict.isEmpty()) && (freezej.index == null || freezej.index.isEmpty())) { |
| 210 |
// No build artifacts. |
| 211 |
return [:] |
| 212 |
} |
| 213 |
|
| 214 |
def command = buildS2FCommandLine(freezej) |
| 215 |
freezej.files.each { |
| 216 |
command.add(it.getAbsolutePath() ) |
| 217 |
} |
| 218 |
|
| 219 |
LOGGER.info("processing slice:\n${command}") |
| 220 |
|
| 221 |
def sout = new StringBuffer() |
| 222 |
def serr = new StringBuffer() |
| 223 |
|
| 224 |
def p = command.execute(project.slice.env, null) |
| 225 |
p.waitForProcessOutput(sout, serr) |
| 226 |
|
| 227 |
printWarningsAndErrors(serr) |
| 228 |
|
| 229 |
if (p.exitValue() != 0) { |
| 230 |
throw new GradleException("${command[0]} failed with exit code: ${p.exitValue()}") |
| 231 |
} |
| 232 |
return getS2FGenerated(freezej) |
| 233 |
} |
| 234 |
|
| 235 |
def buildS2FCommandLine(freezej) { |
| 236 |
def command = [] |
| 237 |
command.add(project.slice.slice2freezej) |
| 238 |
command.add("--output-dir=" + project.slice.output.getAbsolutePath()) |
| 239 |
if (project.slice.sliceDir) { |
| 240 |
command.add("-I${project.slice.sliceDir}") |
| 241 |
} |
| 242 |
|
| 243 |
freezej.include.each { |
| 244 |
command.add('-I' + it) |
| 245 |
} |
| 246 |
|
| 247 |
freezej.args.split().each { |
| 248 |
command.add(it) |
| 249 |
} |
| 250 |
|
| 251 |
freezej.dict.each { |
| 252 |
def javaType = it.javaType |
| 253 |
command.add("--dict") |
| 254 |
command.add(javaType + "," + it.key + "," + it.value) |
| 255 |
it.index.each { |
| 256 |
command.add("--dict-index") |
| 257 |
def buf = new StringBuffer() |
| 258 |
buf << javaType |
| 259 |
if(it.containsKey('member')) { |
| 260 |
buf << ',' |
| 261 |
buf << it['member'] |
| 262 |
} |
| 263 |
if(it.containsKey('caseSensitive')) { |
| 264 |
buf << ',' |
| 265 |
if(it['caseSensitive']) { |
| 266 |
buf << "case-sensitive" |
| 267 |
}else { |
| 268 |
buf << "case-insensitive" |
| 269 |
} |
| 270 |
} |
| 271 |
command.add(buf.toString()) |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
freezej.index.each { |
| 276 |
command.add("--index") |
| 277 |
def buf = new StringBuffer() |
| 278 |
buf << it.javaType |
| 279 |
buf << ',' |
| 280 |
buf << it.type |
| 281 |
buf << ',' |
| 282 |
buf << it.member |
| 283 |
buf << ',' |
| 284 |
if(it.caseSensitive) { |
| 285 |
buf << "case-sensitive" |
| 286 |
}else { |
| 287 |
buf << "case-insensitive" |
| 288 |
} |
| 289 |
command.add(buf.toString()) |
| 290 |
} |
| 291 |
return command |
| 292 |
} |
| 293 |
|
| 294 |
// Process the written slice file which is of the format: |
| 295 |
// |
| 296 |
// <state> |
| 297 |
// <timestamp value="YYYY"/> |
| 298 |
// <slice name="A.ice"/> |
| 299 |
// <slice name="B.ice"/> |
| 300 |
// <generated name="B.java"/> |
| 301 |
// <generated name="B.ice"/> |
| 302 |
// </state> |
| 303 |
|
| 304 |
class FreezeJBuildState { |
| 305 |
// Timestamp of last build in milliseconds. |
| 306 |
def timestamp |
| 307 |
|
| 308 |
// List of slice files. |
| 309 |
def slice = [] |
| 310 |
|
| 311 |
// List of generated source files. |
| 312 |
def generated = [] |
| 313 |
|
| 314 |
// List of command line arguments. |
| 315 |
def args = [] |
| 316 |
|
| 317 |
def write(stateFile) { |
| 318 |
def writer = new StringWriter() |
| 319 |
def xml = new MarkupBuilder(writer) |
| 320 |
xml.state { |
| 321 |
xml.timestamp("value": timestamp) |
| 322 |
args.each { |
| 323 |
xml.arg("value": it) |
| 324 |
} |
| 325 |
slice.each { |
| 326 |
xml.slice("name": it) |
| 327 |
} |
| 328 |
generated.each { |
| 329 |
xml.generated("name": it) |
| 330 |
} |
| 331 |
} |
| 332 |
stateFile.write(writer.toString()) |
| 333 |
} |
| 334 |
|
| 335 |
def read(stateFile) { |
| 336 |
if(!stateFile.isFile()) { |
| 337 |
return |
| 338 |
} |
| 339 |
|
| 340 |
try { |
| 341 |
def xml = new XmlSlurper().parse(stateFile) |
| 342 |
|
| 343 |
if(xml.name() != "state") { |
| 344 |
throw new GradleException("malformed XML: expected `state'") |
| 345 |
} |
| 346 |
|
| 347 |
xml.children().each { |
| 348 |
if(it.name() == "arg") { |
| 349 |
args.add(it.attributes().get("value")) |
| 350 |
} else if(it.name() == "timestamp") { |
| 351 |
timestamp = it.attributes().get("value").toLong() |
| 352 |
} else if(it.name() == "slice") { |
| 353 |
slice.add(new File(it.attributes().get("name"))) |
| 354 |
} else if(it.name() == "generated") { |
| 355 |
generated.add(new File(it.attributes().get("name"))) |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
catch(Exception ex) { |
| 360 |
LOGGER.info("invalid XML: ${stateFile}") |
| 361 |
println ex |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
def processJava() { |
| 367 |
// Dictionary of A->[B] where A is a slice file and B is the list of generated |
| 368 |
// source files. |
| 369 |
def generated = [:] |
| 370 |
|
| 371 |
// Set of slice files processed. |
| 372 |
Set built = [] |
| 373 |
|
| 374 |
// Complete set of slice files. |
| 375 |
Set files = [] |
| 376 |
|
| 377 |
def stateFile = new File(project.slice.output, "slice2java.df.xml") |
| 378 |
|
| 379 |
// Dictionary of A->[B] where A is the source set name and B is |
| 380 |
// the JavaSourceSet |
| 381 |
def sourceSet = [:] |
| 382 |
|
| 383 |
// Dictionary of A->[B] where B depends on A for the java task. |
| 384 |
def s2jDependencies = [:] |
| 385 |
|
| 386 |
def state = new JavaBuildState() |
| 387 |
state.read(stateFile) |
| 388 |
|
| 389 |
project.slice.java.each { |
| 390 |
it.args = it.args.stripIndent() |
| 391 |
|
| 392 |
if (it.files == null) { |
| 393 |
it.files = project.fileTree(dir: it.srcDir).include('**/*.ice') |
| 394 |
} |
| 395 |
|
| 396 |
it.files.each { |
| 397 |
if(files.contains(it)) { |
| 398 |
throw new GradleException("${it}: input file specified in multiple source sets") |
| 399 |
} |
| 400 |
files.add(it) |
| 401 |
} |
| 402 |
|
| 403 |
if(!it.files.isEmpty()) { |
| 404 |
s2jDependencies << getS2JDependencies(it) |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
project.slice.java.each { |
| 409 |
processJavaSet(it, s2jDependencies, state, generated, built, sourceSet) |
| 410 |
} |
| 411 |
|
| 412 |
if(built.isEmpty() && !stateFile.isFile()) { |
| 413 |
// The state file has never been written and and we have no |
| 414 |
// files to process. No reason to continue as it will only |
| 415 |
// write an empty dependency file. |
| 416 |
LOGGER.info("nothing to do") |
| 417 |
return |
| 418 |
} |
| 419 |
|
| 420 |
// The set of generated files to remove. |
| 421 |
Set oldGenerated = [] |
| 422 |
|
| 423 |
// Add all of the previously generated files from the slice files that were |
| 424 |
// just built in processJavaSet. |
| 425 |
built.each { |
| 426 |
def d = state.slice[it] |
| 427 |
if(d != null) { |
| 428 |
oldGenerated.addAll(d) |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
// Add to the oldGenerated list the generated files for those slice files |
| 433 |
// no longer are in any source set. |
| 434 |
state.slice.each { |
| 435 |
if(!files.contains(it.key)) { |
| 436 |
oldGenerated.addAll(it.value) |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
// Remove all generated files that were just generated in processJavaSet. |
| 441 |
generated.values().each { |
| 442 |
oldGenerated.removeAll(it) |
| 443 |
} |
| 444 |
|
| 445 |
def newState = new JavaBuildState() |
| 446 |
newState.timestamp = System.currentTimeMillis() |
| 447 |
newState.sourceSet = sourceSet |
| 448 |
// Update the dependencies. |
| 449 |
built.each { |
| 450 |
newState.slice[it] = generated[it] |
| 451 |
} |
| 452 |
files.each { |
| 453 |
if(!built.contains(it)) { |
| 454 |
newState.slice[it] = state.slice[it] |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
// Write the new dependencies file. |
| 459 |
newState.write(stateFile) |
| 460 |
|
| 461 |
deleteFiles(oldGenerated) |
| 462 |
} |
| 463 |
|
| 464 |
def processJavaSet(java, s2jDependencies, state, generated, built, sourceSet) { |
| 465 |
def ss = new JavaSourceSet() |
| 466 |
ss.args = buildS2JCommandLine(java) |
| 467 |
java.files.each { |
| 468 |
ss.slice.add(it) |
| 469 |
} |
| 470 |
sourceSet[java.name] = ss |
| 471 |
|
| 472 |
// The JavaSourceSet from the previous build. |
| 473 |
def prevSS = state.sourceSet[java.name] |
| 474 |
|
| 475 |
Set toBuild = [] |
| 476 |
|
| 477 |
// If the source set is new, the sourceSet arguments are different, |
| 478 |
// or the slice2java compiler (always the first argument) has been updated, |
| 479 |
// then rebuild all slice files. |
| 480 |
if(prevSS == null || ss.args != prevSS.args || getTimestamp(new File(ss.args[0])) > state.timestamp) { |
| 481 |
java.files.each { |
| 482 |
toBuild.add(it) |
| 483 |
} |
| 484 |
} else { |
| 485 |
// s2jDependencies is populated in getInputFiles. |
| 486 |
java.files.each { sliceFile -> |
| 487 |
// Build the slice file if it wasn't built before in this source set, |
| 488 |
// or its timestamp is newer than the last build time, |
| 489 |
// or any of its dependencies have a timestamp newer than the last build time. |
| 490 |
if(!prevSS.slice.contains(sliceFile) || (getTimestamp(sliceFile) > state.timestamp) || |
| 491 |
s2jDependencies[sliceFile].any { dependency -> |
| 492 |
getTimestamp(dependency) > state.timestamp |
| 493 |
}) { |
| 494 |
toBuild.add(sliceFile) |
| 495 |
} |
| 496 |
// |
| 497 |
// Check that any previously generated files still exist |
| 498 |
// |
| 499 |
else if(state.slice[sliceFile] != null && state.slice[sliceFile].any { generatedFile -> |
| 500 |
!generatedFile.isFile() || getTimestamp(generatedFile) > state.timestamp |
| 501 |
}) { |
| 502 |
toBuild.add(sliceFile) |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
// Bail out if there is nothing to do (in theory this should not occur) |
| 508 |
if(toBuild.isEmpty()) { |
| 509 |
LOGGER.info("nothing to do") |
| 510 |
return |
| 511 |
} |
| 512 |
|
| 513 |
LOGGER.info("running slice2java on the following slice files") |
| 514 |
toBuild.each { |
| 515 |
LOGGER.info(" ${it}") |
| 516 |
} |
| 517 |
|
| 518 |
// Update the set of java source files generated and the slice files processed. |
| 519 |
generated << executeS2J(java, toBuild) |
| 520 |
built.addAll(toBuild) |
| 521 |
} |
| 522 |
|
| 523 |
def buildS2JCommandLine(java) { |
| 524 |
def command = [] |
| 525 |
command.add(project.slice.slice2java) |
| 526 |
if (project.slice.sliceDir) { |
| 527 |
command.add("-I${project.slice.sliceDir}") |
| 528 |
} |
| 529 |
|
| 530 |
java.include.each { |
| 531 |
command.add('-I' + it) |
| 532 |
} |
| 533 |
|
| 534 |
java.args.split().each { |
| 535 |
command.add(it) |
| 536 |
} |
| 537 |
|
| 538 |
if(project.slice.compat) { |
| 539 |
command.add('--compat') |
| 540 |
} |
| 541 |
|
| 542 |
// |
| 543 |
// If the ice version is less than 3.7 it is still possible that we are compiling |
| 544 |
// 3.7 slice files. |
| 545 |
// |
| 546 |
// compareIceVersion returns -1 if iceVersion < 3.7 |
| 547 |
// |
| 548 |
if(project.slice.compareIceVersion('3.7') == -1) { |
| 549 |
command.add('-D__SLICE2JAVA_COMPAT__') |
| 550 |
} |
| 551 |
|
| 552 |
return command |
| 553 |
} |
| 554 |
|
| 555 |
// Run slice2java. Returns a dictionary of A -> [B] where A is a slice file, |
| 556 |
// and B is the list of produced java source files. |
| 557 |
def executeS2J(java, files) { |
| 558 |
def command = buildS2JCommandLine(java) |
| 559 |
command.add("--list-generated") |
| 560 |
command.add("--output-dir=" + project.slice.output.getAbsolutePath()) |
| 561 |
files.each { |
| 562 |
command.add(it.getAbsolutePath()) |
| 563 |
} |
| 564 |
|
| 565 |
LOGGER.info("processing slice:\n${command}") |
| 566 |
|
| 567 |
def sout = new StringBuffer() |
| 568 |
def serr = new StringBuffer() |
| 569 |
|
| 570 |
def p = command.execute(project.slice.env, null) |
| 571 |
p.waitForProcessOutput(sout, serr) |
| 572 |
|
| 573 |
printWarningsAndErrors(serr, sout) |
| 574 |
|
| 575 |
if (p.exitValue() != 0) { |
| 576 |
throw new GradleException("${command[0]} failed with exit code: ${p.exitValue()}") |
| 577 |
} |
| 578 |
return parseGeneratedXML(new XmlSlurper().parseText(sout.toString())) |
| 579 |
} |
| 580 |
|
| 581 |
// Executes slice2java to determine the slice file dependencies. |
| 582 |
// Returns a dictionary of A -> [B] where A depends on B. |
| 583 |
def getS2JDependencies(java) { |
| 584 |
def command = buildS2JCommandLine(java) |
| 585 |
command.add("--depend-xml") |
| 586 |
java.files.each { |
| 587 |
command.add(it.getAbsolutePath() ) |
| 588 |
} |
| 589 |
|
| 590 |
LOGGER.info("processing dependencies:\n${command}") |
| 591 |
|
| 592 |
def sout = new StringBuffer() |
| 593 |
def serr = new StringBuffer() |
| 594 |
|
| 595 |
def p = command.execute(project.slice.env, null) |
| 596 |
p.waitForProcessOutput(sout, serr) |
| 597 |
|
| 598 |
printWarningsAndErrors(serr) |
| 599 |
|
| 600 |
if (p.exitValue() != 0) { |
| 601 |
throw new GradleException("${command[0]} failed with exit code: ${p.exitValue()}") |
| 602 |
} |
| 603 |
|
| 604 |
LOGGER.info("processing dependencies:\n${command}") |
| 605 |
|
| 606 |
return parseSliceDependencyXML(new XmlSlurper().parseText(sout.toString())) |
| 607 |
} |
| 608 |
|
| 609 |
// Cache of file -> timestamp. |
| 610 |
def timestamps = [:] |
| 611 |
|
| 612 |
// Get the last modified time for the file. Note that this time is in ms. |
| 613 |
def getTimestamp(file) { |
| 614 |
if(timestamps.containsKey(file)) { |
| 615 |
return timestamps[file] |
| 616 |
} |
| 617 |
|
| 618 |
if(!file.isFile()) { |
| 619 |
throw new GradleException("${file}: cannot stat") |
| 620 |
} |
| 621 |
|
| 622 |
def t = file.lastModified() |
| 623 |
timestamps[file] = t |
| 624 |
return t |
| 625 |
} |
| 626 |
|
| 627 |
class JavaSourceSet { |
| 628 |
// List of slice files. |
| 629 |
def slice = [] |
| 630 |
|
| 631 |
// List of arguments. |
| 632 |
def args = [] |
| 633 |
} |
| 634 |
|
| 635 |
// Process the written slice file which is of the format: |
| 636 |
// |
| 637 |
// <state> |
| 638 |
// <timestamp value="xxxx"/> |
| 639 |
// <sourceSet name="default"> |
| 640 |
// <source name="A.ice"> |
| 641 |
// <file name="Demo/Foo.java"/> |
| 642 |
// </source> |
| 643 |
// <source name="Hello.ice"/> |
| 644 |
// <arg value="-I."/> |
| 645 |
// </sourceSet> |
| 646 |
// </state> |
| 647 |
// |
| 648 |
class JavaBuildState { |
| 649 |
// Timestamp of last build in milliseconds. |
| 650 |
def timestamp |
| 651 |
|
| 652 |
// List of slice files. Dictionary of A -> [B] where B is the list of generated java |
| 653 |
// source files. |
| 654 |
def slice = [:] |
| 655 |
|
| 656 |
// Dictionary of source set -> JavaSourceSet |
| 657 |
def sourceSet = [:] |
| 658 |
|
| 659 |
def write(stateFile) { |
| 660 |
|
| 661 |
def writer = new StringWriter() |
| 662 |
def xml = new MarkupBuilder(writer) |
| 663 |
xml.state { |
| 664 |
xml.timestamp("value": timestamp) |
| 665 |
sourceSet.each { |
| 666 |
def key = it.key |
| 667 |
def value = it.value |
| 668 |
xml.sourceSet("name": key) { |
| 669 |
value.slice.each { |
| 670 |
def file = it |
| 671 |
xml.source("name": file) { |
| 672 |
slice[file].each { |
| 673 |
xml.file("name": it) |
| 674 |
} |
| 675 |
} |
| 676 |
} |
| 677 |
value.args.each { |
| 678 |
xml.arg("value": it) |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
stateFile.write(writer.toString()) |
| 685 |
} |
| 686 |
|
| 687 |
def read(stateFile) { |
| 688 |
if(!stateFile.isFile()) { |
| 689 |
return |
| 690 |
} |
| 691 |
|
| 692 |
try { |
| 693 |
def xml = new XmlSlurper().parse(stateFile) |
| 694 |
if(xml.name() != "state") { |
| 695 |
throw new GradleException("malformed XML: expected `state'") |
| 696 |
} |
| 697 |
|
| 698 |
xml.children().each { |
| 699 |
if(it.name() == "timestamp") { |
| 700 |
timestamp = it.attributes().get("value").toLong() |
| 701 |
} else if(it.name() == "sourceSet") { |
| 702 |
def ss = new JavaSourceSet() |
| 703 |
def name = it.attributes().get("name") |
| 704 |
it.children().each { |
| 705 |
if(it.name() == "arg") { |
| 706 |
ss.args.add(it.attributes().get("value")) |
| 707 |
} else if(it.name() == "source") { |
| 708 |
|
| 709 |
def source = new File(it.attributes().get("name")) |
| 710 |
def files = [] |
| 711 |
it.children().each { |
| 712 |
if(it.name() == "file") { |
| 713 |
files.add(new File(it.attributes().get("name"))) |
| 714 |
} |
| 715 |
} |
| 716 |
slice[source] = files |
| 717 |
ss.slice.add(source) |
| 718 |
} |
| 719 |
} |
| 720 |
sourceSet[name] = ss |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
catch(Exception ex) { |
| 725 |
LOGGER.error("invalid XML: ${stateFile}") |
| 726 |
LOGGER.error(ex) |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
// Process the generated XML which is of the format: |
| 732 |
// |
| 733 |
// <generated> |
| 734 |
// <source name="A.ice"> |
| 735 |
// <file name="Demo/_HelloOperations.java"/> |
| 736 |
// </source> |
| 737 |
// </generated> |
| 738 |
def parseGeneratedXML(xml) { |
| 739 |
if(xml.name() != "generated") { |
| 740 |
throw new GradleException("malformed XML: expected `generated'") |
| 741 |
} |
| 742 |
|
| 743 |
def generated =[:] |
| 744 |
xml.children().each { |
| 745 |
if(it.name() == "source") { |
| 746 |
def source = it.attributes().get("name") |
| 747 |
def files = [] |
| 748 |
it.children().each { |
| 749 |
if(it.name() == "file") { |
| 750 |
files.add(new File(it.attributes().get("name"))) |
| 751 |
} |
| 752 |
} |
| 753 |
generated.put(new File(source), files) |
| 754 |
|
| 755 |
} |
| 756 |
} |
| 757 |
return generated |
| 758 |
} |
| 759 |
|
| 760 |
// Parse the dependency XML which is of the format: |
| 761 |
// |
| 762 |
// <dependencies> |
| 763 |
// <source name="A.ice"> |
| 764 |
// <dependsOn name="Hello.ice"/> |
| 765 |
// </source> |
| 766 |
// <source name="Hello.ice"> |
| 767 |
// </source> |
| 768 |
// </dependencies> |
| 769 |
def parseSliceDependencyXML(xml) { |
| 770 |
if(xml.name() != "dependencies") { |
| 771 |
throw new GradleException("malformed XML") |
| 772 |
} |
| 773 |
|
| 774 |
def dependencies =[:] |
| 775 |
xml.children().each { |
| 776 |
if(it.name() == "source") { |
| 777 |
def source = it.attributes().get("name") |
| 778 |
def files = [] |
| 779 |
it.children().each { |
| 780 |
if(it.name() == "dependsOn") { |
| 781 |
def dependsOn = new File(it.attributes().get("name")) |
| 782 |
files.add(dependsOn) |
| 783 |
} |
| 784 |
} |
| 785 |
dependencies.put(new File(source), files) |
| 786 |
} |
| 787 |
} |
| 788 |
return dependencies |
| 789 |
} |
| 790 |
|
| 791 |
def deleteFiles(files) { |
| 792 |
if(!files.isEmpty()) { |
| 793 |
LOGGER.info("the following generated java source files will be deleted") |
| 794 |
files.each { |
| 795 |
LOGGER.info(" ${it}") |
| 796 |
} |
| 797 |
|
| 798 |
String buildDirPath = project.slice.output.getPath() |
| 799 |
files.each { |
| 800 |
String parent = it.getParent() |
| 801 |
if(!parent.startsWith(buildDirPath)) { |
| 802 |
LOGGER.info("not removing ${it} as it is outside the build dir ${buildDirPath}") |
| 803 |
} else { |
| 804 |
it.delete() |
| 805 |
} |
| 806 |
} |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
def printWarningsAndErrors(serr, sout = null) { |
| 811 |
def lineSep = System.getProperty("line.separator") |
| 812 |
def errLines = serr.toString().split(lineSep).findAll { !it.trim().isEmpty() } |
| 813 |
def outLines = [] |
| 814 |
|
| 815 |
// |
| 816 |
// stdout is always xml |
| 817 |
// |
| 818 |
if(sout) { |
| 819 |
def xml = new XmlSlurper().parseText(sout.toString()) |
| 820 |
if(xml.name() != "generated") { |
| 821 |
throw new GradleException("malformed XML: expected `generated'") |
| 822 |
} |
| 823 |
|
| 824 |
xml.children().each { |
| 825 |
if(it.name() == "source") { |
| 826 |
it.children().each { |
| 827 |
if(it.name() == "output") { |
| 828 |
def lines = it.text().split(lineSep).findAll { !it.trim().isEmpty() } |
| 829 |
outLines.addAll(lines) |
| 830 |
} |
| 831 |
} |
| 832 |
} |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
def warningMatch = /(.*):[0-9]+:\s+warning:(.*)/ |
| 837 |
|
| 838 |
for(line in errLines + outLines) { |
| 839 |
switch(line) { |
| 840 |
case ~warningMatch: |
| 841 |
LOGGER.warn(line.trim()) |
| 842 |
break |
| 843 |
default: |
| 844 |
// These should all be errors |
| 845 |
LOGGER.error(line.trim()) |
| 846 |
break |
| 847 |
} |
| 848 |
} |
| 849 |
} |
| 850 |
} |