Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

makefile: conditional execution of target

2,881 views
Skip to first unread message

cedo

unread,
Sep 20, 2011, 8:23:17 AM9/20/11
to
Dear all,

I have a "classical" makefile:

all: compile

compile:
echo Do compilation

clean:
echo Delete files

I would like to add a conditional target to all. I mean, if a file
called condition.h has changed, I would like to force the target clean
in the target all, ie to clean everything before the compilation.

Is this possible?

Thank you by advance,

cedo

des...@verizon.net

unread,
Sep 20, 2011, 11:08:02 AM9/20/11
to
There is nothing classical about what you show.

I can't make much sense of the requirement you propose either.

To compile a program with a header file assuming GNUMake:

HDRS:=hdr.h
PGMS:=main.c
TARGS:=$(subst .c,,$(PGMS))

all: $(TARGS)

%.c: % $(HDRS)
gcc %< -o $@

clean:
rm $(TARGS)


To make this a little more generic, you might dispense with
specific file names:

HDRS:=$(wildcard *.h)


Sorry, none of this tested but based on hundreds of makefiles I've written.



--
Dan Espen

pk

unread,
Sep 20, 2011, 11:13:31 AM9/20/11
to
On Tue, 20 Sep 2011 11:08:02 -0400, des...@verizon.net wrote:

> cedo <cedric...@gmail.com> writes:
>
> > Dear all,
> >
> > I have a "classical" makefile:
> >
> > all: compile
> >
> > compile:
> > echo Do compilation
> >
> > clean:
> > echo Delete files
> >
> > I would like to add a conditional target to all. I mean, if a file
> > called condition.h has changed, I would like to force the target clean
> > in the target all, ie to clean everything before the compilation.
> >
> > Is this possible?
>
> There is nothing classical about what you show.
>
> I can't make much sense of the requirement you propose either.

I think he wants:

when doing "make all", do "compile", but if condition.h has changed (not
clear with respect to what), then do "clean" and then "compile".

des...@verizon.net

unread,
Sep 20, 2011, 11:49:22 AM9/20/11
to
Clean what? He hasn't shown.

Normally clean would remove the compile target.
Why remove it if you are going to replace it.

Maybe he thinks it's good idea to remove the target to force the
compile rule? (It won't at least not on the first pass.)

The correct thing to do is make the compile dependent on the header.

This line:

%: %.c $(HDRS)

says create main if either main.c or hdr.h has changed.
That's the classical way (and logical way) to do it.



--
Dan Espen

James

unread,
Sep 20, 2011, 4:44:36 PM9/20/11
to
Suppose your requirement is
" if condition.h is newer than any other files, then clean and
compile. "
and no parallel make.

LATEST := $(shell find -type f -exec ls -rt {} + | tail -1)
CHECK := $(findstring condition.h,$(LATEST))

ifneq ($(CHECK),)
all: clean compile
else
all: compile
endif

compile:
echo Do compilation

clean:
echo Delete files

James

cedo

unread,
Sep 21, 2011, 2:46:19 AM9/21/11
to
That's the solution of James I was looking for although I know what I
was searching is not the right common way to use make. However, it is
useful for me because I am new to the code so I prefer to force the
clean if the header has changed.

Thank you to all for your comments.

cedo

0 new messages