How to define global shell functions in a Makefile?

前端 未结 7 982
醉酒成梦
醉酒成梦 2021-02-12 12:18

I want to define a shell function

#!/bin/sh

test ()
{
  do_some_complicated_tests $1 $2;
  if something; then
    build_thisway $1 $2;
  else
    build_otherway         


        
7条回答
  •  我在风中等你
    2021-02-12 12:59

    Since the question is tagged gnu-make, you might be happy with Beta's solution, but I think the best solution is to 1) rename the function anything other than test (avoid names like ls and cd as well); for the purpose of discussion let's assume you've renamed it foo, 2) simply write a script named foo and invoke it from the Makefile. If you really want to define a shell function in the Makefile, just define it for each rule:

    F = foo() { \
      do_some_complicated_tests $$1 $$2; \
      if something; then \
        build_thisway $$1 $$2; \
      else \
        build_otherway $$1 $$2; \
      fi \
    }
    
    all: bar
      @$(F); foo baz bar
    

    Not that you must have line continuations on each line of the definition of foo, and the $ are all escaped so that they are passed to the shell.

提交回复
热议问题