Repository: conan-io/conan

Issue:
Implement a way to select cmake generator
Currently `CMake.command_line` selects `Unix Makefiles` for OSX and Linux in most situations. I would want to override that to use the `Ninja` generator everywhere (yes even on windows) since it performs much better in most cases and is the configuration we test with the most here.

I started to look at adding `cmake_generator` to conan.conf but this doesn't seem to be the correct solution since it will affect the binary package and frankly the generator that was used to build the package shouldn't affect downstream dependencies.

My current idea is to define a environment variable to override the generator `CONAN_CMAKE_GENERATOR` perhaps? Any other options?



Discussion:
Yes, if it is for all packages simultaneously, in that case the environment variable could make sense. I'd probably go for a configuration in `conan.conf` too, even if defined/overridable by env variable too. It is nice to have them centralized in a "conan" location instead of having to spread them in .bashrc or win equivalent if we want to persist them in our sessions.

Probably deserves some consideration of other possibilities, as there are other generators that could be considered. Any other similar case that comes to your mind?

I be glad to have as a part of the conan.conf. I  tried this by adding `cmake_generator: [None, Ninja, Unix Makefiles]`  in the `settings.yml` and then doing `if self._settings.cmake_generator is not None: return self._settings.cmake_generator` in the cmake class. This complained that `cmake_generator` was not defined in the settings. But If I understand settings correctly that will affect the binary package similar to what arch, compiler and build_type does. Is this how you think it should work?

Yes, you understood fine, so this is not a good idea. Settings, options and requirements, affect the hash (SHA1), that is, the ID of the binary package. Building with Ninja or Makefiles, AFAIK shouldn't produce a different binary as long as the compiler and the compiler configuration is the same. Adding the generator to the settings will change the package id. That is the same reason why the compiler path is not stored in the settings. So the env var, or other conan configuration way is necessary. Env vars require less code, but I will analyse for something more integrated with conan.conf and how to pass info from there to conanfiles in a clean way.

I think it would be good to have some kind of profiles per site that can be stored in a central place but not affect the hash. Are you fine with me creating a PR for the environment variable for now or should we wait for some other method?


Resolving patch:
diff --git a/conans/client/cmake.py b/conans/client/cmake.py
--- a/conans/client/cmake.py
+++ b/conans/client/cmake.py
@@ -1,6 +1,6 @@
 from conans.errors import ConanException
 from conans.model.settings import Settings
-
+import os
 
 class CMake(object):
 
@@ -30,6 +30,9 @@ def generator(self):
         compiler = str(self._settings.compiler) if self._settings.compiler else None
         arch = str(self._settings.arch) if self._settings.arch else None
 
+        if "CONAN_CMAKE_GENERATOR" in os.environ:
+            return os.environ["CONAN_CMAKE_GENERATOR"]
+
         if compiler == "Visual Studio":
             _visuals = {'8': '8 2005',
                         '9': '9 2008',


Test patch: