Proto library target: How to force regeneration of proto files after protoc is updated?

215 views
Skip to first unread message

Hong Xu

unread,
May 27, 2022, 7:12:17 PM5/27/22
to gn-dev
Hi all,


To build these targets, *.proto files are fed to the protoc executable and protoc generates some C++ files to be used to build a library.

Here's the problem: If protoc (part of the protobuf package) is updated, ninja won't invoke protoc to regenerate files, unless the *.proto files are updated. However, protoc often generates different files across versions. Does gn support any pragmatic approach to let protoc regenerate files upon an update of protoc?

Thanks,
Hong

K. Moon

unread,
May 27, 2022, 8:38:50 PM5/27/22
to Hong Xu, gn-dev
I think you'd need your target to depend on protoc? It looks like the target just assumes protoc is present in the environment, rather than building it from scratch.

Roland McGrath

unread,
May 27, 2022, 8:43:30 PM5/27/22
to Hong Xu, gn-dev
In an `action()` target, `script`, `sources`, and `inputs` are all used as ninja file dependencies.  There is no concrete difference between `sources` and `inputs` for action(), but the convention recommended by `gn help action` is to use `sources` for the things you think of as the inputs to the action per se (such as `.proto` source files) and to use inputs for auxiliary things like other tools or configuration files that the script might use.  So the right approach here is to make sure that the actual protoc binary that is invoked by the Python script used as `script` in the `action()` is listed in `inputs`, along with any other files that Python or things it spawns might read, including other `.py` files (but excluding the `script` file itself, since that's already implicitly tracked).

If it's not so easy to determine all the right paths to those things in the GN template code, then the other approach is to use `depfile` in the `action()` and add logic to your Python code that will emit that file with the dynamic list of inputs used, including the protoc binary path.

See `gn help action` and `gn help depfile` for more details.  You can almost certainly find some existing examples using `depfile` and look at how those scripts generate the dep file text format.

On Fri, May 27, 2022 at 4:12 PM 'Hong Xu' via gn-dev <gn-...@chromium.org> wrote:
Reply all
Reply to author
Forward
0 new messages