How to define global shell functions in a Makefile?

前端 未结 7 993
醉酒成梦
醉酒成梦 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 13:01

    Why not using .ONESHELL in Makefile and define a Makefile function like bash function:

    jeromesun@km:~/workshop/hello.test$ tree
    .
    ├── Makefile
    └── mkfiles
        └── func_test.mk
    
    1 directory, 2 files
    jeromesun@km:~/workshop/hello.test$ cat Makefile 
    .ONESHELL:
    
    -include mkfiles/*.mk
    
    test:
            @$(call func_test, one, two)
    jeromesun@km:~/workshop/hello.test$ cat mkfiles/func_test.mk 
    .ONESHELL:
    
    define func_test
        echo "func_test parameters: 0:$0, 1:$1, 2:$2"
        if [ ! -d abc ]; then
            mkdir abc
            echo "abc not found"
        else
           echo "abc found"
        fi
    endef
    jeromesun@km:~/workshop/hello.test$ 
    

    Result:

    jeromesun@km:~/workshop/hello.test$ make test 
    func_test parameters: 0:func_test, 1: one, 2: two
    abc not found
    jeromesun@km:~/workshop/hello.test$ make test 
    func_test parameters: 0:func_test, 1: one, 2: two
    abc found
    

提交回复
热议问题