#!/bin/csh 
# $Id: mkmf,v 1.14 2001-07-30 16:36:54 ansari 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

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


#  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 ) 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 ../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 PIext ] ; then cd PIext ; $(MAKE) ; fi' >> GNUmakefile


echo 'mkmf: Writing shared library building part of the makefile :'

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)libPIext.a'   >> GNUmakefile
echo '#  List of external library references'       >> GNUmakefile
echo 'EXTLIB  = -L$(EXTLIBPATH)' $extlib_list       >> GNUmakefile
echo 'EXTSLB  = -L$(EXTSLBPATH)' $extslb_list       >> GNUmakefile
echo 'XPILIBS = $(LIBXPIPATH) $(LIBXPILIST)'        >> GNUmakefile
echo ''                                  >> GNUmakefile
echo '#  main dependence list'           >> GNUmakefile
echo 'slball : slb slbext slbpi '        >> GNUmakefile
echo 'slb : $(SLB)libsophya.so '         >> GNUmakefile
echo 'slbext : $(SLB)libextsophya.so   ' >> GNUmakefile
echo 'slbpi : $(SLB)libPI.so'            >> 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 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.so : $(PSLB)'                             >> GNUmakefile
echo '	$(CMDTILSHL) '                                          >> GNUmakefile
echo '	cd  $(OBJ); \'                                          >> GNUmakefile
echo '	$(CMDSHLCXX) -o $(SLB)libsophya.so  \'                  >> GNUmakefile
echo '	$(FGSHLACXX) $(ALLOBJS)  $(FGSHLNCXX)\'                 >> GNUmakefile
echo '	-L$(SLB) $(LIBLSHL)'                                    >> GNUmakefile
echo ' '                                                        >> GNUmakefile

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

echo '# Building PI shared library'  >> GNUmakefile
echo '$(SLB)libPI.so : $(PIPSLB)'                               >> GNUmakefile
echo '	$(CMDTILSHL) '                                          >> GNUmakefile
echo '	cd  $(OBJ); \'                                          >> GNUmakefile
echo '	$(CMDSHLCXX) -o $(SLB)libPI.so  \'                      >> 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 *.so ; 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 ***'

