I have a Makefile of a project. It is valid, and it works: if I type make, I'll get a binary compiled in the current folder. I want to find a way to programmatically (from a script) get the binary's name or, if make produces multiple files, get full list of these files.
I suppose there can be three ways to do this:
make -d or something similar and analyze its output to get filenames. How reliable is it? Will it work for all Makefiles or only for the most standard ones?make command which cleans all object files, so I get only resulting binaries in the build directory. Is there such command?Makefile's content and get all information from it with a foreign utility. Again, how reliable can it be?I want to find a universal solution for any Makefile-based project but if there is a particular solution for those generated by CMake, it would help me in some cases, too.
That's an interesting problem. A question: what's a binary?
Suppose I have a Makefile which looks like
foo.pdf foo.aux: foo.tex
pdflatex foo
foo.tex: foo.tex.in edits.sed
sed -f edits.sed foo.tex.in >foo.tex
PRODUCTS: foo.pdf
(that is, not just a Makefile which builds an executable program). Is foo.pdf a binary? Is foo.aux, which is a secondary product? Is foo.tex, which is an intermediate one, and which isn't a 'binary' in any standard sense? Is PRODUCTS, which is a dummy target, but which is still the target of a dependency rule, even a 'binary'? Thinking along these lines, I suspect that there's no really useful, even slightly fixed, definition of what a 'binary' is in the sense you appear to be looking for. That is, I suspect that the question you've asked might be vaguer than it appears.
Therefore, it might be better to step up a level, forget about binaries, and ask if there's any way of extracting the dependency graph of a Makefile. Once you've got that, you can look at it and identify those things which are a 'binary' in the sense you're looking for.
How do you get that dependency graph? The GNU 'make' info page mentions dependency graphs, but no more detail, and in particular doesn't provide a way of emitting the one that it generates internally.
It might be reasonably easy, in fact, to simply construct the graph for yourself by analysing the Makefile. And there we go – googling 'makefile parser' does produce a few hits: Perl only as far as I can see, but you can probably do something with that.
This is a long way of saying that I can't think of a nifty faster way (patching automake would be one route, if you were generating these Makefiles by that route, but your question suggests otherwise).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With