Repository: conan-io/conan

Issue:
Conan installation with Visual studio 2019 installed fails to build
Note: I have visual studio 2019 installed, but I'm targeting vs2017.

I open up a visual studio 2017 command prompt and the example https://docs.conan.io/en/latest/getting_started.html#an-md5-encrypter-using-the-poco-libraries fails to build.

First of all:
```ERROR: Missing prebuilt package for 'zlib/1.2.11@conan/stable'```
the binary package is not available (makes sense, but I did not know what to do about it, so maybe offer a suggestion of using -b with that error message.

then I tried 
```conan install .. -b ```
which failed with:
CMake Error: Could not create named generator Visual Studio 16 2019

That is probably CMake lagging behind (or my version of it is not up to date, so I switched to Ninja using ```set CONAN_CMAKE_GENERATOR=Ninja```

and re-run ```conan install .. -b``` 

Then I run into:
```
strawberryperl/5.28.1.1@conan/stable package(): Packaged 3 '.bs' files: Streamer.bs, Map.bs, Layers.bs
strawberryperl/5.28.1.1@conan/stable package(): Packaged 1 '.json' file: alien.json
strawberryperl/5.28.1.1@conan/stable package(): Packaged 2 '.words' files: README.linux.words, README2.linux.words
strawberryperl/5.28.1.1@conan/stable package(): Packaged 1 '.xml' file: ppm.xml
strawberryperl/5.28.1.1@conan/stable package(): Packaged 1 '.perl' file: portable.perl
strawberryperl/5.28.1.1@conan/stable package(): Packaged 7 '.url' files
strawberryperl/5.28.1.1@conan/stable: Package '456f15897172eef340fcbac8a70811f2beb26a93' created
strawberryperl/5.28.1.1@conan/stable: Created package revision 09f05c6bdc9cb2b1d5dc90a060b3753a
zlib/1.2.11@conan/stable: WARN: Build folder is dirty, removing it: C:\Users\Jan Wilmans\.conan\data\zlib\1.2.11\conan\stable\build\3fb49604f9c2f729b85ba3115852006824e72cab
zlib/1.2.11@conan/stable: Copying sources to build folder
zlib/1.2.11@conan/stable: Building your package in C:\Users\Jan Wilmans\.conan\data\zlib\1.2.11\conan\stable\build\3fb49604f9c2f729b85ba3115852006824e72cab
zlib/1.2.11@conan/stable: Generator cmake created conanbuildinfo.cmake
zlib/1.2.11@conan/stable: Calling build()
zlib/1.2.11@conan/stable: WARN: Visual environment already set to 15
 Current settings visual version: 16
CMake Error at CMakeLists.txt:2 (project):
  Generator

    Ninja

  does not support platform specification, but platform

    x64

  was specified.


CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
See also "C:/Users/Jan Wilmans/.conan/data/zlib/1.2.11/conan/stable/build/3fb49604f9c2f729b85ba3115852006824e72cab/source_subfolder/_build/CMakeFiles/CMakeOutput.log".
zlib/1.2.11@conan/stable:
zlib/1.2.11@conan/stable: ERROR: Package '3fb49604f9c2f729b85ba3115852006824e72cab' build failed
zlib/1.2.11@conan/stable: WARN: Build folder C:\Users\Jan Wilmans\.conan\data\zlib\1.2.11\conan\stable\build\3fb49604f9c2f729b85ba3115852006824e72cab
ERROR: zlib/1.2.11@conan/stable: Error in build() method, line 49
        self._build_zlib()
while calling '_build_zlib', line 100
        self._build_zlib_cmake()
while calling '_build_zlib_cmake', line 75
        cmake.configure(build_dir=".")
        ConanException: Error 1 while executing cd . && cmake -G "Ninja" -A "x64" -DCONAN_LINK_RUNTIME="/MD" -DCMAKE_BUILD_TYPE="Release" -DCONAN_EXPORTED="1" -DCONAN_IN_LOCAL_CACHE="ON" -DCONAN_COMPILER="Visual Studio" -DCONAN_COMPILER_VERSION="16" -DCONAN_CXX_FLAGS="/MP12" -DCONAN_C_FLAGS="/MP12" -DBUILD_SHARED_LIBS="OFF" -DCMAKE_INSTALL_PREFIX="C:\Users\Jan Wilmans\.conan\data\zlib\1.2.11\conan\stable\package\3fb49604f9c2f729b85ba3115852006824e72cab" -DCMAKE_INSTALL_BINDIR="bin" -DCMAKE_INSTALL_SBINDIR="bin" -DCMAKE_INSTALL_LIBEXECDIR="bin" -DCMAKE_INSTALL_LIBDIR="lib" -DCMAKE_INSTALL_INCLUDEDIR="include" -DCMAKE_INSTALL_OLDINCLUDEDIR="include" -DCMAKE_INSTALL_DATAROOTDIR="share" -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY="ON" -Wno-dev "C:\Users\Jan Wilmans\.conan\data\zlib\1.2.11\conan\stable\build\3fb49604f9c2f729b85ba3115852006824e72cab"
```

And I had to change ``` compiler.version=16 ``` to ```compiler.version=15``` in C:\Users\<username>\.conan\profiles\default  to work around that.

---------------------------------------------------------------
```
>cmake  --version
cmake version 3.12.18081601-MSVC_2

CMake suite maintained and supported by Kitware (kitware.com/cmake).

>conan --version
Conan version 1.14.3

D:\conan\poco-md5-example\build>
```
---------------------------------------------------------------



C:\Users\<username>\.conan\profiles\default
```
[settings]
os=Windows
os_build=Windows
arch=x86_64
arch_build=x86_64
compiler=Visual Studio
compiler.version=16    // if I change this to 15 it works....
build_type=Release
[options]
[build_requires]
[env]
```

C:\Users\<username>\.conan\settings.yml
```

# Only for cross building, 'os_build/arch_build' is the system that runs Conan
os_build: [Windows, WindowsStore, Linux, Macos, FreeBSD, SunOS]
arch_build: [x86, x86_64, ppc32, ppc64le, ppc64, armv5el, armv5hf, armv6, armv7, armv7hf, armv7s, armv7k, armv8, armv8_32, armv8.3, sparc, sparcv9, mips, mips64, avr, s390, s390x]

# Only for building cross compilation tools, 'os_target/arch_target' is the system for
# which the tools generate code
os_target: [Windows, Linux, Macos, Android, iOS, watchOS, tvOS, FreeBSD, SunOS, Arduino]
arch_target: [x86, x86_64, ppc32, ppc64le, ppc64, armv5el, armv5hf, armv6, armv7, armv7hf, armv7s, armv7k, armv8, armv8_32, armv8.3, sparc, sparcv9, mips, mips64, avr, s390, s390x]

# Rest of the settings are "host" settings:
# - For native building/cross building: Where the library/program will run.
# - For building cross compilation tools: Where the cross compiler will run.
os:
    Windows:
        subsystem: [None, cygwin, msys, msys2, wsl]
    WindowsStore:
        version: ["8.1", "10.0"]
    Linux:
    Macos:
        version: [None, "10.6", "10.7", "10.8", "10.9", "10.10", "10.11", "10.12", "10.13", "10.14"]
    Android:
        api_level: ANY
    iOS:
        version: ["7.0", "7.1", "8.0", "8.1", "8.2", "8.3", "9.0", "9.1", "9.2", "9.3", "10.0", "10.1", "10.2", "10.3", "11.0", "11.1", "11.2", "11.3", "11.4", "12.0", "12.1"]
    watchOS:
        version: ["4.0", "4.1", "4.2", "4.3", "5.0", "5.1"]
    tvOS:
        version: ["11.0", "11.1", "11.2", "11.3", "11.4", "12.0", "12.1"]
    FreeBSD:
    SunOS:
    Arduino:
        board: ANY
arch: [x86, x86_64, ppc32, ppc64le, ppc64, armv5el, armv5hf, armv6, armv7, armv7hf, armv7s, armv7k, armv8, armv8_32, armv8.3, sparc, sparcv9, mips, mips64, avr, s390, s390x]
compiler:
    sun-cc:
        version: ["5.10", "5.11", "5.12", "5.13", "5.14"]
        threads: [None, posix]
        libcxx: [libCstd, libstdcxx, libstlport, libstdc++]
    gcc:
        version: ["4.1", "4.4", "4.5", "4.6", "4.7", "4.8", "4.9",
                  "5", "5.1", "5.2", "5.3", "5.4", "5.5",
                  "6", "6.1", "6.2", "6.3", "6.4",
                  "7", "7.1", "7.2", "7.3",
                  "8", "8.1", "8.2"]
        libcxx: [libstdc++, libstdc++11]
        threads: [None, posix, win32] #  Windows MinGW
        exception: [None, dwarf2, sjlj, seh] # Windows MinGW
    Visual Studio:
        runtime: [MD, MT, MTd, MDd]
        version: ["8", "9", "10", "11", "12", "14", "15", "16"]
        toolset: [None, v90, v100, v110, v110_xp, v120, v120_xp,
                  v140, v140_xp, v140_clang_c2, LLVM-vs2012, LLVM-vs2012_xp,
                  LLVM-vs2013, LLVM-vs2013_xp, LLVM-vs2014, LLVM-vs2014_xp,
                  LLVM-vs2017, LLVM-vs2017_xp, v141, v141_xp, v141_clang_c2, v142]
    clang:
        version: ["3.3", "3.4", "3.5", "3.6", "3.7", "3.8", "3.9", "4.0",
                  "5.0", "6.0", "7.0",
                  "8"]
        libcxx: [libstdc++, libstdc++11, libc++]
    apple-clang:
        version: ["5.0", "5.1", "6.0", "6.1", "7.0", "7.3", "8.0", "8.1", "9.0", "9.1", "10.0"]
        libcxx: [libstdc++, libc++]

build_type: [None, Debug, Release, RelWithDebInfo, MinSizeRel]
cppstd: [None, 98, gnu98, 11, gnu11, 14, gnu14, 17, gnu17, 20, gnu20]
```


Discussion:
Hi, about the error for the missing binary, we are not generating binaries for VS 2019 yet in conan-center, so it is normal. Hopefully we can do it soon. 
I think the message actually tells you what to do, right? e.g:

```
zlib/1.2.11@conan/stable: WARN: Can't find a 'zlib/1.2.11@conan/stable' package for the specified settings, options and dependencies:
- Settings: arch=ppc32, build_type=Release, compiler=gcc, compiler.version=7, os=Linux
- Options: fPIC=True, minizip=False, shared=False
- Dependencies: 
- Package ID: aabb35281493fa466f0981b5e28055c24debcb33

ERROR: Missing prebuilt package for 'zlib/1.2.11@conan/stable'
Try to build it from sources with "--build zlib"
Or read "http://docs.conan.io/en/latest/faq/troubleshooting.html#error-missing-prebuilt-package"
```

About the `CMake` error, For the visual 2019 support I think you need `CMake >= 3.14`



> About the CMake error, For the visual 2019 support I think you need CMake >= 3.14

Nope. I've got CMake 3.14.3 and I'm still getting this error. The problem is that conan assumes that msvc is always used with Visual studio generator and does not correctly handle the case for MSVC + Ninja builds (which are default for VS when you use "Open Folder" mode).

The error
```
  Generator
    Ninja
  does not support platform specification, but platform
    x64
  was specified.
```

is caused by this command line within conan's build:

```
 cmake -G "Ninja" -A "x64" -DCONAN_LINK_RUNTIME="/MD" -DCMAKE_BUILD_TYPE="Release" -DCONAN_EXPORTED="1" -DCONAN_IN_LOCAL_CACHE="ON" -DCONAN_COMPILER="Visual Studio" -DCONAN_COMPILER_VERSION="16" -DCONAN_CXX_FLAGS="/MP8" -DCONAN_C_FLAGS="/MP8" -DCMAKE_INSTALL_PREFIX="C:\Users\jenkins\.conan\data\GTest\1.8.0.2\microblink\stable\package\bd908d6af41ee9974ebf9d74a4df66b5b25c0bca" -DCMAKE_INSTALL_BINDIR="bin" -DCMAKE_INSTALL_SBINDIR="bin" -DCMAKE_INSTALL_LIBEXECDIR="bin" -DCMAKE_INSTALL_LIBDIR="lib" -DCMAKE_INSTALL_INCLUDEDIR="include" -DCMAKE_INSTALL_OLDINCLUDEDIR="include" -DCMAKE_INSTALL_DATAROOTDIR="share" -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY="ON" -Wno-dev ...
```
.

The problematic part is flag `-A "x64"`, which is appended in [this line](https://github.com/conan-io/conan/blob/develop/conans/client/build/cmake.py#L119) because `self.generator_platform` is `True` because [this function](https://github.com/conan-io/conan/blob/develop/conans/client/build/cmake_flags.py#L81) only checks whether compiler is Visual Studio, but not also if generator is "visual studio". Therefore, it appends this cmake flag even in ninja/makefile builds.

This needs to be fixed.

Resolving patch:
diff --git a/conans/client/build/cmake.py b/conans/client/build/cmake.py
--- a/conans/client/build/cmake.py
+++ b/conans/client/build/cmake.py
@@ -50,7 +50,8 @@ def __init__(self, conanfile, generator=None, cmake_system_name=True,
         self._cmake_program = os.getenv("CONAN_CMAKE_PROGRAM") or cmake_program or "cmake"
 
         self.generator = generator or get_generator(conanfile.settings)
-        self.generator_platform = generator_platform or get_generator_platform(conanfile.settings)
+        self.generator_platform = generator_platform or get_generator_platform(conanfile.settings,
+                                                                               self.generator)
         if not self.generator:
             self._conanfile.output.warn("CMake generator could not be deduced from settings")
         self.parallel = parallel
diff --git a/conans/client/build/cmake_flags.py b/conans/client/build/cmake_flags.py
--- a/conans/client/build/cmake_flags.py
+++ b/conans/client/build/cmake_flags.py
@@ -66,7 +66,7 @@ def get_generator(settings):
     return "Unix Makefiles"
 
 
-def get_generator_platform(settings):
+def get_generator_platform(settings, generator):
     if "CONAN_CMAKE_GENERATOR_PLATFORM" in os.environ:
         return os.environ["CONAN_CMAKE_GENERATOR_PLATFORM"]
 
@@ -74,7 +74,8 @@ def get_generator_platform(settings):
     arch = settings.get_safe("arch")
     compiler_version = settings.get_safe("compiler.version")
 
-    if compiler == "Visual Studio" and Version(compiler_version) >= "16":
+    if compiler == "Visual Studio" and Version(compiler_version) >= "16" \
+            and "Visual" in generator:
         return {"x86": "Win32",
                 "x86_64": "x64",
                 "armv7": "ARM",


Test patch:
diff --git a/conans/test/unittests/client/build/cmake_test.py b/conans/test/unittests/client/build/cmake_test.py
--- a/conans/test/unittests/client/build/cmake_test.py
+++ b/conans/test/unittests/client/build/cmake_test.py
@@ -249,7 +249,7 @@ def cmake_generator_platform_vs2017_test(self, arch, generator):
                            ('x86_64', 'x64'),
                            ('armv7', 'ARM'),
                            ('armv8', 'ARM64')])
-    def cmake_generator_platform_vs2019_test(self, arch, platform):
+    def cmake_generator_platform_vs2019_test(self, arch, pf):
         settings = Settings.loads(default_settings_yml)
         settings.os = "Windows"
         settings.compiler = "Visual Studio"
@@ -260,7 +260,21 @@ def cmake_generator_platform_vs2019_test(self, arch, platform):
         conan_file.settings = settings
 
         cmake = CMake(conan_file)
-        self.assertIn('-G "Visual Studio 16 2019" -A "%s"' % platform, cmake.command_line)
+        self.assertIn('-G "Visual Studio 16 2019" -A "%s"' % pf, cmake.command_line)
+
+    def cmake_generator_platform_vs2019_with_ninja_test(self):
+        settings = Settings.loads(default_settings_yml)
+        settings.os = "Windows"
+        settings.compiler = "Visual Studio"
+        settings.compiler.version = "16"
+        settings.arch = "x86_64"
+
+        conan_file = ConanFileMock()
+        conan_file.settings = settings
+
+        cmake = CMake(conan_file, generator="Ninja")
+        self.assertIn('-G "Ninja"', cmake.command_line)
+        self.assertNotIn("-A", cmake.command_line)
 
     @parameterized.expand([('arm',),
                            ('ppc',),