From 2b34983b9f23d84a69110706ec507194e036fb72 Mon Sep 17 00:00:00 2001
From: Rafael Kitover <rkitover@gmail.com>
Date: Sat, 7 Dec 2019 16:58:12 +0000
Subject: [PATCH] cmake: Support VS Ninja/jom builds on Windows.

Only set CMAKE_GENERATOR_PLATFORM to "x64" for 64 bit toolchains for any
of the "Visual Studio" generators, which use msbuild.

For other generators such as Ninja or NMake (jom) set
CMAKE_C[XX]_COMPILER to "cl" to let cmake find the compiler in the
user's PATH and set up the toolchain appropriately. On the user's part,
all that is required is running the 64 bit dev tools command prompt, or
setting up the equivalent environment in powershell.

We are no longer restricted to msbuild, and other generators such as
Ninja will work correctly.

Switch the appveyor job to Ninja, this requires some wrangling because
the appveyor environment does not include ninja. I used this:

https://github.com/boostorg/hana/blob/master/.appveyor.yml

Also update the README.md instructions to use Ninja.

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
---
 .appveyor.yml                   | 39 +++++++++++++++++++++++++++++++++
 README.md                       |  6 ++---
 appveyor.yml                    | 29 ------------------------
 cmake/Set-Toolchain-vcpkg.cmake |  8 ++++++-
 4 files changed, 49 insertions(+), 33 deletions(-)
 create mode 100644 .appveyor.yml
 delete mode 100644 appveyor.yml

diff --git a/.appveyor.yml b/.appveyor.yml
new file mode 100644
index 00000000..3ae51d15
--- /dev/null
+++ b/.appveyor.yml
@@ -0,0 +1,39 @@
+version: '{build}'
+
+image:
+  - Visual Studio 2017
+
+build:
+  verbosity: detailed
+
+configuration:
+  - Release
+
+platform:
+  - x64
+
+environment:
+  matrix:
+    - arch: Win64
+
+matrix:
+  fast_finish: true
+
+install:
+  - set NINJA_URL="https://github.com/ninja-build/ninja/releases/download/v1.9.0/ninja-win.zip"
+  - appveyor DownloadFile %NINJA_URL% -FileName ninja.zip
+  - 7z x ninja.zip -oc:\projects\ninja > nul
+  - set PATH=c:\projects\ninja;%PATH%
+  - ninja --version
+
+before_build:
+  - call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
+
+build_script:
+  - mkdir build
+  - cd build
+  - cmake .. -DVCPKG_TARGET_TRIPLET=x64-windows -DCMAKE_BUILD_TYPE=Release -G Ninja
+  - ninja
+
+cache:
+  - c:\projects\vcpkg\installed
diff --git a/README.md b/README.md
index 84510e22..356428aa 100644
--- a/README.md
+++ b/README.md
@@ -59,7 +59,7 @@ make -j`nproc`
 Solus, OpenSUSE, Gentoo and RHEL/CentOS) and Mac OS X (homebrew, macports or
 fink.)
 
-The Ninja cmake generator is also now supported (except for Visual Studio.)
+The Ninja cmake generator is also now supported.
 
 ## Building a Libretro core
 
@@ -88,8 +88,8 @@ To build in the visual studio command prompt, use something like this:
 ```
 mkdir build
 cd build
-cmake .. -DVCPKG_TARGET_TRIPLET=x64-windows
-msbuild -m -p:BuildInParallel=true -p:Configuration=Release .\ALL_BUILD.vcxproj 
+cmake .. -DVCPKG_TARGET_TRIPLET=x64-windows -DCMAKE_BUILD_TYPE=Release -G Ninja
+ninja
 ```
 
 This support is new and we are still working out some issues, including support
diff --git a/appveyor.yml b/appveyor.yml
deleted file mode 100644
index 6983df94..00000000
--- a/appveyor.yml
+++ /dev/null
@@ -1,29 +0,0 @@
-version: '{build}'
-
-image: 
-  - Visual Studio 2017
-
-configuration:
-  - Release
-
-platform:
-  - x64
-
-environment:
-  matrix:
-    - arch: Win64
-
-matrix:
-  fast_finish: true
-
-before_build:
-- cmd: |-
-    mkdir build
-    cd build
-    cmake .. -DVCPKG_TARGET_TRIPLET=x64-windows
-
-build_script:
-  - msbuild -m -p:BuildInParallel=true -p:Configuration=Release ALL_BUILD.vcxproj
-
-cache:
-  - c:\projects\vcpkg\installed
diff --git a/cmake/Set-Toolchain-vcpkg.cmake b/cmake/Set-Toolchain-vcpkg.cmake
index 21f214e2..ac35c3f2 100644
--- a/cmake/Set-Toolchain-vcpkg.cmake
+++ b/cmake/Set-Toolchain-vcpkg.cmake
@@ -95,10 +95,16 @@ if(VCPKG_TARGET_TRIPLET)
         WORKING_DIRECTORY ${VCPKG_ROOT}
     )
 
-    if(WIN32 AND VCPKG_TARGET_TRIPLET MATCHES x64)
+    if(WIN32 AND VCPKG_TARGET_TRIPLET MATCHES x64 AND CMAKE_GENERATOR MATCHES "Visual Studio")
         set(CMAKE_GENERATOR_PLATFORM x64 CACHE STRING "visual studio build architecture" FORCE)
     endif()
 
+    if(NOT CMAKE_GENERATOR MATCHES "Visual Studio")
+        # set toolchain to VS for e.g. Ninja or jom
+        set(CMAKE_C_COMPILER   cl CACHE STRING "Microsoft C/C++ Compiler" FORCE)
+        set(CMAKE_CXX_COMPILER cl CACHE STRING "Microsoft C/C++ Compiler" FORCE)
+    endif()
+
     set(CMAKE_TOOLCHAIN_FILE ${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake CACHE FILEPATH "vcpkg toolchain" FORCE)
     include(${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake)
 endif()