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

Fix 'compiler_executable' conf to work with Meson when path has spaces #16307

Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 13 additions & 11 deletions conan/tools/meson/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,15 @@ def __init__(self, conanfile, backend=None, native=False):
#: Sets the Meson ``c`` variable, defaulting to the ``CC`` build environment value.
#: If provided as a blank-separated string, it will be transformed into a list.
#: Otherwise, it remains a single string.
self.c = compilers_by_conf.get("c") or build_env.get("CC") or default_comp
self.c = compilers_by_conf.get("c") or self._sanitize_format(build_env.get("CC")) or default_comp
#: Sets the Meson ``cpp`` variable, defaulting to the ``CXX`` build environment value.
#: If provided as a blank-separated string, it will be transformed into a list.
#: Otherwise, it remains a single string.
self.cpp = compilers_by_conf.get("cpp") or build_env.get("CXX") or default_comp_cpp
self.cpp = compilers_by_conf.get("cpp") or self._sanitize_format(build_env.get("CXX")) or default_comp_cpp
#: Sets the Meson ``ld`` variable, defaulting to the ``LD`` build environment value.
#: If provided as a blank-separated string, it will be transformed into a list.
#: Otherwise, it remains a single string.
self.ld = build_env.get("LD")
self.ld = self._sanitize_format(build_env.get("LD"))
# FIXME: Should we use the new tools.build:compiler_executables and avoid buildenv?
# Issue related: https://github.com/mesonbuild/meson/issues/6442
# PR related: https://github.com/mesonbuild/meson/pull/6457
Expand Down Expand Up @@ -420,12 +420,14 @@ def _get_env_list(v):
def _filter_list_empty_fields(v):
return list(filter(bool, v))

@staticmethod
def _sanitize_format(v):
if v is None or isinstance(v, list):
return v
ret = [x.strip() for x in v.split() if x]
return f"{ret[0]}" if len(ret) == 1 else ret
juansblanco marked this conversation as resolved.
Show resolved Hide resolved

def _context(self):
def _sanitize_format(v):
if v is None or isinstance(v, list):
return v
ret = [x.strip() for x in v.split() if x]
return f"'{ret[0]}'" if len(ret) == 1 else ret

apple_flags = self.apple_isysroot_flag + self.apple_arch_flag + self.apple_min_version_flag
extra_flags = self._get_extra_flags()
Expand Down Expand Up @@ -464,9 +466,9 @@ def _sanitize_format(v):
# https://mesonbuild.com/Builtin-options.html#directories
# https://mesonbuild.com/Machine-files.html#binaries
# https://mesonbuild.com/Reference-tables.html#compiler-and-linker-selection-variables
"c": _sanitize_format(self.c),
"cpp": _sanitize_format(self.cpp),
"ld": _sanitize_format(self.ld),
"c": to_meson_value(self.c),
czoido marked this conversation as resolved.
Show resolved Hide resolved
"cpp": to_meson_value(self.cpp),
"ld": to_meson_value(self.ld),
"objc": self.objc,
"objcpp": self.objcpp,
"c_ld": self.c_ld,
Expand Down
17 changes: 17 additions & 0 deletions conans/test/integration/toolchains/meson/test_mesontoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest

from conans.test.assets.genconanfile import GenConanfile
from conan.tools.files import load
from conan.tools.meson import MesonToolchain
from conans.test.utils.tools import TestClient
Expand Down Expand Up @@ -517,3 +518,19 @@ def generate(self):
client.save({"conanfile.py": conanfile})
client.run("install .", assert_error=True)
assert "You can only pass native=True if you're cross-building" in client.out


def test_compiler_path_with_spaces():
conanfile = (GenConanfile().with_name("lib").with_version("1.0").with_generator("MesonToolchain")
.with_settings("compiler"))
juansblanco marked this conversation as resolved.
Show resolved Hide resolved
profile = textwrap.dedent("""
include(default)
juansblanco marked this conversation as resolved.
Show resolved Hide resolved
[conf]
tools.build:compiler_executables={"c":"c compiler path with spaces", "cpp":"cpp compiler path with spaces"}
""")
client = TestClient()
client.save({"conanfile.py": conanfile, "meson-profile": profile})
client.run("install . -pr:h=meson-profile")
conan_meson_native = client.load("conan_meson_native.ini")
assert "c = 'c compiler path with spaces'" in conan_meson_native
assert "cpp = 'cpp compiler path with spaces'" in conan_meson_native