8.4 Supporting Multiple Binary Trees

     

Once the makefile is modified to write binary files into a separate tree, supporting many trees becomes quite simple. For interactive or developer-invoked builds, where a developer initiates a build from the keyboard, there is little or no preparation required. The developer creates the output directory, cd 's to it and invokes make on the makefile .

 $  mkdir -p ~/work/mp3_player_out  $  cd ~/work/mp3_player_out  $  make -f ~/work/mp3_player/makefile  

If the process is more involved than this, then a shell script wrapper is usually the best solution. This wrapper can also parse the current directory and set an environment variable like BINARY_DIR for use by the makefile .

 #! /bin/bash # Assume we are in the source directory. curr=$PWD export SOURCE_DIR=$curr while [[ $SOURCE_DIR ]] do   if [[ -e $SOURCE_DIR/[Mm]akefile ]]   then     break;   fi   SOURCE_DIR=${SOURCE_DIR%/*} done # Print an error if we haven't found a makefile. if [[ ! $SOURCE_DIR ]] then   printf "run-make: Cannot find a makefile" > /dev/stderr   exit 1 fi # Set the output directory to a default, if not set. if [[ ! $BINARY_DIR ]] then   BINARY_DIR=${SOURCE_DIR}_out fi # Create the output directory mkdir --parents $BINARY_DIR # Run the make. make --directory="$BINARY_DIR" "$@" 

This particular script is a bit fancier. It searches for the makefile first in the current directory and then in the parent directory on up the tree until a makefile is found. It then checks that the variable for the binary tree is set. If not, it is set by appending "_out" to the source directory. The script then creates the output directory and executes make .

If the build is being performed on different platforms, some method for differentiating between platforms is required. The simplest approach is to require the developer to set an environment variable for each type of platform and add conditionals to the makefile and source based on this variable. A better approach is to set the platform type automatically based on the output of uname .

 space := $(empty) $(empty) export MACHINE := $(subst $(space),-,$(shell uname -smo)) 

If the builds are being invoked automatically from cron , I've found that a helper shell script is a better approach than having cron invoke make itself. A wrapper script provides better support for setup, error recovery, and finalization of an automated build. The script is also an appropriate place to set variables and command-line parameters.

Finally, if a project supports a fixed set of trees and platforms, you can use directory names to automatically identify the current build. For example:

 ALL_TREES := /builds/hp-386-windows-optimized \              /builds/hp-386-windows-debug     \              /builds/sgi-irix-optimzed        \              /builds/sgi-irix-debug           \              /builds/sun-solaris8-profiled    \              /builds/sun-solaris8-debug BINARY_DIR := $(foreach t,$(ALL_TREES),\                 $(filter $(ALL_TREES)/%,$(CURDIR))) BUILD_TYPE := $(notdir $(subst -,/,$(BINARY_DIR))) MACHINE_TYPE := $(strip                         \                   $(subst /,-,                  \                     $(patsubst %/,%,            \                       $(dir                     \                         $(subst -,/,            \                           $(notdir $(BINARY_DIR))))))) 

The ALL_TREES variable holds a list of all valid binary trees. The foreach loop matches the current directory against each of the valid binary trees. Only one can match. Once the binary tree has been identified, we can extract the build type (e.g., optimized, debug, or profiled) from the build directory name. We retrieve the last component of the directory name by transforming the dash-separated words into slash-separated words and grabbing the last word with notdir . Similarly, we retrieve the machine type by grabbing the last word and using the same technique to remove the last dash component.



Managing Projects with GNU make
Managing Projects with GNU Make (Nutshell Handbooks)
ISBN: 0596006101
EAN: 2147483647
Year: 2003
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net