Skip to content

Commit

Permalink
[MesonToolchain] Added extra_xxxxx flags (#16389)
Browse files Browse the repository at this point in the history
Added extra_xxxxx flags
  • Loading branch information
franramirez688 authored Jun 3, 2024
1 parent 883140b commit 737f9c4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
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

0 comments on commit 737f9c4

Please sign in to comment.