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

- Feature: add backend support to MesonToolchain #9990

Merged
merged 5 commits into from
Nov 16, 2021
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
19 changes: 18 additions & 1 deletion conan/tools/meson/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class MesonToolchain(object):
{% if b_ndebug %}b_ndebug = {{b_ndebug}}{% endif %}
{% if b_staticpic %}b_staticpic = {{b_staticpic}}{% endif %}
{% if cpp_std %}cpp_std = {{cpp_std}}{% endif %}
{% if backend %}backend = {{backend}}{% endif %}
c_args = {{c_args}} + preprocessor_definitions
c_link_args = {{c_link_args}}
cpp_args = {{cpp_args}} + preprocessor_definitions
Expand All @@ -66,8 +67,9 @@ class MesonToolchain(object):
endian = {{endian}}
""")

def __init__(self, conanfile):
def __init__(self, conanfile, backend=None):
self._conanfile = conanfile
self._backend = self._get_backend(backend)
self._build_type = self._conanfile.settings.get_safe("build_type")
self._base_compiler = self._conanfile.settings.get_safe("compiler.base") or \
self._conanfile.settings.get_safe("compiler")
Expand Down Expand Up @@ -99,6 +101,7 @@ def from_build_env(name):
self.buildtype = self._to_meson_build_type(self._build_type) if self._build_type else None
self.default_library = self._to_meson_shared(self._shared) \
if self._shared is not None else None
self.backend = self._to_meson_value(self._backend)

# https://mesonbuild.com/Builtin-options.html#base-options
self.b_vscrt = self._to_meson_vscrt(self._vscrt)
Expand All @@ -117,6 +120,19 @@ def from_build_env(name):

check_using_build_profile(self._conanfile)

def _get_backend(self, recipe_backend):
# Returns the name of the backend used by Meson
conanfile = self._conanfile
# Downstream consumer always higher priority
backend_conf = conanfile.conf["tools.meson.mesontoolchain:backend"]
if backend_conf:
return backend_conf
# second priority: the recipe one:
if recipe_backend:
return recipe_backend
# if not defined, deduce automatically the default one (ninja)
return 'ninja'

@staticmethod
def _to_meson_value(value):
# https://mesonbuild.com/Machine-files.html#data-types
Expand Down Expand Up @@ -211,6 +227,7 @@ def _context(self):
# https://mesonbuild.com/Builtin-options.html#core-options
"buildtype": self.buildtype,
"default_library": self.default_library,
"backend": self.backend,
# https://mesonbuild.com/Builtin-options.html#base-options
"b_vscrt": self.b_vscrt,
"b_staticpic": self.b_staticpic,
Expand Down
Empty file.
57 changes: 57 additions & 0 deletions conans/test/integration/toolchains/meson/test_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os
import platform
import sys
import textwrap

import pytest

from conans.test.assets.sources import gen_function_cpp
from conans.test.utils.tools import TestClient


@pytest.mark.tool_meson
@pytest.mark.skipif(sys.version_info.major == 2, reason="Meson not supported in Py2")
@pytest.mark.skipif(platform.system() != "Windows", reason="requires Windows")
def test_cross_x86():
conanfile_py = textwrap.dedent("""
from conans import ConanFile, tools
from conan.tools.meson import Meson, MesonToolchain


class App(ConanFile):
settings = "os", "arch", "compiler", "build_type"
options = {"shared": [True, False], "fPIC": [True, False]}
default_options = {"shared": False, "fPIC": True}

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def generate(self):
tc = MesonToolchain(self, backend='vs')
tc.generate()

def build(self):
meson = Meson(self)
meson.configure()
meson.build()
""")
meson_build = textwrap.dedent("""
project('tutorial', 'cpp')
executable('demo', 'main.cpp')
""")
main_cpp = gen_function_cpp(name="main")
client = TestClient()
client.save({"conanfile.py": conanfile_py,
"meson.build": meson_build,
"main.cpp": main_cpp})
client.run("install .")
content = client.load("conan_meson_native.ini")
assert "backend = 'vs'" in content
client.run("build .")
assert "Auto detected Visual Studio backend" in client.out
client.run_command(os.path.join("build", "demo"))

assert "main _M_X64 defined" in client.out
assert "main _MSC_VER19" in client.out
assert "main _MSVC_LANG2014" in client.out