diff --git a/conans/client/build/cmake.py b/conans/client/build/cmake.py index bc73e718406..58adb6967ca 100644 --- a/conans/client/build/cmake.py +++ b/conans/client/build/cmake.py @@ -20,7 +20,7 @@ class CMake(object): def __init__(self, conanfile, generator=None, cmake_system_name=True, parallel=True, build_type=None, toolset=None, make_program=None, - set_cmake_flags=False): + set_cmake_flags=False, cmake_program=None): """ :param conanfile: Conanfile instance :param generator: Generator name to use or none to autodetect @@ -32,6 +32,7 @@ def __init__(self, conanfile, generator=None, cmake_system_name=True, applies only to certain generators (e.g. Visual Studio) :param set_cmake_flags: whether or not to set CMake flags like CMAKE_CXX_FLAGS, CMAKE_C_FLAGS, etc. it's vital to set for certain projects (e.g. using CMAKE_SIZEOF_VOID_P or CMAKE_LIBRARY_ARCHITECTURE) + :param cmake_program: Path to the custom cmake executable """ if not isinstance(conanfile, ConanFile): raise ConanException("First argument of CMake() has to be ConanFile. Use CMake(self)") @@ -39,6 +40,7 @@ def __init__(self, conanfile, generator=None, cmake_system_name=True, self._conanfile = conanfile self._settings = conanfile.settings self._build_type = build_type or conanfile.settings.get_safe("build_type") + self._cmake_program = os.getenv("CONAN_CMAKE_PROGRAM") or cmake_program or "cmake" self.generator = generator or get_generator(conanfile.settings) self.parallel = parallel @@ -184,7 +186,7 @@ def configure(self, args=None, defs=None, source_dir=None, build_dir=None, pkg_env = {"PKG_CONFIG_PATH": self._conanfile.install_folder} if set_env else {} with tools.environment_append(pkg_env): - command = "cd %s && cmake %s" % (args_to_string([self.build_dir]), arg_list) + command = "cd %s && %s %s" % (args_to_string([self.build_dir]), self._cmake_program, arg_list) if platform.system() == "Windows" and self.generator == "MinGW Makefiles": with tools.remove_from_path("sh"): self._run(command) @@ -220,7 +222,7 @@ def _build(self, args=None, build_dir=None, target=None): self.build_config, args_to_string(args) ]) - command = "cmake --build %s" % arg_list + command = "%s --build %s" % (self._cmake_program, arg_list) self._run(command) def install(self, args=None, build_dir=None): diff --git a/conans/client/conf/__init__.py b/conans/client/conf/__init__.py index 581a480eab0..c6582e93c23 100644 --- a/conans/client/conf/__init__.py +++ b/conans/client/conf/__init__.py @@ -105,6 +105,7 @@ # non_interactive = False # environment CONAN_NON_INTERACTIVE # conan_make_program = make # environment CONAN_MAKE_PROGRAM (overrides the make program used in AutoToolsBuildEnvironment.make) +# conan_cmake_program = cmake # environment CONAN_CMAKE_PROGRAM (overrides the make program used in CMake.cmake_program) # cmake_generator # environment CONAN_CMAKE_GENERATOR # http://www.vtk.org/Wiki/CMake_Cross_Compiling @@ -202,6 +203,7 @@ def env_vars(self): "CONAN_BASH_PATH": self._env_c("general.bash_path", "CONAN_BASH_PATH", None), "CONAN_MAKE_PROGRAM": self._env_c("general.conan_make_program", "CONAN_MAKE_PROGRAM", None), + "CONAN_CMAKE_PROGRAM": self._env_c("general.conan_cmake_program", "CONAN_CMAKE_PROGRAM", None), "CONAN_TEMP_TEST_FOLDER": self._env_c("general.temp_test_folder", "CONAN_TEMP_TEST_FOLDER", "False"), "CONAN_SKIP_VS_PROJECTS_UPGRADE": self._env_c("general.skip_vs_projects_upgrade", "CONAN_SKIP_VS_PROJECTS_UPGRADE", "False"), "CONAN_HOOKS": self._env_c("hooks", "CONAN_HOOKS", None), diff --git a/conans/test/unittests/client/build/cmake_test.py b/conans/test/unittests/client/build/cmake_test.py index f2ff1b0c43d..0d8f66e1d9e 100644 --- a/conans/test/unittests/client/build/cmake_test.py +++ b/conans/test/unittests/client/build/cmake_test.py @@ -1160,3 +1160,29 @@ def test_vcvars_applied(self, generator, compiler, version): vcvars_mock.__exit__ = mock.MagicMock(return_value=None) cmake.build() self.assertTrue(vcvars_mock.called, "vcvars weren't called") + + def test_cmake_program(self): + conanfile = ConanFileMock() + settings = Settings.loads(default_settings_yml) + settings.os = "Windows" + settings.compiler = "Visual Studio" + settings.compiler.version = "14" + conanfile.settings = settings + + cmake = CMake(conanfile) + cmake.build() + self.assertEqual("cmake --build .", conanfile.command) + + cmake = CMake(conanfile, cmake_program="use_another_cmake") + cmake.build() + self.assertEqual("use_another_cmake --build .", conanfile.command) + + with tools.environment_append({"CONAN_CMAKE_PROGRAM": "my_custom_cmake"}): + cmake = CMake(conanfile) + cmake.build() + self.assertEqual("my_custom_cmake --build .", conanfile.command) + + with tools.environment_append({"CONAN_CMAKE_PROGRAM": "cmake_from_environment_has_priority"}): + cmake = CMake(conanfile, cmake_program="but_not_cmake_from_the_ctor") + cmake.build() + self.assertEqual("cmake_from_environment_has_priority --build .", conanfile.command)