1. Pjotr Kourzanov
  2. LIME

Source

LIME / lib / slimer.sh

##################################################################
# shell helper to drive the top-level compilation flow in LIME
#
#Copyright (C) 2008 NXP Semiconductors B.V.
#
#This file is part of LIME.
#
#LIME is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License version 2
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#LIME is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with LIME.  If not, see <http://www.gnu.org/licenses/>.
##################################################################
export SHELL=/bin/bash

sed_paths=(sed gsed)
sed_version=4.0.7
awk_paths=(awk mawk nawk gawk)
awk_version=3.1.3
make_paths=(make gmake)
make_version=3.81.0
gcc_paths=(gcc-3.3 gcc-3.4 gcc-4.1 gcc-4.2 gcc-4.3 gcc gcc32)
gcc_version=3.3.0

auto_paths=(awk sed make gcc)

s="\${"
e="}"
E="[@]}"

sed=sed
awk=awk
make=make
gcc=gcc
ln=ln

fail() {
	echo "FAILED at" $@ >/dev/stderr
	exit
}

function v2n() {
	$sed -e ':lab;s,^\([()*+0-9]\+\)\.,(\1)*100+,g;t lab'
}

function vex() {
	$awk -vIGNORECASE=1 "/(GNU)?.*$p/ && match(\$0,/\<[0-9.]*\>/)\
		{ n=split(substr(\$0,RSTART,RLENGTH),a,\".\");\
		  for (i=1; i<=3; i++)\
		  	printf \"%s%u\",(i>1 ? \".\" : \"\"),a[i] }" 
}

[[ -z "$JOBS" ]] && {
	if [[ -r /proc/cpuinfo ]]; then
		JOBS=`$awk '/^processor/ {p=$3} END {print (p+1)*2}' /proc/cpuinfo`
	else
		JOBS=2
	fi
}
echo -n using >/dev/stderr
echo -n " $JOBS jobs," >/dev/stderr
for p in ${auto_paths[@]}; do
	eval path="$s${p}_paths$E"
	eval version="$s${p}_version$e"
	vers=$((`echo $version|v2n`))
	found=
	for pa in $path; do
	  oIFS=$IFS
	  IFS=:
	  for x in $PATH; do
		l=$x/$pa
		[ -x "$l" ] || continue
		gnu_version=$((`$l --version | vex | v2n`))
		[[ -z "$gnu_version" ]] && continue
		#echo $l: `$l --version|vex` $gnu_version $vers >/dev/stderr
		[[ "$gnu_version" -lt "$vers" ]] && continue
		eval $p=$l
		echo -n " $l ($gnu_version)" >/dev/stderr
		found=1
		break
	  done
	  IFS=$oIFS
	  [[ $found = 1 ]] && break
	done
	[[ -n "$found" ]] || fail ": GNU $p $version not found"
done
echo >/dev/stderr

export domain=`echo $base | $sed -ne 's,.*dom/\(.*\)/bin.*,\1,p'`
export lib=$dom/lib
export inc=$dom/inc
export rbin=$root/bin
export rinc=$root/inc


[[ "${1##-o}" != "$1" ]] && app=$2 && shift 2
export app

DO_INST=0	
[[ "${1##-i}" != "$1" ]] && DO_INST=1 && shift

DO_DOCS=0	
[[ "${1##-d}" != "$1" ]] && shift && [[ "`which noweb`" != "" ]] && [[ "`which cast`" != "" ]] && DO_DOCS=1

DO_CLEAN=0	
[[ "${1##-c}" != "$1" ]] && DO_CLEAN=1 && shift

[[ "${1##-C}" != "$1" ]] && DO_CLEAN=2 && shift

