<?xml version="1.0" encoding="utf-8"?>
<Project>
  <PropertyGroup Label="UserMacros">
    <ExternalsQtDir>$(ExternalsDir)Qt\Qt6.5.1\</ExternalsQtDir>
    <QtTargetDirDefault>$(ExternalsQtDir)$(Platform)\</QtTargetDirDefault>
    <QTDIR Condition="Exists('$(QtTargetDirDefault)') And ('$(QTDIR)'=='' Or !Exists('$(QTDIR)'))">$(QtTargetDirDefault)</QTDIR>
    <QTDIR Condition="Exists('$(QTDIR)') And !HasTrailingSlash('$(QTDIR)')">$(QTDIR)\</QTDIR>
    <QtDirValid>false</QtDirValid>
    <QtDirValid Condition="Exists('$(QTDIR)')">true</QtDirValid>
    <QtIncludeDir>$(QTDIR)include\</QtIncludeDir>
    <QtLibDir>$(QTDIR)lib\</QtLibDir>
    <QtBinDir>$(QTDIR)bin\</QtBinDir>
    <QtPluginsDir>$(QTDIR)plugins\</QtPluginsDir>
    <QtToolOutDir>$(IntDir)</QtToolOutDir>
    <QtDebugSuffix>d</QtDebugSuffix>
    <QtHostToolsDir>$(ExternalsQtDir)x64\</QtHostToolsDir>
    <QtLibSuffix Condition="'$(Configuration)'=='Debug'">$(QtDebugSuffix)</QtLibSuffix>
    <QtPluginFolder>QtPlugins</QtPluginFolder>
  </PropertyGroup>
  <ItemDefinitionGroup>
    <ClCompile>
      <PreprocessorDefinitions Condition="'$(Configuration)'=='Release'">QT_NO_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <PreprocessorDefinitions>QT_DLL;QT_CORE_LIB;QT_GUI_LIB;QT_WIDGETS_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <PreprocessorDefinitions>QT_USE_QSTRINGBUILDER;QT_NO_CAST_FROM_ASCII;QT_NO_CAST_TO_ASCII;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <AdditionalIncludeDirectories>$(QtIncludeDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
      <AdditionalIncludeDirectories>$(QtIncludeDir)QtCore;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
      <AdditionalIncludeDirectories>$(QtIncludeDir)QtGui;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
      <AdditionalIncludeDirectories>$(QtIncludeDir)QtWidgets;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
      <!--
        As of Qt6.3, Qt needs user code deriving from certain Qt types to have RTTI (AS WELL AS MOC, UGH).
        Do NOT enable in dolphin outside of Qt-dependant code.
      -->
      <RuntimeTypeInfo>true</RuntimeTypeInfo>
    </ClCompile>
    <Link>
      <AdditionalLibraryDirectories>$(QtLibDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
      <AdditionalDependencies>Qt6Core$(QtLibSuffix).lib;Qt6Gui$(QtLibSuffix).lib;Qt6Widgets$(QtLibSuffix).lib;%(AdditionalDependencies)</AdditionalDependencies>
      <SubSystem>Windows</SubSystem>
      <!--
      <AdditionalOptions>"/manifestdependency:type='Win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\" %(AdditionalOptions)</AdditionalOptions>
      -->
    </Link>
  </ItemDefinitionGroup>

  <!--
    Use the moc implementation provided by (somewhat out-of-date version of) "Qt VS Tools"
    see: https://code.qt.io/cgit/qt-labs/vstools.git/tree/QtMSBuild
    This provides a wrapper around moc which deals with parallel dispatch to moc,
    as well as creating ClCompile inputs from the moc outputs.
    Note that we currently pass the same ClCompile.PreprocessorDefinitions to moc,
    but not the include paths, as passing include paths drastically slows down
    moc, and it doesn't appear to actually need them anyway.
  -->
  <Import Project="qt_globals.targets"/>
  <Import Project="qt_tasks.targets"/>
  <ItemDefinitionGroup>
    <!--From qtmoc.props-->
    <QtMoc>
      <ExecutionDescription>moc %(Identity)</ExecutionDescription>
      <QTDIR>$(QtHostToolsDir)</QTDIR>
      <InputFile>%(FullPath)</InputFile>
      <!--
        moc task does not properly detect outputs are outdated if Qt version changes
        (possibly causing Q_MOC_OUTPUT_REVISION to change). This *might* be because we
        don't give moc the include paths, so it doesn't notice the version change.
        In any case, we can workaround it by manually changing output path when needed
        to avoid conflicts. (the numeric postfix is the value of Q_MOC_OUTPUT_REVISION)
        -->
      <OutputFile>$(QtToolOutDir)moc_68\moc_%(Filename).cpp</OutputFile>
      <QtMocDir>$(QtToolOutDir)moc_68\</QtMocDir>
      <QtMocFileName>moc_%(Filename).cpp</QtMocFileName>
      <DynamicSource>output</DynamicSource>
      <ParallelProcess>true</ParallelProcess>
      <CommandLineTemplate>[AllOptions] [AdditionalOptions]</CommandLineTemplate>
      <Outputs>%(OutputFile)</Outputs>
      <OverrideClCompile>false</OverrideClCompile>
    </QtMoc>
  </ItemDefinitionGroup>
  <Import Project="qtmoc.targets" />
  <Target Name="QtCheckQtDir" BeforeTargets="QtMocInit" Condition="!$(QtDirValid)">
    <Error Text="QTDIR not set or non-existent (pull the submodule?)" />
  </Target>

  <!--Similar story with rcc-->
  <ItemDefinitionGroup>
    <!--From qtrcc.props-->
    <QtRcc>
      <ExecutionDescription>rcc %(Identity)</ExecutionDescription>
      <QTDIR>$(QtHostToolsDir)</QTDIR>
      <InputFile>%(FullPath)</InputFile>
      <OutputFile>$(QtToolOutDir)rcc\qrc_%(Filename).cpp</OutputFile>
      <QtRccDir>$(QtToolOutDir)rcc\</QtRccDir>
      <QtRccFileName>qrc_%(Filename).cpp</QtRccFileName>
      <InitFuncName>%(Filename)</InitFuncName>
      <Compression>default</Compression>
      <TwoPass>false</TwoPass>
      <DynamicSource>output</DynamicSource>
      <ParallelProcess>true</ParallelProcess>
      <CommandLineTemplate>[AllOptions] [AdditionalOptions]</CommandLineTemplate>
      <Outputs>%(OutputFile)</Outputs>
    </QtRcc>
  </ItemDefinitionGroup>
  <Import Project="qtrcc.targets" />

  <!--Expose the new targets to VS-->
  <ItemGroup>
    <PropertyPageSchema Include="$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml" />
  </ItemGroup>

  <!--Copy the needed dlls-->
  <ItemGroup>
    <QtDllNames_ Include="Qt6Core;Qt6Gui;Qt6Widgets;Qt6Svg" />
    <QtDllNames Include="@(QtDllNames_ -> '%(Identity)$(QtLibSuffix).dll')" />
    <QtDllsSrc Include="@(QtDllNames -> '$(QtBinDir)%(Identity)')" />
    <QtDllsDst Include="@(QtDllNames -> '$(BinaryOutputDir)%(Identity)')" />
    <QtPluginNames_ Include="iconengines\qsvgicon;imageformats\qsvg;platforms\qdirect2d;platforms\qwindows;styles\qwindowsvistastyle"/>
    <QtPluginNames Include="@(QtPluginNames_ -> '%(Identity)$(QtLibSuffix).dll')" />
    <QtPluginsSrc Include="@(QtPluginNames -> '$(QtPluginsDir)%(Identity)')" />
    <QtPluginsDst Include="@(QtPluginNames -> '$(BinaryOutputDir)$(QtPluginFolder)\%(Identity)')" />
  </ItemGroup>
  <PropertyGroup>
    <QtConfFile>$(BinaryOutputDir)qt.conf</QtConfFile>
  </PropertyGroup>
  <Target Name="QtCopyBinaries"
    AfterTargets="Build"
    Inputs="@(QtDllsSrc);@(QtPluginsSrc)"
    Outputs="@(QtDllsDst);@(QtPluginsDst)">
    <Copy
      SourceFiles="@(QtDllsSrc)"
      DestinationFiles="@(QtDllsDst)"
      SkipUnchangedFiles="true"
    />
    <Copy
      SourceFiles="@(QtPluginsSrc)"
      DestinationFiles="@(QtPluginsDst)"
      SkipUnchangedFiles="true"
    />
  </Target>
  <Target Name="QtCreateConf"
    BeforeTargets="QtCopyBinaries"
    Condition="!Exists('$(QtConfFile)')">
    <!--
      Create a file which tells Qt where to look for "plugins".
      Otherwise Qt only looks in ./<subtype>/type.dll instead of ./$(QtPluginFolder)/<subtype>/type.dll, which is messy
      -->
    <WriteLinesToFile
      File="$(QtConfFile)"
      Lines="[Paths];Plugins = ./$(QtPluginFolder)"
      Overwrite="true"
    />
  </Target>
</Project>
