1 #! /usr/bin/ksh -p
   2 #
   3 # CDDL HEADER START
   4 #
   5 # The contents of this file are subject to the terms of the
   6 # Common Development and Distribution License (the "License").
   7 # You may not use this file except in compliance with the License.
   8 #
   9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10 # or http://www.opensolaris.org/os/licensing.
  11 # See the License for the specific language governing permissions
  12 # and limitations under the License.
  13 #
  14 # When distributing Covered Code, include this CDDL HEADER in each
  15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16 # If applicable, add the following below this CDDL HEADER, with the
  17 # fields enclosed by brackets "[]" replaced with your own identifying
  18 # information: Portions Copyright [yyyy] [name of copyright owner]
  19 #
  20 # CDDL HEADER END
  21 #
  22 
  23 #
  24 # Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  25 # Use is subject to license terms.
  26 #
  27 # compile.ksh compile c programs in a remote host
  28 #
  29 
  30 [ -n "$DEBUG" ] && [ "$DEBUG" != "0" ] && set -x
  31 
  32 function usage
  33 {
  34         echo "ERROR: USAGE: $0 setup | cleanup [file] [flags] [CCPATH]"
  35         echo "\twhere all optional arguments are used in setup only."
  36         echo "\tsetup | cleanup: desired compilation action to be taken."
  37         echo "\tfile: contains the info about files to be compiled and how."
  38         echo "\tflags: are c flags to be used on all files,"
  39         echo "\t be aware that individual c flags are specified for each c file"
  40         echo "\t on the setup file. Contradictions may cause problems."
  41         echo "\tCCPATH: path to cc compiler to use in this machine."
  42         exit -1
  43 }
  44 
  45 [ $# -lt 1 ] && usage
  46 [ $# -eq 1 ] && [ "$1" = "setup" ] && usage
  47 [ "$1" = "setup" ] && [ $# -lt 2 ] && usage
  48 action=$1
  49 [ $# -ge 2 ] && file=$2
  50 [ $# -ge 3 ] && tflags=$3
  51 [ -n "$tflags" ] && flags=${tflags:="-g"}
  52 arch=`uname -p`
  53 if [ $arch = "sparc" ]; then
  54         arch2="i386"
  55 else
  56         arch2="sparc"
  57 fi
  58 [ $# -ge 4 ] && tcc=$4
  59 [ -n "$tcc" ] && CC=\
  60 ${tcc:=/opt/SUNWspro/bin/cc}
  61 
  62 # Check if correct arch is in path (in case default got wrong value)
  63 # Make sure the wrong arch is not in string
  64 res=`echo $CC | grep $arch2`
  65 if [ $? -eq 0 ]; then
  66         # try to fix by replacing with correct arch
  67         CC=`echo $CC | sed "s/$arch2/$arch/g"`
  68 fi
  69 
  70 RES=0
  71 # setup the remote machine
  72 if [ "$action" = "setup" ]; then
  73         if [ ! -r "$file" ]; then
  74                 echo "ERROR: $file does not exist or cannot be read"
  75                 exit 1
  76         fi
  77 
  78         # create cleanup file
  79         echo "$0\n$file" > ./cleanup.list
  80 
  81         if [ ! -x $CC ]; then
  82                 echo "ERROR: machine `hostname`($arch) failed to run $CC."
  83                 exit -1
  84         fi
  85         tmpfile="tmpfile.$$"
  86         egrep -v "^#|^  *$|^$" $file > $tmpfile
  87 
  88         while read comm filename opts objs other
  89         do
  90                 case "$comm" in
  91                 c) echo "${filename}*" >> ./cleanup.list
  92                    if [ ! -r ${filename}.c ]; then
  93                         echo "WARNING: $filename not present"
  94                         RES=1
  95                    else
  96                         [ "$opt" = "\"\"" ] && opt=""
  97                         $CC $opt $flags -c ${filename}.c -o ${filename}.o
  98                    fi;;
  99                 l)if [ ! -r ${filename}.o ]; then
 100                         echo "WARNING: ${filename}.o not present"
 101                         RES=2
 102                    elif [ "$objs" != "\"\"" ] && [ ! -r "$objs" ]; then
 103                         echo "WARNING: $objs not present"
 104                         RES=2
 105                    else
 106                         [ "$objs" = "\"\"" ] && objs=""
 107                         [ "$opt" = "\"\"" ] && opt=""
 108                         $CC $opt $flags $objs ${filename}.o -o $filename
 109                         chmod 777 $filename
 110                    fi;;
 111                 s) echo "${filename}*" >> ./cleanup.list
 112                    if [ ! -r "$filename" ]; then
 113                         echo "WARNING: $filename not present"
 114                         RES=3
 115                    else
 116                         chmod 777 $filename
 117                    fi;;
 118                 *) echo "ERROR in $file: $comm $filename $opts $objs $other"
 119                         exit -1;;
 120                 esac
 121         done < $tmpfile
 122         rm -f $tmpfile > /dev/null 2>&1
 123 else
 124 # cleanup the remote machine
 125         tmpfile="./cleanup.list"
 126         if [ ! -r "$tmpfile" ]; then
 127                 echo "ERROR: $tmpfile does not exist or cannot be read"
 128                 exit 1
 129         fi
 130         files=`cat $tmpfile`
 131         for i in `echo $files`
 132         do
 133                 [ -r "$i" ] && rm -rf $i
 134         done
 135         rm -f $tmpfile > /dev/null 2>&1
 136 fi
 137 exit $RES