Use a custom rule for API files generation
@mihaimaruseac, can you review? @meteorcloudy this is causing the internal build to fail: [cl/520986273](http://cl/520986273). Can you take a look? Yes, will do! Great to have this merged.
From https://github.com/tensorflow/tensorflow/issues/60167#issuecomment-1490907122, this is only a partial fix. How do you plan to finish removing the duplicate compilation? Actually the change was automatically rollbacked due to some breakages, see https://github.com/tensorflow/tensorflow/issues/60298. Working on a rollforwad with fix.

I think the API gen rule is the major one, theoretically we can also fix other similar genrule in a similar way. But in long term,  I think there should be a way to tell Bazel that my target and exec are the exact same configuration, so Bazel can avoid building the target twice. /cc @gregestren @aiuto

On the other hand, I wonder if there is any way to avoid using those genrule at all? /cc @learning-to-play  @mihaimaruseac  In the mean time, can we roll back the commit that caused the regression?
I suppose what you propose will take times to get it. @nouiz Unfortunately, that will prevent updating Bazel to 6.x for TensorFlow. I think with the fix the performance regression is much less significant, which should be fine? The fix is now rolled forward at https://github.com/tensorflow/tensorflow/commit/da439b7d05584f93e745511d66f6d74a58b54b34 I think getting rid of API generation genrule and requiring the files to be checked in is a long-term solution, needs several changes, including adding CI to test that the API files are up to date.
To prevent compiling the C++ code twice, we only want to build `api_gen_binary_target` for the target platform and not the execution platform.
To achieve this without causing confusion with source dependencies (e.g. putting `api_gen_binary_target` in `srcs` of the `genrule`), we use a custom rule to execute the command line for generating the API files.
Fixes https://github.com/tensorflow/tensorflow/issues/60167