src=
while [[ -n "$1" && "$1" != "--" ]]; do
	tgt=$1
	if [[ "${tgt//=/}" != "$tgt" ]]; then
		dst=${tgt//*=/}
		tgt=${tgt//=*/}
		$ln -fs $dst $tgt
		gensrc="$gensrc $tgt"
	fi
	if [[ "${tgt//.c/}" != "$tgt" ]]; then
		src="$src $tgt"
	fi
	if [[ "${tgt//.nw/}" != "$tgt" ]]; then
		lit="$lit $tgt"
	fi
	shift
done
[[ "$1" == "--" ]] && shift
# by default include all domain-specific headers into compilation
syshdr=
for h in $rinc/LIME*.h; do
	[[ -r $h ]] || continue
	syshdr="$syshdr $h"
done
export syshdr
for h in $inc/LIME*.h $inc/$domain*.h; do
	[[ -r $h ]] || continue
	hdr="$hdr $h"
done
uhdr=
while [[ -n "$1" && "$1" != "--" ]]; do
	if [[ "${1//.h/}" != "$1" ]]; then
		uhdr="$uhdr $1"
	fi
	if [[ "${1//-type.nw/}" != "$1" ]]; then
		hlit="$hlit $1"
	fi
	shift
done
hdr="$syshdr $hdr $uhdr"
[[ "$1" == "--" ]] && shift
graphs=
while [[ -n "$1" && "$1" != "--" ]]; do
	graphs="$graph $1"
	shift
done
export graphs

clean() {
	[ -n "$msg" ] && echo cleaning $msg >/dev/stderr
	files=;dirs=
	for x in $@; do
		#echo $x
		[ -f "$x" ] || continue
		[ $x == $app.gxf ] && continue
		files="$files $x"
	done
	for x in $@; do
		[ -d "$x" ] || continue
		dirs="$dirs $x"
	done
	[ -n "$files" ] && rm -f $files
	[ -n "$gensrc" ] && rm -f $gensrc
	[ -n "$dirs" ] && rm -rf $dirs
	return 0
}

Opt="-j$JOBS sed=$sed awk=$awk make=$make gcc=$gcc DOMAIN=$domain r=$root"

rmake() {
	opt="$Opt -f $rlib/main.mk"
	msg=$1
	if [ ${msg%%:} != ${msg} ]; then
		shift
		if [ -z "$1" ]; then return; fi
		if [ $DO_CLEAN -gt 0 ]; then
			clean $@
			return
		fi
		echo -e "making $msg" >/dev/stderr
		$make $opt $@ || fail $msg
	else
		if [ -z "$1" ]; then return; fi
		if [ $DO_CLEAN -gt 0 ]; then
			clean $@
			return
		fi
		$make $opt $@ 
	fi
		
}
dmake() {
	opt="$Opt -f $lib/main.mk"
	msg=$1
	if [ ${msg%%:} != ${msg} ]; then
		shift
		if [ -z "$1" ]; then return; fi
		if [ $DO_CLEAN -gt 0 ]; then
			clean $@
			return
		fi
		echo -e "making $msg" >/dev/stderr
		$make $opt $@ || fail $msg
	else
		if [ -z "$1" ]; then return; fi
		if [ $DO_CLEAN -gt 0 ]; then
			clean $@
			return
		fi
		$make $opt $@ 
	fi
}

litc=${lit//.nw/.c}

litd="${lit//.nw/.nw.tex} ${src//.c/-gen.nw.tex} ${src//.c/-gen.nw}"
litp="${lit//.nw/.pdf} ${src//.c/-gen.pdf}"
lith="${lit//.nw/.html} ${src//.c/-gen.html}"
hierpdf="${lit//.nw/-hier.pdf} ${src//.c/-gen-hier.pdf}"
hierweb="${lit//.nw/-hier.png} ${src//.c/-gen-hier.png}"


hlitc=${hlit//-type.nw/.h}

litd="$litd ${hlit//.nw/.nw.tex} ${uhdr//.h/-gen.nw.tex} ${uhdr//.h/-gen.nw}"
litp="$litp ${hlit//.nw/.pdf} ${uhdr//.h/-gen.pdf}"
lith="$lith ${hlit//.nw/.html} ${uhdr//.h/-gen.html}"
hierpdf="$hierpdf ${hlit//.nw/-hier.pdf} ${uhdr//.h/-gen-hier.pdf}"
hierweb="$hierweb ${hlit//.nw/-hier.png} ${uhdr//.h/-gen-hier.png}"

#hlitd="${hlit//.nw/.nw.tex} ${hdr//.h/-gen.nw.tex}"
#hlitp="${hlit//.nw/.pdf} ${hdr//.h/-gen.pdf}"
#hhierpdf="${hlit//.nw/-hier.pdf} ${hdr//.h/-gen-hier.pdf}"
#hlith="${hlit//.nw/.html} ${hdr//.h/-gen.html}"
#hhierweb="${hlit//.nw/-hier.png} ${hdr//.h/-gen-hier.png}"

rmake Literate: $litc $hlitc

src="$src $litc"
hdr="$hdr $hlitc"

if [[ $DO_DOCS == 1 ]]; then
	rmake PDF\&WEB: $litd $hierpdf $litp $hierweb $lith
    if [ $DO_CLEAN == 0 ]; then
	mkdir -p doc-gen
	for x in $litp $hierpdf $hierweb; do
		cp $x doc-gen/`basename ${x//-gen/}`
	done
	for x in $lith; do
		sed -e 's,"[.]\+/,",g;s,-gen-hier.png,-hier.png,g' $x > doc-gen/`basename ${x//-gen/}`
	done
    fi
fi

export imports=${src//.c/.imports}
export modules=${src//.c/.module}
#export graphs=${graph//.graph/.graph.xml}
export comps=${src//.c/}

[ -z "$depver" ] && depver=depx

extra="${src//.c/.$depver.h} ${src//.c/.domains}"
#extra="$extra $app.0.dot"
extra="$extra $app.1.dot"
extra="$extra $app.2.dot"
extra="$extra $app.3.dot"
extra="$extra $app.dot $app.png $app.map"
extra="$extra $app-full.dot $app-full.png $app-full.map"
extra="$extra $app-alt.dot $app-alt.png $app-alt.map"

# don't clean this stuff
if [ $DO_CLEAN != 1 ]; then
    types=
    for x in ${hdr//.h/.types}; do
	rmake $x: $x ${x/.types/.imports}
	export types="$types${types:+ }$x"
    done
fi

rmake Modules\&Graphs: $imports $modules $app.gxf $extra

gen=
if [ -r $app.gxf ]; then
    export gcomps=`gcomp $app.gxf`
    for x in $gcomps; do
    	gen="$gen $x.deps.h $x.simports $x.sdomains"
    done
fi

[ -n "$gen" ] && rmake Materialized: $gen