Although this question involves the C language, the Gradle C Plugin, and an oldschool C static analyzer called splint, I believe this question can be answered by any Gradle guru who understands how to wire a Gradle build up to an executable process.
It's very simple: I have configured splint locally to analyze my simple C project's source code with the following command line:
splint +never-include -retvalint src/derpus/c/*.c
I am managing my project's build via Gradle (the C plugin), and would now like to invoke static analysis (splint) at the appropriate point in the build sequence (whatever that may be).
splint outputs to the console, and unfortunately no where else. And so I would like to see if I can "hook" this console output, check for certain keywords ("error", "warning", etc.) and fail/halt the build if splint complained about anything.
And so I'm trying to tackle several problems here:
My best attempt thus far is:
task check(type:Exec) {
commandLine 'cmd', '/c', 'C:/splint-3.1.1/bin/splint.exe', '+never-include', '-retvalint', 'src/derpus/c/*.c'
standardOutput = new ByteArrayOutputStream()
doLast {
String output = standardOutput.toString()
if(output.contains("error") || output.contains("")) {
println "Chuggington!"
} else {
println "Meeska! Mooseka! Mickey Mouse! Output is: ${output}"
}
}
}
This produces:
Defining custom ▒check▒ task is deprecated when using standard lifecycle plugin has been deprecated and is scheduled to be removed in Gradle 3.0
:checkSplint 3.1.1 --- 12 April 2003
Finished checking --- no warnings
Chuggington!
BUILD SUCCESSFUL
Total time: 3.327 secs
However I'm 100% confident my workingDir
and commandLine
args are incorrect, I'm not sure how I can fail/halt the build from inside that if
-statement, and I'm not sure how to "position" this check
task to occur before compilation and testing.
Any ideas, Gradle gurus?
workingDir
is a directory where this tool should be run not where it's located. Typically it's project directory. When it comes to commandLine try:
commandLine 'cmd', '/c', 'splint.exe', '+never-include', '-retvalint', 'src/derpus/c/*.c'
In command above all the arguments should be separated with a comma: ,
.
You can omit assignment operator =
.
When it comes to parsing output - it doesn't work because in the closure it's not generated yet. Try:
standardOutput = new ByteArrayOutputStream()
doLast {
String output = standardOutput.toString()
if(output.contains("error") || output.contains("")) {
println "Chuggington!"
} else {
println "Meeska! Mooseka! Mickey Mouse!"
}
}