REM start problem: The batch needs the zbnf.jar File. Either the ZBNFJAX_HOME environment variable is set already,
REM or it is set via a found setZBNFJAX_HOME.bat, or it is on D:\vishia... or you should adapt it.

set ZBNFJAX_HOME=../../ZBNF/zbnfjax
set CDC=%CD%
:loop
cd /D %CDC%
java -cp %ZBNFJAX_HOME%/zbnf.jar org.vishia.jztxtcmd.JZtxtcmd %0
pause
REM call the just generated batch to compile:
call myTestFnAlloc.zmk.bat
goto :loop                                                            
exit /B                                                   

==JZtxtcmd==

//include $ZBNFJAX_HOME/jzTc/Cheader2Refl.jzTc;
currdir=scriptdir;

text = "myTestFnAlloc.zmk.bat";  ##this is the output file for <:>text<.>

String emcSrcDir = "../../emc";

String srcDir = ".";

String objDir=<:><&$TMP>\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj<.>;  ##should written with \
String objTmp = "%TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\myTestFnAlloc.obj";  ##for not absolute path environment path


String cfgCheckDeps = "cfgCheckDeps.cfg";

##options for the gcc compiler
String cOptions = <:>"-std=c99" -Winline<.>;
##String cOptions = <:>-save-temps "-std=c99" -Winline<.>;  ##for deep compiler error search

Fileset header2copySet =
( myTestFnAlloc_Mdlwrp.h
);


Fileset sources = 
(  
, &emcSrcDir/source:emC/Handle_ptr64_emC.c
##, &emcSrcDir/source:emC/StringBase_emC.c
, &emcSrcDir:sourceApplSpecific/SimpleNumCNoExc/ThreadContextSingle_emC.c
##, &emcSrcDir:sourceApplSpecific/applConv/LogException_emC.c
, myTestFnAlloc.c
, myTestFnAlloc_Mdlwrp.c
, LogException_Mdlc.c
);


Fileset includeset = 
( &emcSrcDir/source    ##FilePath: the local path is enhanced with the variables content.
, &emcSrcDir
, &emcSrcDir/incApplSpecific/TargetNumericSimple64
, &emcSrcDir/incComplSpecific/cc_Gcc64   
, &emcSrcDir/genRefl   
, c:/Programs/OpenModelica1.13.264bit/include
, .
);


                                                                                                                     
main()
{
  mkdir &objDir;
  call prepareGcc();
  
  zmake <:><&objDir>/*.o<.> := ccGcc(&sources, objTmp = objTmp); 
  ##generate lib in the working dir of Modelica
  zmake "%TMP%\OpenModelica\OMEdit\MyTestFnC_Mdl\libmyTestFnAlloc.a" := libGcc(&sources, objTmp = objTmp); 
  <+out>success<.+n>

}



sub prepareGcc(){
                   <:>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
===================
===================<.>
    
}



sub ccGcc(Obj target: org.vishia.cmd.ZmakeTarget, String objTmp)
{
  String objDir = target.output.absdir();  ##used for checkDeps, need absolute dir
  ##objTmp is used for bat-output. It should be a path with %TMP% for independent usage.
  List inputs = target.allInputFilesExpanded();
  List includes = includeset.listFiles();
  ##
  Obj checkDeps = new org.vishia.checkDeps_C.CheckDependencyFile(console, 1); ##installation dependency check
  checkDeps.setDirObj(<:><&objDir>/*.o<.>);          ##sets dir and the extension
  checkDeps.readCfgData(cfgCheckDeps, File: &srcDir);
  String depsRead = checkDeps.readDependencies(<:><&objDir>/deps.txt<.>);  ##gets last dependency 
  if(not depsRead) { depsRead = "successful"; }
  <+out><:n>checkDeps_C: <&objDir>/deps.txt read: <&depsRead><.+n> 
                   <:>
===================REM Check Dependencies invoked from jz.cmd:
===================REM <&objTmp>/deps.txt read: <&depsRead>
===================
===================set INCLUDEPATH=<:for:incl:includes> -I"<&incl.file()>"<.for>
===================
===================<.> 
  for(input:inputs) {
    ##The checkDeps algorithm itself may be unnecessary for compilation for compilation of all files.
    ##but it creates the obj directory tree which is necessary for compilation.
    ##The checkDeps checks whether the file is changed, delete the obj file on changed file.
    Obj infoDeps = checkDeps.processSrcfile(File: &input.file(), input.localfile());
    <+out><&infoDeps><.+n> ##show state, info and file name on console.
                   <:>
===================if not exist <&objTmp>\<&input.localnameW()>.o (
===================  if not exist <&objTmp>\<&input.localdirW()> mkdir <&objTmp>\<&input.localdirW()>
===================  if exist <&objTmp>\<&input.localnameW()>.o del <&objTmp>\<&input.localnameW()>.o
===================  echo gcc -o <&objTmp>/<&input.localname()>.o <&input.file()> >>err.txt  
===================  echo gcc <&input.file()>
===================  g++.exe -c <&cOptions> %INCLUDEPATH% -o <&objTmp>/<&input.localname()>.o <&input.file()> 1>>out.txt 2>>err.txt
===================)
===================if not exist <&objTmp>\<&input.localnameW()>.o echo !!!!error^^^^^^^^!!!! >>err.txt
===================<.>
  }
  ##write back the checked dependencies:
  checkDeps.writeDependencies();
  checkDeps.close();
}


sub libGcc(Obj target: org.vishia.cmd.ZmakeTarget, String objTmp)
{
  List inputs = target.allInputFilesExpanded();
  List headers2Copy = header2copySet.listFiles();
                   <:>
===================echo  >>err.txt
===================echo ==== Lib==== >>err.txt
===================del <&target.output.fileW()>
===================ar.exe mc -o <&target.output.fileW()> <:for:obj:inputs> <&objTmp>/<&obj.localname()>.o<.for> 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 <&target.output.fileW()> (
===================  echo ok: <&target.output.file()> 1>>err.txt
===================  echo ok: <&target.output.file()>
===================  REM copy the generated lib to the currdir to save it.                    
===================  copy <&target.output.fileW()> .\*
===================)  
===================<.>
  ##
  ##generate code in batch to copy all header to the modelica working dir which are used in modelica compilation
  for(header: headers2Copy) {
                   <:>
===================copy <&header.file()> <&target.output.dirW()><.>
  }
  ##
  ##pause to view results in command line window 
                   <:>
===================echo press ctrl-C for abort, other key to re-compile                   
===================pause
===================<.>
}




