#!/bin/csh 
# $Id: mkmf,v 1.18 2003-08-28 14:26:09 cmv Exp $

#  Pour debugger 
set DODBG = 'echo'
set DODBG = ''

# Librairies externe
if( ! $?EXTLIBDIR ) then
  echo 'mkmf- WARNING (External libraries) EXTLIBDIR variable not set'
endif

# creation de MakefileUser.h a partir de Makefile.h
chmod +x ./makefileuser.csh
$DODBG ./makefileuser.csh

# creation des liens pour les includes
echo 'mkmf: Creating links for *.h (call mkmflien)'
chmod +x ./mkmflien
$DODBG ./mkmflien -clean

# Liste des modules a construire
# modules : Liste de module SOPHYA, sans dependance exterieure
# extmodules : Liste de modules avec dependance exterieures
#        _pres : Liste des modules presents

set modules = `cat libdirs`
set extmodules = `cat extlibdirs` 
set modules_pres = ''
set extmodules_pres = ''

set module_libnames = ''
set extmodule_libnames = ''
set extlib_list = ''
set extslb_list = ''
#  Constitution de la liste de modules present
foreach f ($modules)
  if (-d ../$f ) then
    set modules_pres = ( $modules_pres $f )
    set module_libnames = ( $module_libnames '$(LIB)lib'$f.a )
  endif
end
foreach f ($extmodules)
  if (-d ../$f ) then
    set extmodules_pres = ( $extmodules_pres $f )
    set extmodule_libnames = ( $extmodule_libnames '$(LIB)lib'$f.a )
    set lil = "../$f/extlib_list"
    if (-f $lil ) set extlib_list = ($extlib_list `sed -e 's/.*/-l&/' $lil`) 
    set lil = "../$f/extslb_list"
    if (-f $lil ) set extslb_list = ($extslb_list `sed -e 's/.*/-l&/' $lil`) 
  endif
end

echo 'mkmf: List of modules ' 
echo '  ... modules= ' $modules_pres
echo $module_libnames 
echo '  ... extmodules= ' $extmodules_pres
echo $extmodule_libnames 

echo 'mkmf: Creating makefile global :'
rm -f GNUmakefile; touch GNUmakefile

echo '# include files defining compiler/linker options '  >> GNUmakefile
echo 'MODULECXXREPNAME := PI'  >> GNUmakefile
echo 'include Mgr/Makefile.h' >> GNUmakefile
echo 'include Mgr/Makefile.slb' >> GNUmakefile
echo ''   >> GNUmakefile

echo 'LIBF = ' `sed -e 's/.*/$(LIB)lib&.a/' libdirs` >> GNUmakefile
# echo 'LIBS = -L$(LIB)' `sed -e 's/.*/-l&/' libdirs` '-lm' >> GNUmakefile
echo 'LIBS = -L$(LIB)' `sed -e 's/.*/-l&/' libdirs`  >> GNUmakefile
# echo 'LIBG = -L$(GLB) -lstdc++' >> GNUmakefile
# echo 'ifeq ($(HOSTTYPE), powerpc)' >> GNUmakefile
# echo '  LIBG = -L$(GLB)' >> GNUmakefile
# echo 'endif' >> GNUmakefile

cat >> GNUmakefile <<EOF

defaut: libs

all: libs extlibs PI

libs: cxxlibs

extlibs: cxxextlibs

EOF

# Dans cet ordre pour les templates de cxx, a cause des dependances
# pour instantiation automatique/manuels

echo 'cxxlibs: ' $modules_pres >>    GNUmakefile
echo '' >> GNUmakefile 
echo 'cxxextlibs: ' $extmodules_pres >> GNUmakefile

# pour creer les repertoires et les liens

echo '#--- Create directory and copy includes for install' >> GNUmakefile
echo 'depend:' >> GNUmakefile
echo 'ifdef DPCDEVREP' >> GNUmakefile
echo 'ifdef EROSCXX' >> GNUmakefile
echo '	@echo "Sophya will be installed under:"' >> GNUmakefile
echo '	@echo "  $(DPCDEVREP)/Include" ' >> GNUmakefile
echo '	@echo "  $(DPCDEVREP)/`uname`-$(EROSCXX)" ' >> GNUmakefile
echo '	if [ -d Mgr ] ; then cd Mgr; \' >> GNUmakefile
echo '	./crerep_sophya $(DPCDEVREP) `uname`-$(EROSCXX); \' >> GNUmakefile
echo '	./makefileuser.csh; \' >> GNUmakefile
echo '	./mkmflien -copy ; \' >> GNUmakefile
echo '	fi' >> GNUmakefile
echo 'else' >> GNUmakefile
echo '	@echo "Variable EROSCXX must be define with setenv"' >> GNUmakefile
echo 'endif' >> GNUmakefile
echo 'else' >> GNUmakefile
echo '	@echo "Variable DPCDEVREP must be define with setenv"' >> GNUmakefile
echo 'endif' >> GNUmakefile
echo '#--------------------------------------------------------------------' >> GNUmakefile
echo '' >> GNUmakefile

