<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- Adds support for building Bazel projects as Visual Studio NMake projects.
       Works for both x64 and GGP platforms.

       Usage: Define 4 properties in your project file:
                BazelTargets:          Labels of Bazel targets to build, e.g. //common:status.
                BazelOutputFile:       Output filename, e.g. cdc_rsync.exe
              Optionally, define:
                BazelIncludePaths:     Include paths, used for Intellisense.
              Import NMakeCMakeProject.targets. -->

  <!-- Check whether all properties are defined. -->
  <Target Name="CheckProperties" BeforeTargets="PrepareForNMakeBuild">
    <Error Condition="'$(BazelTargets)' == ''" Text="Please define property BazelTargets" />
    <Error Condition="'$(BazelOutputFile)' == ''" Text="Please define property BazelOutputFile" />
  </Target>

  <!-- Define Bazel properties. -->
  <PropertyGroup>
    <BazelPlatform Condition="'$(Platform)'=='x64'">windows</BazelPlatform>
    <BazelPlatform Condition="'$(Platform)'=='GGP'">ggp_windows</BazelPlatform>
    <BazelPlatformDir Condition="'$(Platform)'=='x64'">x64-windows</BazelPlatformDir>
    <BazelPlatformDir Condition="'$(Platform)'=='GGP'">k8</BazelPlatformDir>
    <BazelCompilationMode Condition="'$(Configuration)'=='Debug'">dbg</BazelCompilationMode>
    <BazelCompilationMode Condition="'$(Configuration)'=='Release'">opt</BazelCompilationMode>
    <BazelArgs>--config=$(BazelPlatform)</BazelArgs>
    <BazelArgs Condition="'$(Configuration)|$(Platform)'=='Release|GGP'">$(BazelArgs) --linkopt=-Wl,--strip-all</BazelArgs>
    <!-- Prevent protobuf recompilation (protobuf is written to "host" by default for both x84 and ggp, causing recompiles). -->
    <!-- <BazelArgs Condition="'$(Platform)'=='x64'">$(BazelArgs) - -distinct_host_configuration=false</BazelArgs> -->
    <!-- Windows uses /LTCG (link-time code generation), but no /GL (global optimization), which is a requirement for that. -->
    <BazelArgs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(BazelArgs) --copt=/GL</BazelArgs>
    <!-- Strip unused symbols on GGP. -->
    <BazelArgs Condition="'$(Configuration)|$(Platform)'=='Release|GGP'">$(BazelArgs) --copt=-fdata-sections --copt=-ffunction-sections --linkopt=-Wl,--gc-sections</BazelArgs>
    <!-- VS creates a bazel-out DIRECTORY if it doesn't exist yet and prevents Bazel from creating a bazel-out SYMLINK. -->
    <RmBazelOutDir>cmd.exe /Q /C $(SolutionDir)rm_bazel_out_dir.bat &amp;&amp;</RmBazelOutDir>
    <!-- Bazel output is always read-only, which confuses a bunch of tools that upload binaries to gamelets and fail the second time. -->
    <MakeRW>&amp;&amp; attrib -r $(OutDir)*</MakeRW>
    <!-- Include standard libraries for GGP Intellisense -->
    <BazelIncludePaths Condition="'$(Platform)'=='GGP'">$(GGP_SDK_PATH)BaseSDK\LLVM\10.0.1\include\c++\v1;$(GGP_SDK_PATH)BaseSDK\LLVM\10.0.1\lib\clang\10.0.1\include;$(GGP_SDK_PATH)sysroot\usr\include\x86_64-linux-gnu;$(GGP_SDK_PATH)sysroot\usr\include;$(BazelIncludePaths)</BazelIncludePaths>
  </PropertyGroup>

  <!-- Define NMake properties. -->
  <PropertyGroup>
    <NMakeIncludeSearchPath>$(SolutionDir)..\..\bazel-out\$(BazelPlatformDir)-$(BazelCompilationMode)\bin;$(BazelIncludePaths)</NMakeIncludeSearchPath>
    <NMakeBuildCommandLine>$(RmBazelOutDir) bazel build --compilation_mode=$(BazelCompilationMode) $(BazelArgs) $(BazelTargets) $(MakeRW)</NMakeBuildCommandLine>
    <NMakeCleanCommandLine>$(RmBazelOutDir) bazel clean</NMakeCleanCommandLine>
    <NMakeReBuildCommandLine>$(RmBazelOutDir) bazel clean &amp;&amp; bazel build --compilation_mode=$(BazelCompilationMode) $(BazelArgs) $(BazelTargets) $(MakeRW)</NMakeReBuildCommandLine>
    <NMakeOutput>$(OutDir)$(BazelOutputFile)</NMakeOutput>
  </PropertyGroup>
</Project>
