Calling makefiles from Shell Script

19,270

Solution 1

A common idiom is to create a shell script with set -e; this will cause the script to exit on the first error.

#!/bin/sh
set -e
make -f Makefile1
make -f Makefile2
:

If you need more control over the script overall, maybe remove set -e and instead explicitly exit on make failure:

make -f Makefile1 || exit
make -f Makefile2 || exit

To reduce code duplication, create a loop:

for f in Makefile1 Makefile2; do
    make -f "$f" || exit
done

Just to be explicit, the || "or" and && "and" connectives are shorthand for

if make -f Makefile1; then
     : "and" part
else
    : "or" part
fi

Finally, the behavior you describe sounds exactly like how Make itself behaves. Perhaps a top-level Makefile would actually be a suitable solution for your scenario?

.PHONY: all
all:
    $(MAKE) -f Makefile1
    $(MAKE) -f Makefile2

Solution 2

make -f makefile1
make -f makefile2

to run make files in order

to save the output of each makefile

make -f makefile1 >> output1
make -f makefile2 >> output2

to check the result after each make file

make -f makefile1 >> output1

after this line script use
echo $? this in combination with if. if echo$? result zero then your make success so if echo$? result zero then run next file other wise exit

Share:
19,270

Related videos on Youtube

Srini2k6
Author by

Srini2k6

C++ Developer

Updated on September 22, 2022

Comments

  • Srini2k6
    Srini2k6 over 1 year

    I am new to shell script. I want to call a list of make files from Shell script in a particular order. For each makefile I want to get the result (make is success or failure). If any error I want to stop the script execution. If it is success I have to run the next makefile.

  • pfnuesel
    pfnuesel almost 11 years
    It might be helpful to also log the errors, i.e. make -f makefile1 &>> output1
  • tripleee
    tripleee about 4 years
    The &>>file redirection is not portable; some shells support this form as syntactic sugar, but frankly the portable >>file 2>&1 seems perfectly fine to me.