##This file contains zmake subroutines which can be used in the ZmakeUserScript.

Obj checkDeps = new org.vishia.checkDeps_C.CheckDependencyFile(console, 1);



sub openDependencyCheck(Filepath cfgfile = "cfgCheckDep.cfg", Filepath depfile = "dependencies.txt"){
  checkDeps.readCfgData(cfgfile.absfile(), currdir);
  checkDeps.readDependencies(depfile.absfile());

}

sub closeDependencyCheck(){
  checkDeps.writeDependencies();
  checkDeps.close();
}


##Compile only one input file, produce the output file with the same name, for simple routines.
##The target.output may name the output directory only.
##invoke:
## zmake outputPath/* := c1file2exe(&inputfiles);
##
sub c1file2exe(Obj target){

  List inputsExpanded = target.allInputFilesExpanded();
  for(input:inputsExpanded) {
    String nextNr1 = nextNr;
    <+>
      if exist /out:<&target.output.absdirW()>\<&input.name()>.exe goto :<&nextNr1>
      ##
      ##Compiler call:
      ##
      cl.exe /c /TP <&incPath> <&input.absfileW()> /Fo<&target.output.absdirW()>\obj\<&input.name()>.o
        1><&target.output.absdirW()>\obj\<&input.name()>.log 2><&target.output.absdirW()>\obj\<&input.name()>.err
      link.exe /LIBPATH:"<&$MSC6PATH>\VC98\Lib" /out:<&target.output.absdirW()>\<&input.name()>.exe
        <&target.output.absdirW()>\obj\<&input.name()>.o
      :<&nextNr1>
    <.+>
    <+out><&input.absfile()>
    <.+>
  }
  <+>pause
  <.+>
  
}

##Compile all files in target.input and use it to link to the target.output.
##invoke:
## zmake outputPath/output.exe := c12exe(&inputfiles, incPathSet=incpathset);
##
sub c2exeBatch(Obj target, Fileset incPathSet, Filepath objDirExt){

  
  String incPath = <:><:for:incPathEntry:incPathSet.listFilesExpanded()> /I <&incPathEntry.absfileW()><.for><.>; 

  List inputsExpanded = target.allInputFilesExpanded();
  ##
  ##Loop over all sources.c to compile:
  for(input:inputsExpanded) {
    <+out><&input.localfile()><.+n>   ##show in cmd line window
    ##checks whether the file is changed, delete the obj file on changed    
    checkDeps.processSrcfile(input.absfile(), input.localfile(), objDirExt.absdir(), ".obj");  
    String nextNr1 = nextNr;   ##provides an incremented number
    String objname = <:><&objDirExt.absdirW()>\<&input.name()><.>;
    <+>
    
      REM <&objDirExt.absdir()>
      if exist /out:<&target.output.absdirW()>\obj\<&input.name()>.obj goto :<&nextNr1>
      cl.exe /c /TP <&incPath> <&input.absfileW()> /Fo<&objname><&objDirExt.ext()> 1><&objname>.log 2><&objname>.err
      :<&nextNr1>
    <.+>
  }
  <+>
  link.exe /LIBPATH:"<&$MSC6PATH>\VC98\Lib" /out:<&target.output.absfileW()><: >
    <:for:input:inputsExpanded> <&objDirExt.absdirW()>\<&input.name()><&objDirExt.ext()><.for>
  <.+>
  
}




