TheLoneJoker
TheLoneJoker

Reputation: 1629

Conditional function test for presence of file

I want to check the size of a file within a rule, but only if the file is present. check_size is a custom function. In the below make doesn't even get around to calling check_size:

    $(if $(test -f $(file)),\
        @$(call check_size,$(shell stat -f%z $(file),$(size)))

I don't see what is wrong. Any help would be appreciated.

Thanks, -Vj

Upvotes: 0

Views: 255

Answers (1)

MadScientist
MadScientist

Reputation: 100781

There is no make function test, so $(test -f $(file)) causes make to try to expand a make variable named test -f $(file), which obviously doesn't exist, so it expands to the empty string.

Because of that, the if-condition is always false (in make if functions and other conditions, the empty string is false and a non-empty string is true) and the then-clause is not expanded.

It's actually somewhat tricky to use make functions like this inside a recipe and have them do what you want, and also I can't really figure out what your call does here: why are you passing $(file),$(size) to the stat program in the shell?? But since there is no info on what check_size does we'll have to assume it's necessary.

In that case you'll have to use real make functions; the one you want to use to test if a file exists is wildcard which will expand to the name of the file if it exists or the empty string if not... so:

$(if $(wildcard $(file)), \
    @$(call check_size,$(shell stat -f%z $(file),$(size)))

Upvotes: 1

Related Questions