Repository: conan-io/conan

Issue:
[Fix] Remove "namespace" argument from CMakeToolchain
Since 1.47, generating the CMakePresets.json file, it is not used anymore.
Also from the CMake() build helper

Discussion:

Patch:
diff --git a/conan/tools/cmake/cmake.py b/conan/tools/cmake/cmake.py
index 3d4def08b..e6d04073f 100644
--- a/conan/tools/cmake/cmake.py
+++ b/conan/tools/cmake/cmake.py
@@ -48,12 +48,11 @@ class CMake(object):
     are passed to the command line, plus the ``--config Release`` for builds in multi-config
     """
 
-    def __init__(self, conanfile, namespace=None):
+    def __init__(self, conanfile):
         _validate_recipe(conanfile)
 
         # Store a reference to useful data
         self._conanfile = conanfile
-        self._namespace = namespace
 
         cmake_presets = load_cmake_presets(conanfile.generators_folder)
         self._generator = cmake_presets["configurePresets"][0]["generator"]
diff --git a/conan/tools/cmake/toolchain/blocks.py b/conan/tools/cmake/toolchain/blocks.py
index 8413c86f4..655fba520 100644
--- a/conan/tools/cmake/toolchain/blocks.py
+++ b/conan/tools/cmake/toolchain/blocks.py
@@ -8,9 +8,9 @@ from jinja2 import Template
 from conan.tools._compilers import architecture_flag
 from conan.tools.apple.apple import is_apple_os, to_apple_arch
 from conan.tools.build import build_jobs
+from conan.tools.build.cross_building import cross_building
 from conan.tools.cmake.toolchain import CONAN_TOOLCHAIN_FILENAME
 from conan.tools.cmake.utils import is_multi_configuration
-from conan.tools.build.cross_building import cross_building
 from conan.tools.intel import IntelCC
 from conan.tools.microsoft.visual import is_msvc, msvc_version_to_toolset_version
 from conans.errors import ConanException
diff --git a/conan/tools/cmake/toolchain/toolchain.py b/conan/tools/cmake/toolchain/toolchain.py
index 8344dcd99..2fc2935d4 100644
--- a/conan/tools/cmake/toolchain/toolchain.py
+++ b/conan/tools/cmake/toolchain/toolchain.py
@@ -1,8 +1,8 @@
 import os
 import textwrap
-import six
 from collections import OrderedDict
 
+import six
 from jinja2 import Template
 
 from conan.tools._check_build_profile import check_using_build_profile
@@ -95,7 +95,11 @@ class CMakeToolchain(object):
 
         # Variables
         {% for it, value in variables.items() %}
+        {% if value is boolean %}
+        set({{ it }} {{ value|cmake_value }} CACHE BOOL "Variable {{ it }} conan-toolchain defined")
+        {% else %}
         set({{ it }} {{ value|cmake_value }} CACHE STRING "Variable {{ it }} conan-toolchain defined")
+        {% endif %}
         {% endfor %}
         # Variables  per configuration
         {{ iterate_configs(variables_config, action='set') }}
@@ -109,10 +113,9 @@ class CMakeToolchain(object):
         {{ iterate_configs(preprocessor_definitions_config, action='add_definitions') }}
         """)
 
-    def __init__(self, conanfile, generator=None, namespace=None):
+    def __init__(self, conanfile, generator=None):
         self._conanfile = conanfile
         self.generator = self._get_generator(generator)
-        self._namespace = namespace
         self.variables = Variables()
         self.preprocessor_definitions = Variables()
 
@@ -161,7 +164,7 @@ class CMakeToolchain(object):
     def generate(self):
         toolchain_file = self._conanfile.conf.get("tools.cmake.cmaketoolchain:toolchain_file")
         if toolchain_file is None:  # The main toolchain file generated only if user dont define
-            save(self.filename, self.content)
+            save(os.path.join(self._conanfile.generators_folder, self.filename), self.content)
         # If we're using Intel oneAPI, we need to generate the environment file and run it
         if self._conanfile.settings.get_safe("compiler") == "intel-cc":
             IntelCC(self._conanfile).generate()
diff --git a/conans/model/info.py b/conans/model/info.py
index d1a98c6a3..80bd185a2 100644
--- a/conans/model/info.py
+++ b/conans/model/info.py
@@ -349,6 +349,9 @@ class PythonRequireInfo(object):
         self._channel = self._ref.channel
         self._revision = self._ref.revision
 
+    def unrelated_mode(self):
+        self._name = self._version = self._user = self._channel = self._revision = None
+
 
 class PythonRequiresInfo(object):
 


