Dependencies for clean rule

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Dependencies for clean rule

Erik Rull
Hi all,

I have several targets that depend on each other.
For cleaning them up, I have to take care that the clean dependencies are just
the other way round.

e.g.

a: b c
b: d e
c:
d:
e:

If I want to make a the dependencies assert that the build order is correct.
If I want to make "clean" e.g. c, I should clean a first.

Is there a way to find the "reverse-order" of the dependencies? Of course, I
could manually write the clean-rule-dependencies, but that would be too easy
(and generate additional errors when the number of targets and dependencies are
bigger).

Maybe someone solved that already, I did not yet find a smooth solution for that.

Thanks in advance.

Best regards,

Erik

_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

Re: Dependencies for clean rule

Kaz Kylheku (gmake)
On 2019-07-15 12:55, Erik Rull wrote:
> Hi all,
>
> I have several targets that depend on each other.
> For cleaning them up, I have to take care that the clean dependencies
> are just
> the other way round.

No, you don't have to, actually.

There is no concept of dependencies in cleaning. Cleaning means taking a
list of
objects that were produced by a build and deleting them. This can be
done in any
order. If foo.o has gone into libbar.a, which went into prog, we can
delete
these in any order. Or just delete some of them but not others.

(If the dependencies in the build system are correct, it can recover
from the
partial deletion of any arbitrary subset of the intermediate objects.)





_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

Re: Dependencies for clean rule

Christian Hujer
In reply to this post by Erik Rull
Hello Erik,


While I have no direct answer for you, I have a few tips about cleaning.

1. Use double-colon rules.
https://www.gnu.org/software/make/manual/make.html#Double_002dColon
They can be combined, so you could have:
Makefile1:
.PHONY: clean
clean::
$(RM) somefile
-include Makefile2

Makefile2:
.PHONY: clean
clean::
$(RM) someOtherFile

2. Use $(RM) instead of rm
For removing files, use $(RM). It is more portable, and it expands to rm
-f, not just rm.
So you don't have to worry about files that do not exist.

3. Exit Hook + Recursive Make
If cleanup is a MUST, don't provide separate rules.
Instead, use recursive make and a more powerful shell.
For example, if the cleanup is about cleaning up after tests, I use the
following pattern.
In the example, I need a docker container to be started for the test, and I
don't want to keep the container running after the tests.
But that can be anything, replace docker with whatever you want/need in
your context.

export SHELL:=/bin/bash
export SHELLOPTS:=$(if $(SHELLOPTS),$(SHELLOPTS):)pipefail:errexit

.ONESHELL:

.PHONY: test
## Runs all tests.
test:
function tearDown {
    docker stop test-image
}
trap tearDown EXIT
docker run --name test-image …
$(MAKE) testImpl


See also my StackOverflow answer:
https://stackoverflow.com/questions/28597794/how-can-i-clean-up-after-an-error-in-a-makefile/52159940#52159940


Best regards,
Christian



On Tue, Jul 16, 2019 at 1:26 AM Erik Rull <[hidden email]> wrote:

> Hi all,
>
> I have several targets that depend on each other.
> For cleaning them up, I have to take care that the clean dependencies are
> just
> the other way round.
>
> e.g.
>
> a: b c
> b: d e
> c:
> d:
> e:
>
> If I want to make a the dependencies assert that the build order is
> correct.
> If I want to make "clean" e.g. c, I should clean a first.
>
> Is there a way to find the "reverse-order" of the dependencies? Of course,
> I
> could manually write the clean-rule-dependencies, but that would be too
> easy
> (and generate additional errors when the number of targets and
> dependencies are
> bigger).
>
> Maybe someone solved that already, I did not yet find a smooth solution
> for that.
>
> Thanks in advance.
>
> Best regards,
>
> Erik
>
> _______________________________________________
> Help-make mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-make
>


--
Christian Hujer
E: [hidden email]
T: @christianhujer
+91 77 2003 6661 (while in India)
+49 162 4124108 (while in Germany)
+44 7871 912444 (while in UK)
_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

Re: Dependencies for clean rule

Kaz Kylheku (gmake)
On 2019-07-17 04:17, Christian Hujer wrote:
> 2. Use $(RM) instead of rm
> For removing files, use $(RM). It is more portable, and it expands to
> rm
> -f, not just rm.
> So you don't have to worry about files that do not exist.

By the time we reach the level of portability where we are worried
about not having an "rm" command, we have long passed the point
that we can use any GNU-Make-specific feature.

When would we have GNU Make, yet not have an environment which either
provides a POSIX "rm" natively, or via GNU Coreutils?

However, $(RM) is actually GNU-Make-specific. I don't see it in POSIX!

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/make.html#tag_20_76_13_09

Thus, in portable Makefiles, we must actually avoid relying on $(RM);
if we use it, we have to check that it doesn't exist and define it
in that case.

If the program needs POSIX APIs, should the build system worry about
not having rm? (Maybe for cross-compiling: say we want to target a Linux
embedded system with glibc, but our build machine might be an IBM
mainframe
with OS/370, or a DEC VAX with VMS?)

How far do we have go with this? $(CAT), $(SED), $(GREP), $(MV), $(CP),
...



_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

Re: Dependencies for clean rule

David Boyce-3
In reply to this post by Erik Rull
A few notes:

1. You can often make use of MAKECMDGOALS to treat clean targets specially.
The manual gives an example.

2. I've evolved the pattern below for clean targets:

.PHONY: clean
clean: cleanups := $(wildcard *.o *.d core)
clean:
        $(if $(cleanups),$(RM) -r $(cleanups))

Of course the list "*.o *.d core" is just for illustration purposes. What I
like about this pattern is that when it has files to remove it shows you
what they were and if the state is already clean rather than running a
useless "rm -f *.o *.d core" it says, correctly, "Nothing to be done for
'clean'."

3. I've written makefiles in the past that make use of user-defined macros,
eval, whatever to maintain a variable (say "targets") which is a list of
all defined targets. With that you could plug $(targets) in as the list in
the above example. It may be a bit of overkill but it is doable.

On Mon, Jul 15, 2019 at 12:56 PM Erik Rull <[hidden email]> wrote:

> Hi all,
>
> I have several targets that depend on each other.
> For cleaning them up, I have to take care that the clean dependencies are
> just
> the other way round.
>
> e.g.
>
> a: b c
> b: d e
> c:
> d:
> e:
>
> If I want to make a the dependencies assert that the build order is
> correct.
> If I want to make "clean" e.g. c, I should clean a first.
>
> Is there a way to find the "reverse-order" of the dependencies? Of course,
> I
> could manually write the clean-rule-dependencies, but that would be too
> easy
> (and generate additional errors when the number of targets and
> dependencies are
> bigger).
>
> Maybe someone solved that already, I did not yet find a smooth solution
> for that.
>
> Thanks in advance.
>
> Best regards,
>
> Erik
>
> _______________________________________________
> Help-make mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-make
>
_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make