Build script to utilize Makefiles to build multiple applications

You have a bunch of related applications, each has their own Makefile that knows only about their own application, but you have dependencies where one app needs to be built before another. Here’s a skeleton bash script for building any one thing or everything.

#!/bin/bash

function build {
   echo "*** BUILDING APP: $1 ***"
   cd $1/src
   if [ $? != 0 ]; then
      fail $1
   else
      make
      if [ $? != 0 ]; then
         make install
         if [ $? != 0 ]; then
            fail $1
         fi
      fi
   fi
   cd -
}

function fail {
   echo "*** ERROR: Failed while attempting to build $1 ***"
   echo "Exiting..."
   exit 1
}

function usage {
   echo "Usage: $0 [APPNAME]"
   echo "Builds apps. Handles dependencies automagically."
}

case $1 in
"app_name_1")
   build app_dir_1
   ;;
"app_name_2")
   build app_dir_1
   build app_dir_2
   ;;
"app_name_3")
   build app_dir_1
   build app_dir_3
   ;;
"all")
   build app_dir_1
   build app_dir_2
   build app_dir_3
   ;;
*)
   usage
   exit 2
   ;;
esac
exit 0

This makes it very easy to add dependencies and new applications to the chain. All you have to do is add a section to the case statement for a new app, and call build for each of its dependencies.

You can also do this with a Makefile that just has targets. Each target would call the individual Makefile’s targets. If you only need to do the simple make && make install for each app, then a Makefile would be shorter and simpler. But I like the flexibility this script affords if you need to add funky code later, such as processing other code that doesn’t have an associated Makefile or moving source around to different directories.

Posted by admica   @   29 December 2009

Related Posts

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« Handle command line arguments with getopt in Python
Next Post
Makefile breaks when making targets of another Makefile »
Powered by Wordpress   |   Lunated designed by ZenVerse