aboutsummaryrefslogtreecommitdiff
path: root/ports/windows/msvc/genhdr.targets
blob: 9ea6ed28cb4b8730fb3ec97267906f682fae3ed7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" 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;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>
    <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>
  </PropertyGroup>

  <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"))
        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" Inputs="@(ClCompile);@(QstrDependencies)" Outputs="$(QstrDefsCollected)">
    <ItemGroup>
      <PyIncDirs Include="$(PyIncDirs)"/>
      <PreProcDefs Include="%(ClCompile.PreProcessorDefinitions);NO_QSTR"/>
      <PyQstrSourceFiles Include="@(ClCompile)" Exclude="$(PyBuildDir)\frozen_content.c">
        <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>
      <ForceQstrRebuild>@(QstrDependencies->AnyHaveMetadataValue('Changed', 'True'))</ForceQstrRebuild>
      <RunPreProcConcat>@(PyQstrSourceFiles->AnyHaveMetadataValue('Changed', 'True'))</RunPreProcConcat>
    </PropertyGroup>

    <MakeDir Directories="@(PyQstrSourceFiles->'%(OutDir)')"/>
    <Exec Command="$(PyClTool) /nologo /I@(PyIncDirs, ' /I') /D@(PreProcDefs, ' /D') /Fi%(PyQstrSourceFiles.OutFile) /P %(PyQstrSourceFiles.Identity)"
          Condition="'%(PyQstrSourceFiles.Changed)' == 'True' Or '$(ForceQstrRebuild)' == 'True'"/>
    <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="MakeModuleDefs" DependsOnTargets="MakeDestDir">
    <PropertyGroup>
      <DestFile>$(DestDir)moduledefs.h</DestFile>
      <TmpFile>$(DestFile).tmp</TmpFile>
    </PropertyGroup>
    <ItemGroup>
      <PyUserModuleFiles Include="@(ClCompile)">
        <Path>$([System.String]::new('%(FullPath)').Replace('$(PyBaseDir)', ''))</Path>
      </PyUserModuleFiles>
    </ItemGroup>
    <Exec Command="$(PyPython) $(PySrcDir)makemoduledefs.py --vpath=&quot;., $(PyBaseDir), $(PyUserCModules)&quot; @(PyUserModuleFiles->'%(Path)', ' ') > $(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"/>
    <Exec Command="$(PyPython) $(PySrcDir)makeqstrdata.py $(DestDir)qstrdefs.preprocessed.h $(QstrDefsCollected) > $(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" Inputs="$(FrozenManifest)" Outputs="$(PyBuildDir)frozen_content.c">
    <ItemGroup>
      <ClCompile Include="$(PyBuildDir)frozen_content.c"/>
      <ClCompile>
        <PreprocessorDefinitions>MICROPY_MODULE_FROZEN_MPY=1;MICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      </ClCompile>
    </ItemGroup>
    <Exec Command="$(PyPython) $(PyBaseDir)tools\makemanifest.py -v MPY_DIR=$(PyBaseDir) -v MPY_LIB_DIR=$(PyBaseDir)../micropython-lib -v PORT_DIR=$(PyWinDir) -f&quot;-mcache-lookup-bc&quot; -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>