Repository: conan-io/conan
Pull request #15073 (2023-11-08 12:34:10)

[BazelToolchain] Removed useless extra_xxxx attrs
Changelog: Feature: Removed extra_xxxx attributes from BazelToolchain
Docs: https://github.com/conan-io/docs/pull/3457




Patch:
diff --git a/conan/tools/google/toolchain.py b/conan/tools/google/toolchain.py
index 05a661a5b..4c051c4ec 100644
--- a/conan/tools/google/toolchain.py
+++ b/conan/tools/google/toolchain.py
@@ -46,7 +46,7 @@ class BazelToolchain:
 
     bazelrc_name = "conan_bzl.rc"
     bazelrc_config = "conan-config"
-    bazelrc_template = textwrap.dedent("""
+    bazelrc_template = textwrap.dedent("""\
     # Automatic bazelrc file created by Conan
     {% if copt %}build:conan-config {{copt}}{% endif %}
     {% if conlyopt %}build:conan-config {{conlyopt}}{% endif %}
@@ -67,13 +67,6 @@ class BazelToolchain:
                                         "deprecated as it's not used anymore.")
         check_using_build_profile(self._conanfile)
 
-        # Flags
-        # TODO: Should we read the buildenv to get flags?
-        self.extra_cxxflags = []
-        self.extra_cflags = []
-        self.extra_ldflags = []
-        self.extra_defines = []
-
         # Bazel build parameters
         shared = self._conanfile.options.get_safe("shared")
         fpic = self._conanfile.options.get_safe("fPIC")
@@ -113,13 +106,13 @@ class BazelToolchain:
     def cxxflags(self):
         ret = [self.cppstd]
         conf_flags = self._conanfile.conf.get("tools.build:cxxflags", default=[], check_type=list)
-        ret = ret  + self.extra_cxxflags + conf_flags
+        ret = ret  + self.cxxopt + conf_flags
         return self._filter_list_empty_fields(ret)
 
     @property
     def cflags(self):
         conf_flags = self._conanfile.conf.get("tools.build:cflags", default=[], check_type=list)
-        ret = self.extra_cflags + conf_flags
+        ret = self.conlyopt + conf_flags
         return self._filter_list_empty_fields(ret)
 
     @property
@@ -130,15 +123,15 @@ class BazelToolchain:
                                                    check_type=list))
         linker_scripts = self._conanfile.conf.get("tools.build:linker_scripts", default=[], check_type=list)
         conf_flags.extend(["-T'" + linker_script + "'" for linker_script in linker_scripts])
-        ret = self.extra_ldflags + conf_flags
+        ret = self.linkopt + conf_flags
         return self._filter_list_empty_fields(ret)
 
     def _context(self):
         return {
             "copt": " ".join(f"--copt={flag}" for flag in self.copt),
-            "conlyopt": " ".join(f"--conlyopt={flag}" for flag in (self.conlyopt + self.cflags)),
-            "cxxopt": " ".join(f"--cxxopt={flag}" for flag in (self.cxxopt + self.cxxflags)),
-            "linkopt": " ".join(f"--linkopt={flag}" for flag in (self.linkopt + self.ldflags)),
+            "conlyopt": " ".join(f"--conlyopt={flag}" for flag in self.cflags),
+            "cxxopt": " ".join(f"--cxxopt={flag}" for flag in self.cxxflags),
+            "linkopt": " ".join(f"--linkopt={flag}" for flag in self.ldflags),
             "force_pic": self.force_pic,
             "dynamic_mode": self.dynamic_mode,
             "compilation_mode": self.compilation_mode,


Test patch:
diff --git a/conans/test/integration/toolchains/google/test_bazeltoolchain.py b/conans/test/integration/toolchains/google/test_bazeltoolchain.py
index 7dcf5d1b1..42fc6987e 100644
--- a/conans/test/integration/toolchains/google/test_bazeltoolchain.py
+++ b/conans/test/integration/toolchains/google/test_bazeltoolchain.py
@@ -109,3 +109,65 @@ def test_bazel_toolchain_and_cross_compilation(conanfile):
     c.run("install . -pr:b profile -pr:h profile_host")
     content = load(c, os.path.join(c.current_folder, "conan", BazelToolchain.bazelrc_name))
     assert "build:conan-config --cpu=darwin_arm64" in content
+
+
+def test_toolchain_attributes_and_conf_priority():
+    """
+    Tests that all the attributes are appearing correctly in the conan_bzl.rc even defining
+    some conf variables
+    """
+    profile = textwrap.dedent("""
+    [settings]
+    arch=x86_64
+    build_type=Release
+    compiler=apple-clang
+    compiler.cppstd=gnu17
+    compiler.libcxx=libc++
+    compiler.version=13.0
+    os=Macos
+    [conf]
+    tools.build:cxxflags=["--flag1"]
+    tools.build:cflags+=["--flag3"]
+    tools.build:sharedlinkflags+=["--linkflag5"]
+    tools.build:exelinkflags+=["--linkflag6"]
+    """)
+    conanfile = textwrap.dedent("""
+    from conan import ConanFile
+    from conan.tools.google import BazelToolchain
+    class ExampleConanIntegration(ConanFile):
+        settings = "os", "arch", "build_type", "compiler"
+        options = {"shared": [True, False], "fPIC": [True, False]}
+        default_options = {"shared": False, "fPIC": True}
+
+        def generate(self):
+            bz = BazelToolchain(self)
+            bz.copt = ["copt1"]
+            bz.conlyopt = ["conly1"]
+            bz.cxxopt = ["cxxopt1"]
+            bz.linkopt = ["linkopt1"]
+            bz.force_pic = True
+            bz.dynamic_mode = "auto"
+            bz.compilation_mode = "fastbuild"
+            bz.compiler = "gcc"
+            bz.cpu = "armv8"
+            bz.crosstool_top = "my_crosstool"
+            bz.generate()
+    """)
+    c = TestClient()
+    c.save({"conanfile.py": conanfile,
+            "profile": profile})
+    c.run("install . -pr profile")
+    content = load(c, os.path.join(c.current_folder, BazelToolchain.bazelrc_name))
+    expected = textwrap.dedent("""\
+    # Automatic bazelrc file created by Conan
+    build:conan-config --copt=copt1
+    build:conan-config --conlyopt=conly1 --conlyopt=--flag3
+    build:conan-config --cxxopt=-std=gnu++17 --cxxopt=cxxopt1 --cxxopt=--flag1
+    build:conan-config --linkopt=linkopt1 --linkopt=--linkflag5 --linkopt=--linkflag6
+    build:conan-config --force_pic=True
+    build:conan-config --dynamic_mode=auto
+    build:conan-config --compilation_mode=fastbuild
+    build:conan-config --compiler=gcc
+    build:conan-config --cpu=armv8
+    build:conan-config --crosstool_top=my_crosstool""")
+    assert expected == content