Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Solved] Makefile: listing targets
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
juniper
l33t
l33t


Joined: 22 Oct 2004
Posts: 959
Location: we the north

PostPosted: Wed Mar 20, 2024 4:45 pm    Post subject: [Solved] Makefile: listing targets Reply with quote

I am trying to make a makefile. I read that make doesn't have a default command to list targets so I put

Code:

list:###list all targets
        @grep '^[^#[:space:]].*:' makefile


This does the trick, but I want to understand the command. Does it look for anything and then a colon? I would like to modify this command. I want it to only print certain targets, and the targets I want it to print are ones that I have a comment after. So it would find

target: dependency ###this target does X

rather than just

target: dependency

How would I modify the grep command? Thanks.


Last edited by juniper on Mon Apr 01, 2024 7:27 pm; edited 1 time in total
Back to top
View user's profile Send private message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3408

PostPosted: Wed Mar 20, 2024 6:01 pm    Post subject: Reply with quote

Code:
^[^#[:space:]].*:

means:
^ at the start of line find
[^ exactly 1 character NOT listed (therefore anything but # and variants of horizontal white spaces) ]
.* any string (including 0-length)
: a literal colon

So, it is looking for colons which are not a part of comments.
It might be a good idea to drop the start of line anchor (initial ^ ) to filter out lines containing a command followed by a comment, if makefile syntax allows their existence.
E.g. the regex you posted
matches line 'this is fine' # Oppsie: insert "room on fire" meme here
but_NOT_this_line: starting with a space


Quote:
target: dependency ###this target does X

grep '^[^#]\{1,\}:[^#].*#'
[^#]\{1,\} means from 1 to any occurrences of not #
_________________
Make Computing Fun Again
Back to top
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 3685
Location: Rasi, Finland

PostPosted: Wed Mar 20, 2024 11:09 pm    Post subject: Re: Makefile: listing targets Reply with quote

juniper wrote:
I am trying to make a makefile. I read that make doesn't have a default command to list targets
... Maybe not quite:
Jose Quinteiro wrote:
FreeBSD and NetBSD make(1) have a built-in variable called .ALLTARGETS. You can print out its contents like this

Code:
make -V .ALLTARGETS

and
Timmmm wrote:
As of Jan 8th, 2024, Make has a --print-targets option that should do this properly without hacky regexes. The current version is Make 4.4.1 so the next release after that will have this feature.


Also there are lot's of these workarounds. ;)
_________________
..: Zucca :..

My gentoo installs:
init=/sbin/openrc-init
-systemd -logind -elogind seatd

Quote:
I am NaN! I am a man!
Back to top
View user's profile Send private message
juniper
l33t
l33t


Joined: 22 Oct 2004
Posts: 959
Location: we the north

PostPosted: Thu Mar 21, 2024 5:30 pm    Post subject: Reply with quote

hmmmm. make -V .ALLTARGETS (is that a period before ALLTARGETS?) doesn't seem to work.

I sort of like szatox's hacky version because, using comments, I can control which targets are printed and with comments.
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 22601

PostPosted: Thu Mar 21, 2024 5:49 pm    Post subject: Reply with quote

Yes, that is a leading period. Leading periods are a common convention for Makefile pseudo-targets. With which make did you try that? The quote only says FreeBSD and NetBSD make have it. On typical Gentoo systems, the most likely make is GNU make.
Back to top
View user's profile Send private message
juniper
l33t
l33t


Joined: 22 Oct 2004
Posts: 959
Location: we the north

PostPosted: Mon Apr 01, 2024 7:27 pm    Post subject: Reply with quote

I am using the default make on gentoo. So no it's not working

I am using szatox's hacky make. Works like a charm for me!
Back to top
View user's profile Send private message
JustAnother
Apprentice
Apprentice


Joined: 23 Sep 2016
Posts: 191

PostPosted: Fri Apr 05, 2024 2:26 am    Post subject: Reply with quote

Try this:
Code:
remake --targets
Back to top
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 3685
Location: Rasi, Finland

PostPosted: Sun Apr 07, 2024 9:19 am    Post subject: Reply with quote

JustAnother,
    I think Juniper was looking a way to accomplish this with the system provided make. In this case, GNU make.

_________________
..: Zucca :..

My gentoo installs:
init=/sbin/openrc-init
-systemd -logind -elogind seatd

Quote:
I am NaN! I am a man!
Back to top
View user's profile Send private message
juniper
l33t
l33t


Joined: 22 Oct 2004
Posts: 959
Location: we the north

PostPosted: Mon Apr 08, 2024 4:46 pm    Post subject: Reply with quote

Zucca wrote:
JustAnother,
    I think Juniper was looking a way to accomplish this with the system provided make. In this case, GNU make.


Zucca is correct. I am using the make that comes standard with gentoo.

@JustAnother. that didn't work.
Back to top
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 3685
Location: Rasi, Finland

PostPosted: Mon Apr 08, 2024 6:40 pm    Post subject: Reply with quote

GNU make 4.4.2 should have support for --print-targets, but if users of your software use stable distros (your users use non-rolling, stable distros, like debian, RH, etc) you're better off with the hacks.
_________________
..: Zucca :..

My gentoo installs:
init=/sbin/openrc-init
-systemd -logind -elogind seatd

Quote:
I am NaN! I am a man!
Back to top
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 3685
Location: Rasi, Finland

PostPosted: Thu Sep 12, 2024 8:26 pm    Post subject: Reply with quote

I recently had a need for this too so I came up with this:
Code:
list: # List targets
        @awk '/^[[:alnum:]]+[^[:space:]]+:/ {printf "%s",substr($$1,1,length($$1)-1); if (match($$0,/#/)) {desc=substr($$0,RSTART+1); sub(/^[[:space:]]+/,"",desc); printf " - %s\n",desc} else printf "\n" }' "$(firstword $(MAKEFILE_LIST))"

_________________
..: Zucca :..

My gentoo installs:
init=/sbin/openrc-init
-systemd -logind -elogind seatd

Quote:
I am NaN! I am a man!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum