4

I'm building C/C++ code with gradle under Linux (gcc) and Windows (VS10) like it's described in Building native software documentation of gradle.

The build process is fine and I can start my application. If errors occur during the build process you can see the compiler output like warnings and errors on the console. This output is additionally written to a file: build/tmp/"TaskName"/output.txt. The problem is that this output is not displayed on the console if the build runs without errors, nevertheless the file is written.

I'm searching for a way to display the compiler output like warnings or errors on console even if the build succeeded.

It's possible to start the gradle build with a higher log level: gradle build --info, but there is so much addional output that I do not want.

I already tried something like the code below in the build.gradle file to set the log level only for all compiling tasks, but it makes no difference as I would start the gradle build just with: gradle build.

tasks.whenTaskAdded { task ->
   if (task instanceof CCompile || task instanceof CppCompile) {
      logging.setLevel(LogLevel.INFO)
   }
}

My question:

Is there any way to get the compiler errors/warnings on the console without the overhead? Is it possible at all to set the LogLevel for some tasks? If so what's wrong with the code above?

Any help will be appreciated.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
  • Seems like a [issue for gradle](https://discuss.gradle.org/t/how-to-report-a-bug/12055) even if it may be just a documentation issue. – stefaanv May 23 '17 at 07:13

2 Answers2

1

I solved it now because the already existing answer of Johan Engblom pointed me into the right direction. Instead of using cat outputfile.txt which is platform dependent (Linux), I use the capabilties of gradle so that it will work on either Windows or Linux. Sure that solution does not output the compiler output while it is compiling, but it outputs it after the job has finished.

To add a task after the compile and link task I add the following (this task will be executed if the build failed or succeeded).

tasks.withType(InstallExecutable) {
   finalizedBy showCompilerOutput
}

If you just want to execute showCompilerOutput if the build was successful you can use this:

build.finalizedBy showCompilerOutput

The task showCompilerOutput is for displaying the output of the files I mentioned in my question. Therefore it builds a file tree which points to the files that I mentioned: output.txt.

task showCompilerOutput {
   dependsOn showCompilerOptions
   doLast {
      println '\n-----------------------------------------------------\n'
      println 'Compiler output:'
      FileTree tree = fileTree('build').include('**/output.txt')
      // Iterate over the contents of a tree
      tree.each {File file ->
          println 'Content of file ' + file + ':\n'
          println file.text
          println '------\n'
      }
   }
}

This task is also dependend of another task which is called showCompilerOptions, which is doing the same but for the files with the compiler options: options.txt.

task showCompilerOptions {
   doLast {
      println '\n-----------------------------------------------------\n'
      println 'Compiler options:'
      FileTree tree = fileTree('build').include('**/options.txt')
      // Iterate over the contents of a tree
      tree.each {File file ->
          println 'Content of file ' + file + ':\n'
          println file.text
          println '------\n'
      }
   }
}
Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
0

just add a cat outputfile.txt after you have run your gradle command

Johan Engblom
  • 215
  • 1
  • 12
  • That would be easy but in the output folder (`build/tmp/`) there are a lot of folders (`"TaskName"`) like: `compileBasecodeLinux_x86_64SharedLibraryBasecodeCpp` or `linkBasecodeLinux_x86_64SharedLibrary`. I would have to extend your answer to go into every folder and output it. Either I concat every name for my self or I go into every folder that exists.Thank you for your idea! – Andre Kampling Jun 08 '17 at 08:29