genhdr.targets 6.08 KB
Newer Older
stijn's avatar
stijn committed
1
2
3
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="GenerateHeaders">

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

6
  <!--Generate qstrdefs.generated.h and mpversion.h similar to what is done in py/mkrules.mk and py/py.mk-->
7
  <Target Name="GenerateHeaders" DependsOnTargets="MakeVersionHdr;MakeQstrData">
stijn's avatar
stijn committed
8
9
10
  </Target>

  <PropertyGroup>
11
    <DestDir>$(PyBuildDir)genhdr\</DestDir>
12
    <PySrcDir>$(PyBaseDir)py\</PySrcDir>
13
    <QstrDefs>$(PyBaseDir)unix\qstrdefsport.h</QstrDefs>
14
    <PyQstrDefs>$(PySrcDir)qstrdefs.h</PyQstrDefs>
15
16
    <QstrDefsCollected>$(DestDir)qstrdefscollected.h</QstrDefsCollected>
    <QstrGen>$(DestDir)qstrdefs.generated.h</QstrGen>
17
    <PyPython Condition="'$(PyPython)' == ''">python</PyPython>
stijn's avatar
stijn committed
18
19
20
21
22
23
  </PropertyGroup>

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

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  <!-- 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"))
        outFile.WriteLine( line );
}
        ]]>
      </Code>
    </Task>
  </UsingTask>

46
47
48
49
50
  <!-- 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>

51
  <!-- Preprocess changed files, concatenate and feed into makeqstrdefs.py split/cat-->
52
  <Target Name="MakeQstrDefs" DependsOnTargets="MakeDestDir" Inputs="@(ClCompile);@(QstrDependencies)" Outputs="$(QstrDefsCollected)">
53
54
    <ItemGroup>
      <PyIncDirs Include="$(PyIncDirs)"/>
55
      <PreProcDefs Include="%(ClCompile.PreProcessorDefinitions);NO_QSTR;N_X64;N_X86;N_THUMB;N_ARM"/>
56
57
58
      <PyQstrSourceFiles Include="@(ClCompile)">
        <OutFile>$([System.String]::new('%(FullPath)').Replace('$(PyBaseDir)', '$(DestDir)qstr\'))</OutFile>
      </PyQstrSourceFiles>
59
      <PyQstrSourceFiles>
60
61
        <OutFile>$([System.IO.Path]::ChangeExtension('%(OutFile)', '.pp'))</OutFile>
        <OutDir>$([System.IO.Path]::GetDirectoryName('%(OutFile)'))</OutDir>
62
63
      </PyQstrSourceFiles>
      <PyQstrSourceFiles>
64
        <Changed Condition="$([System.DateTime]::Compare($([System.IO.File]::GetLastWriteTime('%(FullPath)')), $([System.IO.File]::GetLastWriteTime('%(OutFile)')))) &gt; 0">True</Changed>
65
      </PyQstrSourceFiles>
66
67
68
      <QstrDependencies>
        <Changed Condition="$([System.DateTime]::Compare($([System.IO.File]::GetLastWriteTime('%(FullPath)')), $([System.IO.File]::GetLastWriteTime('$(DestDir)qstr.i.last')))) &gt; 0">True</Changed>
      </QstrDependencies>
69
    </ItemGroup>
70
71
72
    <PropertyGroup>
      <ForceQstrRebuild>@(QstrDependencies->AnyHaveMetadataValue('Changed', 'True'))</ForceQstrRebuild>
    </PropertyGroup>
73

74
75
    <MakeDir Directories="@(PyQstrSourceFiles->'%(OutDir)')"/>
    <Exec Command="cl /nologo /I@(PyIncDirs, ' /I') /D@(PreProcDefs, ' /D') /Fi%(PyQstrSourceFiles.OutFile) /P %(PyQstrSourceFiles.Identity)"
76
          Condition="'%(PyQstrSourceFiles.Changed)' == 'True' Or '$(ForceQstrRebuild)' == 'True'"/>
77
    <ConcatPreProcFiles InputFiles="@(PyQstrSourceFiles->'%(OutFile)')" OutputFile="$(DestDir)qstr.i.last"
78
                        Condition="@(PyQstrSourceFiles->AnyHaveMetadataValue('Changed', 'True')) Or '$(ForceQstrRebuild)' == 'True'"/>
79
80
    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdefs.py split $(DestDir)qstr.i.last $(DestDir)qstr $(QstrDefsCollected)"/>
    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdefs.py cat $(DestDir)qstr.i.last $(DestDir)qstr $(QstrDefsCollected)"/>
81
82
  </Target>

83
  <Target Name="MakeQstrData" DependsOnTargets="MakeQstrDefs" Inputs="$(QstrDefsCollected);$(PyQstrDefs);$(QstrDefs)" Outputs="$(QstrGen)">
84
    <PropertyGroup>
85
      <TmpFile>$(QstrGen).tmp</TmpFile>
86
    </PropertyGroup>
87
88
    <Exec Command="cl /nologo /I@(PyIncDirs, ' /I') /D@(PreProcDefs, ' /D') /E $(PyQstrDefs) $(QstrDefs) > $(DestDir)qstrdefspreprocessed.h"/>
    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdata.py $(DestDir)qstrdefspreprocessed.h $(QstrDefsCollected) > $(TmpFile)"/>
89
    <MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="CopyFileIfDifferent" Properties="SourceFile=$(TmpFile);DestFile=$(QstrGen)"/>
stijn's avatar
stijn committed
90
91
  </Target>

92
  <Target Name="MakeVersionHdr" DependsOnTargets="MakeDestDir">
stijn's avatar
stijn committed
93
    <PropertyGroup>
94
      <DestFile>$(DestDir)mpversion.h</DestFile>
95
      <TmpFile>$(DestFile).tmp</TmpFile>
stijn's avatar
stijn committed
96
    </PropertyGroup>
97
    <Exec Command="$(PyPython) $(PySrcDir)makeversionhdr.py $(TmpFile)"/>
98
99
100
    <MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="CopyFileIfDifferent" Properties="SourceFile=$(TmpFile);DestFile=$(DestFile)"/>
  </Target>

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

105
106
107
108
109
110
  <!--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">
111
112
      <Output TaskParameter="ExitCode" PropertyName="FilesDiffer" />
    </Exec>
113
    <Copy SourceFiles="$(SourceFile)" DestinationFiles="$(DestFile)" Condition="'$(FilesDiffer)'!='0'"/>
stijn's avatar
stijn committed
114
115
116
  </Target>

</Project>