views:

445

answers:

2

Look at this makefile, it has some sort of primitive progress indication (could have been a progress bar).

Please give me suggestions/comments on it!



# BUILD is initially undefined
ifndef BUILD

# max equals 256 x's
sixteen := x x x x x x x x x x x x x x x x
MAX := $(foreach x,$(sixteen),$(sixteen))

# T estimates how many targets we are building by replacing BUILD with a special string
T := $(shell $(MAKE) -nrRf $(firstword $(MAKEFILE_LIST)) $(MAKECMDGOALS) \
            BUILD="COUNTTHIS" | grep -c "COUNTTHIS")

# N is the number of pending targets in base 1, well in fact, base x :-)
N := $(wordlist 1,$T,$(MAX))

# auto-decrementing counter that returns the number of pending targets in base 10
counter = $(words $N)$(eval N := $(wordlist 2,$(words $N),$N))

# BUILD is now defined to show the progress, this also avoids redefining T in loop
BUILD = @echo $(counter) of $(T)
endif

# dummy phony targets

.PHONY: all clean

all: target
    @echo done

clean:
    @rm -f target *.c

# dummy build rules

target: a.c b.c c.c d.c e.c f.c g.c
    @touch $@
    $(BUILD)

%.c:
    @touch $@
    $(BUILD)


All suggestions welcome!

+1  A: 

Nice trick! (-:

But not really scalable for growing projects that are distributed across many directories with lots of makefiles.

I'd be more inclined to have logging sprinkled through the [Mm]akefiles* in your project and use that to keep track of progress.

Just a thought. BTW Thanks for sharing this.

Edit: Just had a thought. This could be useful in a modified form to display a throbber to show progress while a long task proceeds, e.g unpacking a large distribution tarball instead of just specifying the -v option to the tar command. Still a bit of sugar coating but a bit of fun aswell. (-:

cheers,

Rob

Rob Wells
Thanks for your comment, can you please explain why you think this does not scales? Ok the code is just a sketch, but MAX can hold 64k x's if needed, and the calculation of the T variable is quite fast.
Helltone
It doesn't scale because it won't work if you use make over multiple directories, with each sub-directory having it's own makefile.
Scottie T
@ScottieT812, cheers, that was the main reason for my comment! (-: The other one is continually having to calculate and update the number of x's needed as the project expands. Still. nice trick though.
Rob Wells
Rob you did not understood my script. It WORKS if you have makefiles over multiple directories (although I do not recommend recursive make) and the x's are NOT number of targets, its just an upper-bound (256, can be more). The exact number of targets is automatically discovered (see code of $T)
Helltone
+1  A: 

See http://snippets.dzone.com/posts/show/6843

Helltone