<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <RuntimeIdentifiers>ios-arm64</RuntimeIdentifiers>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <Version>1.0.0-dirty</Version>
    <DefineConstants Condition=" '$(ExtraDefineConstants)' != '' ">$(DefineConstants);$(ExtraDefineConstants)</DefineConstants>
    <SigningCertificate Condition=" '$(SigningCertificate)' == '' ">-</SigningCertificate>
    <TieredPGO>true</TieredPGO>
    
    <PublishAot>true</PublishAot>
    <PublishAotUsingRuntimePack>true</PublishAotUsingRuntimePack>
    <AotCompilerOptions>-flto -Ofast -funroll-loops</AotCompilerOptions>
    <UseNativeAOTRuntime>true</UseNativeAOTRuntime>
    <TrimmerSingleWarn>false</TrimmerSingleWarn>
    <OptimizationPreference>Speed</OptimizationPreference>
  </PropertyGroup>

  <!-- iOS linking stuff from godot -->

  <PropertyGroup>
    <InvariantGlobalization>true</InvariantGlobalization>
    <LinkStandardCPlusPlusLibrary>true</LinkStandardCPlusPlusLibrary>
    <FindXCode Condition=" '$(XCodePath)' == '' and '$([MSBuild]::IsOsPlatform(OSX))' ">true</FindXCode>
    <XCodePath Condition=" '$(XCodePath)' == '' ">/Applications/Xcode.app/Contents/Developer</XCodePath>
    <XCodePath>$([MSBuild]::EnsureTrailingSlash('$(XCodePath)'))</XCodePath>
  </PropertyGroup>

  <Target Name="PrepareBeforeIlcCompile"
          BeforeTargets="IlcCompile">

    <Copy SourceFiles="%(ResolvedRuntimePack.PackageDirectory)/runtimes/$(RuntimeIdentifier)/native/icudt.dat" DestinationFolder="$(PublishDir)"/>

    <!-- We need to find the path to Xcode so we can set manual linker args to the correct SDKs
        Once https://github.com/dotnet/runtime/issues/88737 is released, we can take this out
    -->

    <Exec Command="xcrun xcode-select -p" ConsoleToMSBuild="true" Condition=" '$(FindXCode)' == 'true' ">
      <Output TaskParameter="ConsoleOutput" PropertyName="XcodeSelect" />
    </Exec>

    <PropertyGroup Condition=" '$(FindXCode)' == 'true' ">
      <XCodePath>$(XcodeSelect)</XCodePath>
      <XCodePath>$([MSBuild]::EnsureTrailingSlash('$(XCodePath)'))</XCodePath>
    </PropertyGroup>

    <Message Importance="normal" Text="Found XCode at $(XcodeSelect)"  Condition=" '$(FindXCode)' == 'true' "/>

    <ItemGroup>
<!--      <LinkerArg Include="-Wl,-ld_classic" /> -->
      <LinkerArg Include="-flto -Ofast" />
      <LinkerArg Include="-isysroot %22$(XCodePath)Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk%22"
                 Condition=" $(RuntimeIdentifier.Contains('simulator')) "/>
      <LinkerArg Include="-isysroot %22$(XCodePath)Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk%22"
                 Condition=" !$(RuntimeIdentifier.Contains('simulator')) "/>
    </ItemGroup>

  </Target>

  <Target Name="FixDylib"
          AfterTargets="Publish">
    <!-- The dylib id needs to be changed to have @rpath -->

    <Exec Command="install_name_tool -id @rpath/$(TargetName).dylib $(NativeBinary)" ConsoleToMSBuild="true" />
  </Target>

  <Target Name="FixSymbols"
          AfterTargets="Publish">

    <RemoveDir Directories="$(PublishDir)$(TargetName).framework.dSYM"/>

    <!-- create-xcframework (called from the export plugin wants the symbol files in a directory
    with a slightly different name from the one created by dotnet publish, so we copy them over
    to the correctly-named directory -->
    <ItemGroup>
      <SymbolFiles Include="$(NativeBinary).dsym\**\*.*"/>
    </ItemGroup>
    <Copy SourceFiles="@(SymbolFiles)" DestinationFolder="$(PublishDir)$(TargetName).framework.dSYM"/>
  </Target>

  <!-- end iOS linking stuff from godot -->

  <ItemGroup>
    <PackageReference Include="OpenTK.Core" />
    <!-- <PackageReference Include="Ryujinx.Graphics.Nvdec.Dependencies" /> -->
  </ItemGroup>

  <Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="false">
    <Exec Command="codesign --entitlements '$(ProjectDir)..\..\distribution\macos\entitlements.xml' -f --deep -s $(SigningCertificate) '$(TargetDir)$(TargetName)'" />
  </Target>

  <ItemGroup>
    <ProjectReference Include="..\Ryujinx.Graphics.Vulkan\Ryujinx.Graphics.Vulkan.csproj" />
    <ProjectReference Include="..\Ryujinx.Input\Ryujinx.Input.csproj" />
    <ProjectReference Include="..\Ryujinx.Input.SDL2\Ryujinx.Input.SDL2.csproj" />
    <ProjectReference Include="..\Ryujinx.Audio.Backends.SDL2\Ryujinx.Audio.Backends.SDL2.csproj" />
    <ProjectReference Include="..\Ryujinx.Common\Ryujinx.Common.csproj" />
    <ProjectReference Include="..\Ryujinx.HLE\Ryujinx.HLE.csproj" />
    <ProjectReference Include="..\ARMeilleure\ARMeilleure.csproj" />
    <ProjectReference Include="..\Ryujinx.Graphics.OpenGL\Ryujinx.Graphics.OpenGL.csproj" />
    <ProjectReference Include="..\Ryujinx.Graphics.Gpu\Ryujinx.Graphics.Gpu.csproj" />
    <ProjectReference Include="..\Ryujinx.Ui.Common\Ryujinx.Ui.Common.csproj" />
    <ProjectReference Include="..\Ryujinx.Audio.Backends.OpenAL\Ryujinx.Audio.Backends.OpenAL.csproj" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="CommandLineParser" />
    <PackageReference Include="Ryujinx.Graphics.Vulkan.Dependencies.MoltenVK" Condition="'$(RuntimeIdentifier)' != 'linux-x64' AND '$(RuntimeIdentifier)' != 'win-x64'" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="..\..\distribution\legal\THIRDPARTY.md">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <TargetPath>THIRDPARTY.md</TargetPath>
    </Content>
    <Content Include="..\..\LICENSE.txt">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <TargetPath>LICENSE.txt</TargetPath>
    </Content>
  </ItemGroup>

  <ItemGroup Condition="'$(RuntimeIdentifier)' == 'linux-x64'">
    <Content Include="..\..\distribution\linux\Ryujinx.sh">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <EmbeddedResource Include="Ryujinx.bmp" />
  </ItemGroup>

  <!-- Due to .net core 3.1 embedded resource loading -->
  <PropertyGroup>
    <EmbeddedResourceUseDependentUponConvention>false</EmbeddedResourceUseDependentUponConvention>
    <ApplicationIcon>..\Ryujinx\Ryujinx.ico</ApplicationIcon>
  </PropertyGroup>

  <PropertyGroup Condition="'$(RuntimeIdentifier)' != ''">
    <PublishSingleFile>false</PublishSingleFile>
    <PublishTrimmed>true</PublishTrimmed>
    <TrimMode>partial</TrimMode>
  </PropertyGroup>
</Project>
