<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="GenerateHeaders">

  <Target Name="GenerateHeaders" DependsOnTargets="MakeQstrData;MakeVersionHeader">
  </Target>

  <PropertyGroup>
    <SrcDir>$(MsBuildThisFileDirectory)..\..\py\</SrcDir>
    <DestDir>$(MsBuildThisFileDirectory)..\build\genhdr\</DestDir>
  </PropertyGroup>

  <Target Name="MakeDestDir">
    <MakeDir Directories="$(DestDir)"/>
  </Target>

  <!--see py/py.mk under #qstr data-->
  <Target Name="MakeQstrData" DependsOnTargets="MakeDestDir">
    <PropertyGroup>
      <PreProc>$(DestDir)qstrdefs.preprocessed.h</PreProc>
      <QstrDefs>$(MsBuildThisFileDirectory)..\..\unix\qstrdefsport.h</QstrDefs>
      <DestFile>$(DestDir)qstrdefs.generated.h</DestFile>
    </PropertyGroup>
    <Exec Command="cl /nologo /I$(SrcDir) /I$(MsBuildThisFileDirectory).. /Fi$(PreProc) /P $(SrcDir)qstrdefs.h"/>
    <Exec Command="python $(SrcDir)makeqstrdata.py $(PreProc) $(QstrDefs) > $(DestFile).tmp"/>
    <Exec Command="fc /B $(DestFile).tmp $(DestFile) > NUL 2>&amp;1" IgnoreExitCode="true">
      <Output TaskParameter="ExitCode" PropertyName="FilesDiffer" />
    </Exec>
    <Copy SourceFiles="$(DestFile).tmp" DestinationFiles="$(DestFile)" Condition="'$(FilesDiffer)'!='0'"/>
  </Target>

  <!--see py/py-version.sh-->
  <Target Name="GetGitState">
    <Exec Command="git describe --dirty --always" ConsoleToMSBuild="true" IgnoreExitCode="true">
      <Output TaskParameter="ConsoleOutput" PropertyName="GitTag" />
    </Exec>
    <Exec Command="git rev-parse --short HEAD 2>NUL || echo unknown" ConsoleToMSBuild="true" IgnoreExitCode="true">
      <Output TaskParameter="ConsoleOutput" PropertyName="GitHash" />
    </Exec>
    <Exec Command="git diff --no-ext-diff --quiet --exit-code 2>NUL || echo 0" ConsoleToMSBuild="true" IgnoreExitCode="true">
      <Output TaskParameter="ConsoleOutput" PropertyName="GitFilesAreClean" />
    </Exec>
    <Exec Command="git diff-index --cached --quiet HEAD -- 2>NUL || echo 0" ConsoleToMSBuild="true" IgnoreExitCode="true" Condition="'$(GitFilesAreClean)'==''">
      <Output TaskParameter="ConsoleOutput" PropertyName="GitFilesAreClean" />
    </Exec>
  </Target>

  <Target Name="MakeVersionHeader" DependsOnTargets="MakeDestDir;GetGitState">
    <PropertyGroup Condition="'$(GitFilesAreClean)'=='0'">
      <GitHash>$(GitHash)-dirty</GitHash>
    </PropertyGroup>
    <PropertyGroup>
      <DestFile>$(DestDir)py-version.h</DestFile>
    </PropertyGroup>
    <ItemGroup>
      <Lines Include="// This file was generated by $([System.IO.Path]::GetFileName(`$(MsBuildThisFile)`))"/>
      <Lines Include="#define MICROPY_GIT_TAG &quot;$(GitTag)&quot;"/>
      <Lines Include="#define MICROPY_GIT_HASH &quot;$(GitHash)&quot;"/>
      <Lines Include="#define MICROPY_BUILD_DATE &quot;$([System.DateTime]::Now.ToString(`yyyy-MM-dd`))&quot;"/>
    </ItemGroup>
    <WriteLinesToFile Lines="@(Lines)" File="$(DestFile).tmp" Overwrite="true"/>
    <Exec Command="fc /B $(DestFile).tmp $(DestFile) > NUL 2>&amp;1" IgnoreExitCode="true">
      <Output TaskParameter="ExitCode" PropertyName="FilesDiffer" />
    </Exec>
    <Copy SourceFiles="$(DestFile).tmp" DestinationFiles="$(DestFile)" Condition="'$(FilesDiffer)'!='0'"/>
  </Target>

</Project>
