1#!/bin/sh
2# Create a loadable object from a C++ function defined in a .cpp file
3# $Id$
4thecommand="$0"
5dircommand=`dirname "$0"`
6
7FFVERSION='@VERSION@'
8FFCXXFLAGS='@CXXFLAGS@ @CPPFLAGS@ @UMFPACK_CPPFLAGS@ @HDF5_CPPFLAGS@'
9FFFFLAGS='@FFLAGS@'
10FFFLIBS='@FLIBS@'
11FFCXX11FLAGS='@CXX11FLAGS@'
12INCFF=
13if [ "@ENABLE_FFCS@" != "yes" ] ;then ffcs=0; else ffcs=1;fi
14if [ -z "$CXX" ] ; then CXX='@CXX@' ; fi
15if [ -z "$MPICXX" ] ; then MPICXX="@MPICXX@" ; fi
16if [ -z "$MPI_LIB" ] ; then MPI_LIB="@MPI_LIB@" ; fi
17if [ -z "$F77" ] ; then F77="@F77@" ; fi
18if [ "$CXX" = '@'CXX'@' ] ; then CXX=""; fi
19if [ "$MPICXX" = '@'MPICXX'@' ] ; then MPICXX=""; fi
20if [ "$F77" = '@'F77'@' ] ; then F77=""; fi
21if [ "$FFCXXFLAGS" = '@'CXXFLAGS'@' ] ; then FFCXXFLAGS=""; fi
22if [ "$FFFFLAGS" = '@'FFLAGS'@' ] ; then FFFFLAGS=""; fi
23if [ "$FFFLIBS" = '@'FLIBS'@' ] ; then FFFLIBS=""; fi
24if [ "$FFVERSION" = '@'VERSION'@' ] ; then FFVERSION=""; fi
25if [ -z "$INCFF" ] ; then
26    if [ -f "@INCFF@/ff++.hpp" ] ; then
27	INCFF="'-I@INCFF@'"
28    elif [ -f "${dircommand}/include/ff++.hpp" ]; then
29	INCFF="'-I${dircommand}/include'"
30    elif [ -f ../seq/include/ff++.hpp ]; then
31	INCFF="-I../seq/include"
32    elif [ -f "@FFAPPLI_INC@/ff++.hpp" ] ; then
33	INCFF="'-I@FFAPPLI_INC@'"
34    elif [ -f '@ff_prefix_dir@/include/ff++.hpp' ] ; then
35	INCFF="'-I@ff_prefix_dir@/include'"
36    else
37	ff=`which freefem++.exe`;
38	ffinc=`dirname "$ff"`/seq/include
39	if [ -f "$ffinc/ff++.hpp" ]; then
40	  INCFF=-I"'$ffinc'";
41        else
42	  echo " erreur PB include freefem++ directory " ;
43	  exit 1;
44	fi
45    fi
46fi
47do="yes"
48DEBUG=""
49CONLY="false"
50uu=`uname -s`
51INC=""
52LIBS=""
53DLL=""
54bin="."
55out=""
56files=""
57fileso=""
58OTHER="";
59WIN32="";
60
61case "$uu" in
62  CYGWIN*|MINGW*|MSYS*)
63  if [ -z "@WIN32DLLTARGET@" ] ; then
64      onwin32=1; uu="cygwin-version";
65  else
66      onwin32=1; uu="win32"; # pure windows
67  fi
68  WIN32="win32-"
69  wff=`which FreeFem++.exe`
70  dff=`dirname "$wff"`
71  if [ "$bin" = "." -a  -f ../../src/bin-win32/libff.dll  ]; then
72    bin=../../src/bin-win32
73  elif [ "$bin" = "." -a  -f ../src/bin-win32/libff.dll  ]; then
74      bin=../src/bin-win32
75  elif [  -f "$dff/libff.dll"  ]; then
76#  modif for install on cygwin
77    bin="$dff"
78  elif [ "$bin" = "." -a  -f ../libff.dll  ]; then
79    bin=..
80  elif [ "$bin" = "." -a  -f ../../libff.dll  ]; then
81    bin=../..
82  elif [ "$bin" = "." -a  -f ../../../libff.dll  ]; then
83    bin=../../..
84  fi;;
85esac
86autodep=""
87withmpi=""
88args=""
89
90while [ $# -ne 0 ]  ; do
91    argsp=$args
92    args="$args '$1'"
93    case "$1" in
94	-[h?]*) echo usage $0 "[-n] [-g] [-win32] [-l libfile] [-I includedir]  files"
95	    echo "    -auto :  build automaticaly the dependance (in test FH) "
96	    echo "    -n  :  do nothing just print"
97	    echo "    -g  :  compile with -g option add remove optimisation"
98	    echo "    -c  : compile only"
99	    echo "    -mpi:  with  mpi"
100	    echo "    -nompi:  without   $MPI_LIB links "
101	    echo "    -O*  :  compile with -O* option"
102	    echo "    -cygwin: compile for cygwin/win32 OS (Window XP, ...)"
103	    echo "    -win32:  compile for win32 OS (Window XP, ...) default under cygwin"
104	    echo "    -l files  add files to the ld process (link)"
105	    echo "    -I dir  add dir in include seach dir for compilation"
106	    echo "    -b dir  to change the default install  dir "
107	    echo "    -dll file  add  dll and this file copie in the install dir"
108	    echo "    -o outputfile without suffix"
109	    echo "    file.{cpp,cp,cxx,c,f,F}"
110	    exit 0;
111	    ;;
112	LIBFF=*) LIBFF=`echo $1|sed 's/[A-Z0-9]*=//'` ;;
113	F77=*) F77=`echo $1|sed 's/[A-Z0-9]*=//'` ;;
114	CXX=*) CXX=`echo $1|sed 's/[A-Z0-9]*=//'` ;;
115	MPICXX=*) MPICXX=`echo $1|sed 's/[A-Z0-9]*=//'`;withmpi=yes ;;
116	F77=*) F77=`echo $1|sed 's/[A-Z0-9]*=//'` ;;
117	CXXFLAGS=*) CXXFLAGS=`echo $1|sed 's/[A-Z0-9]*=//'` ;;
118	FFLAGS=*) FFLAGS=`echo $1|sed 's/[A-Z0-9]*=//'` ;;
119	MPI_LIB=*) MPI_LIB=`echo $1|sed 's/[A-Z0-9]*=//'` ;;
120	-std=*)	 CXXFLAGS="$1 $CXXFLAGS" ;;
121	-auto) autodep=yes;args=$argsp;;
122	-mpi) withmpi=yes;;
123	-nompi) MPI_LIB='';withmpi='';;
124	-n)  do="no";;
125	-g)  DEBUG="$DEBUG $1";
126            FFCXXFLAGS=`echo $FFCXXFLAGS| sed -e 's/-O[0-9]*//g' -e 's/-DNDEBUG//g' ` ;;
127	-c)  CONLY=yes;;
128	-O*) DEBUG="$DEBUG $1";;
129	-cygwin)  onwin32=1; uu="cygwin-version";;
130	-win32)   onwin32=1; uu="win32";;
131	-b)     bin=$2  ; shift;;
132	-I*)    INC="$INC '$1'";;
133	-D*)    INC="$INC '$1'";;
134	-dll)   DLL="$DLL '$2'";shift;;
135	-[Ll]*)    LIBS="$LIBS '$1'" ;; # FFCS - 27/10/11 - need quotes for MPICH libraries in 'Program Files' under Windows
136	*.a)     LIBS="$LIBS $1" ;;
137	*.so)    LIBS="$LIBS $1" ;;
138	*.dll)   if [ -f "$1" ] ; then  LIBS="$LIBS '$1'"
139	    elif [ -f "$bin/$1" ] ; then  LIBS="$LIBS '$bin/$1'"
140	    else
141	      echo " erreur file not found $1" ;
142	      exit 1;
143	    fi;;
144	-Wl*)    LIBS="$LIBS $1" ;;
145	*.cpp) files="$files '$1'";  o=`basename "$1" .cpp` ; fileso="$fileso '$o.o'"; args=$argsp;;
146	*.cp)  files="$files '$1'";  o=`basename "$1" .cp`   ; fileso="$fileso '$o.o'"; args=$argsp;;
147	*.cxx) files="$files '$1'";  o=`basename "$1" .cxx` ; fileso="$fileso '$o.o'"; args=$argsp;;
148	*.c)   files="$files '$1'";  o=`basename "$1" .c` ; fileso="$fileso '$o.o'"; args=$argsp;;
149	*.f)   ffiles="$ffiles '$1'";  o=`basename "$1" .f` ; fileso="$fileso '$o.o'";;
150	*.F)   ffiles="$ffiles '$1'";  o=`basename "$1" .F` ; fileso="$fileso '$o.o'";;
151	*.o)   fileso="$fileso '$1'"; o=`basename "$1" .o` ;;
152	-o)    out="$2"; shift;;
153	*) OTHER="$OTHER $1";;
154    esac
155    shift
156done
157
158#  remove old file... FH sep 2013..
159SUF=@DYLIB_SUFFIX@
160
161if [ -n "$autodep"  ] ; then
162#    echo "$thecommand" $args  `eval "'$dircommand/ff-get-dep'" -ff $files`
163    argsdep=`eval "'$dircommand/ff-get-dep'" -ff $files`
164
165     error=`echo "$argsdep"| grep ERROR`
166     if [ -n "$error" ] ; then
167	 echo " WARNING in  auto dependance seach ( missing plugin $2) .  sorry : $error "
168
169	 # FFCS - 28/11/11 - we need to stop compiling as soon as there is an error because we want a fixed set of features in
170	 # FFCS
171	 # return a error when FFCS enable .. FH.
172	 echo " -- $error " >>Missing-plugins-@DYLIB_SUFFIX@.log
173	 exit $ffcs;
174     fi
175    echo eval "$thecommand" $args  $argsdep
176    eval "$thecommand" $args  $argsdep
177
178    # FFCS needs an error exit code to make sure that all libraries are correctly compiled
179    exit $?;
180fi
181
182if [  -n "$onwin32" -a ! -f "$bin/libff.dll"  ] ; then
183    echo " Error the file libff.dll  must be exist in '$bin' the install directory,"
184    echo " to link on windows OS "
185    echo " Use the parameter -b to set the correct install directory "
186    exit 1;
187fi
188
189if [ -z "$out" ] ; then  out=$o  ; fi;
190if [ -f  "$out.$SUF" ] ; then rm $out.$SUF ; fi
191
192
193# Default compiler
194if [ -z "$CXX"  ];then  CXX=g++ ; fi
195if [ -n "$withmpi" -a -z "$MPICXX" ] ; then echo $0: Sorry NO MPI; exit 0; fi
196if [ -n "$withmpi" ]; then  CXX=$MPICXX;fi
197
198test "$withmpi" = "yes" && WMPI_LIB="$MPI_LIB"
199
200# build a mpi version of the plugin if the plugin do exist un mpi dir
201# and if the mpi version exist and if
202OWMPI=""
203test  -d ../mpi -a -n "$MPI_LIB" -a "$withmpi" != "yes" -a ! -f  "../mpi/$out.cpp"  &&  OWMPI="../mpi/$out.$SUF"
204test -f "$OWMPI" -a -n "$OWMPI" && rm "$OWMPI"
205
206
207
208INC="$INCFF $INC"
209SHARED="-shared"
210case "$WIN32$uu" in
211    Darwin*)
212#        echo "export MACOSX_DEPLOYMENT_TARGET=10.3"
213#	export MACOSX_DEPLOYMENT_TARGET=10.4
214#	SUF=dylib
215	SHARED="-bundle -undefined dynamic_lookup"
216
217	if [  -f "$LIBFF" ] ; then
218		echo " xcode interface  with $LIBFF"
219		SHARED="-bundle"
220		out=`dirname "$LIBFF"`/$out
221		LIBS="$LIBFF $LIBS"
222		OWMPI=
223	fi
224	;;
225    win32-CYGWIN*|win32-win32)
226        echo " Window without cygwin "
227	WMPI_LIB=''
228        b="$bin"
229        LIBS=" '$b/libff.dll'  $LIBS $DLL"
230
231	# FFCS - 17/10/12 - --unresolved-symbols=ignore-all is not understood by the current mingw64 compilers
232        SHARED="-shared -Wl,--enable-auto-import"
233
234	# FFCS - 17/10/12 - -mno-cygwin is not understood by the current mingw64 compilers
235        ###FLAGS='  -mno-cygwin '
236#        SUF=dll
237	;;
238    win32-cygwin-version)
239        echo " cygwin-version "
240	WMPI_LIB=''
241        b=$bin
242        FLAGS=' '
243        LIBS="'$b/libff.dll' $LIBS $DLL"
244#        SUF=dll
245	;;
246    FreeBSD|DragonFly|NetBSD)
247	SHARED="-shared"
248	FLAGS="-fPIC $LDFLAGS";;
249	# 64 bit Linux needs -fPIC (ALH)
250    SunOS)
251        SHARED="-shared"
252        FLAGS='-fPIC';;
253        # 64 bit Linux needs -fPIC (ALH)
254    Linux)
255	FLAGS='-fPIC'
256	SHARED="-shared " ;;
257    *)
258	echo "sorry unknown achitecture "`uname`
259	exit 1;;
260esac
261
262
263#FLAGS="$FLAGS $DEBUG $FFCXXFLAGS"
264#  change F. H Version 3.17
265FLAGS="$FLAGS $DEBUG"
266
267if [ -n "$ffiles$files$fileso" ] ; then
268    if [  -n "$WIN32"  -a  ! -f "$bin/libff.dll"  ]; then
269	echo " Sorry, no freefem .dll file (libff.dll)  in $bin dir "
270	echo " try with -b dir-path where the file libff.dll exist"
271	exit 1;
272    fi
273    if [ "$files" ] ;then
274	echo $CXX -c $FLAGS $CXXFLAGS $FFCXXFLAGS $INC $PIC $files
275	if [  $do = yes ] ; then
276	    eval $CXX -c $INC $FLAGS $CXXFLAGS $FFCXXFLAGS  $PIC  $files
277	    ret=$?; if [ $ret -ne 0 ] ; then exit $ret ;fi
278	fi
279	echo
280    fi
281    if [ "$ffiles" ] ;then
282	echo $F77 -c $FLAGS $FFLAGS $FFFFLAGS $INC $PIC $ffiles
283	LIBS="$FFFLIBS $LIBS"
284	if [  $do = yes ] ; then
285	    eval $F77 -c $INC $FFLAGS $FLAGS $FFFFLAGS $PIC  $ffiles
286	    ret=$?; if [ $ret -ne 0 ] ; then exit $ret ;fi
287	fi
288	echo
289    fi
290    if [ "$CONLY" != yes ]; then
291	echo $CXX $SHARED $FLAGS $CXXFLAGS $FFCXXFLAGS $fileso -o $out.$SUF $LIBS $DLL $OTHER $WMPI_LIB
292	if [  $do = yes ] ; then
293	   eval $CXX $SHARED $FLAGS $CXXFLAGS $FFCXXFLAGS $fileso -o $out.$SUF $LIBS $DLL $OTHER $WMPI_LIB
294	    ret=$?; if [ $ret -ne 0 ] ; then exit $ret ;fi
295	fi
296	echo
297	if [  "$bin" != "." ]; then
298	    echo cp $out.$SUF  $bin
299	    test $do = yes &&   cp $out.$SUF  "$bin"
300	fi
301	if [ -n "$DLL" ] ; then
302	    echo cp $DLL $bin
303	    test $do = yes &&   cp $DLL "$bin"
304	fi
305	if [ -n "$OWMPI" ] ; then
306	    echo $CXX $SHARED $FLAGS $CXXFLAGS $FFCXXFLAGS $fileso -o $OWMPI $LIBS $DLL $OTHER $MPI_LIB
307	    if [  $do = yes ] ; then
308		eval $CXX $SHARED $FLAGS $CXXFLAGS $FFCXXFLAGS $fileso -o $OWMPI $LIBS $DLL $OTHER $MPI_LIB
309		ret=$?; if [ $ret -ne 0 ] ; then exit $ret ;fi
310	    fi
311	    echo
312	fi
313    fi
314fi
315