1

I need to add some logic to a make file and I am stuck. I see several examples out there but I'm not sure which one is the right one for me.

What I have:

$(UBIN)/%:
    $(CC) $(CFLAGS) -o $(UBIN)/$* $(OBJS) -L $(ORAHOME) $(ORALIBS) \
        $(LNKPATH) $(DSTN_LIBS)
    @echo ""   

What I want:

$(UBIN)/%:
    if the file $(UBIN)/$* exists
    then
      $(CC) $(CFLAGS) -o $(UBIN)/$* $(OBJS) -L $(ORAHOME) $(ORALIBS) \
          $(LNKPATH) $(DSTN_LIBS)          
      @echo ""
    endif

But I can't figure out what the right syntax is. Some idea were to use a wildcard string holder, some use some -a option, some use if some use ifeq some include semicolons and some do not.

The current list of make files we use has ZERO examples of this logic so I have nothing to compare it to for my build environment.

Many thanks

user2226755
  • 12,494
  • 5
  • 50
  • 73
user3115693
  • 31
  • 1
  • 2

2 Answers2

3

Assuming you don't want to see the compilation or the echo commands something like the following should work (untested written in the answer box).

$(UBIN)/%:
    @if [ -f '$@' ]; then \
        $(CC) $(CFLAGS) -o '$@' $(OBJS) -L $(ORAHOME) $(ORALIBS) \
        $(LNKPATH) $(DSTN_LIBS); \
        echo ""; \
    fi

If you do want to see the compilation command (but not the echo command) that's likely a bit more complicated (I don't have a ready solution to that offhand).

user2226755
  • 12,494
  • 5
  • 50
  • 73
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • Make variable for the target of the rule. Effectively the same as what you were doing with `$(UBIN)/$*` manually. Hm... I should have used that in the compilation command too... Fixed. – Etan Reisner Dec 30 '13 at 21:05
  • Thank you. I have to use $(UBIN)/$* because that is what is defined in other areas as the target. – user3115693 Dec 30 '13 at 21:11
  • My point is that it is the same file path and that constructing it manually is a poor style and will cause you trouble at some point. Test my assertion of their equality if you want to be sure (echo them both out or similar). `$*` is the stem of the pattern rule. The pattern is the `%` in the target. So if you replace `%` with `$*` in `$(UBIN)/%` you get `$(UBIN)/$*` and since `$@` is defined to be the target filename they are identical (in this case). A target body should always output to `$@` for readability and simplicity. – Etan Reisner Dec 30 '13 at 22:19
1

GNU make has some nice macrose encoded in functions, consider wildcard and shell as exemplars to address this ask (no guarauntees, but look at the macros):

ifeq(,$(wildcard paths.txt))
target: export PATH=$(PATH):$(subst \n,:,$(shell cat paths.txt))
else
target: export PATH=$(PATH_CLEAN)
endif
target:
    $(MAKE) -C task_in_path
user2226755
  • 12,494
  • 5
  • 50
  • 73
msudder
  • 505
  • 3
  • 14