<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net8.0</TargetFrameworks>

    <OutputType>Library</OutputType>
    <AssemblyName>RenodeNativeInterface</AssemblyName>
    <RootNamespace>Antmicro.Renode.NativeInterface</RootNamespace>
    <LangVersion>12.0</LangVersion>
    <RollForward>Major</RollForward>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>

    <!-- Path to Renode's platform-lib subdirectory that our outputs will reside in. Default to Renode build output with the current RID -->
    <PlatformOutputDir Condition="'$(PlatformOutputDir)' == ''">$([MSBuild]::NormalizeDirectory('$(MSBuildThisFileDirectory)', '..', '..', '..', 'output', 'bin', '$(Configuration)', 'platform-lib', '$(DnneRuntimeIdentifier)'))</PlatformOutputDir>
    <OutputPath>$(PlatformOutputDir)</OutputPath>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
    <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
    <EnableDynamicLoading>true</EnableDynamicLoading>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <RestoreAdditionalProjectSources>$([MSBuild]::NormalizeDirectory('$(MSBuildThisFileDirectory)', '..', 'third-party', 'DNNE', 'src', 'dnne-pkg', 'bin', '$(Configuration)'))</RestoreAdditionalProjectSources>
    <DnnePackageVersion>2.1.2-renode.1</DnnePackageVersion>
  </PropertyGroup>

  <PropertyGroup Condition="'$(DesignTimeBuild)' != 'true'">
    <DnneGenRollForward>Major</DnneGenRollForward>
    <DnneBuildExports>true</DnneBuildExports>
    <DnneGenerateExports>true</DnneGenerateExports>
    <DnneNativeBinaryName>librenode</DnneNativeBinaryName> <!-- librenode.{so,dylib,dll} -->
    <DnneSelfContained_Experimental>true</DnneSelfContained_Experimental>
    <DnneForceGnuToolchain>true</DnneForceGnuToolchain>
    <DnneCompilerCommand>cc</DnneCompilerCommand>
    <DnneLinkerUserFlags Condition="$([MSBuild]::IsOSPlatform('OSX'))">$(DnneLinkerUserFlags) -Wl,-install_name,@rpath/$(DnneNativeBinaryName).dylib</DnneLinkerUserFlags>

    <_DnneOSPrefix Condition="$([MSBuild]::IsOSPlatform('Windows'))">win</_DnneOSPrefix>
    <_DnneOSPrefix Condition="$([MSBuild]::IsOSPlatform('OSX'))">osx</_DnneOSPrefix>
    <_DnneOSPrefix Condition="$([MSBuild]::IsOSPlatform('Linux'))">linux</_DnneOSPrefix>
    <!-- ProcessArchitecture not OSArchitecture so Rosetta x64 processes get osx-x64, not osx-arm64 -->
    <_DnneArch>$([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture.ToString().ToLower())</_DnneArch>
    <DnneRuntimeIdentifier Condition="'$(_DnneOSPrefix)' != ''">$(_DnneOSPrefix)-$(_DnneArch)</DnneRuntimeIdentifier>
  </PropertyGroup>

  <Target Name="SetLinkerFlags" BeforeTargets="DnneBuildNativeExports" Condition="'$(DesignTimeBuild)' != 'true' And $([MSBuild]::IsOSPlatform('Windows'))">
    <PropertyGroup>
      <!-- This implib is not actually used (as we generate one with absolute paths in the CMake library), but DNNE.targets expects it, so just generate it anyway. -->
      <DnneLinkerUserFlags>$(DnneLinkerUserFlags) -Xlinker --out-implib="$([MSBuild]::NormalizePath('$(MSBuildProjectDirectory)', '$(DnneGeneratedBinPath)', '$(DnneNativeBinaryName).lib'))"</DnneLinkerUserFlags>
    </PropertyGroup>
  </Target>

  <Target Name="CopyWindowsNetHost" AfterTargets="DnneBuildC99Exports" Condition="'$(DesignTimeBuild)' != 'true' And $([MSBuild]::IsOSPlatform('Windows'))">
    <Copy SourceFiles="$(DnneNetHostDir)/nethost.dll" DestinationFolder="$(PlatformOutputDir)" />
  </Target>

  <ItemGroup>
    <Compile Remove="DNNE/**/*.cs" />
    <PackageReference Include="DNNE" Version="$(DnnePackageVersion)" />
  </ItemGroup>

  <ItemGroup>
    <!-- Renode must already be built, see the comment above. ./build.sh with the shared option builds in the correct order. -->
    <Reference Include="$(PlatformOutputDir)/../../*.dll" Private="false" />
    <Reference Remove="$(PlatformOutputDir)/../../Xwt.WPF.dll" />
    <Reference Remove="$(PlatformOutputDir)/../../RenodeWPF.dll" />
  </ItemGroup>
</Project>
