skvadrik / re2c

Lexer generator for C, C++, Go and Rust.
https://re2c.org
Other
1.09k stars 170 forks source link

Option to generate depfiles #343

Closed vinipsmaker closed 3 years ago

vinipsmaker commented 3 years ago

Hi,

GCC has an option to generate depfiles using a Makefile-like syntax. For instance:

// foo.c
#include "bar.h"

If you call gcc -MMD foo.c, it'll generate the following file:

foo.o: foo.c bar.h

ninja supports reading those depfiles and converting them to ninja's internal dependency format, and meson lets you specify a "depfile" argument to e.g. custom_target() or generator().

eli-schwartz

I'm using meson to describe the build of my project and I have a few files to be processed through re2c. Some of the files use the /*!include:re2c "my-re-defs.re" */ re2c's syntax and it'd be nice to have them automatically rebuilt when the included file changes. The build system can take care of this if re2c accepts an option to generate a second output (the depfile). Can we have an option to build a depfile alongside the main output?

skvadrik commented 3 years ago

Hi, that's a good idea. I thought something was missing about include files. :)

Here is an initial implementation: https://github.com/skvadrik/re2c/commit/3ba9108047f516a29e62493a04c00651acf02c9a, to be used as re2c file.re -o file.c --depfile file.d. I'll add documentation and tests later. Let me know if it works for you. I opted out of -MMD because that doesn't fall in line with re2c convention on short/long options, and I opted into passing an explicit depfile argument, as it is more generic. But it's not set in stone until the next release.

vinipsmaker commented 3 years ago

Here is an initial implementation

Wow that was quick. Thanks.

Let me know if it works for you.

It didn't. I also use the option -I so the output is pointing to a non-existent file. Here's a sample output:

emilua.p/actor.cpp: ../src/actor.ypp emilua/modules.re

I used the flag -I ../include so I expect the output will be in the likes of:

emilua.p/actor.cpp: ../src/actor.ypp ../include/emilua/modules.re

(given modules.re was only found in the directory passed as a -I arg)

I looked on one of the depfiles generated by the compiler in the same project and I think this change should be enough.

skvadrik commented 3 years ago

Thanks for checking, fixed in https://github.com/skvadrik/re2c/commit/a30aa9fabca3eb5939f78222b14ede3b3500466c.

vinipsmaker commented 3 years ago

fixed in a30aa9f

It's working now. Thank you.

vinipsmaker commented 3 years ago

Here's an example on how to use re2c depfiles on meson: https://gitlab.com/emilua/emilua/-/commit/37cccb233749f91fca65af8ab2965a35b2df6305#0cc1139e3347f573ae1feee5b73dbc8a8a21fcfa_40_41

You just need to specify some template string for the generator()'s depfile argument and then reference '@DEPFILE@' in re2c arguments.

skvadrik commented 3 years ago

Thanks for sharing the example!