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 Meson default cross-build x64->32 #17266

Merged
merged 4 commits into from
Nov 4, 2024
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
22 changes: 12 additions & 10 deletions conan/tools/meson/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from conans.util.files import save


class MesonToolchain(object):
class MesonToolchain:
"""
MesonToolchain generator
"""
Expand Down Expand Up @@ -156,7 +156,7 @@ def __init__(self, conanfile, backend=None, native=False):
self._conanfile = conanfile
self._native = native
self._is_apple_system = is_apple_os(self._conanfile)
is_cross_building = cross_building(conanfile, skip_x64_x86=True)
is_cross_building = cross_building(conanfile) # x86_64->x86 is considered cross-building
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the bug fix

if not is_cross_building and native:
raise ConanException("You can only pass native=True if you're cross-building, "
"otherwise, it could cause unexpected results.")
Expand Down Expand Up @@ -424,13 +424,13 @@ 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 = self._conanfile.conf.get("tools.build:defines", default=[], check_type=list)
defines = self._conanfile_conf.get("tools.build:defines", default=[], check_type=list)
memsharded marked this conversation as resolved.
Show resolved Hide resolved
sys_root = [f"--sysroot={self._sys_root}"] if self._sys_root else [""]
ld = sharedlinkflags + exelinkflags + linker_script_flags + sys_root + self.extra_ldflags
return {
"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,
"ldflags": ld,
"defines": [f"-D{d}" for d in (defines + self.extra_defines)]
}

Expand All @@ -447,11 +447,11 @@ def _filter_list_empty_fields(v):
def _sanitize_env_format(value):
if value is None or isinstance(value, list):
return value
assert isinstance(value, str)
memsharded marked this conversation as resolved.
Show resolved Hide resolved
ret = [x.strip() for x in value.split() if x]
return ret[0] if len(ret) == 1 else ret

def _context(self):

apple_flags = self.apple_isysroot_flag + self.apple_arch_flag + self.apple_min_version_flag
extra_flags = self._get_extra_flags()

Expand All @@ -474,10 +474,11 @@ def _context(self):

subproject_options = {}
for subproject, listkeypair in self.subproject_options.items():
if listkeypair is not None and listkeypair is not []:
subproject_options[subproject] = []
for keypair in listkeypair:
subproject_options[subproject].append({k: to_meson_value(v) for k, v in keypair.items()})
if listkeypair:
if not isinstance(listkeypair, list):
raise ConanException("MesonToolchain.subproject_options must be a list of dicts")
subproject_options[subproject] = [{k: to_meson_value(v) for k, v in keypair.items()}
for keypair in listkeypair]

return {
# https://mesonbuild.com/Machine-files.html#properties
Expand Down Expand Up @@ -555,6 +556,7 @@ def generate(self):
If Windows OS, it will be created a ``conanvcvars.bat`` as well.
"""
check_duplicated_generator(self, self._conanfile)
self._conanfile.output.info(f"MesonToolchain generated: {self._filename}")
save(self._filename, self._content)
# FIXME: Should we check the OS and compiler to call VCVars?
VCVars(self._conanfile).generate()
21 changes: 17 additions & 4 deletions test/integration/toolchains/meson/test_mesontoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from conan.test.assets.genconanfile import GenConanfile
from conan.test.utils.tools import TestClient
from conan.tools.files import load
from conan.tools.meson import MesonToolchain


Expand Down Expand Up @@ -540,8 +539,8 @@ def generate(self):
"build": build,
"conanfile.py": conanfile})
client.run("install . -pr:h host -pr:b build")
native_content = load(None, os.path.join(client.current_folder, MesonToolchain.native_filename))
cross_content = load(None, os.path.join(client.current_folder, MesonToolchain.cross_filename))
native_content = client.load(MesonToolchain.native_filename)
cross_content = client.load(MesonToolchain.cross_filename)
expected_native = textwrap.dedent("""
[binaries]
c = 'clang'
Expand Down Expand Up @@ -640,9 +639,23 @@ def test_meson_sysroot_app():
"host": profile})
client.run("install . -pr:h host -pr:b build")
# Check the meson configuration file
conan_meson = client.load(os.path.join(client.current_folder, "conan_meson_cross.ini"))
conan_meson = client.load("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)


def test_cross_x86_64_to_x86():
"""
https://github.com/conan-io/conan/issues/17261
"""

c = TestClient()
c.save({"conanfile.py": GenConanfile().with_settings("os", "compiler", "arch", "build_type")})
c.run("install . -g MesonToolchain -s arch=x86 -s:b arch=x86_64")
assert not os.path.exists(os.path.join(c.current_folder, MesonToolchain.native_filename))
cross = c.load(MesonToolchain.cross_filename)
assert "cpu = 'x86_64'" in cross # This is the build machine
assert "cpu = 'x86'" in cross # This is the host machine