How to define global shell functions in a Makefile?

前端 未结 7 1045
醉酒成梦
醉酒成梦 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:50

    This solution does not rely on an external temporary file and does not force you to tinker with the SHELL variable.

    TESTTOOL=sh -c '\
      do_some_complicated_tests $$1 $$2; \
      if something; then
        build_thisway $$1 $$2;
      else
        build_otherway $$1 $$2;
      fi' TESTTOOL
    
    ifneq (,$(findstring n,$(MAKEFLAGS)))
    TESTTOOL=: TESTTOOL
    endif
    
    foo: bar
        ${TESTTOOL} foo baz
    

    The ifneq…endif block checks for the -n flag on the command line and sets the expansion of TESTTOOL to : TESTTOOL which is easy to read and safe to execute.

    The best solution could be to turn the shell function into an actual program if this is an option for you.

提交回复
热议问题