def __init__(self, conanfile, generator=None, cmake_system_name=True, parallel=True, build_type=None, toolset=None, make_program=None, set_cmake_flags=False, msbuild_verbosity=None, cmake_program=None): """ :param conanfile: Conanfile instance :param generator: Generator name to use or none to autodetect :param cmake_system_name: False to not use CMAKE_SYSTEM_NAME variable, True for auto-detect or directly a string with the system name :param parallel: Try to build with multiple cores if available :param build_type: Overrides default build type coming from settings :param toolset: Toolset name to use (such as llvm-vs2014) or none for default one, 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 msbuild_verbosity: verbosity level for MSBuild (in case of Visual Studio generator) :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)" ) 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) if not self.generator: self._conanfile.output.warn( "CMake generator could not be deduced from settings") self.parallel = parallel # Initialize definitions (won't be updated if conanfile or any of these variables change) builder = CMakeDefinitionsBuilder(self._conanfile, cmake_system_name=cmake_system_name, make_program=make_program, parallel=parallel, generator=self.generator, set_cmake_flags=set_cmake_flags, forced_build_type=build_type, output=self._conanfile.output) # FIXME CONAN 2.0: CMake() interface should be always the constructor and self.definitions. # FIXME CONAN 2.0: Avoid properties and attributes to make the user interface more clear self.definitions = builder.get_definitions() self.toolset = toolset or get_toolset(self._settings) self.build_dir = None self.msbuild_verbosity = (os.getenv("CONAN_MSBUILD_VERBOSITY") or msbuild_verbosity or "minimal")
def _get_cmake_definitions(self): """ Detect all definitions conan sends to cmake by command line and convert them to cmake commands. Removes the CONAN_EXPORTED definition, so that users may check if conan has been invoked or not. :return: List of lines to print into a cmake file """ with get_env_context_manager(self.conanfile): ret = ["\t\t\"cmakeSettings\": {"] generator = get_generator(self.conanfile.settings) self.conanfile.install_folder = "" def_builder = CMakeDefinitionsBuilder(self.conanfile, generator=generator) definitions = def_builder.get_definitions(None) for name, value in definitions.items(): ret.append("\t\t\t\"{name}\": \"{value}\",".format( name=name, value=value)) deps = [] for _, dep_cpp_info in self.deps_build_info.dependencies: deps.append("\t\t\t\t\t" + DepsCppCmake(dep_cpp_info).rootpath) deps[:-1] = [line + "," for line in deps[:-1]] ret.append("\t\t\t\"CMAKE_PREFIX_PATH\": [") ret.extend(deps) ret.append("\t\t\t],") ret.append("\t\t\t\"CMAKE_MODULE_PATH\": [") ret.extend(deps) ret.append("\t\t\t]") return ret
def __init__( self, conanfile, generator=None, generator_platform=None, build_type=None, cmake_system_name=True, toolset=None, parallel=True, make_program=None, # cmake_program=None, # TODO: cmake program should be considered in the environment ): self._conanfile = conanfile self._fpic = self._deduce_fpic() self._vs_static_runtime = self._deduce_vs_static_runtime() self._set_rpath = True self._set_std = True self._set_libcxx = True # To find the generated cmake_find_package finders self._cmake_prefix_path = "${CMAKE_BINARY_DIR}" self._cmake_module_path = "${CMAKE_BINARY_DIR}" self._generator = generator or get_generator(self._conanfile) self._generator_platform = (generator_platform or get_generator_platform( self._conanfile.settings, self._generator)) self._toolset = toolset or get_toolset(self._conanfile.settings, self._generator) self._build_type = build_type or self._conanfile.settings.get_safe( "build_type") builder = CMakeDefinitionsBuilder(self._conanfile, cmake_system_name=cmake_system_name, make_program=make_program, parallel=parallel, generator=self._generator, set_cmake_flags=False, output=self._conanfile.output) self.definitions = Definitions() self.definitions.update(builder.get_definitions()) # FIXME: Removing too many things. We want to bring the new logic for the toolchain here # so we don't mess with the common code. self.definitions.pop("CMAKE_BUILD_TYPE", None) self.definitions.pop("CONAN_IN_LOCAL_CACHE", None) self.definitions.pop("CMAKE_PREFIX_PATH", None) self.definitions.pop("CMAKE_MODULE_PATH", None) self.definitions.pop("CONAN_LINK_RUNTIME", None) for install in ("PREFIX", "BINDIR", "SBINDIR", "LIBEXECDIR", "LIBDIR", "INCLUDEDIR", "OLDINCLUDEDIR", "DATAROOTDIR"): self.definitions.pop("CMAKE_INSTALL_%s" % install, None)
def __init__(self, conanfile, generator=None, cmake_system_name=True, parallel=True, build_type=None, toolset=None, make_program=None, set_cmake_flags=False): """ :param settings_or_conanfile: Conanfile instance (or settings for retro compatibility) :param generator: Generator name to use or none to autodetect :param cmake_system_name: False to not use CMAKE_SYSTEM_NAME variable, True for auto-detect or directly a string with the system name :param parallel: Try to build with multiple cores if available :param build_type: Overrides default build type comming from settings :param toolset: Toolset name to use (such as llvm-vs2014) or none for default one, 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) """ if not isinstance(conanfile, ConanFile): raise ConanException( "First argument of CMake() has to be ConanFile. Use CMake(self)" ) self._settings = conanfile.settings self._conanfile = conanfile self.generator = generator or get_generator(self._settings) self.toolset = toolset or get_toolset(self._settings) self.build_dir = None self.parallel = parallel self._set_cmake_flags = set_cmake_flags self._cmake_system_name = cmake_system_name self._make_program = make_program # Initialize definitions (won't be updated if conanfile or any of these variables change) builder = CMakeDefinitionsBuilder( self._conanfile, cmake_system_name=self._cmake_system_name, make_program=self._make_program, parallel=self.parallel, generator=self.generator, set_cmake_flags=self._set_cmake_flags) self.definitions = builder.get_definitions() if build_type is None: self.build_type = self._settings.get_safe("build_type") else: self.build_type = build_type
def cmake_find_package_multi_definitions_test(self): """ CMAKE_PREFIX_PATH and CMAKE_MODULE_PATH must be present in cmake_find_package_multi definitions """ settings_mock = _MockSettings(build_type="Release") conanfile = ConanFile(TestBufferConanOutput(), None) conanfile.initialize(settings_mock, EnvValues()) install_folder = "/c/foo/testing" setattr(conanfile, "install_folder", install_folder) conanfile.generators = ["cmake_find_package_multi"] definitions_builder = CMakeDefinitionsBuilder(conanfile) definitions = definitions_builder.get_definitions() self.assertEqual(install_folder, definitions["CMAKE_PREFIX_PATH"]) self.assertEqual(install_folder, definitions["CMAKE_MODULE_PATH"])
def test_cmake_find_package_multi_definitions(self): # CMAKE_PREFIX_PATH and CMAKE_MODULE_PATH must be in cmake_find_package_multi definitions settings_mock = _MockSettings(build_type="Release") conanfile = ConanFile(Mock(), None) conanfile.initialize(settings_mock, EnvValues()) install_folder = "/c/foo/testing" conanfile.folders.set_base_install(install_folder) conanfile.generators = ["cmake_find_package_multi"] definitions_builder = CMakeDefinitionsBuilder(conanfile) definitions = definitions_builder.get_definitions("3.13") self.assertEqual(install_folder, definitions["CMAKE_PREFIX_PATH"]) self.assertEqual(install_folder, definitions["CMAKE_MODULE_PATH"])
def test_cmake_definitions_apple(self): #https://github.com/conan-io/conan/issues/7875 settings_mock = _MockSettings(build_type="Release") settings_mock.os = "iOS" settings_mock.os_build = "Macos" conanfile = ConanFile(TestBufferConanOutput(), None) install_folder = "/c/foo/testing" setattr(conanfile, "install_folder", install_folder) conanfile.initialize(settings_mock, EnvValues()) definitions_builder = CMakeDefinitionsBuilder(conanfile) definitions = definitions_builder.get_definitions("3.13") self.assertEqual("Darwin", definitions["CMAKE_SYSTEM_NAME"]) definitions = definitions_builder.get_definitions("3.14") self.assertEqual("iOS", definitions["CMAKE_SYSTEM_NAME"])
def _def_builder(self): return CMakeDefinitionsBuilder( self._conanfile, cmake_system_name=self._cmake_system_name, make_program=self._make_program, parallel=self.parallel, generator=self.generator, set_cmake_flags=self._set_cmake_flags)
def _get_cmake_definitions(self): """ Detect all definitions conan sends to cmake by command line and convert them to cmake commands. Removes the CONAN_EXPORTED definition, so that users may check if conan has been invoked or not. :return: string to print into a cmake file """ with get_env_context_manager(self.conanfile): ret = [] build_type = self.conanfile.settings.get_safe("build_type") generator = get_generator(self.conanfile.settings) def_builder = CMakeDefinitionsBuilder(self.conanfile, generator=generator, forced_build_type=build_type) cmake_version = self.get_version() definitions = def_builder.get_definitions(cmake_version) if "CONAN_EXPORTED" in definitions: del definitions["CONAN_EXPORTED"] for name, value in definitions.items(): value = _cmake_escape_backslash(value) ret.append("set({name} {value})".format(name=name, value=value)) return os.linesep.join(ret)
def __init__(self, conanfile, generator=None, cmake_system_name=True, parallel=True, build_type=None, toolset=None, make_program=None, set_cmake_flags=False, msbuild_verbosity="minimal", cmake_program=None, generator_platform=None, append_vcvars=False): """ :param conanfile: Conanfile instance :param generator: Generator name to use or none to autodetect :param cmake_system_name: False to not use CMAKE_SYSTEM_NAME variable, True for auto-detect or directly a string with the system name :param parallel: Try to build with multiple cores if available :param build_type: Overrides default build type coming from settings :param toolset: Toolset name to use (such as llvm-vs2014) or none for default one, 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 msbuild_verbosity: verbosity level for MSBuild (in case of Visual Studio generator) :param cmake_program: Path to the custom cmake executable :param generator_platform: Generator platform name or none to autodetect (-A cmake option) """ self._append_vcvars = append_vcvars 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_platform = generator_platform self.generator = generator or get_generator(conanfile) if not self.generator: self._conanfile.output.warn( "CMake generator could not be deduced from settings") self.parallel = parallel # Initialize definitions (won't be updated if conanfile or any of these variables change) builder = CMakeDefinitionsBuilder(self._conanfile, cmake_system_name=cmake_system_name, make_program=make_program, parallel=parallel, generator=self.generator, set_cmake_flags=set_cmake_flags, forced_build_type=build_type, output=self._conanfile.output) # FIXME CONAN 2.0: CMake() interface should be always the constructor and self.definitions. # FIXME CONAN 2.0: Avoid properties and attributes to make the user interface more clear try: cmake_version = self.get_version() self.definitions = builder.get_definitions(cmake_version) except ConanException: self.definitions = builder.get_definitions(None) self.definitions["CONAN_EXPORTED"] = "1" if hasattr(self._conanfile, 'settings_build'): # https://github.com/conan-io/conan/issues/9202 if self._conanfile.settings_build.get_safe("os") == "Macos" and \ self._conanfile.settings.get_safe("os") == "iOS": self.definitions["CMAKE_FIND_ROOT_PATH_MODE_INCLUDE"] = "BOTH" self.definitions["CMAKE_FIND_ROOT_PATH_MODE_LIBRARY"] = "BOTH" self.definitions["CMAKE_FIND_ROOT_PATH_MODE_PACKAGE"] = "BOTH" self.toolset = toolset or get_toolset(self._settings, self.generator) self.build_dir = None self.msbuild_verbosity = os.getenv( "CONAN_MSBUILD_VERBOSITY") or msbuild_verbosity