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

  <Import Project="paths.props" Condition="'$(PyPathsIncluded)' != 'True'"/>

  <!--Generate qstrdefs.generated.h and mpversion.h similar to what is done in py/mkrules.mk and py/py.mk-->
  <Target Name="GenerateHeaders" DependsOnTargets="MakeVersionHdr;MakeModuleDefs;MakeRootPointers;MakeCompressedData;MakeQstrData">
  </Target>

  <PropertyGroup>
    <DestDir>$(PyBuildDir)genhdr\</DestDir>
    <PySrcDir>$(PyBaseDir)py\</PySrcDir>
    <QstrDefs>$(PyBaseDir)ports\unix\qstrdefsport.h</QstrDefs>
    <PyQstrDefs>$(PySrcDir)qstrdefs.h</PyQstrDefs>
    <QstrDefsCollected>$(DestDir)qstrdefscollected.h</QstrDefsCollected>
    <QstrGen>$(DestDir)qstrdefs.generated.h</QstrGen>
    <ModuleDefsCollected>$(DestDir)/moduledefs.collected</ModuleDefsCollected>
    <RootPointersCollected>$(DestDir)/root_pointers.collected</RootPointersCollected>
    <CompressedCollected>$(DestDir)/compressed.collected</CompressedCollected>
    <PyPython Condition="'$(PyPython)' == ''">$(MICROPY_CPYTHON3)</PyPython>
    <PyPython Condition="'$(PyPython)' == ''">python</PyPython>
    <CLToolExe Condition="'$(CLToolExe)' == ''">cl.exe</CLToolExe>
    <PyClTool>$([System.IO.Path]::Combine(`$(CLToolPath)`, `$(CLToolExe)`))</PyClTool>
    <PyPlatformToolsetNum>120</PyPlatformToolsetNum> <!--Minimum we support is VS2013.-->
    <PyPlatformToolsetNum Condition="$(PlatformToolset.StartsWith('v'))">$(PlatformToolset.Replace('v', ''))</PyPlatformToolsetNum>
    <PyParallelPreProc Condition="'$(PyPlatformToolsetNum)' &gt; '140'">True</PyParallelPreProc> <!--VS2017 and up.-->
  </PropertyGroup>

  <UsingTask Condition="'$(PyParallelPreProc)' == 'True'"
             TaskName="ParallelCustomBuild" AssemblyFile="$(VCTargetsPath)\Microsoft.Build.CppTasks.Common.dll"/>

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

  <!-- Concatenate preprocessed files for use with makeqstrdefs.py.
       Filters out any lines which aren't used by makeqstrdefs.py so the resulting file is
       hundreds of times smaller and processing in python takes substantially less time-->
  <UsingTask TaskName="ConcatPreProcFiles" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
    <ParameterGroup>
      <OutputFile Required="true" ParameterType="System.String"/>
      <InputFiles Required="true" ParameterType="System.String[]"/>
    </ParameterGroup>
    <Task>
      <Code Type="Fragment" Language="cs">
        <![CDATA[
using(var outFile = System.IO.File.CreateText(OutputFile)) {
  foreach(var inFile in InputFiles)
    foreach(var line in System.IO.File.ReadAllLines(inFile))
      if((line.Contains(".c") && line.StartsWith("#line")) || line.Contains("MP_QSTR") ||
          line.Contains("MP_REGISTER") || line.Contains("MP_COMPRESSED_ROM_TEXT"))
        outFile.WriteLine( line );
}
        ]]>
      </Code>
    </Task>
  </UsingTask>

  <!-- If any of these changed we'll force all qstrs to be generated again-->
  <ItemGroup>
    <QstrDependencies Include="$(PyWinDir)mpconfigport.h;$(PySrcDir)mpconfig.h;$(PySrcDir)makeqstrdata.py"/>
  </ItemGroup>

  <!-- Preprocess changed files, concatenate and feed into makeqstrdefs.py split/cat-->
  <Target Name="MakeQstrDefs" DependsOnTargets="MakeDestDir;MakeVersionHdr" Inputs="@(ClCompile);@(QstrDependencies)" Outputs="$(QstrDefsCollected)">
    <ItemGroup>
      <PyIncDirs Include="$(PyIncDirs)"/>
      <PreProcDefs Include="%(ClCompile.PreProcessorDefinitions);NO_QSTR"/>
      <PyQstrSourceFiles Include="@(ClCompile)" Exclude="$(PyBuildDir)\frozen_content.c">
        <Changed>False</Changed>
        <OutFile>$([System.String]::new('%(FullPath)').Replace('$(PyBaseDir)', '$(DestDir)qstr\'))</OutFile>
      </PyQstrSourceFiles>
      <PyQstrSourceFiles>
        <OutFile>$([System.IO.Path]::ChangeExtension('%(OutFile)', '.pp'))</OutFile>
        <OutDir>$([System.IO.Path]::GetDirectoryName('%(OutFile)'))</OutDir>
      </PyQstrSourceFiles>
      <PyQstrSourceFiles>
        <Changed Condition="$([System.DateTime]::Compare($([System.IO.File]::GetLastWriteTime('%(FullPath)')), $([System.IO.File]::GetLastWriteTime('%(OutFile)')))) &gt; 0">True</Changed>
      </PyQstrSourceFiles>
      <QstrDependencies>
        <Changed Condition="$([System.DateTime]::Compare($([System.IO.File]::GetLastWriteTime('%(FullPath)')), $([System.IO.File]::GetLastWriteTime('$(DestDir)qstr.i.last')))) &gt; 0">True</Changed>
      </QstrDependencies>
    </ItemGroup>
    <PropertyGroup>
      <PyPreProcCommand>$(PyClTool) /nologo /I@(PyIncDirs, ' /I') /D@(PreProcDefs, ' /D')</PyPreProcCommand>
      <ForceQstrRebuild>@(QstrDependencies->AnyHaveMetadataValue('Changed', 'True'))</ForceQstrRebuild>
      <RunPreProcConcat>@(PyQstrSourceFiles->AnyHaveMetadataValue('Changed', 'True'))</RunPreProcConcat>
    </PropertyGroup>
    <ItemGroup>
      <PyQstrSourceFilesToPreProc Include="@(PyQstrSourceFiles)" Condition="'%(Changed)' == 'True' Or '$(ForceQstrRebuild)' == 'True'">
        <Command>$(PyPreProcCommand) /Fi%(OutFile) /P %(Identity)</Command>
        <Outputs>%(OutFile)</Outputs>
      </PyQstrSourceFilesToPreProc>
    </ItemGroup>

    <MakeDir Directories="@(PyQstrSourceFilesToPreProc->'%(OutDir)')"/>
    <Exec Condition="'$(PyParallelPreProc)' != 'True'" Command="%(PyQstrSourceFilesToPreProc.Command)" />
    <ParallelCustomBuild Condition="'$(PyParallelPreProc)' == 'True' And '@(PyQstrSourceFilesToPreProc)' != ''" Sources="@(PyQstrSourceFilesToPreProc)" />

    <ConcatPreProcFiles InputFiles="@(PyQstrSourceFiles->'%(OutFile)')" OutputFile="$(DestDir)qstr.i.last"
                        Condition="'$(RunPreProcConcat)' == 'True' Or '$(ForceQstrRebuild)' == 'True'"/>
    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdefs.py split qstr $(DestDir)qstr.i.last $(DestDir)qstr _"/>
    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdefs.py cat qstr _ $(DestDir)qstr $(QstrDefsCollected)"/>
  </Target>

  <Target Name="CollectCompressedData" DependsOnTargets="MakeQstrDefs" Inputs="$(DestDir)qstr.i.last" Outputs="$(CompressedCollected)">
    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdefs.py split compress $(DestDir)qstr.i.last $(DestDir)compress _"/>
    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdefs.py cat compress _ $(DestDir)compress $(CompressedCollected)"/>
  </Target>

  <Target Name="CollectModuleDefs" DependsOnTargets="MakeQstrDefs" Inputs="$(DestDir)qstr.i.last" Outputs="$(ModuleDefsCollected)">
    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdefs.py split module $(DestDir)qstr.i.last $(DestDir)module _"/>
    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdefs.py cat module _ $(DestDir)module $(ModuleDefsCollected)"/>
  </Target>

  <Target Name="MakeModuleDefs" DependsOnTargets="CollectModuleDefs" Inputs="$(ModuleDefsCollected)" Outputs="$(DestDir)moduledefs.h">
    <PropertyGroup>
      <DestFile>$(DestDir)moduledefs.h</DestFile>
      <TmpFile>$(DestFile).tmp</TmpFile>
    </PropertyGroup>
    <Exec Command="$(PyPython) $(PySrcDir)makemoduledefs.py $(ModuleDefsCollected) > $(TmpFile)"/>
    <MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="CopyFileIfDifferent" Properties="SourceFile=$(TmpFile);DestFile=$(DestFile)"/>
  </Target>

  <Target Name="CollectRootPointers" DependsOnTargets="MakeQstrDefs" Inputs="$(DestDir)qstr.i.last" Outputs="$(RootPointersCollected)">
    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdefs.py split root_pointer $(DestDir)qstr.i.last $(DestDir)root_pointer _"/>
    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdefs.py cat root_pointer _ $(DestDir)root_pointer $(RootPointersCollected)"/>
  </Target>

  <Target Name="MakeRootPointers" DependsOnTargets="CollectRootPointers" Inputs="$(PySrcDir)make_root_pointers.py;$(RootPointersCollected)" Outputs="$(DestDir)root_pointers.h">
    <PropertyGroup>
      <DestFile>$(DestDir)root_pointers.h</DestFile>
      <TmpFile>$(DestFile).tmp</TmpFile>
    </PropertyGroup>
    <Exec Command="$(PyPython) $(PySrcDir)make_root_pointers.py $(RootPointersCollected) > $(TmpFile)"/>
    <MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="CopyFileIfDifferent" Properties="SourceFile=$(TmpFile);DestFile=$(DestFile)"/>
  </Target>

  <Target Name="MakeCompressedData" DependsOnTargets="CollectCompressedData" Inputs="$(CompressedCollected)" Outputs="$(DestDir)compressed.data.h">
    <PropertyGroup>
      <DestFile>$(DestDir)compressed.data.h</DestFile>
      <TmpFile>$(DestFile).tmp</TmpFile>
    </PropertyGroup>
    <Exec Command="$(PyPython) $(PySrcDir)makecompresseddata.py $(CompressedCollected) > $(TmpFile)"/>
    <MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="CopyFileIfDifferent" Properties="SourceFile=$(TmpFile);DestFile=$(DestFile)"/>
  </Target>

  <Target Name="MakeQstrData" DependsOnTargets="MakeQstrDefs" Inputs="$(QstrDefsCollected);$(PyQstrDefs);$(QstrDefs)" Outputs="$(QstrGen)">
    <PropertyGroup>
      <TmpFile>$(QstrGen).tmp</TmpFile>
    </PropertyGroup>
    <Exec Command="$(PyClTool) /nologo /I@(PyIncDirs, ' /I') /D@(PreProcDefs, ' /D') /E $(PyQstrDefs) $(QstrDefs) > $(DestDir)qstrdefs.preprocessed.h"/>
    <!--Because makemanifest.py relies on this file to have all Q() and QCFG() entries.-->
    <Exec Command="type $(QstrDefsCollected) >> $(DestDir)qstrdefs.preprocessed.h"/>
    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdata.py $(DestDir)qstrdefs.preprocessed.h > $(TmpFile)"/>
    <MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="CopyFileIfDifferent" Properties="SourceFile=$(TmpFile);DestFile=$(QstrGen)"/>
  </Target>

  <Target Name="MakeVersionHdr" DependsOnTargets="MakeDestDir">
    <PropertyGroup>
      <DestFile>$(DestDir)mpversion.h</DestFile>
      <TmpFile>$(DestFile).tmp</TmpFile>
    </PropertyGroup>
    <Exec Command="$(PyPython) $(PySrcDir)makeversionhdr.py $(TmpFile)"/>
    <MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="CopyFileIfDifferent" Properties="SourceFile=$(TmpFile);DestFile=$(DestFile)"/>
  </Target>

  <Target Name="FreezeModules" Condition="'$(FrozenManifest)' != ''" DependsOnTargets="MakeQstrData;MakeRootPointers" Inputs="$(FrozenManifest)" Outputs="$(PyBuildDir)frozen_content.c">
    <ItemGroup>
      <ClCompile Include="$(PyBuildDir)frozen_content.c"/>
    </ItemGroup>
    <Exec Command="$(PyPython) $(PyBaseDir)tools\makemanifest.py -v MPY_DIR=$(PyBaseDir) -v MPY_LIB_DIR=$(PyBaseDir)/lib/micropython-lib -v PORT_DIR=$(PyWinDir) -o $(PyBuildDir)frozen_content.c -b $(PyBuildDir) $(FrozenManifest)"/>
    <WriteLinesToFile File="$(TLogLocation)frozen.read.1.tlog" Lines="$(FrozenManifest)" Overwrite="True"/>
  </Target>

  <Target Name="RemoveGeneratedFiles" AfterTargets="Clean">
    <RemoveDir Directories="$(DestDir)"/>
  </Target>

  <!--Copies SourceFile to DestFile only if SourceFile's content differs from DestFile's.
      We use this to 'touch' the generated files only when they are really newer
      so a build is only triggered if the generated content actually changed,
      and not just because the file date changed since the last build-->
  <Target Name="CopyFileIfDifferent">
    <Exec Command="fc /B $(SourceFile) $(DestFile) > NUL 2>&amp;1" IgnoreExitCode="true">
      <Output TaskParameter="ExitCode" PropertyName="FilesDiffer" />
    </Exec>
    <Copy SourceFiles="$(SourceFile)" DestinationFiles="$(DestFile)" Condition="'$(FilesDiffer)'!='0'"/>
  </Target>

</Project>
