Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BazelToolchain] Removed useless extra_xxxx attrs #15073

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions conan/tools/google/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -67,13 +67,6 @@ def __init__(self, conanfile, namespace=None):
"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")
Expand Down Expand Up @@ -113,13 +106,13 @@ def _filter_list_empty_fields(v):
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
Expand All @@ -130,15 +123,15 @@ def ldflags(self):
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,
Expand Down
62 changes: 62 additions & 0 deletions conans/test/integration/toolchains/google/test_bazeltoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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