#!/bin/bash
#On mingw64/win32, static build is ok. Dynamic build requires libgcc_s_sjlj-1.dll to be shipped, unfortunately. -static -static-libgcc does not work in this case, because dynamically linking libcpdf causes the whole thing to be linked dynamically. mingw64/win64 does not suffer from this fault
cfiles=(cpdflibwrapper)
finalcfile=cpdflibtest
libname=cpdf
mlfiles=(cpdflib)
mlifiles=(cpdflib)
mllibs=()
ocamlfind=ocamlfind
ocamlfind_packs=(camlpdf cpdf)
stubs=(camlpdf)
echo ***EXTRACTING INFORMATION
#Extract some information from ocamlc and set up basic parameters
ranlib=`ocamlc -config 2>/dev/null | grep ranlib | sed 's/ranlib: //'`
echo ranlib: $ranlib
native_c_libraries=`ocamlc -config 2>/dev/null | grep native_c_libraries | sed 's/native_c_libraries: //'`
echo native_c_libraries: $native_c_libraries
libdir=`ocamlc -where`
echo libdir: $libdir
uname=`uname -s`
if [ "${uname:0:6}" == "CYGWIN" ];then
dllname="dll"	
libdir=`echo $libdir | dos2unix`
libdir=`cygpath $libdir`
link="flexlink -chain mingw -exe"
exesuffix=".exe"
else
dllname="so"
link="cc"
exesuffix=""
fi
echo dllname: $dllname
echo uname: $uname
echo libdir: $libdir
echo link: $link
echo exesuffix: $exesuffix
if [ ${#mllibs[@]} -gt 0 ]; then
camllibs=$(printf "%s.cmxa " "${mllibs[@]}")
fi
if [ ${#mlfiles[@]} -gt 0 ]; then
camlcmx=$(printf "%s.cmx " "${mlfiles[@]}")
fi
if [ ${#mllibs[@]} -gt 0 ]; then
camllink=$(printf -- "-l%s " "${mllibs[@]}")
fi
if [ ${#ocamlfind_packs[@]} -gt 0 ]; then
camlpackages=$(printf -- "-package %s " "${ocamlfind_packs[@]}")
fi
if [ ${#ocamlfind_packs[@]} -gt 0 ]; then
camlpackcmxa=$(printf "%s.cmxa " "${ocamlfind_packs[@]}")
fi
if [ ${#cfiles[@]} -gt 0 ]; then
wrapperos=$(printf "%s.o" "${cfiles[@]}")
fi
echo camllibs: $camllibs
echo camlcmx: $camlcmx
echo camllink: $camllink
echo camlpackages: $camlpackages
echo camlpackcmxa: $camlpackcmxa
echo wrapperos: $wrapperos
echo ***PROCESSING .mli files
#Process the .mli files
for x in "${mlifiles[@]}"
do
$ocamlfind ocamlc $camlpackages ${x}.mli
done
echo ***PROCESSING .ml files
#Process the .ml files
for x in "${mlfiles[@]}"
do
$ocamlfind ocamlopt $camlpackages -c ${x}.ml
done
#Using -output-obj for static library
$ocamlfind ocamlopt $camlpackages -output-obj -o impl.o $camllibs $camlpackcmxa $camlcmx
echo ***PROCESSING .c files
#Compile the .c files
for x in "${cfiles[@]}"
do
$ocamlfind ocamlc -c ${x}.c
done
#Using -output-obj for dynamic library, if on Windows
if [ "${uname:0:6}" == "CYGWIN" ];then
$ocamlfind ocamlopt $camlpackages -output-obj -o lib${libname}.$dllname $wrapperos $camllibs $camlpackcmxa $camlcmx
fi
echo ***BUILD static .a
#Build the static .a
rm -rf lib${libname}.a
ar rc lib${libname}.a impl.o
mkdir -p tmp_asmrun
asmrun_members=$(ar t ${libdir}/libasmrun.a | grep -v '__.*')
for stub in $stubs
do
if [ "${uname:0:6}" == "CYGWIN" ];then	
thefile=`ocamlfind query ${stub} | dos2unix`
thefile=`cygpath $thefile`
else
thefile=`ocamlfind query ${stub}`
fi
therest="/lib${stub}_stubs.a"
stub_members=$(ar t ${thefile}${therest} | grep -v '__.*')
( cd tmp_asmrun && ar x ${thefile}${therest} )
for mem in $stub_members; do ar rc lib${libname}.a tmp_asmrun/$mem; done
done
( cd tmp_asmrun && ar x ${libdir}/libasmrun.a )
for mem in $asmrun_members; do ar rc lib${libname}.a tmp_asmrun/$mem; done
for x in "${cfiles[o]}"
do
ar rc lib${libname}.a ${x}.o 
done
$ranlib lib${libname}.a
rm -rf tmp_asmrun
#echo ***BUILD example executable with static .a
#Build an example executable, with static .a
$ocamlfind ocamlc -c ${finalcfile}.c
linkflags="-L.. -L. -l${libname} -L$libdir $camllink $native_c_libraries"
#echo "STATIC LINKER: "$link
#echo "STATIC LINKER FLAGS: "$linkflags
#echo "STATIC LINKER CALL: "$link -o $finalcfile$exesuffix ${finalcfile}.o $linkflags
$link -o $finalcfile$exesuffix ${finalcfile}.o $linkflags

cd examples
$ocamlfind ocamlc -c merge.c
$ocamlfind ocamlc -c squeeze.c
$link -o merge$exesuffix merge.o $linkflags
$link -o squeeze$exesuffix squeeze.o $linkflags
cd ..

echo ***BUILD example executable with dynamic .dll library on Windows
#Build an example executable, with dynamic .dll library on Windows
if [ "${uname:0:6}" == "CYGWIN" ];then
echo "DYNAMIC LINKER CALL"
echo "cc -o $finalcfile-shared$exesuffix ${finalcfile}.o -Wl,-rpath,. -L. -l:lib${libname}.dll -Wl,-rpath,$libdir -L$libdir $camllink"
cc -o $finalcfile-shared$exesuffix ${finalcfile}.o -Wl,-rpath,. -L. -l:lib${libname}.dll -Wl,-rpath,$libdir -L$libdir $camllink
fi


