List goals/targets in GNU make that contain variab

2019-01-20 22:17发布

I have a fairly large makefile that creates a number of targets on the fly by computing names from variables. (eg foo$(VAR) : $(PREREQS)). Is there any way that gnu make can be convinced to spit out a list of targets after it has expanded these variables?

I'd like to be able to get the targets for an aribitrary makefile. I'm trying to write a completion function for my shell.

16条回答
三岁会撩人
2楼-- · 2019-01-20 22:23

Can you parse the output from make -pn (i.e. make --print-data-base --dry-run)? It prints out all the variables, rules, implicit rules and which commands will be run in laborious detail.

查看更多
混吃等死
3楼-- · 2019-01-20 22:23

Guess I'm a little late to this party, but if you're looking for a specific command you could try

make -qp | grep -v '^# ' | grep -v '^[[:space:]]' | grep --only-matching '^.*:'

This mostly works, although it might still include some non-target stuff like a vpath directive. If you don't depend on make's built-in rules, you can use make -qpR as the first command in the pipeline.

查看更多
爷、活的狠高调
4楼-- · 2019-01-20 22:23

Ruby solution:

`make -qp`.split("\n").select{|l| l =~ /^\w/ && l !~ /=/}.map{|l| l.sub(/:.*/,'')}
查看更多
一纸荒年 Trace。
5楼-- · 2019-01-20 22:25

Im working on solaris 10 anda turbo C shell. The given solution doesn´t work for my makefile project. even after altering the command line above to tcsh syntax. However, I found out you can get an easy solution using

remake --tasks | grep -v "clean some static output or just grep tabs on start of line" | awk ´{print $1}´

remake version:

remake --version

is

GNU Make 3.82+dbg0.8
Built for sparc-sun-sparc2-9

and some other unimportant version data

查看更多
乱世女痞
6楼-- · 2019-01-20 22:27

I'm not sure if this is only a gnu make thing, but this works well:

make help

查看更多
Deceive 欺骗
7楼-- · 2019-01-20 22:29

This will give a nice output:

make -pn | perl -F: -ane 'print "$F[0]\n" if /^\w+\s*:/' | sort
查看更多
登录 后发表回答