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

[MesonToolchain] Added extra_xxxxx flags #16389

Merged
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
22 changes: 17 additions & 5 deletions conan/tools/meson/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ def __init__(self, conanfile, backend=None, native=False):
if vscrt:
self._b_vscrt = str(vscrt).lower()

# Extra flags
#: List of extra CXX flags. Added to cpp_args
self.extra_cxxflags = []
#: List of extra C flags. Added to c_args
self.extra_cflags = []
#: List of extra linker flags. Added to c_link_args and cpp_link_args
self.extra_ldflags = []
#: List of extra preprocessor definitions. Added to c_args and cpp_args with the
#: format -DFLAG1
self.extra_defines = []

#: Dict-like object that defines Meson``properties`` with ``key=value`` format
self.properties = {}
#: Dict-like object that defines Meson ``project options`` with ``key=value`` format
Expand Down Expand Up @@ -408,13 +419,14 @@ def _get_extra_flags(self):
exelinkflags = self._conanfile_conf.get("tools.build:exelinkflags", default=[], check_type=list)
linker_scripts = self._conanfile_conf.get("tools.build:linker_scripts", default=[], check_type=list)
linker_script_flags = ['-T"' + linker_script + '"' for linker_script in linker_scripts]
defines = [f"-D{d}" for d in self._conanfile.conf.get("tools.build:defines", default=[], check_type=list)]
defines = self._conanfile.conf.get("tools.build:defines", default=[], check_type=list)
sys_root = [f"--sysroot={self._sys_root}"] if self._sys_root else [""]
return {
"cxxflags": cxxflags + sys_root,
"cflags": cflags + sys_root,
"ldflags": sharedlinkflags + exelinkflags + linker_script_flags + sys_root,
"defines": defines
"cxxflags": cxxflags + sys_root + self.extra_cxxflags,
"cflags": cflags + sys_root + self.extra_cflags,
"ldflags": sharedlinkflags + exelinkflags + linker_script_flags
+ sys_root + self.extra_ldflags,
"defines": [f"-D{d}" for d in (defines + self.extra_defines)]
}

@staticmethod
Expand Down
40 changes: 40 additions & 0 deletions test/integration/toolchains/meson/test_mesontoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,46 @@ def test_extra_flags_via_conf():
assert "cpp_link_args = ['-flag0', '-other=val', '-flag5', '-flag6']" in content


def test_extra_flags_via_toolchain():
profile = textwrap.dedent("""
[settings]
os=Windows
arch=x86_64
compiler=gcc
compiler.version=9
compiler.cppstd=17
compiler.libcxx=libstdc++
build_type=Release

[buildenv]
CFLAGS=-flag0 -other=val
CXXFLAGS=-flag0 -other=val
LDFLAGS=-flag0 -other=val
""")
t = TestClient()
conanfile = textwrap.dedent("""
from conan import ConanFile
from conan.tools.meson import MesonToolchain
class Pkg(ConanFile):
settings = "os", "compiler", "arch", "build_type"
def generate(self):
tc = MesonToolchain(self)
tc.extra_cxxflags = ["-flag1", "-flag2"]
tc.extra_cflags = ["-flag3", "-flag4"]
tc.extra_ldflags = ["-flag5", "-flag6"]
tc.extra_defines = ["define1=0"]
tc.generate()
""")
t.save({"conanfile.py": conanfile,
"profile": profile})
t.run("install . -pr:h=profile -pr:b=profile")
content = t.load(MesonToolchain.native_filename)
assert "cpp_args = ['-flag0', '-other=val', '-flag1', '-flag2', '-Ddefine1=0', '-D_GLIBCXX_USE_CXX11_ABI=0']" in content
assert "c_args = ['-flag0', '-other=val', '-flag3', '-flag4', '-Ddefine1=0']" in content
assert "c_link_args = ['-flag0', '-other=val', '-flag5', '-flag6']" in content
assert "cpp_link_args = ['-flag0', '-other=val', '-flag5', '-flag6']" in content


def test_linker_scripts_via_conf():
profile = textwrap.dedent("""
[settings]
Expand Down