Test patch:
diff --git a/conans/test/integration/package_id/python_requires_package_id_test.py b/conans/test/integration/package_id/python_requires_package_id_test.py
index bb9a67fd7..716e7b5c4 100644
--- a/conans/test/integration/package_id/python_requires_package_id_test.py
+++ b/conans/test/integration/package_id/python_requires_package_id_test.py
@@ -50,6 +50,19 @@ class PythonRequiresPackageIDTest(unittest.TestCase):
         self.assertIn("tool/1.1.2", self.client2.out)
         self.assertIn("pkg/0.1:387c1c797a011d426ecb25a1e01b28251e443ec8 - Build", self.client2.out)
 
+    def test_unrelated_conf(self):
+        # change the policy in conan.conf
+        self.client2.run("config set general.default_python_requires_id_mode=unrelated_mode")
+        self.client2.run("create . pkg/0.1@")
+        self.assertIn("tool/1.1.1", self.client2.out)
+        self.assertIn("pkg/0.1:c941ae50e2daf4a118c393591cfef6a55cd1cfad - Build", self.client2.out)
+
+        # with any change the package id doesn't change
+        self.client.run("export . tool/1.1.2@")
+        self.client2.run("create . pkg/0.1@ --build missing")
+        self.assertIn("tool/1.1.2", self.client2.out)
+        self.assertIn("pkg/0.1:c941ae50e2daf4a118c393591cfef6a55cd1cfad - Cache", self.client2.out)
+
     def test_change_mode_package_id(self):
         # change the policy in package_id
         conanfile = textwrap.dedent("""
diff --git a/conans/test/integration/toolchains/test_toolchain_namespaces.py b/conans/test/integration/toolchains/test_toolchain_namespaces.py
index 57a40afb8..b98f8b1aa 100644
--- a/conans/test/integration/toolchains/test_toolchain_namespaces.py
+++ b/conans/test/integration/toolchains/test_toolchain_namespaces.py
@@ -2,39 +2,10 @@ import os
 import textwrap
 
 from conan.tools import CONAN_TOOLCHAIN_ARGS_FILE
-from conan.tools.cmake.presets import load_cmake_presets
 from conan.tools.files.files import load_toolchain_args
 from conans.test.utils.tools import TestClient
 
 
-def test_cmake_namespace():
-    client = TestClient()
-    namespace = "somename"
-    conanfile = textwrap.dedent("""
-            from conans import ConanFile
-            from conan.tools.cmake import CMakeToolchain, CMake
-
-            class Conan(ConanFile):
-                settings = "os", "arch", "compiler", "build_type"
-                def generate(self):
-                    cmake = CMakeToolchain(self, namespace='{0}')
-                    cmake.generate()
-                def build(self):
-                    cmake = CMake(self, namespace='{0}')
-                    self.output.info(cmake._generator)
-                    self.output.info(cmake._toolchain_file)
-            """.format(namespace))
-
-    client.save({"conanfile.py": conanfile})
-    client.run("install . ")
-    presets = load_cmake_presets(client.current_folder)
-    toolchain_file = presets["configurePresets"][0]["toolchainFile"]
-    generator = presets["configurePresets"][0]["generator"]
-    client.run("build . ")
-    assert generator in client.out
-    assert toolchain_file in client.out
-
-
 def test_bazel_namespace():
     client = TestClient()
     namespace = "somename"
diff --git a/conans/test/unittests/tools/cmake/test_cmaketoolchain.py b/conans/test/unittests/tools/cmake/test_cmaketoolchain.py
index ddc5baf75..da9d29b66 100644
--- a/conans/test/unittests/tools/cmake/test_cmaketoolchain.py
+++ b/conans/test/unittests/tools/cmake/test_cmaketoolchain.py
@@ -1,3 +1,4 @@
+import os
 import types
 
 import pytest
@@ -10,6 +11,8 @@ from conans.client.conf import get_default_settings_yml
 from conans.errors import ConanException
 from conans.model.conf import Conf
 from conans.model.env_info import EnvValues
+from conans.test.utils.test_files import temp_folder
+from conans.util.files import load
 
 
 @pytest.fixture
@@ -462,3 +465,17 @@ def test_apple_cmake_osx_sysroot_sdk_mandatory(os, os_sdk, arch, expected_sdk):
     with pytest.raises(ConanException) as excinfo:
         CMakeToolchain(c).content()
         assert "Please, specify a suitable value for os.sdk." % expected_sdk in str(excinfo.value)
+
+
+def test_variables_types(conanfile):
+    generator_folder = temp_folder()
+    conanfile.folders.set_base_generators(generator_folder)
+    # This is a trick for 1.X to use base_generator and not install folder
+    conanfile.folders.generators = "here"
+
+    toolchain = CMakeToolchain(conanfile)
+    toolchain.variables["FOO"] = True
+    toolchain.generate()
+
+    contents = load(os.path.join(conanfile.generators_folder, "conan_toolchain.cmake"))
+    assert 'set(FOO ON CACHE BOOL "Variable FOO conan-toolchain defined")' in contents