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: sysroot property #16011

Open
wants to merge 5 commits into
base: develop2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions conan/tools/meson/toolchain.py
Expand Up @@ -442,6 +442,14 @@ def _context(self):
# These link_args have already the LDFLAGS env value so let's add only the new possible ones
self.objc_link_args.extend(apple_flags + extra_flags["ldflags"])
self.objcpp_link_args.extend(apple_flags + extra_flags["ldflags"])
# Meson properties
sys_root = self._conanfile.conf.get("tools.build:sysroot", check_type=str)
if sys_root:
self.properties["sys_root"] = sys_root
self.c_args.append("--sysroot=" + sys_root)
self.cpp_args.append("--sysroot=" + sys_root)
self.c_link_args.append("--sysroot=" + sys_root)
self.cpp_link_args.append("--sysroot=" + sys_root)
Comment on lines +448 to +452
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If sys_root is a property, does it still need to be added as a flag? Wouldn't Meson take care of that automatically?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it looks weird. I'm going to figure out how Meson manages this attribute because I think that it should automatically inject that one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's needed: https://mesonbuild.com/Cross-compilation.html#properties

Since 0.52.0 The sys_root property may point to the root of the host system path (the system that will run the compiled binaries). This is used internally by Meson to set the PKG_CONFIG_SYSROOT_DIR environment variable for pkg-config. If this is unset the host system is assumed to share a root with the build system.

The sys_root property only configures the PKG_CONFIG_SYSROOT_DIR, nothing more.

But how Meson users manage --sysroot then?

They pass to c_flags and cxx_flags:


On the other hand, Conan users could have it defined in tools.build:cflags and so on.


if self.libcxx:
self.cpp_args.append(self.libcxx)
Expand Down
78 changes: 78 additions & 0 deletions test/functional/toolchains/meson/test_meson.py
Expand Up @@ -309,3 +309,81 @@ def build(self):
client.run("build .")
assert "unrecognized character escape sequence" not in str(client.out) # if Visual
assert "unknown escape sequence" not in str(client.out) # if mingw


@pytest.mark.tool("meson")
@pytest.mark.skipif(platform.system() != "Linux", reason="Requires --sysroot on Linux")
@pytest.mark.skipif(sys.version_info.minor < 8, reason="Latest Meson versions needs Python >= 3.8")
def test_meson_sysroot_app():
"""Testing when users pass tools.build:sysroot on the profile with Meson

The generated conan_meson_cross.ini needs to contain both sys_root property to fill the
PKG_CONFIG_PATH and the compiler flags with --sysroot.

When cross-building, Meson needs both compiler_executables in the config, otherwise it will fail
when running setup.
"""
sysroot = "/my/new/sysroot/path"
client = TestClient()
profile = textwrap.dedent("""
[settings]
os=Linux
arch=armv8
compiler=gcc
compiler.version=8
compiler.libcxx=libstdc++11
build_type=Release

[conf]
tools.build:sysroot=%s
tools.build:compiler_executables={"c": "gcc", "cpp": "g++"}
tools.build:verbosity=verbose
tools.compilation:verbosity=verbose
""" % sysroot)
conanfile = textwrap.dedent(f"""
from conan import ConanFile
from conan.tools.meson import Meson
from conan.tools.files import copy
from conan.tools.layout import basic_layout
import os
class Pkg(ConanFile):
settings = "os", "compiler", "build_type", "arch"
exports_sources = "meson.build", "src/*"
generators = "MesonToolchain"
def build(self):
meson = Meson(self)
meson.configure()
meson.build()
def layout(self):
self.folders.generators = '{client.cache_folder}/build/gen_folder'
self.folders.build = "{client.cache_folder}/build"
""")
meson_build = textwrap.dedent("""
project('foobar', 'cpp')
executable('foobar', 'src/main.cpp')
""")
main_cpp = textwrap.dedent("int main() { return 0; }")

client.save({"conanfile.py": conanfile,
"meson.build": meson_build,
"profile": profile,
"src/main.cpp": main_cpp,
})

client.run("create . --name=foobar --version=0.1.0 -pr:h=profile -pr:b=default")

# Check the meson configuration file
conan_meson = client.load(os.path.join(client.cache_folder, "build", "gen_folder", "conan_meson_cross.ini"))
assert f"sys_root = '{sysroot}'\n" in conan_meson
assert re.search(r"c_args =.+--sysroot={}.+".format(sysroot), conan_meson)
assert re.search(r"c_link_args =.+--sysroot={}.+".format(sysroot), conan_meson)
assert re.search(r"cpp_args =.+--sysroot={}.+".format(sysroot), conan_meson)
assert re.search(r"cpp_link_args =.+--sysroot={}.+".format(sysroot), conan_meson)

# Check the meson-log.txt
meson_log = client.load(os.path.join(client.cache_folder, "build", "meson-logs", "meson-log.txt"))
assert re.search(r"Detecting linker via:.+--sysroot={}".format(sysroot), meson_log)

# Check compiler calls based on Conan output
assert re.search(r"\[1/2\].+--sysroot=/my/new/sysroot/path", client.out)
assert re.search(r"\[2/2\].+--sysroot=/my/new/sysroot/path", client.out)