##Compile all files in target.input and use it to link to the target.output.
##invoke:
## zmake outputPath/output.exe := c12exe(&inputfiles, incPathSet=incpathset);
##
sub c2exejz(Obj target, Fileset incPathSet, Filepath objDirExt){

  Stringjar msg;

  List args;
  //args += <:><&$MSC6PATH>\VC98\Bin\CL.EXE<.>;
  args += "/c";
  args += "/TP" ;
  for(incPathEntry:incPathSet.listFilesExpanded()){
    args += "/I";
    args += &incPathEntry.absfileW();
  }

  ##With the known localization in $MSC6PATH (environment) expand the path and set MSC6-needed environment variables:
  set Path=<:><: >
    <&$MSC6PATH>\COMMON\MSDev98\Bin;<: >
    <&$MSC6PATH>\VC98\Bin;<: >
    <&$PATH><.>;
  set INCLUDE=<:><&$MSC6PATH>\VC98\ATL\INCLUDE;<&$MSC6PATH>\VC98\INCLUDE<.>;
  set LIB=<:><&$MSC6PATH>\VC98\LIB<.>;
  
  
  List inputsExpanded = target.allInputFilesExpanded();
  ##
  ##Loop over all sources.c to compile:
  <+out>Calctime before for: <&jzcmd.calctime()> <.+n>

  for(input:inputsExpanded) {
    <+out><&input.localfile()><.+n>   ##show in cmd line window
    ##checks whether the file is changed, delete the obj file on changed    
    checkDeps.processSrcfile(input.absfile(), input.localfile(), objDirExt.absdir(), ".obj");  
    String nextNr1 = nextNr;   ##provides an incremented number
    String objname = <:><&objDirExt.absdirW()>\<&input.name()><.>;
    msg = "";
    ##
    ##invocation of compiler cl.exe
    ##
    ##Note: The path variable is not used here, only invoke direct.
    <+out>Calctime before cmd: <&jzcmd.calctime()> <.+n>
    msg += cmd <:><&$MSC6PATH>\VC98\Bin\CL.EXE<.> <:args:args> <:><&input.absfileW()><.>
      <:>/Fo<&objname><&objDirExt.ext()><.>;    
    <+out><&jzcmdsub.cmdErrorlevel:"%8X">: <&msg><.+n>
    if(msg >= "error"){
      <+>
        Compilation error for <&input.localfile()>
        <&msg>
      <.+>  
    }  
  }
  ##
  <+out>Calctime before link: <&jzcmd.calctime()> <.+n>
  List objs;
  for(input:inputsExpanded) {
    objs += <:><&objDirExt.absdirW()>\<&input.name()><&objDirExt.ext()><.>;
  }
  msg = "";
    ##
    ##invocation of link.exe
    ##
  msg += cmd !argsCheck! <:><&$MSC6PATH>\VC98\bin\link.exe<.> <:>/LIBPATH:<&$MSC6PATH>\VC98\LIB<.>
         <:>/out:<&target.output.absfileW()><.>
         <:args:objs>;
  <+out>Calctime after link: <&jzcmd.calctime()> <.+n>
  <+out><&jzcmdsub.cmdErrorlevel:"%8X">: <&msg><.+n>
  <+out><:for:arg:argsCheck><:n>--><&arg><--<.for><.+n>
  if(msg >= "error"){
    <+>
      Linker error for <&input.localfile()>
      <&msg>
    <.+>  
  }  
  <+out>Calctime finish c2exejz: <&jzcmd.calctime()> <.+n>

  
}


sub setPathAllMsc6(){
 set Path=<:><: >
    ##<&$MSC6PATH>\..\dll;<: >
    <&$MSC6PATH>\COMMON\MSDev98\Bin;<: >
    ##<&$MSC6PATH>\COMMON\MSDev98\Bin\IDE;<: >
    ##<&$MSC6PATH>\COMMON\Tools;<: >
    ##<&$MSC6PATH>\COMMON\Tools\APE;<: >
    ##<&$MSC6PATH>\COMMON\IDE\IDE98;<: >
    ##<&$MSC6PATH>\COMMON\IDE\IDE98\resources\1033;<: >
    <&$MSC6PATH>\VC98\Bin;<: >
    ##<&$MSC6PATH>\VIntDev98\bin;<: >
    ##<&$MSC6PATH>\VC98\Lib;<: >
    ##<&$MSC6PATH>\COMMON\Wizards98;<: >
    <&$PATH><.>;
 
}