aboutsummaryrefslogtreecommitdiffstats
path: root/src/Qt.DotNet.Adapter/build/Qt.DotNet.targets
blob: 31b0bc97c55bc7436c2c600c130ccecff8dc8f6a (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?xml version="1.0" encoding="utf-8"?>
<!--
****************************************************************************************************
 Copyright (C) 2025 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
****************************************************************************************************
-->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <Import
    Condition="'$(QtDotNetPropsImported)' != 'true'"
    Project="$(MSBuildThisFileDirectory)Qt.DotNet.props"/>

  <!--
  //////////////////////////////////////////////////////////////////////////////////////////////////
  // QtDotNetSetupQml
  -->
  <Target Name="QtDotNetSetupQml" Outputs="@(Qml);@(QmlModule)">
    <ItemGroup>
      <!-- Set QML file properties -->
      <Qml>
        <IsRoot Condition="'%(RelativeDir)' == ''">true</IsRoot>
        <IsRoot Condition="'%(RelativeDir)' != ''">false</IsRoot>
      </Qml>
      <Qml>
        <SourceDir Condition="%(IsRoot)">Application\</SourceDir>
        <SourceDir Condition="!%(IsRoot)">%(RelativeDir)</SourceDir>
      </Qml>
      <Qml>
        <Uri>$([System.String]::Copy(%(SourceDir))
                .Replace('\','/').Replace('/','.').Trim('.'))</Uri>
        <TypeName>%(FileName)</TypeName>
      </Qml>
      <QmlModule Include="%(Qml.Uri)">
        <IsRoot>%(Qml.IsRoot)</IsRoot>
        <SourceDir>%(Qml.SourceDir)</SourceDir>
      </QmlModule>
    </ItemGroup>
  </Target>

  <!--
  //////////////////////////////////////////////////////////////////////////////////////////////////
  // QtDotNetAddQmlFiles
  -->
  <Target Name="QtDotNetAddQmlFiles" BeforeTargets="CoreCompile" DependsOnTargets="QtDotNetSetupQml"
    Inputs="@(Qml)" Outputs="$(ProjectDir)$(IntermediateOutputPath)$(QtQmlFilesCs)"
    Condition="'$(DesignTimeBuild)' != 'true'">
    <!-- Copy QML files to native source dir -->
    <Copy Condition="'@(Qml)' != ''"
      SourceFiles="$(ProjectDir)%(RelativeDir)%(Filename)%(Extension)"
      DestinationFolder="$(IntermediateOutputPath)$(QtQmlSourceDir)%(SourceDir)" />
    <MakeDir Condition="'@(Qml)' != ''"
      Directories="$(IntermediateOutputPath)qtdotnet\csharp" />
    <!-- Add C# source with paths of QML files; this will be an input for code generation -->
    <WriteLinesToFile Condition="'@(Qml)' != ''" Overwrite="true" WriteOnlyWhenDifferent="false"
      File="$(ProjectDir)$(IntermediateOutputPath)$(QtQmlFilesCs)"
      Lines="@(Qml->'[assembly: Qt.Quick.QmlFile(
    Uri = &quot;%(Uri)&quot;, TypeName = &quot;%(TypeName)&quot;, IsRoot = %(IsRoot),
    Path = @&quot;%(SourceDir)%(Filename)%(Extension)&quot;)]')" />
    <ItemGroup>
      <Compile Condition="'@(Qml)' != ''"
        Include="$(IntermediateOutputPath)qtdotnet\csharp\QmlFiles.cs" />
    </ItemGroup>
  </Target>
  <!-- END QtDotNetAddQmlFiles -->

  <!--
  //////////////////////////////////////////////////////////////////////////////////////////////////
  // QtCheckVisualStudioWorkloads
  -->
  <Target Name="QtCheckVisualStudioWorkloads"
          BeforeTargets="QtDotNetGenerate"
          Condition="'$(OS)'=='Windows_NT' and '$(QtVsCheckWorkloads)'=='true'">

    <Warning Condition="!Exists('$(VsWhereExe)')"
             Text="vswhere.exe not found '$(VsWhereExe)'. Ignore workload check." />
    <PropertyGroup Condition="!Exists('$(VsWhereExe)')">
      <SkipWorkloadCheck>true</SkipWorkloadCheck>
    </PropertyGroup>

    <!-- Desktop C++ -->
    <Exec Condition="'$(SkipWorkloadCheck)'!='true'"
          Command="&quot;$(VsWhereExe)&quot; -nologo -products * -format json -requires &quot;$(DesktopWorkload)&quot;"
          ConsoleToMSBuild="true"
          IgnoreExitCode="true">
      <Output TaskParameter="ConsoleOutput" PropertyName="VsDesktopJson" />
    </Exec>

    <Error Condition="'$(SkipWorkloadCheck)'!='true'
           and ($([System.String]::Copy('$(VsDesktopJson)').Trim()) == ''
           or $([System.String]::Copy('$(VsDesktopJson)').Trim()) == '[]')"
           Text="Missing workload: Desktop development with C++. Please install via the Visual Studio Installer." />

    <!-- Optional: cross-platform C++ -->
    <Exec Condition="'$(SkipWorkloadCheck)'!='true' and '$(QtVsCheckCrossPlat)'=='true'"
          Command="&quot;$(VsWhereExe)&quot; -nologo -products * -format json -requires &quot;$(CrossPlatformWorkload)&quot;"
          ConsoleToMSBuild="true"
          IgnoreExitCode="true">
      <Output TaskParameter="ConsoleOutput" PropertyName="VsCrossJson" />
    </Exec>

    <Error Condition="'$(SkipWorkloadCheck)'!='true' and '$(QtVsCheckCrossPlat)'=='true'
           and ($([System.String]::Copy('$(VsCrossJson)').Trim()) == ''
           or $([System.String]::Copy('$(VsCrossJson)').Trim()) == '[]')"
           Text="Missing workload: Linux, Mac, and embedded development with C++. Please install via the Visual Studio Installer." />
  </Target>
  <!-- END QtCheckVisualStudioWorkloads -->

  <!--
  //////////////////////////////////////////////////////////////////////////////////////////////////
  // QtDotNetGenerate
  -->
  <Target Name="QtDotNetGenerate" AfterTargets="CoreCompile" BeforeTargets="_CreateAppHost"
    DependsOnTargets="QtDotNetAddQmlFiles"
    Inputs="$(TargetPath);$(ProjectDir)$(IntermediateOutputPath)$(TargetFileName)"
    Outputs="@(QtDotNetNativeSource)"
    Condition="'$(DesignTimeBuild)' != 'true'">

    <Message Importance="high" Text="Qt/.NET: Generating native code..." />

    <Error Condition="'$(QtDotNetGen)' == '' OR !Exists('$(QtDotNetGen)')"
      Text="Error: Qt/.NET generator tool not found" />
    <Error Condition="'$(QtDotNetGenRules)' == '' OR !Exists('$(QtDotNetGenRules)')"
      Text="Error: Qt/.NET generations rules not found" />
    <Error Condition="'$(QtDotNetAdapter)' == '' OR !Exists('$(QtDotNetAdapter)')"
      Text="Error: Qt/.NET adapter tool not found" />
    <Error Condition="'$(QtDotNetLib)' == '' OR !Exists('$(QtDotNetLib)/qdotnethost.h')"
      Text="Error: Qt/.NET library not found" />

    <PropertyGroup>
      <ProjectIntermediateDir>$(ProjectDir)$(IntermediateOutputPath)</ProjectIntermediateDir>
      <QtGenCmd
        >$(QtGenCmd) --source "$(ProjectIntermediateDir)$(TargetFileName)"</QtGenCmd>
      <QtGenCmd
        >$(QtGenCmd) --target "$(ProjectIntermediateDir)qtdotnet\native"</QtGenCmd>
      <QtGenCmd
        >$(QtGenCmd) @(_ResolvedProjectReferencePaths->'--ref &quot;%(Identity)&quot;', ' ')</QtGenCmd>
      <QtGenCmd
        >$(QtGenCmd) @(RuntimeCopyLocalItems->'--ref &quot;%(Identity)&quot;', ' ')</QtGenCmd>
    </PropertyGroup>

    <Message Importance="high" Text="$(QtGenCmd)" />
    <Exec Command="$(QtGenCmd)" EchoOff="true" IgnoreExitCode="true">
      <Output TaskParameter="ExitCode" PropertyName="QtGenResult" />
    </Exec>
    <Error Condition="'$(QtGenResult)' != '0'" Text="Error generating native code!" />
    <WriteLinesToFile Overwrite="true" WriteOnlyWhenDifferent="true"
      File="$(IntermediateOutputPath)qtdotnet\native\source\qtdotnet.cmake"
      Lines="include_directories(&quot;$(QtDotNetLib.Replace('\', '/'))&quot;)" />
    <ItemGroup>
      <QtDotNetNativeSource Include="$(IntermediateOutputPath)qtdotnet\native\source\**\*" />
    </ItemGroup>
  </Target>
  <!-- END QtDotNetGenerate-->

  <!--
  //////////////////////////////////////////////////////////////////////////////////////////////////
  // QtDotNetBuild
  -->
  <Target Name="QtDotNetBuild"
    DependsOnTargets="QtDotNetGenerate" BeforeTargets="_CreateAppHost"
    Inputs="@(QtDotNetNativeSource)" Outputs="@(QtDotNetNativeBuild)"
    Condition="'$(DesignTimeBuild)' != 'true'">

    <Message Importance="high" Text="Qt/.NET: Building native code..." />

    <PropertyGroup Condition="'$(VsInstallRoot)' == ''">
      <VsWhereCmd>"$(VsWhereExe)"</VsWhereCmd>
      <VsWhereCmd>$(VsWhereCmd) -latest</VsWhereCmd>
      <VsWhereCmd>$(VsWhereCmd) -property installationPath</VsWhereCmd>
    </PropertyGroup>
    <Exec Condition="'$(VsInstallRoot)' == ''" Command="$(VsWhereCmd)" ConsoleToMsBuild="true">
      <Output TaskParameter="ConsoleOutput" ItemName="VsWhereOutput" />
    </Exec>
    <PropertyGroup Condition="'$(VsInstallRoot)' == ''">
      <VsInstallRoot Condition="Exists('%(VsWhereOutput.Identity)')">%(VsWhereOutput.Identity)</VsInstallRoot>
    </PropertyGroup>
    <PropertyGroup Condition="'$(VsInstallRoot)' == '' OR '$(QtBuildSystem)' == 'ninja'">
      <CMakeBuild>true</CMakeBuild>
    </PropertyGroup>

    <MakeDir Directories="$(IntermediateOutputPath)qtdotnet\native\build" />
    <RemoveDir Directories="$(IntermediateOutputPath)qtdotnet\native\bin" />
    <MakeDir Directories="$(IntermediateOutputPath)qtdotnet\native\bin" />
    <PropertyGroup>
      <CMakeCmd>
        CALL "$(VsInstallRoot)\VC\Auxiliary\Build\vcvars64.bat" &gt; NUL
        SET CMAKE_PREFIX_PATH=$(QtDir);%CMAKE_PREFIX_PATH%"
        CD "$(IntermediateOutputPath)qtdotnet\native\build"
      </CMakeCmd>
      <CMakeCmd Condition="'$(CMakeBuild)' != 'true'">
        $(CMakeCmd)
        cmake ..\source -DCMAKE_PROJECT_INCLUDE=qtdotnet.cmake -G "Visual Studio 17 2022"
      </CMakeCmd>
      <CMakeCmd Condition="'$(CMakeBuild)' == 'true'">
        $(CMakeCmd)
        cmake ..\source -DCMAKE_PROJECT_INCLUDE=qtdotnet.cmake -G Ninja -DCMAKE_BUILD_TYPE=Release
        cmake --build . --parallel
      </CMakeCmd>
    </PropertyGroup>
    <Exec Command="$(CMakeCmd)" EchoOff="true" IgnoreExitCode="true">
      <Output TaskParameter="ExitCode" PropertyName="CMakeResult" />
    </Exec>
    <Error Condition="'$(CMakeResult)' != '0'" Text="Error building native code!" />

    <ItemGroup Condition="'$(CMakeBuild)' != 'true'">
      <NativeSln Include="$(IntermediateOutputPath)qtdotnet\native\build\*.sln" />
    </ItemGroup>
    <PropertyGroup Condition="'$(CMakeBuild)' != 'true'">
      <NativeApp>%(NativeSln.RelativeDir)%(NativeSln.Filename)</NativeApp>
    </PropertyGroup>
    <ItemGroup Condition="'$(CMakeBuild)' != 'true'">
      <NativeProject Include="$(NativeApp)_tooling.vcxproj;
                              $(NativeApp)_qmlimportscan.vcxproj;
                              $(NativeApp)_autogen.vcxproj;
                              $(NativeApp)_automoc_json_extraction.vcxproj;
                              $(NativeApp).vcxproj" />
      <NativeProject Remove="@(NativeProject)" Condition="!Exists('%(NativeProject.FullPath)')" />
    </ItemGroup>
    <MSBuild Condition="'$(CMakeBuild)' != 'true'"
      Projects="@(NativeProject)" BuildInParallel="false" StopOnFirstFailure="true"
      Properties="Platform=x64;Configuration=Release;CL_MP=true" Targets="Build" />
  </Target>
  <!-- END QtDotNetBuild-->

  <!--
  //////////////////////////////////////////////////////////////////////////////////////////////////
  // QtDotNetReplaceBinary
  -->
  <Target Name="QtDotNetReplaceBinary"
    AfterTargets="QtDotNetBuild" BeforeTargets="_CreateAppHost"
    Inputs="$(TargetPath)" Outputs="$(AppHostSourcePath)"
    Condition="'$(DesignTimeBuild)' != 'true'">

    <Message Importance="high" Text="Qt/.NET: Replacing native host binary..." />

    <ItemGroup>
      <QtNativeBinary Include="$(IntermediateOutputPath)qtdotnet\native\bin\*.exe" />
    </ItemGroup>
    <PropertyGroup>
      <QtNativeHostPath Condition="Exists('%(QtNativeBinary.FullPath)')"
        >%(QtNativeBinary.FullPath)</QtNativeHostPath>
    </PropertyGroup>
    <Error Condition="'$(QtNativeHostPath)' == ''"
      Text="ERROR: Missing native host binary" />
    <Touch Files="$(QtNativeHostPath)" />
    <PropertyGroup>
      <AppHostSourcePath>$(QtNativeHostPath)</AppHostSourcePath>
    </PropertyGroup>
  </Target>
  <!-- END QtDotNetReplaceBinary-->

  <!--
  //////////////////////////////////////////////////////////////////////////////////////////////////
  // QtDotNetDeploy
  -->
  <Target Name="QtDotNetDeploy"
    AfterTargets="CopyFilesToOutputDirectory" DependsOnTargets="QtDotNetBuild"
    Inputs="$(RunCommand)" Outputs="$(TargetDir)qtdeploy.txt"
    Condition="'$(DesignTimeBuild)' != 'true'">

    <Message Importance="high" Text="Qt/.NET: Deploying Qt..." />

    <PropertyGroup Condition="'$(VsInstallRoot)' == ''">
      <VsWhereCmd>"$(VsWhereExe)"</VsWhereCmd>
      <VsWhereCmd>$(VsWhereCmd) -latest</VsWhereCmd>
      <VsWhereCmd>$(VsWhereCmd) -property installationPath</VsWhereCmd>
    </PropertyGroup>
    <Exec Condition="'$(VsInstallRoot)' == ''" Command="$(VsWhereCmd)" ConsoleToMsBuild="true">
      <Output TaskParameter="ConsoleOutput" ItemName="VsWhereOutput" />
    </Exec>
    <PropertyGroup Condition="'$(VsInstallRoot)' == ''">
      <VsInstallRoot Condition="Exists('%(VsWhereOutput.Identity)')"
        >%(VsWhereOutput.Identity)</VsInstallRoot>
    </PropertyGroup>

    <PropertyGroup>
      <VcVarsCmd Condition="'$(VsInstallRoot)' != ''"
        >CALL "$(VsInstallRoot)\VC\Auxiliary\Build\vcvars64.bat" &gt; NUL</VcVarsCmd>
      <DeployCmd>"$(QtDir)\bin\windeployqt.exe"</DeployCmd>
      <DeployCmd>$(DeployCmd) --qmldir "$(ProjectDir.TrimEnd('\'))"</DeployCmd>
      <DeployCmd>$(DeployCmd) --no-translations --release</DeployCmd>
      <DeployCmd>$(DeployCmd) --no-compiler-runtime</DeployCmd>
      <DeployCmd>$(DeployCmd) "$(RunCommand)"</DeployCmd>
      <DeployCmd>$(DeployCmd) &gt; NUL</DeployCmd>
    </PropertyGroup>
    <Exec Command="
          $(VcVarsCmd)
          $(DeployCmd)"
      WorkingDirectory="$(TargetDir)" IgnoreExitCode="true">
      <Output TaskParameter="ExitCode" PropertyName="DeployResult" />
    </Exec>
    <Error Condition="'$(DeployResult)' != '0'" Text="Error deploying Qt binaries!" />
    <Exec WorkingDirectory="$(TargetDir)" IgnoreExitCode="true"
      Command="$(DeployCmd) --list relative &gt; qtdeploy.txt 2&gt; NUL" />

    <ItemGroup>
      <QmlDeploy Condition="'@(Qml)' != ''"
        Include="$(ProjectDir)$(IntermediateOutputPath)$(QtQmlSourceDir)%(Qml.SourceDir)*">
        <SourceDir>%(Qml.SourceDir)</SourceDir>
      </QmlDeploy>
    </ItemGroup>
    <Copy Condition="'@(QmlDeploy)' != ''"
      SourceFiles="%(Identity)"
      DestinationFolder="$(TargetDir)%(SourceDir)" />
    <WriteLinesToFile Condition="'@(QmlDeploy)' != ''"
      File="$(TargetDir)qtdeploy.txt" Overwrite="false"
      Lines="%(SourceDir)%(Filename)%(Extension)" />
  </Target>
  <!-- END QtDotNetDeploy-->

  <!--
  //////////////////////////////////////////////////////////////////////////////////////////////////
  // QtDotNetClean
  -->
  <Target Name="QtDotNetClean" AfterTargets="Clean">
    <RemoveDir Directories="$(IntermediateOutputPath)qtdotnet" />
    <ReadLinesFromFile Condition="Exists('$(TargetDir)qtdeploy.txt')"
      File="$(TargetDir)qtdeploy.txt">
      <Output TaskParameter="Lines" ItemName="QtDeploy"/>
    </ReadLinesFromFile>
    <Delete Files="@(QtDeploy->'$(TargetDir)%(Identity)')" />
    <Delete Files="$(TargetDir)qtdeploy.txt" />
    <RemoveDir Condition="$([System.String]::Copy('%(QtDeploy.Identity)').Contains('\'))"
       Directories="$(TargetDir)$([System.IO.Path]::GetDirectoryName(%(QtDeploy.Identity)).Split('\')[0])" />
  </Target>
  <!-- END QtDotNetClean-->

</Project>