How does compileDebugCpp task recognize whether project FROM-CACHE, UP-TO-DATE or has to be compiled?

I am trying to develop a custom gradle plugin for c/c++ code. This custom plugin task should run only when there is source code change which requires compilation.

I understand that default compileDebugCpp task already compiles only when required else it says FROM-CACHE or UP-TO-DATE .

  1. How do i achieve this if possible in my custom task?
  2. Has gradle exposed any API which conveys whether task compileDebugCpp was actually executed for a project? so that i can use that information in my custom plugin task and decide to run the task.