﻿<?xml version="1.0" encoding="utf-8"?>
<Project
	xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="DownloadZstandard">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - DownloadZstandard" />
        <Message
            Condition="!Exists('$(MSBuildProjectDirectory)\_archives\$(ZSTANDARD_SRC_TAR_GZ)')"
            Text="Downloading '$(ZSTANDARD_URL)/$(ZSTANDARD_SRC_TAR_GZ)'." />
        <WebDownload
            Condition="!Exists('$(MSBuildProjectDirectory)\_archives\$(ZSTANDARD_SRC_TAR_GZ)')"
            FileUri="$(ZSTANDARD_URL)/$(ZSTANDARD_SRC_TAR_GZ)"
            FileName="$(MSBuildProjectDirectory)\_archives\$(ZSTANDARD_SRC_TAR_GZ)"
            UseDefaultCredentials="True" />    
    </Target>
    <Target Name="ExtractZstandard">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - ExtractZstandard" />
        <ExtractWith7za
            Condition="!Exists('$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)')"
            FileToExtract="$(MSBuildProjectDirectory)\_archives\$(ZSTANDARD_SRC_TAR_GZ)" />
    </Target>
    <Target Name="ConfigureZstandard_X86">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - ConfigureZstandard_X86" />
        <MakeDir
            Condition="!Exists('$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build32')"
            Directories="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build32" />
        <PropertyGroup>
            <ExecCommand><![CDATA[
            call $(MSBuildProjectDirectory)\SetVCVars.bat $(VSVer) x86
            "$(CMAKE_EXE)" -G "$(CMakeGenerator)" $(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build\cmake
            ]]></ExecCommand>
        </PropertyGroup>
        <Exec
            Command="$(ExecCommand)"
            WorkingDirectory="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build32" />
    </Target>
    <Target Name="ConfigureZstandard_X64">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - ConfigureZstandard_X64" />
        <MakeDir
            Condition="!Exists('$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build64')"
            Directories="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build64" />
        <PropertyGroup>
            <ExecCommand><![CDATA[
            call $(MSBuildProjectDirectory)\SetVCVars.bat $(VSVer) x64
            "$(CMAKE_EXE)" -G "$(CMakeGenerator)" -A x64 $(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build\cmake
            ]]></ExecCommand>
        </PropertyGroup>
        <Exec
            Command="$(ExecCommand)"
            WorkingDirectory="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build64" />
    </Target>
    <Target Name="ConfigureZstandard">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - ConfigureZstandard" />
        <CallTarget
            Targets="ConfigureZstandard_X86;ConfigureZstandard_X64" />
    </Target>
    <Target Name="BuildZstandard_X86">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - BuildZstandard_X86" />
        <Exec
            Command="&quot;$(CMAKE_EXE)&quot; --build . --config Debug"
            WorkingDirectory="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build32" />
        <Exec
            Command="&quot;$(CMAKE_EXE)&quot; --build . --config Release"
            WorkingDirectory="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build32" />
    </Target>
    <Target Name="BuildZstandard_X64">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - BuildZstandard_X64" />
        <Exec
            Command="&quot;$(CMAKE_EXE)&quot; --build . --config Debug"
            WorkingDirectory="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build64" />
        <Exec
            Command="&quot;$(CMAKE_EXE)&quot; --build . --config Release"
            WorkingDirectory="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build64" />
    </Target>
    <Target Name="BuildZstandard">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - BuildZstandard" />
        <CallTarget
            Targets="ConfigureZstandard_X86;ConfigureZstandard_X64;BuildZstandard_X86;BuildZstandard_X64" />
    </Target>
    <Target Name="CopyZstandard">
        <ItemGroup>
            <ZSTANDARD_BIN_X64_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build64\lib\Debug\*.dll" />
            <ZSTANDARD_BIN_X64_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build64\lib\Debug\*.pdb" />
            <ZSTANDARD_BIN_X64_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build64\lib\Release\*.dll" />
            <ZSTANDARD_BIN_X64_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build64\lib\Release\*.pdb" />
            <ZSTANDARD_BIN_X86_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build32\lib\Debug\*.dll" />
            <ZSTANDARD_BIN_X86_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build32\lib\Debug\*.pdb" />
            <ZSTANDARD_BIN_X86_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build32\lib\Release\*.dll" />
            <ZSTANDARD_BIN_X86_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build32\lib\Release\*.pdb" />
            <ZSTANDARD_DOCS_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\CONTRIBUTING.md" />
            <ZSTANDARD_DOCS_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\COPYING" />
            <ZSTANDARD_DOCS_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\LICENSE" />
            <ZSTANDARD_DOCS_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\TESTING.md" />
            <ZSTANDARD_DOCS_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\doc\**\*.*" />
            <ZSTANDARD_INCLUDE_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\lib\zstd.h" />
            <ZSTANDARD_INCLUDE_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\lib\deprecated\zbuff.h" />
            <ZSTANDARD_INCLUDE_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\lib\dictBuilder\zdict.h" />
            <ZSTANDARD_INCLUDE_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\lib\dictBuilder\cover.h" />
            <ZSTANDARD_INCLUDE_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\lib\common\zstd_errors.h" />
            <ZSTANDARD_LIB_X64_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build64\lib\Debug\*.lib" />
            <ZSTANDARD_LIB_X64_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build64\lib\Release\*.lib" />
            <ZSTANDARD_LIB_X86_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build32\lib\Debug\*.lib" />
            <ZSTANDARD_LIB_X86_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)\build32\lib\Release\*.lib" />
        </ItemGroup>
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - CopyZstandard" />
        <MakeDir
            Condition="!Exists('$(COMMON_SDKS)\docs\Zstandard')"
            Directories="$(COMMON_SDKS)\docs\Zstandard" />
        <Copy
            SourceFiles="@(ZSTANDARD_BIN_X64_DEBUG_FILES)"
            DestinationFolder="$(COMMON_SDKS)\bin\x64\Debug"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(ZSTANDARD_BIN_X64_RELEASE_FILES)"
            DestinationFolder="$(COMMON_SDKS)\bin\x64\Release"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(ZSTANDARD_BIN_X86_DEBUG_FILES)"
            DestinationFolder="$(COMMON_SDKS)\bin\x86\Debug"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(ZSTANDARD_BIN_X86_RELEASE_FILES)"
            DestinationFolder="$(COMMON_SDKS)\bin\x86\Release"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(ZSTANDARD_DOCS_FILES)"
            DestinationFolder="$(COMMON_SDKS)\docs\Zstandard\%(RecursiveDir)"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(ZSTANDARD_INCLUDE_FILES)"
            DestinationFolder="$(COMMON_SDKS)\include"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(ZSTANDARD_LIB_X64_DEBUG_FILES)"
            DestinationFolder="$(COMMON_SDKS)\lib\x64\Debug"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(ZSTANDARD_LIB_X64_RELEASE_FILES)"
            DestinationFolder="$(COMMON_SDKS)\lib\x64\Release"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(ZSTANDARD_LIB_X86_DEBUG_FILES)"
            DestinationFolder="$(COMMON_SDKS)\lib\x86\Debug"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(ZSTANDARD_LIB_X86_RELEASE_FILES)"
            DestinationFolder="$(COMMON_SDKS)\lib\x86\Release"
            SkipUnchangedFiles="true" />
    </Target>
    <Target Name="CleanZstandard">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - CleanZstandard" />
        <RemoveDir
            Condition="Exists('$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)')"
            Directories="$(MSBuildProjectDirectory)\$(ZSTANDARD_DIR)" />
    </Target>
    <Target Name="Zstandard">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - Zstandard" />
        <CallTarget
            Condition="!Exists('$(MSBuildProjectDirectory)\build_state\Zstandard_build_successful')"
            Targets="DownloadZstandard;ExtractZstandard;BuildZstandard;CopyZstandard" />
        <Touch
            AlwaysCreate='true'
            Files="$(MSBuildProjectDirectory)\build_state\Zstandard_build_successful" />
    </Target>
</Project>