#  Attention, ne pas changer l'ordre de libnames dependances cxx  ????? 
set libnames = ($modules_pres $extmodules_pres)
set i = 1
set j = $#libnames ; @ j--
while ($i <= $#libnames)
  set f = $libnames[$i]
  set k = $i ; @ k++
  if ( -d ../$f && ! -f ../$f/no_mkmf ) then 
    set cmd = "./mkmflib $f "
    echo "mkmf: Creation makefile lib $f"
    echo "   execution $cmd"
    $DODBG $cmd
  endif
  echo '.PHONY: ' $f >> GNUmakefile
  echo $f':' >> GNUmakefile
  echo '	if [ -d '$f' ] ; then cd ' $f '; $(MAKE) ; fi' >> GNUmakefile
  echo '' >> GNUmakefile
  @ i++
end


if ( -d ../PI ) then 
  set cmd = './mkmfPI PI BaseTools'
  echo "mkmf: Creation makefile lib PI"
  echo "   execution $cmd"
  $DODBG  $cmd
endif
if ( -d ../PIGcont ) then 
  set cmd = './mkmfPI PIGcont BaseTools'
  echo "mkmf: Creation makefile lib PIGcont"
  echo "   execution $cmd"
  $DODBG  $cmd
endif
if ( -d ../PIext ) then 
  set cmd = "./mkmfPI PIext $libnames PI"
  echo "mkmf: Creation makefile lib PIext"
  echo "   execution $cmd"
  $DODBG $cmd
endif
echo '.PHONY: PI' >> GNUmakefile
echo 'PI:' >> GNUmakefile
echo '	if [ -d PI ] ; then cd PI ; $(MAKE) ; fi' >> GNUmakefile
echo '	if [ -d PIGcont ] ; then cd PIGcont ; $(MAKE) ; fi' >> GNUmakefile
echo '	if [ -d PIext ] ; then cd PIext ; $(MAKE) ; fi' >> GNUmakefile


echo 'mkmf: Writing shared library building part of the makefile :'
if ( `uname` == "Darwin" ) then
  set sosfx = "dylib"
else
  set sosfx = "so"
endif

echo '#  Shared library creation part of the Makefile'       >> GNUmakefile
echo ''                                 >> GNUmakefile
echo '#  List of libraries (.a)'         >> GNUmakefile
echo 'PSLB = ' $module_libnames          >> GNUmakefile
echo 'EXTPSLB = ' $extmodule_libnames    >> GNUmakefile
echo 'PIPSLB =  $(LIB)libPI.a $(LIB)libPIGcont.a $(LIB)libPIext.a'   >> GNUmakefile
echo '#  List of external library references'       >> GNUmakefile
echo 'EXTLIBL  = -L$(EXTLIBPATH)' $extlib_list       >> GNUmakefile
echo 'EXTSLBL  = -L$(EXTSLBPATH)' $extslb_list       >> GNUmakefile
echo ''                                   >> GNUmakefile
echo '# defining the other external libraries needed on different systems'   >> GNUmakefile
echo ''                                   >> GNUmakefile
echo '# On Linux   lapack and blas and f2c/g2c ere needed'   >> GNUmakefile
echo 'ifeq ($(MACHEROS),Linux)'   >> GNUmakefile
echo '#EXTLIB = $(EXTLIBL) -llapack -lblas -lf2c'   >> GNUmakefile
echo '#EXTSLB = $(EXTSLBL) -llapack -lblas -lf2c'   >> GNUmakefile
echo 'EXTLIB = $(EXTLIBL) -llapack -lblas -lg2c'   >> GNUmakefile
echo 'EXTSLB = $(EXTSLBL) -llapack -lblas -lg2c'   >> GNUmakefile
echo 'endif'   >> GNUmakefile
echo '# On OSF1 libdxml contains Lapack, but the fortran support library is needed'   >> GNUmakefile
echo 'ifeq ($(MACHEROS),OSF1)'   >> GNUmakefile
echo 'EXTLIB = $(EXTLIBL) -ldxml -lfor'   >> GNUmakefile
echo 'EXTSLB = $(EXTSLBL) -ldxml -lfor'   >> GNUmakefile
echo 'endif'   >> GNUmakefile
echo ''                                   >> GNUmakefile
echo '# On IRIX , complib.sgimath contains Lapack'   >> GNUmakefile
echo 'ifeq ($(MACHEROS),IRIX64)'   >> GNUmakefile
echo 'EXTLIB = $(EXTLIBL) -lcomplib.sgimath '   >> GNUmakefile
echo 'EXTSLB = $(EXTSLBL) -lcomplib.sgimath '   >> GNUmakefile
echo 'endif'   >> GNUmakefile
echo ''                                   >> GNUmakefile
echo 'XPILIBS = $(LIBXPIPATH) $(LIBXPILIST)'        >> GNUmakefile
echo ''                                   >> GNUmakefile
echo '#  main dependence list'            >> GNUmakefile
echo 'slball : slb slbext slbpi '         >> GNUmakefile
echo 'slb : $(SLB)libsophya.'$sosfx       >> GNUmakefile
echo 'slbext : $(SLB)libextsophya.'$sosfx >> GNUmakefile
echo 'slbpi : $(SLB)libPI.'$sosfx         >> GNUmakefile
echo ''                                  >> GNUmakefile

echo '# List of .o files for each module'  >> GNUmakefile
set objalls =  ''
foreach f ($modules_pres)
  echo 'OBJ'$f '= $(shell echo' '`cat' "./$f/$f.o.list" '`)'  >> GNUmakefile
  set objalls = ( $objalls '$(OBJ'$f')' )
end
echo 'ALLOBJS =' $objalls                >> GNUmakefile
echo ''                                 >> GNUmakefile

echo '# List of .o files modules with reference to external libraries'  >> GNUmakefile
set objalls =  ''
foreach f ($extmodules_pres)
  echo 'OBJ'$f '= $(shell echo' '`cat' "./$f/$f.o.list" '`)'  >> GNUmakefile
  set objalls = ( $objalls '$(OBJ'$f')' )
end
echo 'ALLEXTOBJS =' $objalls             >> GNUmakefile
echo ''                                 >> GNUmakefile

echo '# List of .o files for PI modules '  >> GNUmakefile
set objalls =  ''
foreach f (PI PIGcont PIext)
  echo 'OBJ'$f '= $(shell echo' '`cat' "./$f/$f.o.list" '`)'  >> GNUmakefile
  set objalls = ( $objalls '$(OBJ'$f')' )
end
echo 'ALLPIOBJS =' $objalls              >> GNUmakefile
echo ''                                 >> GNUmakefile

echo '# Building Sophya shared library'  >> GNUmakefile
echo '$(SLB)libsophya.'$sosfx' : $(PSLB)'                       >> GNUmakefile
echo '	$(CMDTILSHL) '                                          >> GNUmakefile
echo '	cd  $(OBJ); \'                                          >> GNUmakefile
echo '	$(CMDSHLCXX) -o $(SLB)libsophya.'$sosfx'  \'            >> GNUmakefile
echo '	$(FGSHLACXX) $(ALLOBJS)  $(FGSHLNCXX)\'                 >> GNUmakefile
echo '	-L$(SLB) $(LIBLSHL)'                                    >> GNUmakefile
echo ''                                                        >> GNUmakefile

echo '# Building ExtSophya shared library'  >> GNUmakefile
echo '$(SLB)libextsophya.'$sosfx' : $(EXTPSLB)'                 >> GNUmakefile
echo '	$(CMDTILSHL) '                                          >> GNUmakefile
echo '	cd  $(OBJ); \'                                          >> GNUmakefile
echo '	$(CMDSHLCXX) -o $(SLB)libextsophya.'$sosfx'  \'         >> GNUmakefile
echo '	$(FGSHLACXX) $(ALLEXTOBJS)  $(FGSHLNCXX)\'              >> GNUmakefile
echo '	-L$(SLB) -lsophya $(EXTLIB) $(LIBLSHL)'                 >> GNUmakefile
echo ''                                                        >> GNUmakefile

echo '# Building PI shared library'  >> GNUmakefile
echo '$(SLB)libPI.'$sosfx' : $(PIPSLB)'                         >> GNUmakefile
echo '	$(CMDTILSHL) '                                          >> GNUmakefile
echo '	cd  $(OBJ); \'                                          >> GNUmakefile
echo '	$(CMDSHLCXX) -o $(SLB)libPI.'$sosfx'  \'                >> GNUmakefile
echo '	$(FGSHLACXX) $(ALLPIOBJS) $(FGSHLNCXX) \'               >> GNUmakefile
echo '	-L$(SLB) -lsophya -lextsophya $(EXTLIB) $(XPILIBS) $(LIBLSHL)'      >> GNUmakefile
echo ''                                                        >> GNUmakefile


cat >> GNUmakefile << __END__

clean:
	if [ -d \$(LIB) ] ; then cd \$(LIB)/. ; rm -f *.a  ; fi
	if [ -d \$(SLB) ] ; then cd \$(SLB)/. ; rm -f *.$sosfx ; fi
	if [ -d \$(OBJ) ] ; then cd \$(OBJ)/. ; rm -f *.o  rm -rf cxxrep* ; fi
#	if [ -d PI ]     ; then cd PI/.     ; \$(MAKE) clean ; fi

__END__

mv GNUmakefile ../GNUmakefile

echo '*** mkmf done ***'

