<?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>

  <!--don't let regenerating these files trigger builds-->
  <UsingTask TaskName="MakeSameWriteTime" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
    <ParameterGroup>
      <SourceFile Required="true" ParameterType="System.String"/>
      <DestFile Required="true" ParameterType="System.String"/>
    </ParameterGroup>
    <Task>
      <Code Type="Fragment" Language="cs">
        <![CDATA[
        System.IO.File.SetLastWriteTime( DestFile, System.IO.File.GetLastWriteTime( SourceFile ) );
        ]]>
      </Code>
    </Task>
  </UsingTask>

  <!--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 /I$(SrcDir) /I$(MsBuildThisFileDirectory).. /Fi$(PreProc) /P $(SrcDir)qstrdefs.h"/>
    <Exec Command="python $(SrcDir)makeqstrdata.py $(PreProc) $(QstrDefs) > $(DestFile)"/>
    <MakeSameWriteTime SourceFile="$(MsBuildThisFile)" DestFile="$(DestFile)"/>
  </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)" Overwrite="true"/>
    <MakeSameWriteTime SourceFile="$(MsBuildThisFile)" DestFile="$(DestFile)"/>
  </Target>

</Project>
