Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define global shell functions in a Makefile?

Tags:

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 $1 $2;   fi } 

in such a way that I can use it in every rule of my Makefile, such as:

foo: bar   test foo baz 

To be clear, I want the shell function to be part of the Makefile. What is the most elegant way to do this? Bonus points if you can do it without calling make recursively.


Background: My actual problem is that make -n produces a very long and unreadable output. Each rule uses almost the same sequence of unreadable shell commands, and there are many rules. The above solution would make the output of make -n more useful.

like image 442
Holger Avatar asked Oct 08 '12 03:10

Holger


People also ask

What Shell is used in Makefile?

$(shell) is a special function in gmake that runs an external command and captures the output for use in the makefile.

What is $$ in Makefile?

$$ means be interpreted as a $ by the shell. the $(UNZIP_PATH) gets expanded by make before being interpreted by the shell.


1 Answers

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.

like image 141
Michaël Le Barbier Avatar answered Sep 27 '22 17:09

Michaël Le Barbier