echo off
REM set the path due to the OpenModelica installation path to use the OpenModelica gcc.
REM another gcc may be incompatible.   
set PATH=%OPENMODELICAHOME%\tools\msys\mingw64\bin;%OPENMODELICAHOME%\tools\msys\mingw64\bin\..\..\usr\bin;%PATH%
REM out.txt and err.txt are in the calling dir
set CDC=%CD%
echo CDC=%CDC%
:loopCC
del out.txt
del err.txt
cls
REM from last usage -save-temps:
if exist *.s del *.s
if exist *.i del *.i


REM Check Dependencies invoked from jz.cmd:
REM %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/deps.txt read: successful

set INCLUDEPATH= -I"../../emc/source" -I"../../emc/" -I"../../emc/incApplSpecific/TargetNumericSimple64" -I"../../emc/incComplSpecific/cc_Gcc64" -I"../../emc/genRefl" -I"c:/Programs/OpenModelica1.13.264bit/include" -I"."


if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\emC\Handle_ptr64_emC.o (
  if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\emC mkdir %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\emC
  if exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\emC\Handle_ptr64_emC.o del %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\emC\Handle_ptr64_emC.o
  echo gcc -o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/emC/Handle_ptr64_emC.o ../../emc/source/emC/Handle_ptr64_emC.c >>err.txt  
  echo gcc ../../emc/source/emC/Handle_ptr64_emC.c
  g++.exe -c "-std=c99" -Winline %INCLUDEPATH% -o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/emC/Handle_ptr64_emC.o ../../emc/source/emC/Handle_ptr64_emC.c 1>>out.txt 2>>err.txt
)
if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\emC\Handle_ptr64_emC.o echo !!!!error^^^^^^^^!!!! >>err.txt

if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\sourceApplSpecific\SimpleNumCNoExc\ThreadContextSingle_emC.o (
  if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\sourceApplSpecific\SimpleNumCNoExc mkdir %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\sourceApplSpecific\SimpleNumCNoExc
  if exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\sourceApplSpecific\SimpleNumCNoExc\ThreadContextSingle_emC.o del %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\sourceApplSpecific\SimpleNumCNoExc\ThreadContextSingle_emC.o
  echo gcc -o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/sourceApplSpecific/SimpleNumCNoExc/ThreadContextSingle_emC.o ../../emc/sourceApplSpecific/SimpleNumCNoExc/ThreadContextSingle_emC.c >>err.txt  
  echo gcc ../../emc/sourceApplSpecific/SimpleNumCNoExc/ThreadContextSingle_emC.c
  g++.exe -c "-std=c99" -Winline %INCLUDEPATH% -o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/sourceApplSpecific/SimpleNumCNoExc/ThreadContextSingle_emC.o ../../emc/sourceApplSpecific/SimpleNumCNoExc/ThreadContextSingle_emC.c 1>>out.txt 2>>err.txt
)
if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\sourceApplSpecific\SimpleNumCNoExc\ThreadContextSingle_emC.o echo !!!!error^^^^^^^^!!!! >>err.txt

if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\myTestFnAlloc.o (
  if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\. mkdir %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\.
  if exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\myTestFnAlloc.o del %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\myTestFnAlloc.o
  echo gcc -o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/myTestFnAlloc.o myTestFnAlloc.c >>err.txt  
  echo gcc myTestFnAlloc.c
  g++.exe -c "-std=c99" -Winline %INCLUDEPATH% -o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/myTestFnAlloc.o myTestFnAlloc.c 1>>out.txt 2>>err.txt
)
if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\myTestFnAlloc.o echo !!!!error^^^^^^^^!!!! >>err.txt

if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\myTestFnAlloc_Mdlwrp.o (
  if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\. mkdir %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\.
  if exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\myTestFnAlloc_Mdlwrp.o del %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\myTestFnAlloc_Mdlwrp.o
  echo gcc -o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/myTestFnAlloc_Mdlwrp.o myTestFnAlloc_Mdlwrp.c >>err.txt  
  echo gcc myTestFnAlloc_Mdlwrp.c
  g++.exe -c "-std=c99" -Winline %INCLUDEPATH% -o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/myTestFnAlloc_Mdlwrp.o myTestFnAlloc_Mdlwrp.c 1>>out.txt 2>>err.txt
)
if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\myTestFnAlloc_Mdlwrp.o echo !!!!error^^^^^^^^!!!! >>err.txt

if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\LogException_Mdlc.o (
  if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\. mkdir %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\.
  if exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\LogException_Mdlc.o del %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\LogException_Mdlc.o
  echo gcc -o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/LogException_Mdlc.o LogException_Mdlc.c >>err.txt  
  echo gcc LogException_Mdlc.c
  g++.exe -c "-std=c99" -Winline %INCLUDEPATH% -o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/LogException_Mdlc.o LogException_Mdlc.c 1>>out.txt 2>>err.txt
)
if not exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj\LogException_Mdlc.o echo !!!!error^^^^^^^^!!!! >>err.txt

echo  >>err.txt
echo ==== Lib==== >>err.txt
del %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\libmyTestFnAlloc.a
ar.exe mc -o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\libmyTestFnAlloc.a  %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/emC/Handle_ptr64_emC.o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/sourceApplSpecific/SimpleNumCNoExc/ThreadContextSingle_emC.o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/myTestFnAlloc.o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/myTestFnAlloc_Mdlwrp.o %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj/LogException_Mdlc.o 1>>out.txt 2>>err.txt
if not errorlevel 1 goto :okLib
  type err.txt
  echo abort though compile error press ctrl-C
  pause
  ::goto :loopCC
  exit /B
:okLib
echo off
if exist %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\libmyTestFnAlloc.a (
  echo ok: %TMP%/OpenModelica/OMEdit/MyTestFnC_Mdl/libmyTestFnAlloc.a 1>>err.txt
  echo ok: %TMP%/OpenModelica/OMEdit/MyTestFnC_Mdl/libmyTestFnAlloc.a
  REM copy the generated lib to the currdir to save it.                    
  copy %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\libmyTestFnAlloc.a .\*
)  

copy myTestFnAlloc_Mdlwrp.h %TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl
echo press ctrl-C for abort, other key to re-compile                   
pause
